Documentation
¶
Index ¶
- Variables
- func QueryAsync[T any](ctx context.Context, db QueryDB, mapper RowMapper[T], query string, ...) (ch <-chan ValueOrError[T], leave func())
- func QueryJSONAsync(ctx context.Context, db JsonQueryDB, query string, params ...any) (ch <-chan ValueOrError[json.RawMessage], leave func())
- func QueryJSONMapAsync(ctx context.Context, db JsonQueryDB, query string, params ...any) (ch <-chan ValueOrError[map[string]any], leave func())
- func RegisterDBFactory(name string, factory DBFactory)
- func RegisterDialectFactory(name string, factory DialectFactory)
- type CatalogAdmin
- type CatalogRef
- type ColumnRef
- type DB
- type DBFactory
- type Dialect
- type DialectFactory
- type Expression
- type Expressions
- type FilterOptions
- type GoquExpression
- type JsonQueryDB
- type JsonRowMapper
- type NilAny
- type Option
- type Options
- type QueryCondition
- type QueryDB
- type QueryDef
- type QueryOrder
- type RelationRef
- type RelationType
- type RowMapper
- type RowScan
- type SchemaAdmin
- type SchemaRef
- type TableAdmin
- type TableListOptions
- type ValueOrError
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotSupported = errors.New("sqconnect: feature not supported") ErrDropOldTablePostCopy = errors.New("sqlconnect move table: dropping old table after copying its contents to the new table") )
Functions ¶
func QueryAsync ¶
func QueryAsync[T any](ctx context.Context, db QueryDB, mapper RowMapper[T], query string, params ...any) (ch <-chan ValueOrError[T], leave func())
QueryAsync executes a query and returns a channel that will receive the results or an error, along with a function that the caller can use to leave the channel early. The channel will be closed when the query is done or when the context is canceled.
func QueryJSONAsync ¶
func QueryJSONAsync(ctx context.Context, db JsonQueryDB, query string, params ...any) (ch <-chan ValueOrError[json.RawMessage], leave func())
QueryJSONAsync executes a query and returns a channel that will receive the results as json or an error, along with a function that the caller can use to leave the channel early. The channel will be closed when the query is done or when the context is canceled.
Non-finite floats (NaN, +Inf, -Inf) are emitted as JSON null, at any depth, because JSON cannot represent them and json.Marshal would otherwise fail the entire result set rather than the one offending cell. The substitution is silent and lossy in one specific way: in the emitted JSON a non-finite value is indistinguishable from a real SQL NULL, so a caller deriving null-rate style metrics from this output will count such rows as unpopulated. Use QueryJSONMapAsync, which does not perform the substitution, when the distinction matters.
func QueryJSONMapAsync ¶
func QueryJSONMapAsync(ctx context.Context, db JsonQueryDB, query string, params ...any) (ch <-chan ValueOrError[map[string]any], leave func())
QueryJSONMapAsync executes a query and returns a channel that will receive the results as a map or an error, along with a function that the caller can use to leave the channel early. The channel will be closed when the query is done or when the context is canceled.
func RegisterDBFactory ¶
func RegisterDialectFactory ¶ added in v1.23.0
func RegisterDialectFactory(name string, factory DialectFactory)
Types ¶
type CatalogAdmin ¶ added in v1.1.0
type CatalogAdmin interface {
// CurrentCatalog returns the current catalog.
// If this operation is not supported by the warehouse [ErrNotSupported] will be returned.
CurrentCatalog(ctx context.Context) (CatalogRef, error)
// ListCatalogs returns all available catalogs linked to the credentials. System catalogs are filtered out.
// If this operation is not supported by the warehouse [ErrNotSupported] will be returned.
ListCatalogs(ctx context.Context) ([]CatalogRef, error)
}
type CatalogRef ¶ added in v1.24.0
type CatalogRef struct {
Name string `json:"name"` // the catalog/database name
}
CatalogRef provides a reference to a database catalog
func (CatalogRef) String ¶ added in v1.24.0
func (c CatalogRef) String() string
type ColumnRef ¶
type ColumnRef struct {
Name string `json:"name"`
Type string `json:"type"`
RawType string `json:"rawType"`
}
ColumnRef provides a reference to a table column
type DB ¶
type DB interface {
// SqlDB returns the underlying *sql.DB
SqlDB() *sql.DB
CatalogAdmin
SchemaAdmin
TableAdmin
JsonRowMapper
Dialect
// contains filtered or unexported methods
}
type Dialect ¶
type Dialect interface {
// QuoteTable quotes a table name
QuoteTable(table RelationRef) string
// QuoteIdentifier quotes an identifier, e.g. a column name
QuoteIdentifier(name string) string
// FormatTableName formats a table name, typically by lower or upper casing it, depending on the database
//
// Deprecated: to be removed in future versions, since its behaviour is not consistent across databases, e.g. using lowercase for BigQuery while it shouldn't.
// If you want to have a consistent behaviour across databases, use [NormaliseIdentifier] and [ParseRelationRef] instead.
FormatTableName(name string) string
// NormaliseIdentifier normalises the identifier's parts that are unquoted, typically by lower or upper casing them, depending on the database
NormaliseIdentifier(identifier string) string
// ParseRelationRef parses a string into a RelationRef after normalising the identifier and stripping out surrounding quotes.
// The result is a RelationRef with case-sensitive fields, i.e. it can be safely quoted (see [QuoteTable] and, for instance, used for matching against the database's information schema.
ParseRelationRef(identifier string) (RelationRef, error)
// QueryCondition returns a dialect-specific query expression for the provided identifier, operator and value(s).
//
// E.g. QueryCondition("age", "gt", 18) returns "age > 18"
//
// Each operator has a different number of arguments, e.g. [eq] requires one argument, [in] requires at least one argument, etc.
// See [op] package for the list of supported operators
QueryCondition(identifier, operator string, args ...any) (Expression, error)
// ParseGoquExpression converts a goqu Expression to an Expression
ParseGoquExpression(goquExpression GoquExpression) (Expression, error)
// Expressions returns the dialect-specific expressions
Expressions() Expressions
}
func NewDialect ¶ added in v1.23.0
func NewDialect(name string, optionsJSON json.RawMessage) (Dialect, error)
NewDialect creates a new database dialect for the provided name.
type DialectFactory ¶ added in v1.23.0
type DialectFactory func(optionsJSON json.RawMessage) (Dialect, error)
type Expression ¶ added in v1.10.0
type Expression interface {
GoquExpression() GoquExpression
fmt.Stringer
}
Expression represents a dialect-specific expression. One can get the expression's SQL string by calling [String()] on it.
type Expressions ¶ added in v1.10.0
type Expressions interface {
// TimestampAdd returns an expression that adds the interval to the timestamp value.
// The value can either be a string literal (column, timestamp, function etc.) or a [time.Time] value.
TimestampAdd(timeValue any, interval int, unit string) (Expression, error)
// DateAdd returns an expression that adds the interval to the date value.
// The value can either be a string literal (column, timestamp, function etc.) or a [time.Time] value.
// Values are cast to [DATE].
DateAdd(dateValue any, interval int, unit string) (Expression, error)
// Literal creates a literal sql expression
Literal(sql string, args ...any) (Expression, error)
}
Expressions provides dialect-specific expressions
type FilterOptions ¶ added in v1.25.0
type FilterOptions struct {
Catalog string
}
func NewFilterOptions ¶ added in v1.25.0
func NewFilterOptions(opts ...Option) (FilterOptions, error)
type GoquExpression ¶ added in v1.10.0
type GoquExpression = goqu.Expression
GoquExpression represents a goqu expression
type JsonQueryDB ¶
type JsonRowMapper ¶
type Option ¶
type Option func(options *Options)
func WithCatalog ¶
func WithPrefix ¶ added in v1.25.0
func WithRelationType ¶
func WithRelationType(relationType RelationType) Option
func WithSchema ¶
type Options ¶ added in v1.25.0
type Options struct {
Schema string
Catalog string
Type RelationType
Prefix string
}
func NewOptions ¶ added in v1.25.0
type QueryCondition ¶
type QueryCondition struct {
Column string `json:"column,omitempty"`
Operator string `json:"operator,omitempty"`
Value string `json:"value,omitempty"`
}
QueryCondition defines a query condition.
type QueryDef ¶
type QueryDef struct {
Table RelationRef `json:"table"` // Reference to table that should be queried
Columns []string `json:"columns,omitempty"` // Columns that should be included. Defaults to "*" if nil or empty.
Conditions []*QueryCondition `json:"conditions,omitempty"` // Conditions is a list of query conditions.
OrderBy *QueryOrder `json:"order_by,omitempty"` // OrderBy defines the query's order by clause.
}
QueryDef describes a query that consists of a table and columns that should be queried.
type QueryOrder ¶
type QueryOrder struct {
Column string // the order by column
Order string // supported values are ('ASC', 'DESC')
}
QueryOrder defines the query's order by clause.This only supports one order by column.
type RelationRef ¶
type RelationRef struct {
Name string `json:"name"` // the relation's name
Schema string `json:"schema,omitempty"` // the relation's schema
Catalog string `json:"catalog,omitempty"` // the relation's catalog
Type RelationType `json:"type,omitempty"` // the relation's type
}
RelationRef provides a reference to a database table
func NewRelationRef ¶
func NewRelationRef(name string, options ...Option) RelationRef
func NewSchemaTableRef ¶
func NewSchemaTableRef(schema, table string) RelationRef
NewSchemaTableRef creates a new RelationRef with a schema and a table
func (RelationRef) String ¶
func (t RelationRef) String() string
func (*RelationRef) UnmarshalJSON ¶
func (r *RelationRef) UnmarshalJSON(data []byte) error
type RelationType ¶
type RelationType string
const ( TableRelation RelationType = "table" ViewRelation RelationType = "view" )
type RowMapper ¶
type RowMapper[T any] func(cols []*sql.ColumnType, row RowScan) (T, error)
RowMapper is a function that maps database rows to a value
type SchemaAdmin ¶
type SchemaAdmin interface {
// CreateSchema creates a schema
CreateSchema(ctx context.Context, schema SchemaRef) error
// ListSchemas returns a list of schemas.
//
// Supported options:
// - [WithCatalog]: scope the listing to a specific catalog.
//
// schemas, err := db.ListSchemas(ctx, WithCatalog("my_catalog"))
ListSchemas(ctx context.Context, opts ...Option) ([]SchemaRef, error)
// SchemaExists returns true if the schema exists.
//
// Supported options:
// - [WithCatalog]: scope the check to a specific catalog.
//
// exists, err := db.SchemaExists(ctx, schemaRef, WithCatalog("my_catalog"))
SchemaExists(ctx context.Context, schemaRef SchemaRef, opts ...Option) (bool, error)
// DropSchema drops a schema.
DropSchema(ctx context.Context, schema SchemaRef) error
}
type SchemaRef ¶
type SchemaRef struct {
Name string `json:"name"` // the schema
}
SchemaRef provides a reference to a database schema
type TableAdmin ¶
type TableAdmin interface {
// CreateTestTable creates a test table
CreateTestTable(ctx context.Context, relation RelationRef) error
// ListTables returns a list of tables in the given schema.
//
// Supported options:
// - [WithCatalog]: scope the listing to a specific catalog.
// - [WithPrefix]: filter tables by name prefix.
//
// tables, err := db.ListTables(ctx, schema, WithCatalog("my_catalog"), WithPrefix("test"))
ListTables(ctx context.Context, schema SchemaRef, opts ...Option) ([]RelationRef, error)
// TableExists returns true if the table exists. If RelationRef.Catalog is set, the check is scoped to that catalog.
TableExists(ctx context.Context, relation RelationRef) (bool, error)
// ListColumns returns a list of columns for the given table
ListColumns(ctx context.Context, relation RelationRef) ([]ColumnRef, error)
// ListColumnsForSqlQuery returns a list of columns for the given sql query
ListColumnsForSqlQuery(ctx context.Context, sql string) ([]ColumnRef, error)
// CountTableRows returns the number of rows in the given table
CountTableRows(ctx context.Context, table RelationRef) (count int, err error)
// DropTable drops a table
DropTable(ctx context.Context, ref RelationRef) error
// TruncateTable truncates a table
TruncateTable(ctx context.Context, ref RelationRef) error
// RenameTable renames a table. It might fall back to using MoveTable if the underlying database does not support renaming tables.
RenameTable(ctx context.Context, oldRef, newRef RelationRef) error
// MoveTable creates a new table by copying the old table's contents to it and then drops the old table. Returns [ErrDropOldTablePostCopy] if the old table could not be dropped after copy.
MoveTable(ctx context.Context, oldRef, newRef RelationRef) error
// CreateTableFromQuery creates a table from the results of a query
CreateTableFromQuery(ctx context.Context, table RelationRef, query string) error
// GetRowCountForQuery returns the number of rows returned by the query
GetRowCountForQuery(ctx context.Context, query string, params ...any) (int, error)
}
type TableListOptions ¶ added in v1.25.0
func NewTableListOptions ¶ added in v1.25.0
func NewTableListOptions(opts ...Option) (TableListOptions, error)
type ValueOrError ¶
ValueOrError represents a value or an error