Browse Source

更新条件判断

master v0.2.2
guzeng 3 years ago
parent
commit
19c1efb26d
3 changed files with 63 additions and 21 deletions
  1. +60
    -18
      chain.go
  2. +1
    -1
      chain_test.go
  3. +2
    -2
      go.mod

+ 60
- 18
chain.go View File

@ -3,15 +3,15 @@ package dbquery
import ( import (
"database/sql" "database/sql"
"errors" "errors"
"log"
// "log"
"strconv" "strconv"
"strings" "strings"
"git.tetele.net/tgo/helper" "git.tetele.net/tgo/helper"
) )
var stmt *sql.Stmt
var err error
type Query struct { type Query struct {
dbname string dbname string
table string table string
@ -99,14 +99,13 @@ func (this *Query) QueryStmt() error {
if this.dbname == "" && this.table == "" { if this.dbname == "" && this.table == "" {
return errors.New("参数错误,没有数据表") return errors.New("参数错误,没有数据表")
} }
if len(this.where)+len(this.where_or) < len(this.value) {
return errors.New("参数错误,条件值错误")
}
// if len(this.where)+len(this.where_or) < len(this.value) {
// return errors.New("参数错误,条件值错误")
// }
table := getTableName(this.dbname, this.table) table := getTableName(this.dbname, this.table)
var stmt *sql.Stmt
var err error
// var err error
var sql, title string var sql, title string
@ -167,7 +166,17 @@ func (this *Query) QueryStmt() error {
sql = helper.StringJoin(sql, " limit ", from, " , ", offset) sql = helper.StringJoin(sql, " limit ", from, " , ", offset)
} }
} }
log.Println(sql)
// log.Println(sql)
condition_len := 0 //所有条件数
for _, ch2 := range sql {
if string(ch2) == "?" {
condition_len++
}
}
if condition_len != len(this.value) {
return errors.New("参数错误,条件值错误")
}
stmt, err = DB.Prepare(sql) stmt, err = DB.Prepare(sql)
if err != nil { if err != nil {
@ -191,8 +200,8 @@ func (this *Query) UpdateStmt() error {
dbName := getTableName(this.dbname, this.table) dbName := getTableName(this.dbname, this.table)
var stmt *sql.Stmt
var err error
// var stmt *sql.Stmt
// var err error
var sql string var sql string
@ -200,6 +209,17 @@ func (this *Query) UpdateStmt() error {
sql = helper.StringJoin(sql, " where ", strings.Join(this.where, " and ")) sql = helper.StringJoin(sql, " where ", strings.Join(this.where, " and "))
condition_len := 0 //所有条件数
for _, ch2 := range sql {
if string(ch2) == "?" {
condition_len++
}
}
if condition_len != len(this.value) {
return errors.New("参数错误,条件值错误")
}
stmt, err = DB.Prepare(sql) stmt, err = DB.Prepare(sql)
if err != nil { if err != nil {
@ -220,13 +240,24 @@ func (this *Query) CreateStmt() error {
dbName := getTableName(this.dbname, this.table) dbName := getTableName(this.dbname, this.table)
var stmt *sql.Stmt
var err error
// var stmt *sql.Stmt
// var err error
var sql string var sql string
sql = helper.StringJoin("insert into ", dbName, " set ", strings.Join(this.data, " , ")) sql = helper.StringJoin("insert into ", dbName, " set ", strings.Join(this.data, " , "))
condition_len := 0 //所有条件数
for _, ch2 := range sql {
if string(ch2) == "?" {
condition_len++
}
}
if condition_len != len(this.value) {
return errors.New("参数错误,条件值错误")
}
stmt, err = DB.Prepare(sql) stmt, err = DB.Prepare(sql)
if err != nil { if err != nil {
@ -247,14 +278,14 @@ func (this *Query) DeleteStmt() error {
if len(this.where) < 1 { if len(this.where) < 1 {
return errors.New("参数错误,缺少条件") return errors.New("参数错误,缺少条件")
} }
if len(this.where) != len(this.value) {
return errors.New("参数错误,条件值错误")
}
// if len(this.where) != len(this.value) {
// return errors.New("参数错误,条件值错误")
// }
dbName := getTableName(this.dbname, this.table) dbName := getTableName(this.dbname, this.table)
var stmt *sql.Stmt
var err error
// var stmt *sql.Stmt
// var err error
var sql string var sql string
@ -264,6 +295,17 @@ func (this *Query) DeleteStmt() error {
sql = helper.StringJoin(sql, " limit ", strconv.Itoa(this.page_size)) sql = helper.StringJoin(sql, " limit ", strconv.Itoa(this.page_size))
} }
condition_len := 0 //所有条件数
for _, ch2 := range sql {
if string(ch2) == "?" {
condition_len++
}
}
if condition_len != len(this.value) {
return errors.New("参数错误,条件值错误")
}
stmt, err = DB.Prepare(sql) stmt, err = DB.Prepare(sql)
if err != nil { if err != nil {


+ 1
- 1
chain_test.go View File

@ -7,7 +7,7 @@ import (
func Test_Chain(t *testing.T) { func Test_Chain(t *testing.T) {
Connect("127.0.0.1", "root", "123456", "test1_tetele_com", "3306") 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()
ret, err := new(Query).Db("test1_tetele_com").Table("ttl_user").Title("id,username").WhereOr("id =?").WhereOr("id = ?").Value(2).Value(4).Value(4).PageSize(4).Select()
t.Log(len(ret)) t.Log(len(ret))
t.Log(ret) t.Log(ret)


+ 2
- 2
go.mod View File

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

Loading…
Cancel
Save