database

package
v0.2.13 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

norm/database

Example

	db, err := database.New(sdb, sql.LevelSerializable, database.DefaultLogger)
	if err != nil {
    	// handle err
	}

	type Part struct {
    	ID string
    	Name string
    	TotalQuantity int64
	}

	part := Part{
		Name: "part01"
	}

	tx, err := db.Read(ctx, id)
	if err != nil {
		// handle err
	}

	query, err := statement.Select().Comment("request-id: ?", id).
		WithRecursive(
			"included_parts",
			Select().Columns("sub_part", "part", "quantity").
				From("parts").Where("part = ?", part.Name).
				UnionAll(
					Select().Columns("p.sub_part", "p.part", "p.quantity").
						From("included_parts AS pr").
						JoinInner("parts AS p", "p.part = pr.sub_part"),
				),
		).Columns("sub_part", "SUM(quantity) as total_quantity").
			From("included_parts").GroupBy("sub_part").String()

	var parts []Part
	if err = tx.Query(&parts, stmt); err != nil {
	// handle err
	}

	// do things with parts

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultLogger

func DefaultLogger(message, tid string, err error, d time.Duration, query string)

DefaultLogger for database operations

Types

type Cursor

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

Cursor is a cursor to a database result set.

func (*Cursor) Close

func (c *Cursor) Close() (err error)

Close closes the Cursor, preventing further enumeration. If Next is called and returns false and there are no further result sets, the Cursor is closed automatically and it will suffice to check the result of Err. Close is idempotent and does not affect the result of Err.

func (*Cursor) Err

func (c *Cursor) Err() (err error)

Err returns the error, if any, that was encountered during iteration. Err may be called after an explicit or implicit Close.

func (*Cursor) Next

func (c *Cursor) Next() (ok bool)

Next prepares the next result row for reading with the Scan method. It returns true on success, or false if there is no next result row or an error happened while preparing it. Err should be consulted to distinguish between the two cases.

Every call to Scan, even the first one, must be preceded by a call to Next.

func (*Cursor) Scan

func (c *Cursor) Scan(dst interface{}) (err error)

Scan copies the current row columns into the struct fields or map values pointed at by dst. If the type of dst changes during calls to scan it will return a error.

type DB

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

DB is safe sql.DB wrapper which enforces transactional access to the database, transaction query caching and operation logging and plays nicely with `noorm/statement`.

func New

func New(db *sql.DB, level sql.IsolationLevel, logger Logger) (d *DB, err error)

New creates a new database from an existing *sql.DB with the given sql.IsolationLevel and logger.

func (*DB) Close added in v0.1.9

func (d *DB) Close() (err error)

Close closes the database and prevents new queries from starting. Close then waits for all queries that have started processing on the server to finish.

func (*DB) Ping added in v0.1.8

func (d *DB) Ping(ctx context.Context) (err error)

PingContext verifies a connection to the database is still alive, establishing a connection if necessary.

func (*DB) Read

func (d *DB) Read(ctx context.Context, tid string) (tx *Tx, err error)

Read creates a read-only transaction with the default DB isolation level. The tid argument is the transaction identifier that will be used to log operations done within the transaction.

func (*DB) Tx

func (d *DB) Tx(ctx context.Context, tid string, opts *sql.TxOptions) (tx *Tx, err error)

Tx creates a database transaction with the provided options. The tid argument is the transaction identifier that will be used to log operations done within the transaction.

func (*DB) Update

func (d *DB) Update(ctx context.Context, tid string) (tx *Tx, err error)

Update creates a read-write transaction with the default DB isolation level. The tid argument is the transaction identifier that will be used to log operations done within the transaction.

type Logger

type Logger func(message, tid string, err error, d time.Duration, query string)

Logger type for database operations

type Stmt added in v0.2.11

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

func (*Stmt) Close added in v0.2.11

func (s *Stmt) Close() (err error)

Close closes the statement.

func (*Stmt) Exec added in v0.2.11

func (s *Stmt) Exec(args ...interface{}) (r sql.Result, err error)

Exec executes a prepared statement with the given arguments and returns a Result summarizing the effect of the statement.

func (*Stmt) Query added in v0.2.11

func (s *Stmt) Query(dst interface{}, args ...interface{}) (err error)

Query executes a prepared query statement with the given arguments and returns the query results as a *Rows.

type Tx

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

Tx represents a database transaction

func (*Tx) Commit

func (t *Tx) Commit() (err error)

Commit the transaction.

func (*Tx) Cursor

func (t *Tx) Cursor(stmt statement.Statement) (i *Cursor, err error)

Cursor executes a query that returns a database cursor like sql.Rows. It its useful for working with large result sets or/and when memory utilization is a concern.

The caller must call Cursor.Close() on the returned cursor in order to release the sql.Rows resources.

func (*Tx) Exec

func (t *Tx) Exec(stmt statement.Statement) (r sql.Result, err error)

Exec executes a query that doesn't return rows.

func (*Tx) ExecSQL added in v0.2.2

func (t *Tx) ExecSQL(query string, values ...interface{}) (r sql.Result, err error)

ExecSQL is like Exec but accepts a raw SQL statement and values for interpolation

func (*Tx) Prepare added in v0.2.11

func (t *Tx) Prepare(query string) (stmt *Stmt, err error)

Prepare creates a prepared statement for use within a transaction.

func (*Tx) Query

func (t *Tx) Query(dst interface{}, stmt statement.Statement) (err error)

Query executes a query that returns rows.

func (*Tx) QueryCache

func (t *Tx) QueryCache(dst interface{}, stmt statement.Statement) (err error)

QueryCache is like Query, but will add query results to or return already cached results from the transaction query cache.

func (*Tx) QueryCacheSQL added in v0.2.2

func (t *Tx) QueryCacheSQL(dst interface{}, query string, values ...interface{}) (err error)

QueryCacheSQL is like QueryCache but accepts a raw SQL statement and values for interpolation

func (*Tx) QuerySQL added in v0.2.2

func (t *Tx) QuerySQL(dst interface{}, query string, values ...interface{}) (err error)

QuerySQL is like Query but accepts a raw SQL statement and values for interpolation

func (*Tx) Rollback

func (t *Tx) Rollback() (err error)

Rollback aborts the transaction.

Jump to

Keyboard shortcuts

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