db

package
v2.0.0-rc3 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2021 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package db have some common utilities shared by db/l2db and db/historydb, the most relevant ones are: - SQL connection utilities - Managing the SQL schema: this is done using migration files placed under db/migrations. The files are executed by order of the file name. - Custom meddlers: used to easily transform struct <==> table

Index

Constants

View Source
const (
	// OrderAsc indicates ascending order when using pagination
	OrderAsc = "ASC"
	// OrderDesc indicates descending order when using pagination
	OrderDesc = "DESC"
)

Variables

This section is empty.

Functions

func BulkInsert

func BulkInsert(db meddler.DB, q string, args interface{}) error

BulkInsert performs a bulk insert with a single statement into the specified table. Example: `db.BulkInsert(myDB, "INSERT INTO block (eth_block_num, timestamp, hash) VALUES %s", blocks[:])` Note that all the columns must be specified in the query, and they must be in the same order as in the table. Note that the fields in the structs need to be defined in the same order as in the table columns.

func ConnectSQLDB

func ConnectSQLDB(port int, host, user, password, name string) (*sqlx.DB, error)

ConnectSQLDB connects to the SQL DB

func InitSQLDB

func InitSQLDB(port int, host, user, password, name string) (*sqlx.DB, error)

InitSQLDB runs migrations and registers meddlers

func InitTestSQLDB

func InitTestSQLDB() (*sqlx.DB, error)

InitTestSQLDB opens test PostgreSQL database

func MigrationsDown

func MigrationsDown(db *sql.DB, migrationsToRun uint) error

MigrationsDown runs the SQL migrations Down, migrationsToRun specifies how many migrations will be run, 0 means any.

func MigrationsUp

func MigrationsUp(db *sql.DB) error

MigrationsUp runs the SQL migrations Up

func Rollback

func Rollback(txn *sqlx.Tx)

Rollback an sql transaction, and log the error if it's not nil

func RowsClose

func RowsClose(rows *sql.Rows)

RowsClose close the rows of an sql query, and log the errir if it's not nil

func SlicePtrsToSlice

func SlicePtrsToSlice(slice interface{}) interface{}

SlicePtrsToSlice converts any []*Foo to []Foo

func SliceToSlicePtrs

func SliceToSlicePtrs(slice interface{}) interface{}

SliceToSlicePtrs converts any []Foo to []*Foo

Types

type APIConnectionController

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

APIConnectionController is used to limit the SQL open connections used by the API

func NewAPIConnectionController

func NewAPIConnectionController(maxConnections int, timeout time.Duration) *APIConnectionController

NewAPIConnectionController initialize APIConnectionController

func (*APIConnectionController) Acquire

func (acc *APIConnectionController) Acquire() (context.CancelFunc, error)

Acquire reserves a SQL connection. If the connection is not acquired within the timeout, the function will return an error

func (*APIConnectionController) Release

func (acc *APIConnectionController) Release()

Release frees a SQL connection

type BigIntMeddler

type BigIntMeddler struct{}

BigIntMeddler encodes or decodes the field value to or from JSON

func (BigIntMeddler) PostRead

func (b BigIntMeddler) PostRead(fieldPtr, scanTarget interface{}) error

PostRead is called after a Scan operation for fields that have the BigIntMeddler

func (BigIntMeddler) PreRead

func (b BigIntMeddler) PreRead(fieldAddr interface{}) (scanTarget interface{}, err error)

PreRead is called before a Scan operation for fields that have the BigIntMeddler

func (BigIntMeddler) PreWrite

func (b BigIntMeddler) PreWrite(fieldPtr interface{}) (saveValue interface{}, err error)

PreWrite is called before an Insert or Update operation for fields that have the BigIntMeddler

type BigIntNullMeddler

type BigIntNullMeddler struct{}

BigIntNullMeddler encodes or decodes the field value to or from JSON

func (BigIntNullMeddler) PostRead

func (b BigIntNullMeddler) PostRead(fieldPtr, scanTarget interface{}) error

PostRead is called after a Scan operation for fields that have the BigIntNullMeddler

func (BigIntNullMeddler) PreRead

func (b BigIntNullMeddler) PreRead(fieldAddr interface{}) (scanTarget interface{}, err error)

PreRead is called before a Scan operation for fields that have the BigIntNullMeddler

func (BigIntNullMeddler) PreWrite

func (b BigIntNullMeddler) PreWrite(fieldPtr interface{}) (saveValue interface{}, err error)

PreWrite is called before an Insert or Update operation for fields that have the BigIntNullMeddler

Directories

Path Synopsis
Package historydb is responsible for storing and retrieving the historic data of the Hermez network.
Package historydb is responsible for storing and retrieving the historic data of the Hermez network.
Package kvdb provides a key-value database with Checkpoints & Resets system
Package kvdb provides a key-value database with Checkpoints & Resets system
Package l2db is responsible for storing and retrieving the data received by the coordinator through the api.
Package l2db is responsible for storing and retrieving the data received by the coordinator through the api.

Jump to

Keyboard shortcuts

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