Skip to content

Commit

Permalink
Merge branch 'master' of github.com:kisielk/sqlstruct
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverCory committed Feb 6, 2024
2 parents c9077e5 + dae28ed commit 86c60f3
Showing 1 changed file with 59 additions and 71 deletions.
130 changes: 59 additions & 71 deletions sqlstruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 86c60f3

Please sign in to comment.