sqldb

package module
v0.0.0-...-0228714 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 13 Imported by: 0

README

/* go install github.com/fraenky8/tables-to-go@master

tables-to-go -v -t pg -h 127.0.0.1 -s public -d postgres -u postgres -p admin -pn models -of ./models */

Documentation

Overview

Package sqldb provides a lightweight database/sql helper with simple query building and struct scanning.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatSQL

func FormatSQL(query string, args []any) string

func Scan

func Scan(queryer Queryer, dest any, query string, args ...any) error

func ScanContext

func ScanContext(ctx context.Context, queryer Queryer, dest any, query string, args ...any) error

Types

type DB

type DB struct {
	*sql.DB

	Flavor Flavor
	Option option
	// contains filtered or unexported fields
}

func Connect

func Connect(driverName, dataSourceName string) (*DB, error)

Connect to a database and verify with a ping.

func NewSQLDB

func NewSQLDB(db *sql.DB, flavor Flavor, opts ...Option) *DB

NewSQLDB is an alias of NewSqlDB.

func NewSqlDB

func NewSqlDB(db *sql.DB, flavor Flavor, opts ...Option) *DB

func Open

func Open(driverName, dataSourceName string, opts ...Option) (*DB, error)

Open is the same as sql.Open, but returns an *sqlx.DB instead.

func (*DB) Begin

func (db *DB) Begin() (*Tx, error)

func (*DB) BeginTx

func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

func (DB) Clone

func (b DB) Clone() *builder

func (DB) Count

func (b DB) Count() (int, error)

func (*DB) Exec

func (db *DB) Exec(query string, args ...any) (sql.Result, error)

func (*DB) ExecContext

func (db *DB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

func (DB) GroupBy

func (b DB) GroupBy(expr string) *builder

func (DB) Insert

func (b DB) Insert(data any) (sql.Result, error)

func (DB) Limit

func (b DB) Limit(limit int64) *builder

func (DB) Offset

func (b DB) Offset(offset int64) *builder

func (DB) OrderBy

func (b DB) OrderBy(column, direction string) *builder

func (*DB) Prepare

func (db *DB) Prepare(query string) (*sql.Stmt, error)

func (*DB) PrepareContext

func (db *DB) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

func (*DB) Query

func (db *DB) Query(query string, args ...any) (*sql.Rows, error)

func (*DB) QueryContext

func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

func (*DB) QueryRow

func (db *DB) QueryRow(query string, args ...any) *sql.Row

func (*DB) QueryRowContext

func (db *DB) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

func (*DB) QueryScan

func (db *DB) QueryScan(dest any, query string, args ...any) error

func (*DB) QueryScanContext

func (db *DB) QueryScanContext(ctx context.Context, dest any, query string, args ...any) error

func (DB) Reset

func (b DB) Reset()

func (DB) Scan

func (b DB) Scan(dest any) error

func (DB) Select

func (b DB) Select(columns ...string) *builder

func (*DB) Table

func (db *DB) Table(table string) *builder

Table starts a new query builder for the given table.

NOTE: This intentionally returns a fresh builder to avoid shared mutable state on DB when chaining builder methods.

func (*DB) Transaction

func (db *DB) Transaction(txFunc func(*Tx) error) (err error)

func (DB) Update

func (b DB) Update(data any) (sql.Result, error)

func (DB) Where

func (b DB) Where(column, operator string, value any) *builder

type DatabaseProvider

type DatabaseProvider interface {
	Execer
	Queryer
}

type Execer

type Execer interface {
	Exec(query string, args ...any) (sql.Result, error)
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}

type ExecerAndQueryer

type ExecerAndQueryer interface {
	Execer
	Queryer
}

type Flavor

type Flavor int

Flavor is the flag to control the format of compiled sql.

const (
	MySQL Flavor
	PostgreSQL
	SQLite
)

Supported drivers.

func (Flavor) String

func (f Flavor) String() string

String returns the name of f.

type Option

type Option func(opt *option)

func WithDebug

func WithDebug(debug bool) Option

func WithLog

func WithLog(log func(string, ...any)) Option

func WithTraceSQL

func WithTraceSQL(traceSQL bool) Option

type Queryer

type Queryer interface {
	Query(query string, args ...any) (*sql.Rows, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRow(query string, args ...any) *sql.Row
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

type TokenType

type TokenType int

TokenType represents a type of token in a SQL INSERT statement, whether column or value expression.

const (

	// ColumnNameTokenType uses the column name from the struct tag specified by UseStructTag.
	// INSERT INTO tbl (foo, bar, ... baz)
	ColumnNameTokenType TokenType = 0

	// QuestionMarkTokenType uses question marks as value-tokens.
	// VALUES (?, ?, ... ?) -- MySQL, SingleStore
	QuestionMarkTokenType TokenType = 1

	// AtColumnNameTokenType uses @ followed by the column name from the struct tag specified by UseStructTag.
	// VALUES (@foo, @bar, ... @baz) -- MySQL, SingleStore
	AtColumnNameTokenType TokenType = 2

	// OrdinalNumberTokenType uses % plus the value of an ordered sequence of integers starting at 1.
	// %1, %2, ... %n -- Postgres
	OrdinalNumberTokenType TokenType = 3

	// ColonTokenType uses : followed by the column name from the struct tag specified by UseStructTag.
	// :foo, :bar, ... :baz -- Oracle
	ColonTokenType TokenType = 4
)

type Tx

type Tx struct {
	*sql.Tx
	Flavor Flavor
	Option option
}

func (*Tx) Exec

func (tx *Tx) Exec(query string, args ...any) (sql.Result, error)

func (*Tx) ExecContext

func (tx *Tx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

func (*Tx) Prepare

func (tx *Tx) Prepare(query string) (*sql.Stmt, error)

func (*Tx) PrepareContext

func (tx *Tx) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

func (*Tx) Query

func (tx *Tx) Query(query string, args ...any) (*sql.Rows, error)

func (*Tx) QueryContext

func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

func (*Tx) QueryRow

func (tx *Tx) QueryRow(query string, args ...any) *sql.Row

func (*Tx) QueryRowContext

func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

func (*Tx) QueryScan

func (tx *Tx) QueryScan(dest any, query string, args ...any) error

Jump to

Keyboard shortcuts

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