Back to godoc.org
zgo.at/zdb

package zdb

v0.0.0 (2c5d728)
Latest Go to latest
Published: 1 day ago | License: MIT | Module: zgo.at/zdb

Index

Constants

const Date = "2006-01-02 15:04:05"

Date format for SQL.

func ApplyPlaceholders

func ApplyPlaceholders(query string, args ...interface{}) string

ApplyPlaceholders replaces parameter placeholders in query with the values.

This is ONLY for printf-debugging, and NOT for actual usage. Security was NOT a consideration when writing this. Parameters in SQL are sent separately over the write and are not interpolated, so it's very different.

This supports ? placeholders and $1 placeholders *in order* ($\d is simply replace with ?).

func Begin

func Begin(ctx context.Context) (context.Context, *sqlx.Tx, error)

Begin a new transaction.

The returned context is a copy of the original with the DB replaced with a transaction. The same transaction is also returned directly.

func Connect

func Connect(opts ConnectOptions) (*sqlx.DB, error)

Connect to database.

func Dump

func Dump(ctx context.Context, out io.Writer, query string, args ...interface{})

Dump the results of a query to a writer in an aligned table. This is a convenience function intended just for testing/debugging.

Combined with ztest.Diff() it can be an easy way to test the database state.

func DumpString

func DumpString(ctx context.Context, query string, args ...interface{}) string

DumpString is like Dump(), but returns the result as a string.

func ErrNoRows

func ErrNoRows(err error) bool

ErrNoRows reports if this error is sql.ErrNoRows.

func ErrUnique

func ErrUnique(err error) bool

ErrUnique reports if this error reports a UNIQUE constraint violation.

This is the cgo version which works for PostgreSQL and SQLite.

func PgSQL

func PgSQL(db DB) bool

PgSQL reports if this database connection is to PostgreSQL.

func TX

func TX(ctx context.Context, fn func(context.Context, DB) error) error

TX runs the given function in a transaction.

The context passed to the callback has the DB replaced with a transaction.

The transaction is comitted if the fn returns nil, or will be rolled back if it's not.

This is just a more convenient wrapper for Begin().

func With

func With(ctx context.Context, db DB) context.Context

With returns a copy of the context with the DB instance.

type Bool

type Bool bool

Bool converts various types to a boolean.

It's always stored as an integer in the database (the only cross-platform way in SQL).

Supported types:

bool
int* and float*     0 or 1
[]byte and string   "1", "true", "on", "0", "false", "off"
nil                 defaults to false

func (Bool) MarshalJSON

func (b Bool) MarshalJSON() ([]byte, error)

MarshalJSON converts the data to JSON.

func (Bool) MarshalText

func (b Bool) MarshalText() ([]byte, error)

MarshalText converts the data to a human readable representation.

func (*Bool) Scan

func (b *Bool) Scan(src interface{}) error

Scan converts the data from the DB.

func (*Bool) UnmarshalJSON

func (b *Bool) UnmarshalJSON(text []byte) error

UnmarshalJSON converts the data from JSON.

func (*Bool) UnmarshalText

func (b *Bool) UnmarshalText(text []byte) error

UnmarshalText parses text in to the Go data structure.

func (Bool) Value

func (b Bool) Value() (driver.Value, error)

Value converts a bool type into a number to persist it in the database.

type ConnectOptions

type ConnectOptions struct {
	Connect string // Connect string.
	Schema  []byte // Database schema to create on startup.
	Migrate *Migrate

	// ConnectHook for sqlite3.SQLiteDriver; mainly useful to add your own
	// functions:
	//
	//    opts.SQLiteHook = func(c *sqlite3.SQLiteConn) error {
	//        return c.RegisterFunc("percent_diff", func(start, final float64) float64 {
	//            return (final - start) / start * 100
	//        }, true)
	//    }
	//
	// It'll automatically register and connect to a new "sqlite3_zdb" driver.
	SQLiteHook func(*sqlite3.SQLiteConn) error
}

type DB

type DB interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row
	QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
	SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error

	Rebind(query string) string
	DriverName() string
}

DB wraps sqlx.DB so we can add transactions.

func Get

func Get(ctx context.Context) (DB, bool)

Get the DB from the context.

func MustGet

func MustGet(ctx context.Context) DB

MustGet gets the DB from the context, panicking if there is none.

type Floats

type Floats []float64

Floats stores a slice of []float64 as a comma-separated string.

func (Floats) MarshalText

func (l Floats) MarshalText() ([]byte, error)

MarshalText converts the data to a human readable representation.

func (*Floats) Scan

func (l *Floats) Scan(v interface{}) error

Scan converts the data from the DB.

func (Floats) String

func (l Floats) String() string

func (*Floats) UnmarshalText

func (l *Floats) UnmarshalText(v []byte) error

UnmarshalText parses text in to the Go data structure.

func (Floats) Value

func (l Floats) Value() (driver.Value, error)

Value determines what to store in the DB.

type Ints

type Ints []int64

Ints stores a slice of []int64 as a comma-separated string.

func (Ints) MarshalText

func (l Ints) MarshalText() ([]byte, error)

MarshalText converts the data to a human readable representation.

func (*Ints) Scan

func (l *Ints) Scan(v interface{}) error

Scan converts the data from the DB.

func (Ints) String

func (l Ints) String() string

func (*Ints) UnmarshalText

func (l *Ints) UnmarshalText(v []byte) error

UnmarshalText parses text in to the Go data structure.

func (Ints) Value

func (l Ints) Value() (driver.Value, error)

Value determines what to store in the DB.

type Migrate

type Migrate struct {
	DB          DB
	Which       []string          // List of migrations to run.
	Migrations  map[string][]byte // List of all migrations, for production.
	MigratePath string            // Path to migrations, for dev.
}

func NewMigrate

func NewMigrate(db DB, which []string, mig map[string][]byte, path string) *Migrate

func (Migrate) Check

func (m Migrate) Check() error

Check if there are pending migrations and zlog.Error() if there are.

func (Migrate) List

func (m Migrate) List() (haveMig, ranMig []string, err error)

Get a list of all migrations we know about, and all migrations that have already been run.

func (Migrate) Run

func (m Migrate) Run(which ...string) error

Run a migration, or all of then if which is "all" or "auto".

func (Migrate) Schema

func (m Migrate) Schema(n string) (string, error)

Schema of a migration by name.

type Strings

type Strings []string

Strings stores a slice of []string as a comma-separated string.

Note this only works for simple strings (e.g. enums), it DOES NOT ESCAPE COMMAS, and you will run in to problems if you use it for arbitrary text.

You're probably better off using e.g. arrays in PostgreSQL or JSON in SQLite, if you can. This is intended just for simple cross-SQL-engine use cases.

func (Strings) MarshalText

func (l Strings) MarshalText() ([]byte, error)

MarshalText converts the data to a human readable representation.

func (*Strings) Scan

func (l *Strings) Scan(v interface{}) error

Scan converts the data from the DB.

func (Strings) String

func (l Strings) String() string

func (*Strings) UnmarshalText

func (l *Strings) UnmarshalText(v []byte) error

UnmarshalText parses text in to the Go data structure.

func (Strings) Value

func (l Strings) Value() (driver.Value, error)

Value determines what to store in the DB.

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
f or F : Jump to identifier