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(allowedColumns ...string) *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 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 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 "?" } )
Functions ¶
func UsePostgres ¶ added in v1.1.0
func UsePostgres(b *Builder)
Types ¶
type Bool ¶ added in v1.5.0
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 ¶ added in v1.1.0
Delete adds a DELETE statement to the current query builder. Returns the current query builder.
func (*Builder) If ¶ added in v1.3.0
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 ¶ added in v1.2.0
On sets the "rotate" input variable to the given shard ID's rotate name. Returns the current query builder.
func (*Builder) Order ¶ added in v1.4.3
func (b *Builder) Order(allowedColumns ...string) *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 ¶ added in v1.1.0
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 ¶ added in v1.4.4
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 ¶ added in v1.4.4
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 ¶ added in v1.1.0
type BuilderOption func(opts *BuilderOptions)
func WithAllow ¶ added in v1.1.0
func WithAllow(columns ...string) BuilderOption
WithAllow only allowed columns can be written to db
func WithToName ¶ added in v1.1.0
func WithToName(fn func(string) string) BuilderOption
type BuilderOptions ¶ added in v1.1.0
type Client ¶ added in v1.5.0
func (*Client) ExecBuilder ¶ added in v1.5.0
func (*Client) ExecContext ¶ added in v1.5.0
func (*Client) QueryBuilder ¶ added in v1.5.0
func (*Client) QueryContext ¶ added in v1.5.0
func (*Client) QueryRowBuilder ¶ added in v1.5.0
func (*Client) QueryRowContext ¶ added in v1.5.0
type Connector ¶ added in v1.5.0
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.
func (*DB) GetDHT ¶ added in v1.4.1
GetDHT returns the DHT (Distributed Hash Table) with the specified name.
func (*DB) NewDHT ¶ added in v1.4.0
NewDHT creates a new DHT (Distributed Hash Table) with the specified databases.
type DTC ¶ added in v1.5.0
type DTC struct {
// contains filtered or unexported fields
}
DTC Distributed Transaction Coordinator
type Duration ¶ added in v1.3.1
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 ¶ added in v1.4.4
type LimitOption func(q *LimitOptions)
LimitOption is a function type that modifies a LimitedQuery.
func WithOrderBy ¶ added in v1.4.4
func WithOrderBy(ob *OrderByBuilder) LimitOption
WithOrderBy is a LimitedQueryOption that sets the ordering of the LimitedQuery.
func WithPageSize ¶ added in v1.4.4
func WithPageSize(size int64) LimitOption
WithPageSize is a LimitedQueryOption that sets the page size of the LimitedQuery.
type LimitOptions ¶ added in v1.4.4
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 ¶ added in v1.4.4
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 ¶ added in v1.3.0
type MapR[T any] struct { // contains filtered or unexported fields }
MapR is a Map/Reduce Query Provider based on databases.
type OrderByBuilder ¶ added in v1.4.2
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 ¶ added in v1.4.4
func NewOrderBy(allowedColumns ...string) *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 ¶ added in v1.4.3
func (ob *OrderByBuilder) By(raw string) *OrderByBuilder
By order by raw sql. eg By("a asc, b desc")
func (*OrderByBuilder) ByAsc ¶ added in v1.4.3
func (ob *OrderByBuilder) ByAsc(columns ...string) *OrderByBuilder
ByAsc order by ascending with columns
func (*OrderByBuilder) ByDesc ¶ added in v1.4.3
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 ¶ added in v1.3.0
type Query[T any] struct { // contains filtered or unexported fields }
func NewQuery ¶ added in v1.3.0
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 ¶ added in v1.3.0
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 ¶ added in v1.3.0
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 ¶ added in v1.3.0
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 ¶ added in v1.3.0
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 ¶ added in v1.3.0
func WithMonths ¶ added in v1.3.0
func WithMonths[T any](start, end time.Time) QueryOption[T]
func WithQueryer ¶ added in v1.3.0
func WithQueryer[T any](qr Queryer[T]) QueryOption[T]
type Queryer ¶ added in v1.3.0
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 Text ¶
type Text string
Text represents a text token.
type Time ¶ added in v1.5.0
Time represents a nullable time value.
func NewTime ¶ added in v1.5.0
NewTime creates a new Time object with the given time and valid flag.
func (Time) MarshalJSON ¶ added in v1.5.0
MarshalJSON implements the json.Marshaler interface
func (*Time) Scan ¶ added in v1.5.0
Scan implements the sql.Scanner interface.
func (*Time) UnmarshalJSON ¶ added in v1.5.0
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 NewWhere ¶ added in v1.4.4
func NewWhere() *WhereBuilder
NewWhere creates a new instance of WhereBuilder.
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
- 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
- time.go
- token.go
- tokenizer.go
- tx.go
- use.go