Documentation
¶
Overview ¶
Query is SQL Helper Query package contains helper functions to generate SQL statements query.
Index ¶
- Variables
- func Args(row any, forWrite bool) ([]any, error)
- func ArgsAppay(row any, args []any) (err error)
- func Count[T any](attr *SelectAttr) (string, error)
- func Delete[T any](wheres ...string) (string, error)
- func GetNumRows() int
- func Insert[T any]() (string, error)
- func Name[T any]() (name string)
- func Select[T any](attr *SelectAttr) (string, error)
- func SetNumRows(n int)
- func Table[T any]() (string, error)
- func Update[T any](wheres ...string) (string, error)
- type Join
- type Paginator
- type SelectAttr
- type TableName
Constants ¶
This section is empty.
Variables ¶
var ( ErrTypeIsNotStruct = fmt.Errorf("type is not a struct") ErrWhereClauseRequiredForUpdate = fmt.Errorf("where clause should be set in the Update statement") )
Exported errors
Functions ¶
func Args ¶
Args returns the arguments array for the given struct type. The given struct may be a pointer to struct or struct.
The forWrite parameter controls the behavior:
- If forWrite is true, it returns a slice of values for INSERT/UPDATE, skipping autoincrement fields.
- If forWrite is false, it returns a slice of pointers to copies of field values for SELECT (for sql.Scan). These pointers are then used with ArgsAppay to populate the struct.
func ArgsAppay ¶
ArgsAppay sets fields values of the given pointer to struct row from the args array.
It loops through the given struct fields and sets field values from the corresponding arguments in the given args array. Supported types are string, float64, time.Time, int64 and bool. If unsupported type is found, it returns an error.
func Count ¶
func Count[T any](attr *SelectAttr) (string, error)
Count returns a SQL COUNT statement for the given struct type.
The struct may be tagged with "db" tags to specify the database field names. If the "db" tag is not specified, the field name will be used as the database field name. The returned string is a SQL statement that can be executed directly.
The wheres parameter is an optional list of where clauses. If specified, the where clauses will be joined with " and " and added to the SQL statement.
func Delete ¶
Delete returns a SQL DELETE statement for the given struct type.
The struct may be tagged with "db" tags to specify the database field names. If the "db" tag is not specified, the field name will be used as the database field name. The returned string is a SQL statement that can be executed directly.
The wheres parameter is an optional list of where clauses. If specified, the where clauses will be joined with " and " and added to the SQL statement.
func GetNumRows ¶ added in v0.3.1
func GetNumRows() int
GetNumRows returns default number of rows. It may be set by SetNumRows. By default, it is 10.
func Insert ¶
Insert returns a SQL INSERT statement for the given struct type.
The struct may be tagged with "db" tags to specify the database field names. If the "db" tag is not specified, the field name will be used as the database field name. The returned string is a SQL statement that can be executed directly.
func Name ¶ added in v0.3.0
Name returns table Name from struct Name or db_table_name tag.
It takes type T as an argument and returns the table Name as a string. The table Name is the lower case version of the struct Name. If the tag db_table_name is present in any struct field, it is used as the table Name and replaces table Name created from the struct Name.
func Select ¶
func Select[T any](attr *SelectAttr) (string, error)
Select returns a SQL SELECT statement for the given struct type.
The struct may be tagged with "db" tags to specify the database field names. If the "db" tag is not specified, the field name will be used as the database field name. The returned string is a SQL statement that can be executed directly.
The wheres parameter is an optional list of where clauses. If specified, the where clauses will be joined with " and " and added to the SQL statement.
func SetNumRows ¶ added in v0.3.1
func SetNumRows(n int)
SetNumRows sets default number of rows returned by List function. By default, it is 10.
func Table ¶
Table returns a SQL CREATE TABLE statement for the given struct type.
The table is created if it does not already exist. The function returns an error if the given type is not a struct.
Example:
// Iput struct
type Astuct struct {
ID int `db:"id" db_type:"integer" db_key:"not null primary key"`
Name string
}
// Output CREATE TABLE statement
"CREATE TABLE IF NOT EXISTS astuct (id integer not null primary key, name text)"
Struct tagas are used to map database fields to struct fields. The tag is optional. Next tags may be used:
- db:"some_field_name" - set database field name
- db_key:"not null primary key" - set database field key
- db_type:"text" - set database field type
- db_table_name:"some_table" - set database table name
Types ¶
type Join ¶ added in v0.3.0
type Join struct {
Join string // Join type: inner, left, right, full
Name string // Table name
Alias string // Alias (optional)
On string // On clause
Fields []string // Fields
Select string // Select clause (optional)
}
Join defines attributes for JOIN statement.
func MakeJoin ¶ added in v0.3.0
MakeJoin takes a Join struct as input parameter and returns a Join struct with the name and fields set from the given struct type T.
If the Name field of the input Join struct is empty, the function sets it to the table name of the given struct type T.
The function also sets the Fields field of the output Join struct by iterating over all fields of the given struct type T and appending them to the Fields field. If the Alias field of the input Join struct is not empty, the function prefixes each field with the alias and a dot.
The function returns the output Join struct.
type Paginator ¶
type Paginator struct {
// Get list of rows from this position. In other words: skip the specified
// number of rows before starting to output rows.
Offset int
// Number of rows to get. If 0, all rows will be returned.
Limit int
}
Paginator defines attributes for SELECT statement.
type SelectAttr ¶
type SelectAttr struct {
// Offset and limit (optional). Example: "0, 10"
Paginator *Paginator
// Where clauses (optional). Example: "id = ?", "name = ?" joined with " and "
Wheres []string
// Join wheres by "or" if true
WheresJoinOr bool
// Group by (optional). Example: "id, name"
GroupBy string
// Order by (optional). Example: "id desc, name asc"
OrderBy string
// Alias (optional). Table name alias used in the fields and joins conditions
Alias string
// Joins (optional). List of joins to other tables used in select
Joins []Join
// Distinct (optional). If true, the SELECT statement will use the DISTINCT
// keyword
Distinct bool
// Name (optional) replaces the table name. By default, the table name is
// taken from the structure type specified when calling the Select function.
Name *string
}
SelectAttr defines attributes for SELECT statement.