Documentation ¶
Index ¶
- Constants
- Variables
- func DecodeCursor(cursor string) (values.MapAny, error)
- func EncodeCursor(val values.MapAny) string
- func ExecuteListQuery[T any, TPointer RowPointer[T]](ctx context.Context, sql DB, query string, args ...any) ([]TPointer, error)
- func ExecuteQuery[T any, TPointer RowPointer[T]](ctx context.Context, sql DB, res Result[T, TPointer], query string, ...) error
- func ExecuteQueryWithCursor[T any, TPointer RowPointer[T]](ctx context.Context, sql DB, cursor SetCursor[T, TPointer], ...) error
- func ExecuteQueryWithPagination[T any, TPointer RowPointer[T]](ctx context.Context, sql DB, res Result[T, TPointer], query string, ...) error
- func IDString(id uint64) string
- func Int64Array(list []uint64) pq.Int64Array
- func IsNotFoundError(err error) bool
- func Merge(dst any, src any) error
- func NullTime(val *time.Time) sql.NullTime
- func Open(dataSource, database string) (*sql.DB, string, string, error)
- func PageParam(p any) uint32
- func ParseUint(id string) (uint64, error)
- func QueryRow[T any, TPointer RowPointer[T]](ctx context.Context, sql DB, query string, args ...any) (TPointer, error)
- func String(val *string) string
- func TimePtr(val Time) *time.Time
- func Validate(m any) error
- type Bool
- type DB
- type Float
- type HasQueryParams
- type ID
- func (v ID) Invalid() bool
- func (v ID) IsZero() bool
- func (v ID) MarshalJSON() ([]byte, error)
- func (v ID) MarshalYAML() (interface{}, error)
- func (v *ID) Reset()
- func (v *ID) Scan(value any) error
- func (v *ID) Set(val string) error
- func (v ID) String() string
- func (v ID) UInt64() uint64
- func (v *ID) UnmarshalJSON(data []byte) error
- func (v *ID) UnmarshalYAML(unmarshal func(interface{}) error) error
- func (v ID) Valid() bool
- func (v ID) Value() (driver.Value, error)
- type ID32
- type IDArray
- func (n IDArray) Add(id ID) IDArray
- func (n IDArray) Concat(other IDArray) IDArray
- func (n IDArray) Contains(id ID) bool
- func (n IDArray) Int64Array() pq.Int64Array
- func (n IDArray) List() []uint64
- func (n IDArray) Overlaps(other IDArray) bool
- func (n *IDArray) Scan(value any) error
- func (n IDArray) Sort() IDArray
- func (n IDArray) String() string
- func (n IDArray) Strings() []string
- func (n IDArray) Value() (driver.Value, error)
- type IDGenerator
- type Int32
- type Int64
- type Metadata
- type MigrationConfig
- type NULLString
- type PageableByCursor
- type PageableByOffset
- type Provider
- type QueryParams
- type QueryParamsBuilder
- func (b *QueryParamsBuilder) AddArgs(v ...any)
- func (b *QueryParamsBuilder) Args() []any
- func (b *QueryParamsBuilder) Cursor() (limit uint32, cursor any)
- func (b *QueryParamsBuilder) GetEnum(pos uint32) (int32, bool)
- func (b *QueryParamsBuilder) GetFlags() []int32
- func (b *QueryParamsBuilder) IsSet(pos uint32) bool
- func (b *QueryParamsBuilder) Name() string
- func (b *QueryParamsBuilder) Page() (limit uint32, offset uint32)
- func (b *QueryParamsBuilder) Reset()
- func (b *QueryParamsBuilder) Set(pos uint32, v any)
- func (b *QueryParamsBuilder) SetCursor(limit uint32, pos uint32, cursor any)
- func (b *QueryParamsBuilder) SetEnum(pos uint32, v int32)
- func (b *QueryParamsBuilder) SetFlags(v ...int32)
- func (b *QueryParamsBuilder) SetPage(limit, offset uint32)
- type Result
- type ResultWithCursor
- type Row
- type RowPointer
- type RowScanner
- type Rows
- type SQLProvider
- func (p *SQLProvider) BeginTx(ctx context.Context, _ *sql.TxOptions) (Provider, error)
- func (p *SQLProvider) Close() (err error)
- func (p *SQLProvider) Commit() error
- func (p *SQLProvider) ConnectionString() string
- func (p *SQLProvider) DB() DB
- func (p *SQLProvider) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (p *SQLProvider) IDTime(id uint64) time.Time
- func (p *SQLProvider) Name() string
- func (p *SQLProvider) NextID() ID
- func (p *SQLProvider) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (p *SQLProvider) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
- func (p *SQLProvider) Rollback() error
- func (p *SQLProvider) Tx() Tx
- func (p *SQLProvider) WithConnectionString(connstr string) *SQLProvider
- type SetCursor
- type Source
- type Strings
- type Time
- func (ns Time) Add(after time.Duration) Time
- func (ns Time) IsNil() bool
- func (ns Time) IsZero() bool
- func (ns Time) MarshalJSON() ([]byte, error)
- func (ns Time) Ptr() *time.Time
- func (ns *Time) Scan(value any) error
- func (ns Time) String() string
- func (ns Time) UTC() time.Time
- func (ns Time) UnixMilli() int64
- func (ns *Time) UnmarshalJSON(data []byte) error
- func (ns Time) Value() (driver.Value, error)
- type Tx
- type UUID
- type Validator
Constants ¶
const ( MaxLenForName = 64 MaxLenForEmail = 160 MaxLenForShortURL = 256 )
Max values, common for strings
const DefaultPageSize = 500
DefaultPageSize is the default page size
Variables ¶
var DefaultTimeFormat = "2006-01-02T15:04:05.999Z07:00"
DefaultTimeFormat is the default format for Time.String()
var DefaultTrucate = time.Millisecond
DefaultTrucate is the default time to truncate as Postgres time precision is default to 6 However, JavaScript and AWS accept time milliseconds only, 3 digits, so we truncate to 3
var MergeOpts = mergo.WithTransformers(transformers{})
MergeOpts is a mergo option to merge structs
Functions ¶
func DecodeCursor ¶ added in v0.17.76
DecodeCursor decodes the cursor into a map
func EncodeCursor ¶ added in v0.17.76
EncodeCursor encodes the offset or value into a cursor
func ExecuteListQuery ¶ added in v0.7.0
func ExecuteListQuery[T any, TPointer RowPointer[T]](ctx context.Context, sql DB, query string, args ...any) ([]TPointer, error)
ExecuteListQuery runs a query and returns a list of models
func ExecuteQuery ¶ added in v0.16.72
func ExecuteQuery[T any, TPointer RowPointer[T]](ctx context.Context, sql DB, res Result[T, TPointer], query string, args ...any) error
ExecuteQuery runs a query and populates the result with a list of models. args can be a QueryParams or a list of arguments
func ExecuteQueryWithCursor ¶ added in v0.17.76
func ExecuteQueryWithCursor[T any, TPointer RowPointer[T]](ctx context.Context, sql DB, cursor SetCursor[T, TPointer], res ResultWithCursor[T, TPointer], query string, args ...any) error
ExecuteQueryWithCursor runs a query and populates the result with a list of models and the next cursor, if there are more rows to fetch. args can be a QueryParams or a list of arguments followed by the limit and offset.
func ExecuteQueryWithPagination ¶ added in v0.16.72
func ExecuteQueryWithPagination[T any, TPointer RowPointer[T]](ctx context.Context, sql DB, res Result[T, TPointer], query string, args ...any) error
ExecuteQueryWithPagination runs a query and populates the result with a list of models and the next offset, if there are more rows to fetch. args can be a QueryParams or a list of arguments followed by the limit and offset.
func Int64Array ¶ added in v0.17.77
func Int64Array(list []uint64) pq.Int64Array
Int64Array returns pq.Int64Array
func IsNotFoundError ¶
IsNotFoundError returns true, if error is NotFound
func QueryRow ¶ added in v0.6.0
func QueryRow[T any, TPointer RowPointer[T]](ctx context.Context, sql DB, query string, args ...any) (TPointer, error)
QueryRow runs a query and returns a single model
Types ¶
type Bool ¶ added in v0.8.0
type Bool bool
Bool represents SQL bool NULL
func (Bool) MarshalJSON ¶ added in v0.8.0
MarshalJSON implements json.Marshaler interface
func (*Bool) UnmarshalJSON ¶ added in v0.8.0
UnmarshalJSON implements the json.Unmarshaler interface.
type DB ¶
type DB interface { // QueryContext executes a query that returns rows, typically a SELECT. // The args are for any placeholder parameters in the query. QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) // QueryRowContext executes a query that is expected to return at most one row. // QueryRowContext always returns a non-nil value. Errors are deferred until // Row's Scan method is called. // If the query selects no rows, the *Row's Scan will return ErrNoRows. // Otherwise, the *Row's Scan scans the first selected row and discards // the rest. QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row // ExecContext executes a query without returning any rows. // The args are for any placeholder parameters in the query. ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) }
DB provides interface for Db operations It's an interface accepted by Query, QueryRow and Exec methods. Both sql.DB, sql.Conn and sql.Tx can be passed as DB interface.
type Float ¶ added in v0.8.0
type Float float64
Float represents SQL float64 NULL
func (Float) MarshalJSON ¶ added in v0.8.0
MarshalJSON implements json.Marshaler interface
func (*Float) UnmarshalJSON ¶ added in v0.8.0
UnmarshalJSON implements the json.Unmarshaler interface.
type HasQueryParams ¶ added in v0.16.72
type HasQueryParams interface {
QueryParams() QueryParams
}
HasQueryParams is an interface for objects with query parameters.
type ID ¶
type ID struct {
// contains filtered or unexported fields
}
ID defines a type to convert between internal uint64 and external string representations of ID
func TryParseID ¶ added in v0.8.0
TryParseID returns ID or empty if val is not valid ID
func (ID) MarshalJSON ¶
MarshalJSON implements json.Marshaler interface
func (ID) MarshalYAML ¶ added in v0.20.82
func (*ID) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
func (*ID) UnmarshalYAML ¶ added in v0.20.82
type ID32 ¶ added in v0.8.0
type ID32 uint32
ID32 defines a type to convert between internal uint32 and NULL values in DB
func (ID32) MarshalJSON ¶ added in v0.8.0
MarshalJSON implements json.Marshaler interface
func (*ID32) UnmarshalJSON ¶ added in v0.8.0
UnmarshalJSON implements the json.Unmarshaler interface.
type IDArray ¶
type IDArray []ID
IDArray defines a list of IDArray
func NewIDArray ¶ added in v0.13.1
NewIDArray returns IDArray
func (IDArray) Int64Array ¶ added in v0.17.77
func (n IDArray) Int64Array() pq.Int64Array
Int64Array returns pq.Int64Array
func (IDArray) Overlaps ¶ added in v0.19.80
Overlaps returns true if any of the IDs in the list is in the other list
func (IDArray) String ¶ added in v0.16.75
String returns string representation of IDs, concatenated with comma
type IDGenerator ¶
type IDGenerator interface { // NextID generates a next unique ID. NextID() ID IDTime(id uint64) time.Time }
IDGenerator defines an interface to generate unique ID accross the cluster
type Int32 ¶ added in v0.8.0
type Int32 int32
Int32 represents SQL int NULL
func (Int32) MarshalJSON ¶ added in v0.8.0
MarshalJSON implements json.Marshaler interface
func (*Int32) UnmarshalJSON ¶ added in v0.8.0
UnmarshalJSON implements the json.Unmarshaler interface.
type Int64 ¶ added in v0.8.1
type Int64 int64
Int64 represents SQL int64 NULL
func (Int64) MarshalJSON ¶ added in v0.8.1
MarshalJSON implements json.Marshaler interface
func (*Int64) UnmarshalJSON ¶ added in v0.8.1
UnmarshalJSON implements the json.Unmarshaler interface.
type Metadata ¶
Metadata de/encodes the string map to/from a SQL string.
type MigrationConfig ¶
MigrationConfig defines migration configuration
type NULLString ¶
type NULLString string
NULLString de/encodes the string a SQL string.
func (*NULLString) Scan ¶
func (ns *NULLString) Scan(value any) error
Scan implements the Scanner interface.
func (NULLString) String ¶ added in v0.13.1
func (ns NULLString) String() string
String returns string
type PageableByCursor ¶ added in v0.17.76
type PageableByCursor interface { // Cursor returns the limit and cursor for pagination. Cursor() (limit uint32, cursor any) }
PageableByCursor is an interface for pagination. The cursor and limit are the last two arguments. The cursor argument is before limit because it's used in WHERE clause.
type PageableByOffset ¶ added in v0.17.76
type PageableByOffset interface { // Page returns the limit and offset for pagination. Page() (limit uint32, offset uint32) }
PageableByOffset is an interface for pagination. The limit and offset are the last two arguments.
type Provider ¶
type Provider interface { IDGenerator DB Tx // Name returns provider name: postgres, sqlserver, etc Name() string ConnectionString() string // DB returns underlying DB connection DB() DB // Tx returns underlying DB transaction Tx() Tx // Close connection and release resources Close() (err error) BeginTx(ctx context.Context, opts *sql.TxOptions) (Provider, error) }
Provider provides complete DB access
func NewProvider ¶
func NewProvider(dataSource, dbName string, idGen flake.IDGenerator, migrateCfg *MigrationConfig) (Provider, error)
NewProvider creates a Provider instance
type QueryParams ¶ added in v0.16.72
type QueryParams interface { PageableByOffset PageableByCursor Name() string Args() []any // IsSet checks if a positional query parameter is set. IsSet(pos uint32) bool // GetEnum checks if an enum query parameter is set. GetEnum(pos uint32) (int32, bool) // GetFlags returns additional flags for query parameter. GetFlags() []int32 }
QueryParams is an interface for query parameters.
func GetQueryParams ¶ added in v0.16.72
func GetQueryParams(args ...any) QueryParams
GetQueryParams returns query parameters from an object.
type QueryParamsBuilder ¶ added in v0.16.72
type QueryParamsBuilder struct {
// contains filtered or unexported fields
}
QueryParams is a placeholder for query parameters.
func NewQueryParams ¶ added in v0.16.72
func NewQueryParams(queryName string) *QueryParamsBuilder
NewQueryParams creates a new query parameters builder.
func (*QueryParamsBuilder) AddArgs ¶ added in v0.16.72
func (b *QueryParamsBuilder) AddArgs(v ...any)
AddArgs adds an additional query arguments, such as Limit or Offset
func (*QueryParamsBuilder) Args ¶ added in v0.16.72
func (b *QueryParamsBuilder) Args() []any
Args returns a list of query arguments.
func (*QueryParamsBuilder) Cursor ¶ added in v0.17.76
func (b *QueryParamsBuilder) Cursor() (limit uint32, cursor any)
Cursor returns the limit and cursor for pagination, if supported
func (*QueryParamsBuilder) GetEnum ¶ added in v0.16.72
func (b *QueryParamsBuilder) GetEnum(pos uint32) (int32, bool)
GetEnum checks if an enum query parameter is set.
func (*QueryParamsBuilder) GetFlags ¶ added in v0.16.72
func (b *QueryParamsBuilder) GetFlags() []int32
GetFlags returns additional flags for query parameter.
func (*QueryParamsBuilder) IsSet ¶ added in v0.16.72
func (b *QueryParamsBuilder) IsSet(pos uint32) bool
IsSet checks if a positional query parameter is set.
func (*QueryParamsBuilder) Name ¶ added in v0.16.72
func (b *QueryParamsBuilder) Name() string
Name returns a hash of the query parameters.
func (*QueryParamsBuilder) Page ¶ added in v0.16.72
func (b *QueryParamsBuilder) Page() (limit uint32, offset uint32)
Page returns the limit and offset for pagination, if supported
func (*QueryParamsBuilder) Reset ¶ added in v0.16.72
func (b *QueryParamsBuilder) Reset()
func (*QueryParamsBuilder) Set ¶ added in v0.16.72
func (b *QueryParamsBuilder) Set(pos uint32, v any)
Set sets a positional query parameter, and adds it to the list of arguments.
func (*QueryParamsBuilder) SetCursor ¶ added in v0.17.76
func (b *QueryParamsBuilder) SetCursor(limit uint32, pos uint32, cursor any)
SetCursor sets the limit for pagination, and adds it to the list of arguments.
func (*QueryParamsBuilder) SetEnum ¶ added in v0.16.72
func (b *QueryParamsBuilder) SetEnum(pos uint32, v int32)
SetEnum sets an enum query parameter, without adding it to the list of arguments.
func (*QueryParamsBuilder) SetFlags ¶ added in v0.16.72
func (b *QueryParamsBuilder) SetFlags(v ...int32)
SetFlags sets additional flags for query parameter.
func (*QueryParamsBuilder) SetPage ¶ added in v0.16.72
func (b *QueryParamsBuilder) SetPage(limit, offset uint32)
SetPage sets the limit for pagination, and adds it to the list of arguments.
type Result ¶
type Result[T any, TPointer RowPointer[T]] interface { SetResult(rows []TPointer, hasNextPage bool, nextOffset uint32) }
Result describes the result of a list query
type ResultWithCursor ¶ added in v0.17.76
type ResultWithCursor[T any, TPointer RowPointer[T]] interface { SetResultWithCursor(rows []TPointer, hasNextPage bool, cursor func(lastRow TPointer) string) }
ResultWithCursor describes the result of a list query with a cursor
type Row ¶
type Row interface { // Scan copies the columns from the matched row into the values // pointed at by dest. See the documentation on Rows.Scan for details. // If more than one row matches the query, // Scan uses the first row and discards the rest. If no row matches // the query, Scan returns ErrNoRows. Scan(dest ...any) error // Err provides a way for wrapping packages to check for // query errors without calling Scan. // Err returns the error, if any, that was encountered while running the query. // If this error is not nil, this error will also be returned from Scan. Err() error }
Row defines an interface for DB row
type RowPointer ¶
type RowPointer[T any] interface { *T RowScanner }
RowPointer defines a generic interface to scan a single row
type RowScanner ¶
RowScanner defines an interface to scan a single row
type Rows ¶
type Rows interface { io.Closer Row // Next prepares the next result row for reading with the Scan method. It // returns true on success, or false if there is no next result row or an error // happened while preparing it. Err should be consulted to distinguish between // the two cases. // // Every call to Scan, even the first one, must be preceded by a call to Next. Next() bool // NextResultSet prepares the next result set for reading. It reports whether // there is further result sets, or false if there is no further result set // or if there is an error advancing to it. The Err method should be consulted // to distinguish between the two cases. // // After calling NextResultSet, the Next method should always be called before // scanning. If there are further result sets they may not have rows in the result // set. NextResultSet() bool }
Rows defines an interface for DB rows
type SQLProvider ¶
type SQLProvider struct {
// contains filtered or unexported fields
}
SQLProvider represents SQL client instance
func New ¶
func New(name string, db *sql.DB, idGen flake.IDGenerator) (*SQLProvider, error)
New creates a Provider instance
func (*SQLProvider) BeginTx ¶
BeginTx starts a transaction.
The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginTx is canceled.
The provided TxOptions is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.
func (*SQLProvider) Close ¶
func (p *SQLProvider) Close() (err error)
Close connection and release resources
func (*SQLProvider) Commit ¶ added in v0.2.0
func (p *SQLProvider) Commit() error
func (*SQLProvider) ConnectionString ¶ added in v0.18.79
func (p *SQLProvider) ConnectionString() string
func (*SQLProvider) ExecContext ¶ added in v0.2.0
func (p *SQLProvider) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (*SQLProvider) IDTime ¶
func (p *SQLProvider) IDTime(id uint64) time.Time
IDTime returns time when ID was generated
func (*SQLProvider) Name ¶ added in v0.6.0
func (p *SQLProvider) Name() string
Name returns provider name
func (*SQLProvider) QueryContext ¶ added in v0.2.0
func (p *SQLProvider) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
func (*SQLProvider) QueryRowContext ¶ added in v0.2.0
QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.
func (*SQLProvider) Rollback ¶ added in v0.2.0
func (p *SQLProvider) Rollback() error
func (*SQLProvider) WithConnectionString ¶ added in v0.18.79
func (p *SQLProvider) WithConnectionString(connstr string) *SQLProvider
type SetCursor ¶ added in v0.17.76
type SetCursor[T any, TPointer RowPointer[T]] func(lastRow TPointer) string
type Source ¶ added in v0.2.2
type Source struct { Source string Driver string Host string User string Password string Database string Params map[string]string }
Source describes connection info
func ParseConnectionString ¶ added in v0.2.2
ParseConnectionString return parsed Source from sqlserver://username:password@host/instance?param1=value¶m2=value
type Strings ¶
type Strings []string
Strings de/encodes the string slice to/from a SQL string.
type Time ¶
Time implements sql.Time functionality and always returns UTC
func FromUnixMilli ¶
FromUnixMilli returns Time from Unix milliseconds elapsed since January 1, 1970 UTC.
func (Time) IsNil ¶
IsNil reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.
func (Time) IsZero ¶
IsZero reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.
func (Time) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface. The time is a quoted string in RFC 3339 format, with sub-second precision added if present.
func (Time) String ¶
String returns string in RFC3339 format, if it's Zero time, an empty string is returned
func (Time) UnixMilli ¶
UnixMilli returns t as a Unix time, the number of milliseconds elapsed since January 1, 1970 UTC.
func (*Time) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface. The time is expected to be a quoted string in RFC 3339 format.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
internal
|
|
cli
Package cli provides CLI app and global flags
|
Package cli provides CLI app and global flags |
cli/clisuite
Package clisuite to test CLI commands
|
Package clisuite to test CLI commands |
cli/schema
Package schema provides CLI commands
|
Package schema provides CLI commands |
mocks
|
|
mockschema
Package mockschema is a generated GoMock package.
|
Package mockschema is a generated GoMock package. |
mockxdb
Package mockxdb is a generated GoMock package.
|
Package mockxdb is a generated GoMock package. |
pkg
|
|
flake
Package flake implements Snowflake, a distributed unique ID generator inspired by Twitter's Snowflake.
|
Package flake implements Snowflake, a distributed unique ID generator inspired by Twitter's Snowflake. |
print
Package print provides helper package to print objects.
|
Package print provides helper package to print objects. |
Package schema provides helper package to generate schema.
|
Package schema provides helper package to generate schema. |
Package xsql is an SQL statement builder and executor.
|
Package xsql is an SQL statement builder and executor. |