Documentation
¶
Overview ¶
Package sqlp provides some convenience functions for using structs with the Go standard library's database/sql package.
The 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.
For example: ToDo (See Readme)
Index ¶
- Variables
- func Delete[T any](pk any, table string) error
- func DeleteDb[T any](db *sql.DB, pk any, table string) error
- func DeleteR[T Repo](obj T) error
- func DeleteRdb[T Repo](db *sql.DB, obj T) error
- func In(query string, args ...any) error
- func InDb(db *sql.DB, query string, args ...any) (err error)
- func InQuery(query string, args []any) (string, []any, error)
- func Insert[T any](obj T, table string) (int, error)
- func InsertDb[T any](db *sql.DB, obj T, table string) (int, error)
- func InsertR[T Repo](obj T) (int, error)
- func InsertRdb[T Repo](db *sql.DB, obj T) (int, error)
- func Query[T any](query string, args ...any) (results []T, err error)
- func QueryBasic[T string | int | int64 | float32 | float64](query string, args ...any) (results []T, err error)
- func QueryBasicDb[T string | int | int64 | float32 | float64](db *sql.DB, query string, args ...any) (results []T, err error)
- func QueryBasicRow[T string | int | int64 | float32 | float64](query string, args ...any) (result T, err error)
- func QueryBasicRowDb[T string | int | int64 | float32 | float64](db *sql.DB, query string, args ...any) (result T, err error)
- func QueryDb[T any](db *sql.DB, query string, args ...any) (results []T, err error)
- func QueryRow[T any](query string, args ...any) (result T, err error)
- func QueryRowDb[T any](db *sql.DB, query string, args ...any) (result T, err error)
- func SetDatabase(sqldb *sql.DB)
- func ToAny(s any) []any
- func ToSnakeCase(src string) string
- func Update[T any](obj T, table string) error
- func UpdateDb[T any](db *sql.DB, obj T, table string) error
- func UpdateR[T Repo](obj T) error
- func UpdateRdb[T Repo](db *sql.DB, obj T) error
- type Repo
- type Rows
- type Scanner
Constants ¶
This section is empty.
Variables ¶
var ( // NameMapper is the function used to convert struct fields which do not have sql tags // into database column names. // // The default mapper converts field names to lower case. If instead you would prefer // field names converted to snake case, simply assign sqlp.ToSnakeCase to the variable: // // sqlp.NameMapper = sqlp.ToSnakeCase // // Alternatively for a custom mapping, any func(string) string can be used instead. NameMapper = strings.ToLower // TagName is the name of the tag to use on struct fields TagName = "sql" AutoGenTagName = "sql-auto" IgnoreTagName = "sql-ign" QueryReplace = "SELECT *" InQueryReplace = "IN (*)" ErrNotSet = errors.New("sqlp: database not set") )
Functions ¶
func Insert ¶
Insert inserts the given object into the table and returns the last inserted id. Autogenerated fields can be tagged with `sql-auto:""` (AutoGenTagName) in order for them to be ignored during insert.
func QueryBasic ¶
func QueryBasicDb ¶ added in v0.1.15
func QueryBasicDb[T string | int | int64 | float32 | float64](db *sql.DB, query string, args ...any) (results []T, err error)
QueryBasicDb is Query, but for basic data types.
func QueryBasicRow ¶ added in v0.1.4
func QueryBasicRowDb ¶ added in v0.1.15
func QueryBasicRowDb[T string | int | int64 | float32 | float64](db *sql.DB, query string, args ...any) (result T, err error)
QueryBasicRowDb is QueryRow, but for basic data types.
func QueryDb ¶ added in v0.1.15
QueryDb executes the given query using the global database handle and returns the resulting objects in a slice. SetDatabase must be called before using this function. The query should use the QueryReplace (* by default) string to indicate where the columns from the struct type T should be inserted.
For example for the following struct:
type User struct {
ID int
Name string
}
and the following query
SELECT * FROM users WHERE id = ?
the query sent to the database will be
SELECT id, name FROM users WHERE id = ?
and a list of User objects will be returned.
In addition, "IN"-queries are supported. If the query contains the InQueryReplace string, the function will automatically replace it with the correct amount of "?". For example, if you give the following query
SELECT * FROM users WHERE id IN (*)
and the following arguments
Query("SELECT * FROM users WHERE id IN (*) AND name LIKE '%?'", []int{1, 2, 3}, "a")
func QueryRowDb ¶ added in v0.1.15
QueryRowDb works similar to Query except it returns only the first row from the result set. SetDatabase must be called before using this function. Check the Query function for more information.
func SetDatabase ¶
SetDatabase sets the global database handle to be used by the Query function.
func ToSnakeCase ¶
ToSnakeCase 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.