Browse Source

增加链式操作

master v0.2.0
guzeng 3 years ago
parent
commit
da5f7621da
4 changed files with 372 additions and 0 deletions
  1. +345
    -0
      chain.go
  2. +15
    -0
      chain_test.go
  3. +8
    -0
      go.mod
  4. +4
    -0
      go.sum

+ 345
- 0
chain.go View File

@ -0,0 +1,345 @@
package dbquery
import (
"database/sql"
"errors"
"log"
// "log"
"strconv"
"strings"
"git.tetele.net/tgo/helper"
)
type Query struct {
dbname string
table string
alias string
title string
where []string
where_or []string
join [][]string //[["tablea as a","a.id=b.id","left"]]
data []string
value []interface{}
orderby string
page int
page_size int
stmt *sql.Stmt
}
func (this *Query) Db(dbname string) *Query {
this.dbname = dbname
return this
}
func (this *Query) Table(tablename string) *Query {
this.table = tablename
return this
}
func (this *Query) Alias(tablename string) *Query {
this.alias = tablename
return this
}
func (this *Query) Title(title string) *Query {
this.title = title
return this
}
func (this *Query) Page(page int) *Query {
this.page = page
return this
}
func (this *Query) PageSize(page_num int) *Query {
this.page_size = page_num
return this
}
func (this *Query) Orderby(orderby string) *Query {
this.orderby = orderby
return this
}
func (this *Query) Where(where string) *Query {
this.where = append(this.where, where)
return this
}
func (this *Query) WhereOr(where string) *Query {
this.where_or = append(this.where_or, where)
return this
}
func (this *Query) Value(value interface{}) *Query {
this.value = append(this.value, value)
return this
}
func (this *Query) Join(join []string) *Query {
this.join = append(this.join, join)
return this
}
func (this *Query) Data(data string) *Query {
this.data = append(this.data, data)
return this
}
// func (this *Query) Insert(where string) *Query {
// this.insert = append(this.insert, where)
// return this
// }
// 拼查询sql
func (this *Query) QueryStmt() error {
if this.dbname == "" && this.table == "" {
return errors.New("参数错误,没有数据表")
}
if len(this.where)+len(this.where_or) < len(this.value) {
return errors.New("参数错误,条件值错误")
}
table := getTableName(this.dbname, this.table)
var stmt *sql.Stmt
var err error
var sql, title string
if this.title != "" {
title = this.title
} else {
title = "*"
}
sql = helper.StringJoin("select ", title)
if this.alias != "" {
this.table = helper.StringJoin(table, " as ", this.alias)
}
sql = helper.StringJoin(sql, " from ", this.table)
if len(this.join) > 0 {
for _, joinitem := range this.join {
if len(joinitem) < 2 {
continue
}
if len(joinitem) == 3 {
sql = helper.StringJoin(sql, " ", joinitem[2], " join ", getTableName(this.dbname, joinitem[0]), " on ", joinitem[1])
} else { //默认左连接
sql = helper.StringJoin(sql, " left join ", getTableName(this.dbname, joinitem[0]), " on ", joinitem[1])
}
}
}
if len(this.where) > 0 || len(this.where_or) > 0 {
sql = helper.StringJoin(sql, " where ")
}
if len(this.where) > 0 {
sql = helper.StringJoin(sql, " (", strings.Join(this.where, " and "), " ) ")
}
if len(this.where_or) > 0 {
if len(this.where) > 0 {
sql = helper.StringJoin(sql, " or ", strings.Join(this.where_or, " or "))
} else {
sql = helper.StringJoin(sql, strings.Join(this.where_or, " or "))
}
}
if this.orderby != "" {
sql = helper.StringJoin(sql, " order by ", this.orderby)
}
if this.page > 0 || this.page_size > 0 {
if this.page < 0 {
this.page = 0
}
if this.page_size < 1 {
this.page_size = 10
}
from := strconv.Itoa(this.page * this.page_size)
offset := strconv.Itoa(this.page_size)
if from != "" && offset != "" {
sql = helper.StringJoin(sql, " limit ", from, " , ", offset)
}
}
log.Println(sql)
stmt, err = DB.Prepare(sql)
if err != nil {
return err
}
this.stmt = stmt
return nil
}
// 拼更新sql
func (this *Query) UpdateStmt() error {
if this.dbname == "" && this.table == "" {
return errors.New("参数错误,没有数据表")
}
if len(this.where) < 1 {
return errors.New("参数错误,缺少条件")
}
dbName := getTableName(this.dbname, this.table)
var stmt *sql.Stmt
var err error
var sql string
sql = helper.StringJoin("update ", dbName, " set ", strings.Join(this.data, " , "))
sql = helper.StringJoin(sql, " where ", strings.Join(this.where, " and "))
stmt, err = DB.Prepare(sql)
if err != nil {
return err
}
this.stmt = stmt
return nil
}
// 拼插入sql
func (this *Query) CreateStmt() error {
if this.dbname == "" && this.table == "" {
return errors.New("参数错误,没有数据表")
}
dbName := getTableName(this.dbname, this.table)
var stmt *sql.Stmt
var err error
var sql string
sql = helper.StringJoin("insert into ", dbName, " set ", strings.Join(this.data, " , "))
stmt, err = DB.Prepare(sql)
if err != nil {
return err
}
this.stmt = stmt
return nil
}
// 拼删除sql
func (this *Query) DeleteStmt() error {
if this.dbname == "" && this.table == "" {
return errors.New("参数错误,没有数据表")
}
if len(this.where) < 1 {
return errors.New("参数错误,缺少条件")
}
if len(this.where) != len(this.value) {
return errors.New("参数错误,条件值错误")
}
dbName := getTableName(this.dbname, this.table)
var stmt *sql.Stmt
var err error
var sql string
sql = helper.StringJoin("delete from ", dbName, " where ", strings.Join(this.where, " and "))
if this.page_size > 0 {
sql = helper.StringJoin(sql, " limit ", strconv.Itoa(this.page_size))
}
stmt, err = DB.Prepare(sql)
if err != nil {
return err
}
this.stmt = stmt
return nil
}
/**
* 执行查询列表
* return list error
*/
func (this *Query) Select() ([]map[string]string, error) {
err := this.QueryStmt()
if err != nil {
return []map[string]string{}, err
}
if this.stmt == nil {
return []map[string]string{}, errors.New("缺少必要参数")
}
return StmtForQueryList(this.stmt, this.value)
}
/**
* 执行查询一条数据
* return row error
*/
func (this *Query) Find() (map[string]string, error) {
err := this.QueryStmt()
if err != nil {
return map[string]string{}, err
}
if this.stmt == nil {
return nil, errors.New("缺少必要参数")
}
return StmtForQueryRow(this.stmt, this.value)
}
/**
* 执行更新
* return is_updated error
*/
func (this *Query) Update() (int64, error) {
err := this.UpdateStmt()
if err != nil {
return 0, err
}
return StmtForUpdateExec(this.stmt, this.value)
}
/**
* 执行删除
* return is_delete error
*/
func (this *Query) Delete() (int64, error) {
err := this.DeleteStmt()
if err != nil {
return 0, err
}
return StmtForUpdateExec(this.stmt, this.value)
}
/**
* 执行写入
* return is_insert error
*/
func (this *Query) Create() (int64, error) {
err := this.CreateStmt()
if err != nil {
return 0, err
}
return StmtForInsertExec(this.stmt, this.value)
}

+ 15
- 0
chain_test.go View File

@ -0,0 +1,15 @@
package dbquery
import (
"testing"
)
func Test_Chain(t *testing.T) {
Connect("127.0.0.1", "root", "123456", "test1_tetele_com", "3306")
ret, err := new(Query).Db("test1_tetele_com").Table("ttl_user").Title("id,username").WhereOr("id =?").WhereOr("id = ?").Value(2).Value(4).PageSize(4).Select()
t.Log(len(ret))
t.Log(ret)
t.Log(err)
}

+ 8
- 0
go.mod View File

@ -0,0 +1,8 @@
module git.tetele.net/tgo/dbquery
go 1.14
require (
git.tetele.net/tgo/helper v0.1.0 // indirect
github.com/go-sql-driver/mysql v1.5.0 // indirect
)

+ 4
- 0
go.sum View File

@ -0,0 +1,4 @@
git.tetele.net/tgo/helper v0.1.0 h1:ZdsBXUWX3+22ZzHTZRldBfBsQwu+CwUH8qScUvpgimE=
git.tetele.net/tgo/helper v0.1.0/go.mod h1:shYQE/hvMy3fOE8JXKGxvywOXiz3M5Nw4e+u7HR8+NY=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=

Loading…
Cancel
Save