sqldb

package
v0.0.111 Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package sqldb provides an SQL database abstraction for performing queries and interacting with the database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanQuery added in v0.0.54

func CleanQuery(query string) string

CleanQuery cleans sql query from double white space, comments and leading/trailing spaces.

Types

type CockroachDB

type CockroachDB struct {
	*Postgres
}

CockroachDB represents the CockroachDB database engine.

func NewCockroachDB

func NewCockroachDB(dataSourceName string, optFns ...func(o *CockroachDBOptions)) (*CockroachDB, error)

NewCockroachDB creates a new instance of the CockroachDB database engine.

func (*CockroachDB) Dialect

func (e *CockroachDB) Dialect() string

Dialect returns the dialect of the CockroachDB database engine.

func (CockroachDB) Inspect

func (e CockroachDB) Inspect(ctx context.Context, name string, opts *schema.InspectOptions) (map[string]string, error)

Inspect retrieves information about the schema.

type CockroachDBOptions

type CockroachDBOptions struct {
	DriverName string
}

CockroachDBOptions holds options for the CockroachDB database engine.

type Engine

type Engine interface {
	// Dialect returns the dialect of the SQL database engine.
	Dialect() string

	// SampleRowsQuery returns the query to retrieve a sample of rows from the specified table (table) with a limit of (k) rows.
	SampleRowsQuery(table string, k uint) string

	// Inspect retrieves information about the database schema with the specified name (name) and options (opts).
	// The returned map contains table names as keys and their corresponding CREATE TABLE statements as values.
	Inspect(ctx context.Context, name string, opts *schema.InspectOptions) (map[string]string, error)

	// Exec executes an SQL query with the provided query string and arguments (args), returning the result and any errors encountered.
	Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

	// Query executes an SQL query with the provided query string and arguments (args), returning the rows and any errors encountered.
	Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

	// QueryRow executes an SQL query with the provided query string and arguments (args), returning a single row and any errors encountered.
	QueryRow(ctx context.Context, query string, args ...any) *sql.Row

	// Close closes the database connection.
	Close() error
}

Engine defines the interface for an SQL database engine.

type MariaDB

type MariaDB struct {
	*MySQL
}

MariaDB represents the MariaDB database engine.

func NewMariaDB

func NewMariaDB(dataSourceName string, optFns ...func(o *MariaDBOptions)) (*MariaDB, error)

NewMariaDB creates a new instance of the MariaDB database engine.

func (*MariaDB) Dialect

func (e *MariaDB) Dialect() string

Dialect returns the dialect of the MariaDB database engine.

func (MariaDB) Inspect

func (e MariaDB) Inspect(ctx context.Context, name string, opts *schema.InspectOptions) (map[string]string, error)

Inspect retrieves information about the schema.

type MariaDBOptions

type MariaDBOptions struct {
	DriverName string
}

MariaDBOptions holds options for the MariaDB database engine.

type MySQL

type MySQL struct {
	// contains filtered or unexported fields
}

MySQL represents the MySQL database engine.

func NewMySQL

func NewMySQL(dataSourceName string, optFns ...func(o *MySQLOptions)) (*MySQL, error)

NewMySQL creates a new instance of the MySQL database engine.

func (*MySQL) Close

func (e *MySQL) Close() error

Close closes the database connection.

func (*MySQL) Dialect

func (e *MySQL) Dialect() string

Dialect returns the dialect of the MySQL database engine.

func (*MySQL) Exec

func (e *MySQL) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

Exec executes an SQL query with the provided query string and arguments (args), returning the result and any errors encountered.

func (MySQL) Inspect

func (e MySQL) Inspect(ctx context.Context, name string, opts *schema.InspectOptions) (map[string]string, error)

Inspect retrieves information about the schema.

func (*MySQL) Query

func (e *MySQL) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

Query executes an SQL query with the provided query string and arguments (args), returning the rows and any errors encountered.

func (*MySQL) QueryRow

func (e *MySQL) QueryRow(ctx context.Context, query string, args ...any) *sql.Row

QueryRow executes an SQL query with the provided query string and arguments (args), returning a single row and any errors encountered.

func (*MySQL) SampleRowsQuery

func (e *MySQL) SampleRowsQuery(table string, k uint) string

SampleRowsQuery returns the query to retrieve a sample of rows from the specified table (table) with a limit of (k) rows.

type MySQLOptions

type MySQLOptions struct {
	DriverName string
}

MySQLOptions holds options for the MySQL database engine.

type Parser added in v0.0.54

type Parser struct {
	// contains filtered or unexported fields
}

func NewParser added in v0.0.54

func NewParser(query string) *Parser

func (*Parser) IsSelect added in v0.0.54

func (p *Parser) IsSelect() bool

func (*Parser) TableNames added in v0.0.54

func (p *Parser) TableNames() []string

type Postgres

type Postgres struct {
	// contains filtered or unexported fields
}

Postgres represents the Postgres database engine.

func NewPostgres

func NewPostgres(dataSourceName string, optFns ...func(o *PostgresOptions)) (*Postgres, error)

NewPostgres creates a new instance of the Postgres database engine.

func (*Postgres) Close

func (e *Postgres) Close() error

Close closes the database connection.

func (*Postgres) Dialect

func (e *Postgres) Dialect() string

Dialect returns the dialect of the Postgres database engine.

func (*Postgres) Exec

func (e *Postgres) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

Exec executes an SQL query with the provided query string and arguments (args), returning the result and any errors encountered.

func (Postgres) Inspect

func (e Postgres) Inspect(ctx context.Context, name string, opts *schema.InspectOptions) (map[string]string, error)

Inspect retrieves information about the schema.

func (*Postgres) Query

func (e *Postgres) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

Query executes an SQL query with the provided query string and arguments (args), returning the rows and any errors encountered.

func (*Postgres) QueryRow

func (e *Postgres) QueryRow(ctx context.Context, query string, args ...any) *sql.Row

QueryRow executes an SQL query with the provided query string and arguments (args), returning a single row and any errors encountered.

func (*Postgres) SampleRowsQuery

func (e *Postgres) SampleRowsQuery(table string, k uint) string

SampleRowsQuery returns the query to retrieve a sample of rows from the specified table (table) with a limit of (k) rows.

type PostgresOptions

type PostgresOptions struct {
	DriverName string
}

PostgresOptions holds options for the Postgres database engine.

type QueryResult

type QueryResult struct {
	Columns []string
	Rows    [][]string
}

QueryResult holds the result of an SQL query.

func (*QueryResult) String

func (qr *QueryResult) String() string

String returns the string representation of the QueryResult.

type SQLDB

type SQLDB struct {
	// contains filtered or unexported fields
}

SQLDB represents an SQL database.

func New

func New(engine Engine, optFns ...func(o *SQLDBOptions)) (*SQLDB, error)

New creates a new SQLDB instance.

func (*SQLDB) Close

func (db *SQLDB) Close() error

Close closes the database connection.

func (*SQLDB) Dialect

func (db *SQLDB) Dialect() string

Dialect returns the dialect of the SQL database engine.

func (*SQLDB) Query

func (db *SQLDB) Query(ctx context.Context, query string, args ...any) (*QueryResult, error)

Query executes an SQL query and returns the result.

func (*SQLDB) TableInfo

func (db *SQLDB) TableInfo(ctx context.Context) (string, error)

TableInfo retrieves information about the tables in the database.

type SQLDBOptions

type SQLDBOptions struct {
	Schema                string
	Tables                []string
	Exclude               []string
	SampleRowsinTableInfo uint
}

SQLDBOptions holds options for the SQLDB.

type SQLite3

type SQLite3 struct {
	// contains filtered or unexported fields
}

SQLite3 represents the SQLite3 database engine.

func NewSQLite3

func NewSQLite3(dataSourceName string, optFns ...func(o *SQLite3Options)) (*SQLite3, error)

NewSQLite3 creates a new instance of the SQLite3 database engine.

func (*SQLite3) Close

func (e *SQLite3) Close() error

Close closes the database connection.

func (*SQLite3) Dialect

func (e *SQLite3) Dialect() string

Dialect returns the dialect of the SQLite3 database engine.

func (*SQLite3) Exec

func (e *SQLite3) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

Exec executes an SQL query with the provided query string and arguments (args), returning the result and any errors encountered.

func (SQLite3) Inspect

func (e SQLite3) Inspect(ctx context.Context, name string, opts *schema.InspectOptions) (map[string]string, error)

Inspect retrieves information about the schema.

func (*SQLite3) Query

func (e *SQLite3) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

Query executes an SQL query with the provided query string and arguments (args), returning the rows and any errors encountered.

func (*SQLite3) QueryRow

func (e *SQLite3) QueryRow(ctx context.Context, query string, args ...any) *sql.Row

QueryRow executes an SQL query with the provided query string and arguments (args), returning a single row and any errors encountered.

func (*SQLite3) SampleRowsQuery

func (e *SQLite3) SampleRowsQuery(table string, k uint) string

SampleRowsQuery returns the query to retrieve a sample of rows from the specified table (table) with a limit of (k) rows.

type SQLite3Options

type SQLite3Options struct {
	DriverName string
}

SQLite3Options holds options for the SQLite3 database engine.

Jump to

Keyboard shortcuts

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