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 Insert[T any]() (string, error)
- func Select[T any](attr *SelectAttr) (string, error)
- func Table[T any]() (string, error)
- func Update[T any](wheres ...string) (string, error)
- type Paginator
- type SelectAttr
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 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 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 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_type:"text" - set database field type
- db_key:"not null primary key" - set database field key
Types ¶
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 {
Paginator *Paginator // Offset and limit (optional)
Wheres []string // Where clauses (optional)
OrderBy string // Order by (optional)
}
SelectAttr defines attributes for SELECT statement.