xdb

package module
v0.0.0-...-e335e81 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2018 License: MIT Imports: 8 Imported by: 0

README

database query help tools

New

import (
    "database/sql"
    "github.com/baubles/go-xdb"
    _ "github.com/go-sql-driver/mysql" // mysql driver
)

dbConn, err := sql.Open("mysql", dsn)
db = xdb.New(dbConn)

select


type Entity {
    ID   int64 `db:Id`
    Name string
}

type Args {
    ID int64 `db:Id`
}

args := &Args{Id:1}
row := &Entity{}
rows := []*Entity{}

// reflect to struct
err := db.NewQuery().SQL("select Id, Name from table where Id = ${Id}").ReflectArgs(row).ReflectRow(row)
fmt.Println(err, row)

// reflect to struct list
n, err := db.NewQuery().SQL("select Id, Name from table where Id = ${Id}").ReflectArgs(row).ReflectRows(&rows)
fmt.Println(n, err, rows)

//return map[string]Value
mRow, err := db.NewQuery().SQL("select Id, Name from table where Id = ${Id}").ReflectArgs(row).Row()
fmt.Println(err, mRow)

// return []map[string]Value
mRows, err := db.NewQuery().SQL("select Id, Name from table").Rows()
fmt.Println(err, mRows)

insert & update


row := &Entity{
    Id: 1,
    Name: "hello",
}

result, err := db.NewQuery().SQL("insert into table (Id, Name) values (?, ?)").Args(row.Id, row.Name).Exec()

result, err := db.NewQuery().InsertInto("table").Columns("Id, Name").Values("${Id}, ${Name}").ReflectArgs(row).Exec()

result, err := db.NewQuery().SQL("update table set Name = ${Name} where Id = ${Id}").ReflectArgs(row).Exec()

result, err := db.NewQuery().Update("table").Set("Name = ${Name}").Where("Id = ${Id}").ReflectArgs(row).Exec()

delete

row := &Entity{
    Id: 1,
}

result, err := db.NewQuery().DeleteFrom("table").Where("Id = ${Id}").ReflectArgs(row).Exec()
result, err := db.NewQuery().SQL("delete from table where Id = ?").Args(row.Id).Exec()

transaction


tx, err := db.Begin()
...

if ok {
    tx.Commit()
} else {
    tx.Rollback()
}

Prepare


rows := []*Entity{...}

query := db.NewQuery().InsertInto("table").Columns("Id, Name").Values("${Id}, ${Name}").Prepare()
defer query.Close()

for _, row := range rows {
    query.ReflectArgs(row).Exec()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LogFunc func(sql string, args ...interface{})

LogFunc func print sql log

Functions

This section is empty.

Types

type DB

type DB interface {
	Helper
	Begin() (TX, error)
}

DB db

func New

func New(db *sql.DB) DB

New db

type Helper

type Helper interface {
	NewQuery() Query
	Querier() Querier
}

Helper qeury helper

type Querier

type Querier interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row

	Prepare(query string) (*sql.Stmt, error)
}

Querier sql querier

type Query

type Query interface {
	Update(table string) Query
	Set(set string) Query
	DeleteFrom(table string) Query
	InsertInto(table string) Query
	Columns(columns string) Query
	Values(values string) Query
	Select(columns string) Query
	SelectDistinct(columns string) Query
	From(tables string) Query
	Join(join string) Query
	InnerJoin(innerJoin string) Query
	LeftJoin(leftOuterJoin string) Query
	RightJoin(rightOuterJoin string) Query
	OuterJoin(outJoin string) Query
	Where(where string) Query
	Having(having string) Query
	And() Query
	Or() Query
	GroupBy(having string) Query
	OrderBy(orderBy string) Query
	Limit(limit string) Query
	SQL(sqlString string) Query
	String() string

	Prepare() error
	Close() error

	Args(args ...interface{}) Query
	ReflectArgs(args interface{}) Query

	Exec() (sql.Result, error)
	List(columns string) ([]Value, error)
	Value() (Value, error)
	Row() (Row, error)
	Rows() ([]Row, error)
	ReflectRow(row interface{}) error
	ReflectRows(rows interface{}) (int64, error)
}

Query sql

type Row

type Row map[string]Value

Row row value

func (Row) Get

func (r Row) Get(key string) Value

Get get value of key

func (Row) Has

func (r Row) Has(key string) bool

Has return row exist key

func (Row) Set

func (r Row) Set(key string, v Value)

Set set value of key

type Rows

type Rows []Row

Rows row list

type TX

type TX interface {
	Helper
	Rollback() error
	Commit() error
}

TX tx

func NewTX

func NewTX(tx *sql.Tx) TX

NewTX new transaction

type Value

type Value sql.RawBytes

Value colment

func (Value) Bool

func (v Value) Bool() bool

Bool value to bool

func (Value) Float

func (v Value) Float() float64

Float value to float64

func (Value) Int

func (v Value) Int() int64

Int value to int64

func (Value) String

func (v Value) String() string

Jump to

Keyboard shortcuts

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