dbwrapper

package module
v0.0.0-...-89ec693 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2020 License: MIT Imports: 12 Imported by: 0

README

About

THIS IS DEPRECATED. MOVE TO https://github.com/giant-stone/go/gsql.

github.com/jmoiron/sqlx CRUD wrapper:

  • Get - query reocrd
  • Gets - query records
  • Create - insert record
  • Creates - insert records in bulk
  • CreateOrUpdate - create or update record
  • Update update record
  • Del - delete record

Search, MySQL ONLY

  • Search - query records with where EQUAL(=) and LIKE conditions
  • SearchFullText - query records with MySQL fulltext index

Misc

  • RawQuery - custom SQL
  • GetColumns - compose xx in SELECT xx from ...

For more detail about example, see dbwrapper_test.go .

Install

go get -v -u github.com/lib/pq
go get -v -u github.com/go-sql-driver/mysql
go get -v -u github.com/jmoiron/sqlx
go get -v -u github.com/shuge/dbwrapper

See also

Documentation

Overview

github.com/jmoiron/sqlx CRUD wrapper. DO NOT REPEAT YOURSELF.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRecordNotFound      = errors.New("record not found")
	ErrDuplicatedUniqueKey = errors.New("duplicated unique key")
)

Functions

This section is empty.

Types

type DBW

type DBW interface {
	RawQuery(db *sqlx.DB, objs interface{}, s string, args ...interface{}) error
}

type DBWrapper

type DBWrapper struct {
	DriverName string
	Dsn        string
	Debug      bool
	TableName  string
	Columns    []string
}

func NewDBWrapper

func NewDBWrapper() *DBWrapper

NewDBWrapper setup DSN(data source name) and table, sub-class have to override its.

func (*DBWrapper) Create

func (its *DBWrapper) Create(db *sqlx.DB, m *map[string]interface{}) (result sql.Result, err error)

Create insert one record

func (*DBWrapper) CreateOrUpdate

func (its *DBWrapper) CreateOrUpdate(db *sqlx.DB, m *map[string]interface{}) (result sql.Result, err error)

CreateOrUpdate insert record or update record(s)

func (*DBWrapper) Creates

func (its *DBWrapper) Creates(db *sqlx.DB, items *[]map[string]interface{}) (result sql.Result, err error)

Creates insert records in bulk

func (*DBWrapper) Del

func (its *DBWrapper) Del(db *sqlx.DB, pkName string, m *map[string]interface{}) (err error)

Del delete record(s)

func (*DBWrapper) Get

func (its *DBWrapper) Get(db *sqlx.DB, obj interface{}, columns []string, pkName string, pk interface{}) (err error)

Get returns one record at most. parameter `obj“ must be pass by `&MyObject{}`.`

func (*DBWrapper) GetColumns

func (its *DBWrapper) GetColumns(obj interface{}) []string

GetColumns returns query columns from tag `db` in strutt.

func (*DBWrapper) Gets

func (its *DBWrapper) Gets(
	db *sqlx.DB, objs interface{},
	columns []string,
	conditionsWhere *map[string]interface{},
	limit int) (err error)

Gets query multiple records with where conditions(operator in condition alawys equals to =). DEPRECATED. See also GetsWhere.

func (*DBWrapper) GetsWhere

func (its *DBWrapper) GetsWhere(
	db *sqlx.DB, objs interface{},
	columns []string,
	conditionsWhere []map[string]interface{},
	limit int) (err error)

GetsWhere query multiple records with where conditions.

func (*DBWrapper) MustOpenDB

func (its *DBWrapper) MustOpenDB() (db *sqlx.DB)

func (*DBWrapper) OpenDB

func (its *DBWrapper) OpenDB() (db *sqlx.DB, err error)

func (*DBWrapper) RawExec

func (its *DBWrapper) RawExec(db *sqlx.DB, s string, args ...interface{}) (result sql.Result, err error)

RawExec custom SQL

func (*DBWrapper) RawQuery

func (its *DBWrapper) RawQuery(db *sqlx.DB, objs interface{}, s string, args ...interface{}) (err error)

RawQuery custom SQL

func (*DBWrapper) Search

func (its *DBWrapper) Search(
	db *sqlx.DB, objs interface{},
	columns []string,
	conditionsWhere *map[string]interface{},
	conditionsLike *map[string]interface{},
	limit int) (err error)

Search query records with where EQUAL(=) and LIKE conditions, MySQL *ONLY*.

func (*DBWrapper) SearchFullText

func (its *DBWrapper) SearchFullText(
	db *sqlx.DB, objs interface{},
	columns []string,
	columnsSearch []string,
	q string,
	limit int) (err error)

SearchFullText returns query records matched fulltext index. This query required created index likes `alter table mytbl add FULLTEXT ft_search (idx_col_a, idx_col_b, ...) WITH PARSER ngram`. MySQL *ONLY*.

func (*DBWrapper) Update

func (its *DBWrapper) Update(
	db *sqlx.DB,
	pkName string,
	changes map[string]interface{},
) (result sql.Result, err error)

Update update a record

func (*DBWrapper) UpdateWhere

func (its *DBWrapper) UpdateWhere(
	db *sqlx.DB,
	conditionsWhere []map[string]interface{},
	updatesMap map[string]interface{},
) (result sql.Result, err error)

UpdateWhere update multiple records with where conditions.

type JSONB

type JSONB map[string]interface{}

JSONB maps PostgreSQL JSONB type into `map` in Go. See also http://coussej.github.io/2016/02/16/Handling-JSONB-in-Go-Structs/

func (*JSONB) Scan

func (p *JSONB) Scan(src interface{}) error

Scan convert value from PostgreSQL JSONB into map[string]interface{}

func (JSONB) Value

func (p JSONB) Value() (driver.Value, error)

Value convert value from map[string]interface{} into []byte for PostgreSQL JSONB

Jump to

Keyboard shortcuts

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