Browse Source

增加分组功能

master
zhenghaorong 1 year ago
parent
commit
44c03e0282
4 changed files with 30 additions and 7 deletions
  1. +11
    -2
      chain.go
  2. +8
    -4
      db.go
  3. +1
    -1
      db_test.go
  4. +10
    -0
      transaction_chain.go

+ 11
- 2
chain.go View File

@ -24,6 +24,7 @@ type Query struct {
data []string
value []interface{}
orderby string
gruopby string
page int
page_size int
stmt *sql.Stmt
@ -92,6 +93,10 @@ func (this *Query) Orderby(orderby string) *Query {
this.orderby = orderby
return this
}
func (this *Query) Gruopby(gruopby string) *Query {
this.gruopby = gruopby
return this
}
func (this *Query) Where(where string) *Query {
this.where = append(this.where, where)
return this
@ -143,6 +148,7 @@ func (this *Query) Clean() *Query {
this.data = this.data[0:0]
this.value = this.value[0:0]
this.orderby = ""
this.gruopby = ""
this.page = 0
this.page_size = 0
return this
@ -199,7 +205,10 @@ func (this *Query) QueryStmt() error {
sql = helper.StringJoin(sql, strings.Join(this.where_or, " or "))
}
}
if this.gruopby != "" {
sql = helper.StringJoin(sql, " gruop by ", this.gruopby)
}
if this.orderby != "" {
sql = helper.StringJoin(sql, " order by ", this.orderby)
}
@ -391,7 +400,7 @@ func (this *Query) DeleteStmt() error {
func (this *Query) Select() ([]map[string]string, error) {
_, rows, err := FetchRows(this.dbname, this.table, this.alias, this.title, this.join,
this.where, this.where_or, this.value, this.orderby, this.page, this.page_size, this.debug)
this.where, this.where_or, this.value, this.orderby, this.gruopby, this.page, this.page_size, this.debug)
return rows, err
}
@ -422,7 +431,7 @@ func (this *Query) List() ([]map[string]string, error) {
func (this *Query) Find() (map[string]string, error) {
_, row, err := GetRow(this.dbname, this.table, this.alias, this.title, this.join,
this.where, this.where_or, this.value, this.orderby, this.debug)
this.where, this.where_or, this.value, this.orderby, this.gruopby, this.debug)
return row, err
}


+ 8
- 4
db.go View File

@ -284,7 +284,7 @@ func GetData(dbName, table string, title string, where map[string]string, limit
* @param dbName 数据表名
* @param title 查询字段名
*/
func GetRow(dbName, table_name, alias string, titles string, join [][]string, where, where_or []string, valueList []interface{}, orderby string, debug bool) (int, map[string]string, error) {
func GetRow(dbName, table_name, alias string, titles string, join [][]string, where, where_or []string, valueList []interface{}, orderby, gruopby string, debug bool) (int, map[string]string, error) {
var count int = 0
info := make(map[string]string)
@ -334,7 +334,9 @@ func GetRow(dbName, table_name, alias string, titles string, join [][]string, wh
sql_str = helper.StringJoin(sql_str, strings.Join(where_or, " or "))
}
}
if gruopby != "" {
sql_str = helper.StringJoin(sql_str, " gruop by ", gruopby)
}
if orderby != "" {
sql_str = helper.StringJoin(sql_str, " order by ", orderby)
}
@ -422,7 +424,7 @@ func GetRow(dbName, table_name, alias string, titles string, join [][]string, wh
* @param dbName 数据表名
* @param title 查询字段名
*/
func FetchRows(dbName, table_name, alias string, titles string, join [][]string, where, where_or []string, valueList []interface{}, orderby string, page int, page_size int, debug bool) (int, []map[string]string, error) {
func FetchRows(dbName, table_name, alias string, titles string, join [][]string, where, where_or []string, valueList []interface{}, orderby, gruopby string, page int, page_size int, debug bool) (int, []map[string]string, error) {
var count int = 0
list := make([]map[string]string, 0)
@ -473,7 +475,9 @@ func FetchRows(dbName, table_name, alias string, titles string, join [][]string,
sql_str = helper.StringJoin(sql_str, strings.Join(where_or, " or "))
}
}
if gruopby != "" {
sql_str = helper.StringJoin(sql_str, " gruop by ", gruopby)
}
if orderby != "" {
sql_str = helper.StringJoin(sql_str, " order by ", orderby)
}


+ 1
- 1
db_test.go View File

@ -37,7 +37,7 @@ func Test_Connet(t *testing.T) {
debug := true
// count, row, err := GetRow(dbname, table, alias, title, join, where, where_or, valueList, orderby, debug)
count, row, err := FetchRows(dbname, table, alias, title, join, where, where_or, valueList, orderby, 1, 10, debug)
count, row, err := FetchRows(dbname, table, alias, title, join, where, where_or, valueList, orderby, "", 1, 10, debug)
log.Println(count)
log.Println(row)


+ 10
- 0
transaction_chain.go View File

@ -24,6 +24,7 @@ type TxQuery struct {
data []string
value []interface{}
orderby string
gruopby string
page int
page_size int
stmt *sql.Stmt
@ -93,6 +94,11 @@ func (this *TxQuery) Orderby(orderby string) *TxQuery {
this.orderby = orderby
return this
}
func (this *TxQuery) Gruopby(gruopby string) *TxQuery {
this.gruopby = gruopby
return this
}
func (this *TxQuery) Where(where string) *TxQuery {
this.where = append(this.where, where)
return this
@ -143,6 +149,7 @@ func (this *TxQuery) Clean() *TxQuery {
this.data = this.data[0:0]
this.value = this.value[0:0]
this.orderby = ""
this.gruopby = ""
this.page = 0
this.page_size = 0
return this
@ -197,7 +204,10 @@ func (this *TxQuery) QueryStmt() error {
sql = helper.StringJoin(sql, strings.Join(this.where_or, " or "))
}
}
if this.gruopby != "" {
sql = helper.StringJoin(sql, " gruop by ", this.gruopby)
}
if this.orderby != "" {
sql = helper.StringJoin(sql, " order by ", this.orderby)
}


Loading…
Cancel
Save