orm

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2025 License: MIT Imports: 12 Imported by: 1

Documentation

Index

Constants

View Source
const (
	MySQL    = "mysql"
	Postgres = "postgres"
)

Database driver names.

Variables

This section is empty.

Functions

func GetDriver added in v0.2.0

func GetDriver(name string) (sqldriver.Driver, bool)

GetDriver retrieves a registered driver.

func Insert added in v0.3.0

func Insert[T any](ctx context.Context, db *DB, v T, opts ...WriteOpt) (sql.Result, error)

Insert inserts v into its table.

func RegisterDialect added in v0.2.0

func RegisterDialect(name string, d driver.Dialect)

RegisterDialect registers a SQL dialect for a driver name.

func RegisterDriver added in v0.2.0

func RegisterDriver(name string, d sqldriver.Driver)

RegisterDriver registers a database driver.

func RegisterDriverWithDialect added in v0.2.0

func RegisterDriverWithDialect(name string, d sqldriver.Driver, dialect driver.Dialect)

RegisterDriverWithDialect registers a database driver along with its dialect.

func ResetMetaCache added in v0.3.0

func ResetMetaCache()

ResetMetaCache clears cached reflection metadata. Intended for tests.

func SelectAll added in v0.3.0

func SelectAll[T any](ctx context.Context, db *DB, q string, args ...any) ([]T, error)

SelectAll runs the query and scans all rows into []T.

func SelectOne added in v0.3.0

func SelectOne[T any](ctx context.Context, db *DB, q string, args ...any) (T, error)

SelectOne runs the query and scans the first row into T.

func SelectStruct deprecated added in v0.3.0

func SelectStruct[T any](ctx context.Context, db *DB, q string, args ...any) (T, error)

Deprecated: Use SelectOne instead.

func SelectStructs deprecated added in v0.3.0

func SelectStructs[T any](ctx context.Context, db *DB, q string, args ...any) ([]T, error)

Deprecated: Use SelectAll instead.

func Update added in v0.3.0

func Update[T any](ctx context.Context, db *DB, v T, opts ...WriteOpt) (sql.Result, error)

Update updates record v.

func Upsert added in v0.3.0

func Upsert[T any](ctx context.Context, db *DB, v T, opts ...WriteOpt) (sql.Result, error)

Upsert inserts or updates v using primary keys.

Types

type DB

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

DB provides main ORM interface.

func Open

func Open(dsn string) (*DB, error)

Open opens a MySQL database with default pooling. Deprecated: use OpenWithDriver to specify a driver explicitly.

func OpenWithDriver added in v0.1.0

func OpenWithDriver(driverName, dsn string) (*DB, error)

OpenWithDriver opens a database with default pooling for the given driver.

func (*DB) Begin added in v0.0.4

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

Begin starts a transaction for manual control.

func (*DB) BeginTx added in v0.1.2

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

BeginTx starts a transaction using ctx and returns the Tx.

func (*DB) Close

func (db *DB) Close() error

Close closes underlying DB.

func (*DB) Exec added in v0.0.3

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

Exec executes a raw SQL statement.

func (*DB) ExecContext added in v0.0.3

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

ExecContext executes a raw SQL statement with a context.

func (*DB) Model

func (db *DB) Model(v any) *query.Query

Model creates a query for the struct table.

func (*DB) Query added in v0.0.3

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

Query runs a raw SQL query returning multiple rows.

func (*DB) QueryContext added in v0.0.3

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

QueryContext runs Query with a context.

func (*DB) QueryRow added in v0.0.3

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

QueryRow executes a query that is expected to return at most one row.

func (*DB) QueryRowContext added in v0.0.3

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

QueryRowContext executes a query with context returning at most one row.

func (*DB) SQLDB added in v0.0.5

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

SQLDB returns the underlying *sql.DB.

func (*DB) SelectMap deprecated added in v0.3.0

func (db *DB) SelectMap(ctx context.Context, q string, args ...any) (map[string]any, error)

Deprecated: Use SelectOne with map type instead.

func (*DB) SelectMaps deprecated added in v0.3.0

func (db *DB) SelectMaps(ctx context.Context, q string, args ...any) ([]map[string]any, error)

Deprecated: Use SelectAll with map type instead.

func (*DB) Table

func (db *DB) Table(name string) *query.Query

Table creates a query for table name.

func (*DB) Transaction

func (db *DB) Transaction(fn func(tx Tx) error) error

Transaction executes fn in a transaction.

func (*DB) TransactionContext added in v0.1.2

func (db *DB) TransactionContext(ctx context.Context, fn func(tx Tx) error) error

TransactionContext executes fn in a transaction using ctx.

type Tx

type Tx struct {
	*DB
	driver.Tx
}

Tx represents a transaction-scoped DB wrapper.

type WriteOpt added in v0.3.0

type WriteOpt func(*writeOptions)

WriteOpt configures write behavior.

func Columns added in v0.3.0

func Columns(cols ...string) WriteOpt

Columns limits write to specified columns.

func Omit added in v0.3.0

func Omit(cols ...string) WriteOpt

Omit excludes specified columns.

func PK added in v0.3.0

func PK(cols ...string) WriteOpt

PK specifies primary key columns for map writes.

func Returning added in v0.3.0

func Returning(cols ...string) WriteOpt

Returning specifies columns to return (Postgres only).

func Table added in v0.3.0

func Table(name string) WriteOpt

Table sets table name (required for map writes).

func WherePK added in v0.3.0

func WherePK() WriteOpt

WherePK uses primary key columns in WHERE clause.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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