slimmysql

package module
v2.1.1-0...-bea59bd Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2016 License: MIT Imports: 6 Imported by: 2

README

slimmysql

a nice tool for mysql query

go语言下的mysql工具类 摈弃了ORM

contact me:[email protected]

github地址:https://github.com/jesusslim/slimmysql

使用:

完整例子 full example:

Slimgotest

Init(reg connections)

conf, _ := goconfig.LoadConfigFile("./conf/db.ini")
conf_sq := "local"
slimmysql.RegisterConnectionDefault(conf.MustBool(conf_sq, "rwseparate"), conf.MustValue(conf_sq, "host"), conf.MustValue(conf_sq, "port"), conf.MustValue(conf_sq, "db"), conf.MustValue(conf_sq, "user"), conf.MustValue(conf_sq, "pass"), conf.MustValue(conf_sq, "prefix"), false)

the conf shoud be like this:

配置文件参考

[local]
rwseparate = true
user = root
pass = root
host = 127.0.0.1,127.0.0.1,127.0.0.1
port = 3307
db = testgo,testgo2,testgo3
prefix = go_

Multi hosts means read/write separate,use the first host as master for write,others for read.

多个host表示主从读写分离 第一个地址为主库 其他为从库

Then get a Instance when you want to use it.

使用时使用该方法获取一个实例

slimsql, err := slimmysql.NewSqlInstanceDefault()

查询 condition

采用map[string]interface{}作为查询条件传入 map的key代表字段 value代表指 like/>/</between等等都在字段名称后面加上__like等 例如 nickname like : key=nickname__like

默认为=
like:__like
>:__gt
>=:__egt
<:__lt
<=:__elt
!=:__neq
between:__between
in:__in
notin:__notin
isnull:__isnull
isnotnull:__isnotnull

key=relation表示and、or等 默认为and 如果需嵌套condition 则key="__" value=map[string]interfcae{}

例子:

	condition := map[string]interface{}{
	"relation":       "or",
	"nickname__like": "Jesus",
	"_": map[string]interface{}{
		"id__gt": 10385,
		"_": map[string]interface{}{
			"relation": "or",
			"_": map[string]interface{}{
				"status":    1,
				"acoin__gt": 200,
			},
			"type": 2,
		},
		"create_time__between": "1431619200|1433088000",
	},
	"id__egt": 10575,
}

stds, err := slimsql.Table("students").Where(condition).Page(1, 10).Order("id desc").Fields("id,nickname,nickname_cn,acoin,mobile").Select()

//mysql:Select: SELECT id,nickname,nickname_cn,acoin,mobile FROM `pre_students`  WHERE  ( id > 10385  AND ( ( status = '1'  AND acoin > 200 ) OR type = '2' ) AND  ( create_time >= 1431619200 AND create_time <= 1433088000 ) ) OR id >= 10575  OR nickname LIKE '%Jesus%'  ORDER BY id desc  LIMIT 0,10

函数:

where
group
having
order
page
pk 设置主键
join :slimsql.Table("A").join("left join B on A.id = B.aid")
count
find 查询单个 返回一个map
select 返回一个map数组
getField 与select类似 返回一个value为map的map 以getfield中第一个字段作为key
setinc 增长
save update
add insert
delete

事务支持

starttrans
commit
rollback

锁表等

Lock
Unlock
LockRow(forupdate)

ping 测试连接状态

clear 清除slimmysql.Sql对象中的值(不清楚的情况下可重复使用 例如select之后 直接调用count可获得数量 而不需要重新传入condition等)

具体可参考gowalker:https://gowalker.org/github.com/jesusslim/slimmysql

Documentation

Index

Constants

View Source
const VERSION = "2.1.0"

Variables

This section is empty.

Functions

func RegisterConnection

func RegisterConnection(name string, rwSep bool, hosts, port, dbname, user, pass string, pre string, safe bool, maxOpenConnsNmaxIdleConns ...int)

*

  • [RegisterConnection Reg a connection]
  • @param {[string]} name string [connection name]
  • @param {[bool]} rwSep bool [use RWseparate]
  • @param {[string]} hosts [hosts for exp:192.168.0.1,192.168.0.2]
  • @param {[string]} port [port for exp:3306,3307]
  • @param {[string]} dbname [dbnames]
  • @param {[string]} user [users]
  • @param {[string]} pass string [passwords]
  • @param {[string]} pre string [prefix]
  • @param {[bool]} safe bool [safemode]
  • @param {[int]} maxOpenConnsNmaxIdleConns ...int [maxOpenConns and maxIdleConns for exp:2000,1000]

func RegisterConnectionDefault

func RegisterConnectionDefault(rwSep bool, hosts, port, dbname, user, pass string, pre string, safe bool, maxOpenConnsNmaxIdleConns ...int)

*

  • [RegisterConnectionDefault Reg the default connection]
  • @param {[bool]} rwSep bool [use RWseparate]
  • @param {[string]} hosts [hosts for exp:192.168.0.1,192.168.0.2]
  • @param {[string]} port [port for exp:3306,3307]
  • @param {[string]} dbname [dbnames]
  • @param {[string]} user [users]
  • @param {[string]} pass string [passwords]
  • @param {[string]} pre string [prefix]
  • @param {[bool]} safe bool [safemode]
  • @param {[int]} maxOpenConnsNmaxIdleConns ...int [maxOpenConns and maxIdleConns for exp:2000,1000]

Types

type Conns

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

type Sql

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

func NewSqlInstance

func NewSqlInstance(name string) (*Sql, error)

*

  • [NewSqlInstance Get a new instance for user]
  • @param {[string]} name string [connection name]
  • @return (*Sql, error)

func NewSqlInstanceDefault

func NewSqlInstanceDefault() (*Sql, error)

*

  • [NewSqlInstanceDefault Get a new instance for user]

func (*Sql) Add

func (this *Sql) Add(data map[string]interface{}) (int64, error)

*

  • Insert / convert ""/” maybe a bug.
  • @param map of data
  • @return id,err

func (*Sql) Clear

func (this *Sql) Clear() *Sql

func (*Sql) Commit

func (this *Sql) Commit() (bool, string)

func (*Sql) Count

func (this *Sql) Count(filed string) (int, error)

*

  • Count

func (*Sql) Delete

func (this *Sql) Delete() (int64, error)

*

  • Delete
  • @return effect,error

func (*Sql) Exec

func (this *Sql) Exec(theSql string) (sql.Result, error)

func (*Sql) Fields

func (this *Sql) Fields(filed string) *Sql

*

  • Set fields we want select
  • example:"id,title"

func (*Sql) Find

func (this *Sql) Find(id interface{}) (map[string]string, error)

*

  • select one return a single map

func (*Sql) GetConnectionName

func (this *Sql) GetConnectionName() string

func (*Sql) GetField

func (this *Sql) GetField(fields string) (map[string](map[string]string), error)

*

  • select func,return map,key is the first field

func (*Sql) GetSql

func (this *Sql) GetSql(isSelect bool) string

*

  • return the final sql str
  • @param isSelect true select false count

func (*Sql) Group

func (this *Sql) Group(g string) *Sql

*

  • group by

func (*Sql) Having

func (this *Sql) Having(h string) *Sql

*

  • having after group by
  • example:"age > 24"

func (*Sql) Join

func (this *Sql) Join(joinsql string) *Sql

*

  • Join table
  • example:"INNER JOIN B on A.id = B.aid"

func (*Sql) Lock

func (this *Sql) Lock(tables string, wirte bool) (bool, string)

*

  • Lock table

func (*Sql) LockRow

func (this *Sql) LockRow() *Sql

*

  • Lock row / for update

func (*Sql) MustMaster

func (this *Sql) MustMaster(mustMaster bool) *Sql

func (*Sql) NewCondition

func (this *Sql) NewCondition() map[string]interface{}

func (*Sql) Order

func (this *Sql) Order(order string) *Sql

*

  • order by
  • example:"id desc"

func (*Sql) Page

func (this *Sql) Page(page int, pagesize int) *Sql

*

  • page limit

func (*Sql) Ping

func (this *Sql) Ping() (bool, string)

func (*Sql) Pk

func (this *Sql) Pk(pk string) *Sql

*

  • Set primary key

func (*Sql) Query

func (this *Sql) Query(theSql string) (*sql.Rows, error)

func (*Sql) QueryMap

func (this *Sql) QueryMap(theSql string) ([]map[string]string, error)

func (*Sql) QueryRow

func (this *Sql) QueryRow(theSql string) *sql.Row

func (*Sql) Rollback

func (this *Sql) Rollback() (bool, string)

func (*Sql) Save

func (this *Sql) Save(data map[string]interface{}) (int64, error)

*

  • Update / convert ""/” maybe a bug.
  • @param map data
  • @return effect_colunms,err

func (*Sql) Select

func (this *Sql) Select() ([]map[string]string, error)

*

  • select func,return array

func (*Sql) SetInc

func (this *Sql) SetInc(field string, value int) (int64, error)

*

  • SetInc
  • @param fieldname string
  • @param value int
  • @return rows,error

func (*Sql) StartTrans

func (this *Sql) StartTrans() (bool, string)

func (*Sql) Table

func (this *Sql) Table(tablename string) *Sql

*

  • Set table name,use prefix

func (*Sql) TrueTable

func (this *Sql) TrueTable(tablename string) *Sql

*

  • Set table name,no prefix

func (*Sql) Unlock

func (this *Sql) Unlock() (bool, string)

*

  • Unlock

func (*Sql) Where

func (this *Sql) Where(condition interface{}) *Sql

*

  • where
  • @param condition:string/map

Jump to

Keyboard shortcuts

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