{"modulePath":"github.com/kisielk/sqlstruct","version":"v0.0.0-20210630145711-dae28ed37023","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package sqlstruct\n\nPackage sqlstruct provides some convenience functions for using structs with the Go standard library's database/sql package.\n\nThe package matches struct field names to SQL query column names. A field can also specify a matching column with \"sql\" tag, if it's different from field name.  Unexported fields or fields marked with \\`sql:\"-\"\\` are ignored, just like with \"encoding/json\" package.\n\nFor example:\n\n\ttype T struct {\n\t    F1 string\n\t    F2 string `sql:\"field2\"`\n\t    F3 string `sql:\"-\"`\n\t}\n\n\trows, err := db.Query(fmt.Sprintf(\"SELECT %s FROM tablename\", sqlstruct.Columns(T{})))\n\t...\n\n\tfor rows.Next() {\n\t\tvar t T\n\t    err = sqlstruct.Scan(\u0026t, rows)\n\t    ...\n\t}\n\n\terr = rows.Err() // get any errors encountered during iteration\n\nAliased tables in a SQL statement may be scanned into a specific structure identified by the same alias, using the ColumnsAliased and ScanAliased functions:\n\n\ttype User struct {\n\t    Id int `sql:\"id\"`\n\t    Username string `sql:\"username\"`\n\t    Email string `sql:\"address\"`\n\t    Name string `sql:\"name\"`\n\t    HomeAddress *Address `sql:\"-\"`\n\t}\n\n\ttype Address struct {\n\t    Id int `sql:\"id\"`\n\t    City string `sql:\"city\"`\n\t    Street string `sql:\"address\"`\n\t}\n\n\t...\n\n\tvar user User\n\tvar address Address\n\tsql := `\n\tSELECT %s, %s FROM users AS u\n\tINNER JOIN address AS a ON a.id = u.address_id\n\tWHERE u.username = ?\n\t`\n\tsql = fmt.Sprintf(sql, sqlstruct.ColumnsAliased(*user, \"u\"), sqlstruct.ColumnsAliased(*address, \"a\"))\n\trows, err := db.Query(sql, \"gedi\")\n\tif err != nil {\n\t    log.Fatal(err)\n\t}\n\tdefer rows.Close()\n\tif rows.Next() {\n\t    err = sqlstruct.ScanAliased(\u0026user, rows, \"u\")\n\t    if err != nil {\n\t        log.Fatal(err)\n\t    }\n\t    err = sqlstruct.ScanAliased(\u0026address, rows, \"a\")\n\t    if err != nil {\n\t        log.Fatal(err)\n\t    }\n\t    user.HomeAddress = address\n\t}\n\tfmt.Printf(\"%+v\", *user)\n\t// output: \"{Id:1 Username:gedi Email:gediminas.morkevicius@gmail.com Name:Gedas HomeAddress:0xc21001f570}\"\n\tfmt.Printf(\"%+v\", *user.HomeAddress)\n\t// output: \"{Id:2 City:Vilnius Street:Plento 34}\"\n\n## Variables\n\n```go\nvar NameMapper func(string) string = strings.ToLower\n```\nNameMapper is the function used to convert struct fields which do not have sql tags into database column names.\n\nThe 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:\n\n\tsqlstruct.NameMapper = sqlstruct.ToSnakeCase\n\nAlternatively for a custom mapping, any func(string) string can be used instead.\n\n```go\nvar TagName = \"sql\"\n```\nTagName is the name of the tag to use on struct fields\n\n## Functions\n\n```go\nfunc Columns(s interface{}) string\n```\nColumns returns a string containing a sorted, comma-separated list of column names as defined by the type s. s must be a struct that has exported fields tagged with the \"sql\" tag.\n\n```go\nfunc ColumnsAliased(s interface{}, alias string) string\n```\nColumnsAliased works like Columns except it prefixes the resulting column name with the given alias.\n\nFor each field in the given struct it will generate a statement like:\n\n\talias.field AS alias_field\n\nIt is intended to be used in conjunction with the ScanAliased function.\n\n```go\nfunc Scan(dest interface{}, rows Rows) error\n```\nScan scans the next row from rows in to a struct pointed to by dest. The struct type should have exported fields tagged with the \"sql\" tag. Columns from row which are not mapped to any struct fields are ignored. Struct fields which have no matching column in the result set are left unchanged.\n\n```go\nfunc ScanAliased(dest interface{}, rows Rows, alias string) error\n```\nScanAliased works like scan, except that it expects the results in the query to be prefixed by the given alias.\n\nFor example, if scanning to a field named \"name\" with an alias of \"user\" it will expect to find the result in a column named \"user\\_name\".\n\nSee ColumnAliased for a convenient way to generate these queries.\n\n```go\nfunc ToSnakeCase(src string) string\n```\nToSnakeCase converts a string to snake case, words separated with underscores. It's intended to be used with NameMapper to map struct field names to snake case database fields.\n\n## Types\n\n```go\ntype Rows interface {\n\tScan(...interface{}) error\n\tColumns() ([]string, error)\n}\n```\nRows defines the interface of types that are scannable with the Scan function. It is implemented by the sql.Rows type from the standard library\n\n","path":"github.com/kisielk/sqlstruct","name":"sqlstruct","synopsis":"Package sqlstruct provides some convenience functions for using structs with the Go standard library's database/sql package.","isRedistributable":true}
