sqlpro

package module
v0.0.0-...-49d05cb Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2023 License: MIT Imports: 17 Imported by: 0

README

go-sqlpro

WIP

This package is under active development and work-in-progress.

Query(target interface{}, query string, args...)

If you want to insert a custom type (e.g. a struct) into the db your type needs to fullfill follwing interface functions for (un-)marshalling from/to the database.

Marshalling to DB

func (f Metadata) Value() (driver.Value, error)

Unmarshalling from DB

func (f *Metadata) Scan(v interface{}) error`

json is supported as tag!

Documentation

Index

Constants

View Source
const (
	PANIC      DebugLevel = 1
	ERROR                 = 2
	UPDATE                = 4
	INSERT                = 8
	EXEC                  = 16
	QUERY                 = 32
	QUERY_DUMP            = 64
)
View Source
const POSTGRES = "postgres"

The driver strings must match the driver from the stdlib

View Source
const SQLITE3 = "sqlite3"

Variables

View Source
var ErrMismatchedRowsAffected error = errors.New("Mismatched rows affected.")
View Source
var ErrQueryReturnedZeroRows error = errors.New("Query returned 0 rows.")

Functions

func Scan

func Scan(target interface{}, rows *sql.Rows) error

Scan reads data from the given rows into the target.

*int64, *string, etc: First column of first row *struct: First row []int64, []*int64, []string, []*string: First column, all rows []struct, []*struct: All columns, all rows

The mapping into structs is done by analyzing the struct's tag names and using the given "db" key for the mapping. The mapping works on exported fields only. Use "-" as mapping name to ignore the field.

Types

type DB

type DB struct {
	Debug                 bool
	DebugExec             bool
	DebugQuery            bool
	PlaceholderMode       PlaceholderMode
	PlaceholderEscape     rune
	PlaceholderValue      rune
	PlaceholderKey        rune
	MaxPlaceholder        int
	UseReturningForLastId bool
	SupportsLastInsertId  bool
	Driver                dbDriver
	DSN                   string

	LastError error // This is set to the last error
	// contains filtered or unexported fields
}

func New

func New(dbWrap dbWrappable) *DB

NewSqlPro returns a wrapped database handle providing access to the sql pro functions.

func Open

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

Open opens a database connection and returns an sqlpro wrap handle

func (*DB) ActiveTX

func (db *DB) ActiveTX() bool

func (*DB) AfterCommit

func (db *DB) AfterCommit(f func())

func (*DB) AfterRollback

func (db *DB) AfterRollback(f func())

func (*DB) AfterTransaction

func (db *DB) AfterTransaction(f func())

func (*DB) Begin

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

Begin starts a new transaction, (read-write mode)

func (*DB) BeginContext

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

Begin starts a new transaction, (read-write mode)

func (*DB) BeginRead

func (db *DB) BeginRead() (*DB, error)

BeginRead starts a new transaction, read-only mode

func (*DB) Close

func (db *DB) Close() error

func (*DB) Commit

func (db *DB) Commit() error

func (*DB) DB

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

DB returns the wrapped sql.DB handle

func (*DB) Esc

func (db *DB) Esc(s string) string

func (*DB) EscValue

func (db *DB) EscValue(s string) string

func (*DB) EscValueForInsert

func (db *DB) EscValueForInsert(value interface{}, fi *fieldInfo) string

func (*DB) Exec

func (db *DB) Exec(execSql string, args ...interface{}) error

func (*DB) ExecContext

func (db *DB) ExecContext(ctx context.Context, execSql string, args ...interface{}) error

func (*DB) ExecContextRowsAffected

func (db *DB) ExecContextRowsAffected(ctx context.Context, execSql string, args ...interface{}) (int64, int64, error)

ExecContextExp executes execSql in context ctx. If the number of rows affected doesn't match expRows, an error is returned.

func (*DB) Insert

func (db *DB) Insert(table string, data interface{}) error

func (*DB) InsertBulk

func (db *DB) InsertBulk(table string, data interface{}) error

func (*DB) InsertBulkContext

func (db *DB) InsertBulkContext(ctx context.Context, table string, data interface{}) error

InsertBulk takes a table name and a slice of struct and inserts the record in the DB with one Exec. The given data needs to be:

*[]*strcut *[]struct []*struct []struct

sqlpro will executes one INSERT statement per call.

func (*DB) InsertBulkCopyIn

func (db *DB) InsertBulkCopyIn(table string, data interface{}) error

func (*DB) InsertContext

func (db *DB) InsertContext(ctx context.Context, table string, data interface{}) error

func (*DB) IsClosed

func (db *DB) IsClosed() bool

func (*DB) IsWriteMode

func (db *DB) IsWriteMode() bool

func (*DB) Log

func (db *DB) Log() *DB

Log returns a copy with debug enabled

func (*DB) PrintQueryContext

func (db *DB) PrintQueryContext(ctx context.Context, query string, args ...interface{}) error

func (*DB) Query

func (db *DB) Query(target interface{}, query string, args ...interface{}) error

func (*DB) QueryContext

func (db *DB) QueryContext(ctx context.Context, target interface{}, query string, args ...interface{}) error

Query runs a query and fills the received rows or row into the target. It is a wrapper method around the

func (*DB) Rollback

func (db *DB) Rollback() error

func (*DB) Save

func (db *DB) Save(table string, data interface{}) error

Save saves the given data. It performs an INSERT if the only primary key is zero, and and UPDATE if it is not. It panics if it the record has no primary key or less than one

func (*DB) String

func (db *DB) String() string

func (*DB) TX

func (db *DB) TX() *sql.Tx

func (*DB) Update

func (db *DB) Update(table string, data interface{}) error

func (*DB) UpdateBulk

func (db *DB) UpdateBulk(table string, data interface{}) error

func (*DB) UpdateBulkContext

func (db *DB) UpdateBulkContext(ctx context.Context, table string, data interface{}) error

UpdateBulkContext updates all records of the passed slice. It using a single exec to send the data to the database. This is generally faster than calling Update with a slice (which sends individual update requests).

func (*DB) UpdateContext

func (db *DB) UpdateContext(ctx context.Context, table string, data interface{}) error

Update updates the given struct or slice of structs The WHERE clause is put together from the "pk" columns. If not all "pk" columns have non empty values, Update returns an error.

type DebugLevel

type DebugLevel int

type NullJson

type NullJson struct {
	Data  []byte
	Valid bool
}

func (*NullJson) Scan

func (nj *NullJson) Scan(value interface{}) error

type NullRawMessage

type NullRawMessage struct {
	Data  json.RawMessage
	Valid bool
}

func (*NullRawMessage) Scan

func (nj *NullRawMessage) Scan(value interface{}) error

type NullTime

type NullTime struct {
	Time  time.Time
	Valid bool
}

func (*NullTime) Scan

func (ni *NullTime) Scan(value interface{}) error

Scan implements the Scanner interface.

type PlaceholderMode

type PlaceholderMode int
const (
	DOLLAR   PlaceholderMode = 1
	QUESTION                 = 2
)

Jump to

Keyboard shortcuts

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