dbm

package module
v0.0.0-...-68a23e6 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2022 License: MIT Imports: 10 Imported by: 0

README

DBM - database utilities

  • Supports MySQL, PostgreSQL, SQLite
  • Query builder
  • Transactions
  • Error handling
  • Table model description
  • Result values type conversion

Example:

DB := dbm.New(true, "uid").Connect("sqlite3", "./test.db")

DB.Find("user").GetAll().ToJSON("type", "username", "uid")

DB.FindBy("user", "uid", 5).GetOne("username").ToString()

DB.Find("report").Where("published = 1").Where("deleted = 0").Order("published_date", "DESC").Limit(1).GetOne()

columns := map[string]interface{}{
			"name":        name,
			"description": description,
		}
lastID := DB.Create("item", columns).Exec().LastInsertId

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	SQLQ              *sqlq.Sqlq
	IDName            string // {{tableName}}_id
	IDWithTableName   bool
	Db                *sql.DB
	Tx                *sql.Tx
	TxError           error
	Debug             bool
	Dialect           string
	Connection        string
	Error             error
	IsDBError         bool
	Once              bool
	Closed            bool
	MysqlTextProtocol bool
	DefaultTimeString string
}

func New

func New(debug bool, idName string) *DB

New : idName {{tableName}}_id

func (*DB) Begin

func (d *DB) Begin() DB

func (*DB) Close

func (d *DB) Close()

func (*DB) Commit

func (d *DB) Commit()

func (*DB) Connect

func (d *DB) Connect(dialect string, connection string, once ...bool) *DB

func (*DB) Delete

func (d *DB) Delete(table string) *QueryBuilder

func (*DB) DeleteBy

func (d *DB) DeleteBy(table string, column string, value interface{}) *QueryBuilder

func (*DB) DeleteById

func (d *DB) DeleteById(table string, id interface{}) *QueryBuilder

func (*DB) Exec

func (d *DB) Exec(query ...string) ExecResponse

func (*DB) Find

func (d *DB) Find(table string) *QueryBuilder

Find ....

func (*DB) FindBy

func (d *DB) FindBy(table string, column string, value interface{}) *QueryBuilder

func (*DB) FindById

func (d *DB) FindById(table string, id interface{}) *QueryBuilder

func (*DB) GetIDName

func (d *DB) GetIDName(tableName ...string) string

func (*DB) Insert

func (d *DB) Insert(table string, columns sqlq.Map) *QueryBuilder

func (*DB) Query

func (d *DB) Query(query string, model ...Model) Table

func (*DB) QueryRow

func (d *DB) QueryRow(query string, model ...Model) TableRow

func (*DB) Reconnect

func (d *DB) Reconnect()

func (*DB) Rollback

func (d *DB) Rollback()

func (*DB) Update

func (d *DB) Update(table string, columns sqlq.Map, upsert ...bool) *QueryBuilder

func (*DB) UpdateBy

func (d *DB) UpdateBy(table string, column string, value interface{}, columns sqlq.Map, upsert ...bool) *QueryBuilder

func (*DB) UpdateById

func (d *DB) UpdateById(table string, id interface{}, columns sqlq.Map, upsert ...bool) *QueryBuilder

func (*DB) UpdateByIdOne

func (d *DB) UpdateByIdOne(table string, id interface{}, column string, value interface{}, upsert ...bool) *QueryBuilder

func (*DB) UpdateOne

func (d *DB) UpdateOne(table string, column string, value interface{}, upsert ...bool) *QueryBuilder

type ExecResponse

type ExecResponse struct {
	LastInsertId int64
	RowsAffected int64
	Error        error
}

type Model

type Model map[string]ModelParams

type ModelParams

type ModelParams struct {
	Type          string // string, integer, boolean, float, time, timeString
	Skip          bool
	TimeFormatIn  string // in DB
	TimeFormatOut string // return
	BooleanTrue   string
	Rename        string
}

type QueryBuilder

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

func (*QueryBuilder) AddUpdate

func (qb *QueryBuilder) AddUpdate(column string, value interface{}) *QueryBuilder

func (*QueryBuilder) AddUpsertInsert

func (qb *QueryBuilder) AddUpsertInsert(column string, value interface{}) *QueryBuilder

func (*QueryBuilder) AddtInsert

func (qb *QueryBuilder) AddtInsert(column string, value interface{}) *QueryBuilder

func (*QueryBuilder) Count

func (qb *QueryBuilder) Count() int

func (*QueryBuilder) Exec

func (qb *QueryBuilder) Exec() ExecResponse

func (*QueryBuilder) Exist

func (qb *QueryBuilder) Exist() bool

func (*QueryBuilder) GetAll

func (qb *QueryBuilder) GetAll(model ...Model) Table

func (*QueryBuilder) GetOne

func (qb *QueryBuilder) GetOne(model ...Model) TableRow

func (*QueryBuilder) GetOneWithErr

func (qb *QueryBuilder) GetOneWithErr() (TableRow, error)

func (*QueryBuilder) GroupBy

func (qb *QueryBuilder) GroupBy(columns ...interface{}) *QueryBuilder

func (*QueryBuilder) Join

func (qb *QueryBuilder) Join(table interface{}, table1Column interface{}, table2Column interface{}) *QueryBuilder

func (*QueryBuilder) LeftJoin

func (qb *QueryBuilder) LeftJoin(table interface{}, table1Column interface{}, table2Column interface{}) *QueryBuilder

func (*QueryBuilder) Limit

func (qb *QueryBuilder) Limit(value int) *QueryBuilder

func (*QueryBuilder) Offset

func (qb *QueryBuilder) Offset(value int) *QueryBuilder

func (*QueryBuilder) Order

func (qb *QueryBuilder) Order(column interface{}, order string) *QueryBuilder

func (*QueryBuilder) Paginate

func (qb *QueryBuilder) Paginate(page, perPage int) *QueryBuilder

func (*QueryBuilder) RemoveUpdate

func (qb *QueryBuilder) RemoveUpdate(column string) *QueryBuilder

func (*QueryBuilder) RemoveUpsertInsert

func (qb *QueryBuilder) RemoveUpsertInsert(column string) *QueryBuilder

func (*QueryBuilder) RemovetInsert

func (qb *QueryBuilder) RemovetInsert(column string) *QueryBuilder

func (*QueryBuilder) Select

func (qb *QueryBuilder) Select(columns ...interface{}) *QueryBuilder

func (*QueryBuilder) SelectDistinct

func (qb *QueryBuilder) SelectDistinct(columns ...interface{}) *QueryBuilder

func (*QueryBuilder) SelectDistinctModel

func (qb *QueryBuilder) SelectDistinctModel(model Model) *QueryBuilder

func (*QueryBuilder) SelectModel

func (qb *QueryBuilder) SelectModel(model Model) *QueryBuilder

func (*QueryBuilder) Where

func (qb *QueryBuilder) Where(conditions ...string) *QueryBuilder

func (*QueryBuilder) WhereOr

func (qb *QueryBuilder) WhereOr(conditions ...string) *QueryBuilder

type Table

type Table []TableRow

func (Table) IsEmpty

func (t Table) IsEmpty() bool

func (Table) ToIntegerSlice

func (t Table) ToIntegerSlice(column string) (integerSlice []int)

func (Table) ToJSON

func (t Table) ToJSON(selRen ...string) []byte

ToJSON for rename use `rowName AS newRowName`

func (Table) ToStringSlice

func (t Table) ToStringSlice(column string) (stringSlice []string)

type TableRow

type TableRow map[string]interface{}

func (TableRow) Get

func (tr TableRow) Get(name string) value.Value

func (TableRow) IsEmpty

func (tr TableRow) IsEmpty() bool

func (TableRow) ToJSON

func (tr TableRow) ToJSON(selRen ...string) []byte

ToJSON for rename use `rowName AS newRowName`

Jump to

Keyboard shortcuts

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