base

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 26, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ColumnType

type ColumnType interface {
	DatabaseTypeName() string
	DecimalSize() (precision, scale int64, ok bool)
}

type DB

type DB struct {
	*sql.DB
	sqlconnect.Dialect
	// contains filtered or unexported fields
}

func NewDB

func NewDB(db *sql.DB, tunnelCloser func() error, opts ...Option) *DB

func (*DB) Close added in v1.1.0

func (d *DB) Close() error

Close closes the db and the tunnel

func (*DB) CountTableRows

func (c *DB) CountTableRows(ctx context.Context, relation sqlconnect.RelationRef) (int, error)

CountTableRows returns the number of rows in the given table

func (*DB) CreateSchema

func (db *DB) CreateSchema(ctx context.Context, schema sqlconnect.SchemaRef) error

CreateSchema creates a schema

func (*DB) CreateTableFromQuery

func (db *DB) CreateTableFromQuery(ctx context.Context, table sqlconnect.RelationRef, query string) error

CreateTableFromQuery creates a table from the results of a query

func (*DB) CreateTestTable

func (db *DB) CreateTestTable(ctx context.Context, table sqlconnect.RelationRef) error

CreateTestTable creates a test table

func (*DB) CurrentCatalog added in v1.1.0

func (db *DB) CurrentCatalog(ctx context.Context) (string, error)

CurrentCatalog returns the current catalog

func (*DB) DropSchema

func (db *DB) DropSchema(ctx context.Context, schemaRef sqlconnect.SchemaRef) error

DropSchema drops a schema

func (*DB) DropTable

func (db *DB) DropTable(ctx context.Context, ref sqlconnect.RelationRef) error

DropTable drops a table

func (*DB) GetRowCountForQuery

func (db *DB) GetRowCountForQuery(ctx context.Context, query string, params ...any) (int, error)

GetRowCountForQuery returns the number of rows returned by the query

func (*DB) JSONRowMapper

func (db *DB) JSONRowMapper() sqlconnect.RowMapper[map[string]any]

JSONRowMapper returns a row mapper that maps scanned rows to [map[string]any]

func (*DB) ListColumns

func (db *DB) ListColumns(ctx context.Context, relation sqlconnect.RelationRef) ([]sqlconnect.ColumnRef, error)

ListColumns returns a list of columns for the given table

func (*DB) ListColumnsForSqlQuery

func (db *DB) ListColumnsForSqlQuery(ctx context.Context, sql string) ([]sqlconnect.ColumnRef, error)

ListColumnsForSqlQuery returns a list of columns for the given sql query

func (*DB) ListSchemas

func (db *DB) ListSchemas(ctx context.Context) ([]sqlconnect.SchemaRef, error)

ListSchemas returns a list of schemas

func (*DB) ListTables

func (db *DB) ListTables(ctx context.Context, schema sqlconnect.SchemaRef) ([]sqlconnect.RelationRef, error)

ListTables returns a list of tables in the given schema

func (*DB) ListTablesWithPrefix

func (db *DB) ListTablesWithPrefix(ctx context.Context, schema sqlconnect.SchemaRef, prefix string) ([]sqlconnect.RelationRef, error)

ListTablesWithPrefix returns a list of tables in the given schema that have the given prefix

func (*DB) MoveTable

func (db *DB) MoveTable(ctx context.Context, oldRef, newRef sqlconnect.RelationRef) error

MoveTable copies the old table's contents to the new table and drops the old table. Returns [ErrDropOldTablePostCopy] if the old table could not be dropped after the copy.

func (*DB) RenameTable

func (db *DB) RenameTable(ctx context.Context, oldRef, newRef sqlconnect.RelationRef) error

RenameTable renames a table

func (*DB) SchemaExists

func (db *DB) SchemaExists(ctx context.Context, schemaRef sqlconnect.SchemaRef) (bool, error)

SchemaExists returns true if the schema exists

func (*DB) SqlDB

func (db *DB) SqlDB() *sql.DB

SqlDB returns the underlying *sql.DB

func (*DB) TableExists

func (db *DB) TableExists(ctx context.Context, relation sqlconnect.RelationRef) (bool, error)

TableExists returns true if the table exists

func (*DB) TruncateTable

func (db *DB) TruncateTable(ctx context.Context, ref sqlconnect.RelationRef) error

TruncateTable truncates a table

type Option

type Option func(*DB)

func WithColumnTypeMapper

func WithColumnTypeMapper(columnTypeMapper func(ColumnType) string) Option

WithColumnTypeMapper sets the column type mapper for the client

func WithColumnTypeMappings

func WithColumnTypeMappings(columnTypeMappings map[string]string) Option

WithColumnTypeMappings sets the column type mappings for the client

func WithDialect

func WithDialect(dialect sqlconnect.Dialect) Option

WithDialect sets the dialect for the client

func WithJsonRowMapper

func WithJsonRowMapper(jsonRowMapper func(string, any) any) Option

WithJsonRowMapper sets the json row mapper for the client

func WithSQLCommandsOverride

func WithSQLCommandsOverride(override func(defaultCommands SQLCommands) SQLCommands) Option

WithSQLCommandsOverride allows for overriding some of the sql commands that the client uses

type QuotedIdentifier

type QuotedIdentifier string // A quoted identifier is a string that is quoted, e.g. "my_table"

type SQLCommands

type SQLCommands struct {
	// Provides the SQL command to get the current catalog
	CurrentCatalog func() string
	// Provides the SQL command to create a schema
	CreateSchema func(schema QuotedIdentifier) string
	// Provides the SQL command to list all schemas
	ListSchemas func() (sql, columnName string)
	// Provides the SQL command to check if a schema exists
	SchemaExists func(schema UnquotedIdentifier) string
	// Provides the SQL command to drop a schema
	DropSchema func(schema QuotedIdentifier) string
	// Provides the SQL command to create a test table
	CreateTestTable func(table QuotedIdentifier) string
	// Provides the SQL command(s) to list all tables in a schema along with the column name that contains the table name in the result set
	ListTables func(schema UnquotedIdentifier) (sqlAndColumnNamePairs []lo.Tuple2[string, string])
	// Provides the SQL command(s) to list all tables in a schema with a prefix along with the column name that contains the table name in the result set
	ListTablesWithPrefix func(schema UnquotedIdentifier, prefix string) []lo.Tuple2[string, string]
	// Provides the SQL command to check if a table exists
	TableExists func(schema, table UnquotedIdentifier) string
	// Provides the SQL command to list all columns in a table along with the column names in the result set that point to the name and type
	ListColumns func(catalog, schema, table UnquotedIdentifier) (sql, nameCol, typeCol string)
	// Provides the SQL command to count the rows in a table
	CountTableRows func(table QuotedIdentifier) string
	// Provides the SQL command to drop a table
	DropTable func(table QuotedIdentifier) string
	// Provides the SQL command to truncate a table
	TruncateTable func(table QuotedIdentifier) string
	// Provides the SQL command to rename a table
	RenameTable func(schema, oldName, newName QuotedIdentifier) string
	// Provides the SQL command to move a table
	MoveTable func(schema, oldName, newName QuotedIdentifier) string
}

type UnquotedIdentifier

type UnquotedIdentifier string // An unquoted identifier is a string that is not quoted, e.g. my_table

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL