-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:kisielk/sqlstruct
- Loading branch information
Showing
1 changed file
with
59 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,82 +14,71 @@ embedded structs and are recursively scanned. | |
For example: | ||
type T1 struct { | ||
F4 string `sql:"field4"` | ||
} | ||
type T struct { | ||
F1 string | ||
F2 string `sql:"field2"` | ||
F3 string `sql:"-"` | ||
} | ||
type T2 struct { | ||
F5 string `sql:"field5"` | ||
} | ||
type T struct { | ||
F1 string | ||
F2 string `sql:"field2"` | ||
F3 string `sql:"-"` | ||
fieldT1 T1 `sql:",recurse"` | ||
T2 | ||
} | ||
rows, err := db.Query(fmt.Sprintf("SELECT %s FROM tablename", sqlstruct.Columns(T{}))) | ||
... | ||
rows, err := db.Query(fmt.Sprintf("SELECT %s FROM tablename", sqlstruct.Columns(T{}))) | ||
... | ||
for rows.Next() { | ||
var t T | ||
err = sqlstruct.Scan(&t, rows) | ||
... | ||
} | ||
for rows.Next() { | ||
var t T | ||
err = sqlstruct.Scan(&t, rows) | ||
... | ||
} | ||
err = rows.Err() // get any errors encountered during iteration | ||
err = rows.Err() // get any errors encountered during iteration | ||
Aliased tables in a SQL statement may be scanned into a specific structure identified | ||
by the same alias, using the ColumnsAliased and ScanAliased functions: | ||
type User struct { | ||
Id int `sql:"id"` | ||
Username string `sql:"username"` | ||
Email string `sql:"address"` | ||
Name string `sql:"name"` | ||
HomeAddress *Address `sql:"-"` | ||
} | ||
type Address struct { | ||
Id int `sql:"id"` | ||
City string `sql:"city"` | ||
Street string `sql:"address"` | ||
} | ||
... | ||
type User struct { | ||
Id int `sql:"id"` | ||
Username string `sql:"username"` | ||
Email string `sql:"address"` | ||
Name string `sql:"name"` | ||
HomeAddress *Address `sql:"-"` | ||
} | ||
type Address struct { | ||
Id int `sql:"id"` | ||
City string `sql:"city"` | ||
Street string `sql:"address"` | ||
} | ||
... | ||
var user User | ||
var address Address | ||
sql := ` | ||
SELECT %s, %s FROM users AS u | ||
INNER JOIN address AS a ON a.id = u.address_id | ||
WHERE u.username = ? | ||
` | ||
sql = fmt.Sprintf(sql, sqlstruct.ColumnsAliased(*user, "u"), sqlstruct.ColumnsAliased(*address, "a")) | ||
rows, err := db.Query(sql, "gedi") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer rows.Close() | ||
if rows.Next() { | ||
err = sqlstruct.ScanAliased(&user, rows, "u") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
err = sqlstruct.ScanAliased(&address, rows, "a") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
user.HomeAddress = address | ||
} | ||
fmt.Printf("%+v", *user) | ||
// output: "{Id:1 Username:gedi Email:[email protected] Name:Gedas HomeAddress:0xc21001f570}" | ||
fmt.Printf("%+v", *user.HomeAddress) | ||
// output: "{Id:2 City:Vilnius Street:Plento 34}" | ||
var user User | ||
var address Address | ||
sql := ` | ||
SELECT %s, %s FROM users AS u | ||
INNER JOIN address AS a ON a.id = u.address_id | ||
WHERE u.username = ? | ||
` | ||
sql = fmt.Sprintf(sql, sqlstruct.ColumnsAliased(*user, "u"), sqlstruct.ColumnsAliased(*address, "a")) | ||
rows, err := db.Query(sql, "gedi") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer rows.Close() | ||
if rows.Next() { | ||
err = sqlstruct.ScanAliased(&user, rows, "u") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
err = sqlstruct.ScanAliased(&address, rows, "a") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
user.HomeAddress = address | ||
} | ||
fmt.Printf("%+v", *user) | ||
// output: "{Id:1 Username:gedi Email:[email protected] Name:Gedas HomeAddress:0xc21001f570}" | ||
fmt.Printf("%+v", *user.HomeAddress) | ||
// output: "{Id:2 City:Vilnius Street:Plento 34}" | ||
*/ | ||
package sqlstruct | ||
|
||
|
@@ -109,7 +98,7 @@ import ( | |
// The default mapper converts field names to lower case. If instead you would prefer | ||
// field names converted to snake case, simply assign sqlstruct.ToSnakeCase to the variable: | ||
// | ||
// sqlstruct.NameMapper = sqlstruct.ToSnakeCase | ||
// sqlstruct.NameMapper = sqlstruct.ToSnakeCase | ||
// | ||
// Alternatively for a custom mapping, any func(string) string can be used instead. | ||
var NameMapper func(string) string = strings.ToLower | ||
|
@@ -210,8 +199,7 @@ func Columns(s interface{}) string { | |
// given alias. | ||
// | ||
// For each field in the given struct it will generate a statement like: | ||
// | ||
// alias.field AS alias_field | ||
// alias.field AS alias_field | ||
// | ||
// It is intended to be used in conjunction with the ScanAliased function. | ||
func ColumnsAliased(s interface{}, alias string) string { | ||
|