Documentation
¶
Index ¶
- Constants
- Variables
- func DeleteDb[T Repo](db *sql.DB, obj T) error
- func DeletePkDb[T Repo](db *sql.DB, id any) error
- func GetPkDb[T Repo](db *sql.DB, id any) (res T, err error)
- func GetRdb[T Repo](db *sql.DB) ([]T, error)
- func GetSingleWhereRdb[T Repo](db *sql.DB, where string, args ...any) (res T, err error)
- func GetWhereRdb[T Repo](db *sql.DB, where string, args ...any) ([]T, error)
- func InDb(db *sql.DB, query string, args ...any) (err error)
- func InsertDb[T Repo](db *sql.DB, obj T) (int, error)
- func QueryBasicDb[T string | int | int64 | float32 | float64](db *sql.DB, query string, args ...any) (results []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 QueryRowDb[T any](db *sql.DB, query string, args ...any) (result T, err error)
- func UpdateDb[T Repo](db *sql.DB, obj T) error
- type Repo
- type Rows
- type Scanner
Constants ¶
const ( // TagName is the name of the tag to use on struct fields TagName = "sql" // AutoGenTagName is the name of the tag to use on struct fields to indicate that it is a primary key AutoGenTagName = "sql-auto" // IgnoreTagName is the name of the tag to use on struct fields to indicate that it should be ignored for all operations // (insert, update, delete) // todo: use value of this tag to configure for which methods to ignore instead of IgnoreEditTageName IgnoreTagName = "sql-ign" // IgnoreEditTagName is the name of the tag to use on struct fields to indicate that it should be ignored for edit operations, but not insert IgnoreEditTagName = "sql-ign-edit" QueryReplace = "SELECT *" )
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. // Alternatively for a custom mapping, any func(string) string can be used instead. NameMapper = strings.ToLower ErrNotSet = errors.New("sqlp: database not set") )
Functions ¶
func GetSingleWhereRdb ¶ added in v0.1.25
func GetWhereRdb ¶ added in v0.1.25
func QueryBasicDb ¶
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 QueryBasicRowDb ¶
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 ¶
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 ¶
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.