goSqlHelper

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

README

GoSqlHelper

GoSqlHelper is a go library to help you to execute Sql on mysql,you can easy to query a map[string]interface{} from this library.

Usage

download source:

git clone [email protected]:bobby96333/GoSqlHelper.git

or


require github.com/bobby96333/goSqlHelper v0.0.3

Easy to use query result,HelperRow is a map[string]interface{} struct

Demo code

open db

func OpenDb() *goSqlHelper.SqlHelper{

	con,err:= goSqlHelper.MysqlOpen("root:123456@tcp(centos:3306)/db1")
	if err!=nil {
		panic(err)
	}
	return con
}

query row

func main(){
	helper:= util.OpenDb()
	row,err:=helper.QueryRow("select * from tb_tb1 limit 1")
	if err!=nil {
		panic(err)
	}
	fmt.Printf("%+v\n",row)

	//equal to
	row,err=helper.Auto().Select("*").From("tb_tb1").Limit(1).QueryRow()
	if err!=nil {
		panic(err)
	}
	fmt.Printf("%+v\n",row)
	fmt.Println("done\n")

}

output:

map[id:1 val:0WWWWWW]
map[id:1 val:0WWWWWW]
done

insert/update/delete data



func main(){

	helper:= util.OpenDb()
	helper.OpenDebug()
	cnt,err:= helper.Exec("update tb_tb1 set val=concat(val,'W')")
	if err!=nil {
		panic(err)
	}
	fmt.Println("updates ",cnt," record")

	record:=goSqlHelper.HelperRow{
		"val":"8899",
	}
	id,err:=helper.Auto().Insert("tb_tb1").SetRow(&record).ExecInsert()
	if err!=nil {
		panic(err)
	}
	fmt.Printf("insert a record id:%d\n",id)



	updateCnt,err := helper.Auto().Update("tb_tb1").SetRow(&record).Where("id=?").ExecUpdateOrDel(5)
	if err!=nil {
		panic(err)
	}
	fmt.Printf("update record:%d\n",updateCnt)


	updateCnt,err = helper.Auto().Delete("tb_tb1").Where("id=?").ExecUpdateOrDel(id)
	if err!=nil {
		panic(err)
	}
	fmt.Printf("delete record:%d\n",updateCnt)

}

Query a big data

  querying,err := conn.Querying("select * from table where col1 = ? and  col2 = ?","123","abc")
	errCheck(err)
	for row,err:=querying.QueryRow();row!=nil&&err==nil;row,err=querying.QueryRow() {
		fmt.Println("row:",row.ToJson())
	}



Documentation

Index

Constants

View Source
const (
	SQL_SELECT = "SELECT"
	SQL_UPDATE = "UPDATE"
	SQL_DELETE = "DELETE"
	SQL_INSERT = "INSERT"
)
View Source
const QUERY_BUFFER_SIZE = 20

Variables

View Source
var NoFoundError = errors.New("no found data")

Functions

func Float64ToStr

func Float64ToStr(val float64) string

*

float64 转 string

func Int32ToStr

func Int32ToStr(val int32) string

*

int64 转 string

func Int64ToStr

func Int64ToStr(val int64) string

*

int64 转 string

func StrToFloat64

func StrToFloat64(val string) (float64, error)

string转float64

func StrToInt32

func StrToInt32(val string) (int32, error)

string转int32

func StrToInt64

func StrToInt64(val string) (int64, error)

string转int64

Types

type AutoSql added in v0.0.3

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

func NewAutoSql added in v0.0.3

func NewAutoSql(helper *SqlHelper) *AutoSql

func (*AutoSql) Delete added in v0.0.3

func (this *AutoSql) Delete(tbname string) *AutoSql

func (*AutoSql) Exec added in v0.0.3

func (this *AutoSql) Exec(args ...interface{}) (sql.Result, error)

func (*AutoSql) ExecInsert added in v0.0.3

func (this *AutoSql) ExecInsert(args ...interface{}) (int64, error)

execute insert sql

func (*AutoSql) ExecUpdateOrDel added in v0.0.3

func (this *AutoSql) ExecUpdateOrDel(args ...interface{}) (int64, error)

execute update or delete sql

func (*AutoSql) From added in v0.0.3

func (this *AutoSql) From(tbname string) *AutoSql

func (*AutoSql) GenerateSql added in v0.0.3

func (this *AutoSql) GenerateSql() string

func (*AutoSql) Groupby added in v0.0.3

func (this *AutoSql) Groupby(groupBySql string) *AutoSql

func (*AutoSql) Having added in v0.0.3

func (this *AutoSql) Having(having string) *AutoSql

func (*AutoSql) Insert added in v0.0.3

func (this *AutoSql) Insert(tbname string) *AutoSql

func (*AutoSql) Join added in v0.0.3

func (this *AutoSql) Join(joinSql string) *AutoSql

func (*AutoSql) Limit added in v0.0.3

func (this *AutoSql) Limit(limit int) *AutoSql

func (*AutoSql) Orderby added in v0.0.3

func (this *AutoSql) Orderby(OrderbySql string) *AutoSql

func (*AutoSql) OrmDelete added in v0.0.3

func (this *AutoSql) OrmDelete(orm IEntity) (int64, error)

func (*AutoSql) OrmInsert added in v0.0.3

func (this *AutoSql) OrmInsert(orm IEntity) (int64, error)

execute insert sql

func (*AutoSql) OrmUpdate added in v0.0.3

func (this *AutoSql) OrmUpdate(orm IEntity) (int64, error)

func (*AutoSql) QueryOrm added in v0.0.3

func (this *AutoSql) QueryOrm(orm IEntity, args ...interface{}) error

func (*AutoSql) QueryRow added in v0.0.3

func (this *AutoSql) QueryRow(args ...interface{}) (HelperRow, error)

func (*AutoSql) QueryRows added in v0.0.3

func (this *AutoSql) QueryRows(args ...interface{}) ([]HelperRow, error)

func (*AutoSql) QueryScalarInt added in v0.0.3

func (this *AutoSql) QueryScalarInt(args ...interface{}) (int, error)

func (*AutoSql) QueryTable added in v0.0.3

func (this *AutoSql) QueryTable(args ...interface{}) (*HelperTable, error)

func (*AutoSql) Querying added in v0.0.3

func (this *AutoSql) Querying(args ...interface{}) (*Querying, error)

func (*AutoSql) Select added in v0.0.3

func (this *AutoSql) Select(fieldSql string) *AutoSql

func (*AutoSql) Set added in v0.0.3

func (this *AutoSql) Set(setSql string) *AutoSql

func (*AutoSql) SetRow added in v0.0.3

func (this *AutoSql) SetRow(row *HelperRow) *AutoSql

func (*AutoSql) Update added in v0.0.3

func (this *AutoSql) Update(tbname string) *AutoSql

func (*AutoSql) Where added in v0.0.3

func (this *AutoSql) Where(where string) *AutoSql

type HelperRow

type HelperRow map[string]interface{}

func (HelperRow) CleverString

func (this HelperRow) CleverString(key string) string

func (HelperRow) Int

func (this HelperRow) Int(key string) (int, error)

*

int获取key

func (HelperRow) Int64

func (this HelperRow) Int64(key string) (int64, error)

*

int64获取key

func (HelperRow) PInt

func (this HelperRow) PInt(key string) int

func (HelperRow) PInt64

func (this HelperRow) PInt64(key string) int64

func (HelperRow) PString

func (this HelperRow) PString(key string) string

func (HelperRow) String

func (this HelperRow) String(key string) (string, error)

* 字段串获取key

func (HelperRow) ToJson

func (this HelperRow) ToJson() (string, error)

type HelperTable added in v0.0.2

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

func NewTable added in v0.0.2

func NewTable(rows []HelperRow, columns []string) *HelperTable

func (*HelperTable) Columns added in v0.0.2

func (this *HelperTable) Columns() []string

func (*HelperTable) Rows added in v0.0.2

func (this *HelperTable) Rows() []HelperRow

type IEntity added in v0.0.3

type IEntity interface {
	MapFields(columns []string) []interface{}
	PrimaryKeys() []string
	TableName() string
	MapColumn() map[string]interface{}
}

type Querying

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

func NewQuerying

func NewQuerying(rows *sql.Rows) *Querying

func (*Querying) Close

func (this *Querying) Close()

func (*Querying) Columns added in v0.0.2

func (this *Querying) Columns() ([]string, error)

func (Querying) QueryRow

func (this Querying) QueryRow() (HelperRow, error)

func (Querying) Scan added in v0.0.3

func (this Querying) Scan(vals ...interface{}) error

type SqlHelper

type SqlHelper struct {
	Connection *sql.DB
	// contains filtered or unexported fields
}

func MysqlOpen

func MysqlOpen(connectionStr string) (*SqlHelper, error)

* @todo no sql

var obj=new(tb1)
con.Insert(obj)
obj.setup(conn)
obj.Select("id,val").Where("id=2").QueryList()
sqlHelper.Select("id,val").Where("id=2").QueryList()

func New added in v0.0.2

func New(connectionStr string) (*SqlHelper, error)

func (*SqlHelper) Auto added in v0.0.3

func (this *SqlHelper) Auto() *AutoSql

get auto sql

func (*SqlHelper) Begin added in v0.0.3

func (this *SqlHelper) Begin() *SqlHelperRunner

* begin a trasnaction

func (*SqlHelper) BeginContext added in v0.0.3

func (this *SqlHelper) BeginContext(ctx context.Context) *SqlHelperRunner

* begin context

func (*SqlHelper) BeginTx added in v0.0.3

func (this *SqlHelper) BeginTx(ctx context.Context, opts *sql.TxOptions) (*SqlHelperRunner, error)

* begin a trasnaction

func (*SqlHelper) Close

func (this *SqlHelper) Close() error

close db pool

func (*SqlHelper) Exec

func (this *SqlHelper) Exec(sql string, args ...interface{}) (sql.Result, error)

execute sql

func (*SqlHelper) ExecInsert added in v0.0.3

func (this *SqlHelper) ExecInsert(sql string, args ...interface{}) (int64, error)

execute insert sql

func (*SqlHelper) ExecUpdateOrDel added in v0.0.3

func (this *SqlHelper) ExecUpdateOrDel(sql string, args ...interface{}) (int64, error)

execute update or delete sql

func (*SqlHelper) Open

func (this *SqlHelper) Open(connectionStr string) error

*

open db

func (*SqlHelper) OpenDebug added in v0.0.3

func (this *SqlHelper) OpenDebug()

* print sql and parameter at prepare exeucting

func (*SqlHelper) QueryOrm added in v0.0.3

func (this *SqlHelper) QueryOrm(orm IEntity, sql string, args ...interface{}) error

*

orm read data

func (*SqlHelper) QueryRow

func (this *SqlHelper) QueryRow(sql string, args ...interface{}) (HelperRow, error)

*

read a record row

func (*SqlHelper) QueryRows

func (this *SqlHelper) QueryRows(sql string, args ...interface{}) ([]HelperRow, error)

*

query muliti rows

func (*SqlHelper) QueryScalarInt

func (this *SqlHelper) QueryScalarInt(sql string, args ...interface{}) (int, error)

*

read a int value

func (*SqlHelper) QueryTable added in v0.0.2

func (this *SqlHelper) QueryTable(sql string, args ...interface{}) (*HelperTable, error)

*

read a table rows

func (*SqlHelper) Querying

func (this *SqlHelper) Querying(sql string, args ...interface{}) (*Querying, error)

* get Querying handler

func (*SqlHelper) SetDB

func (this *SqlHelper) SetDB(conn *sql.DB)

* set db object

type SqlHelperRunner added in v0.0.3

type SqlHelperRunner struct {
	SqlHelper
}

func (*SqlHelperRunner) Begin added in v0.0.3

func (this *SqlHelperRunner) Begin() error

func (*SqlHelperRunner) BeginTx added in v0.0.3

func (this *SqlHelperRunner) BeginTx(ctx context.Context, opts *sql.TxOptions) error

func (*SqlHelperRunner) SetContext added in v0.0.3

func (this *SqlHelperRunner) SetContext(ctx context.Context)

Directories

Path Synopsis
orm

Jump to

Keyboard shortcuts

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