From 44c03e0282d09ac1e824ecc52e50655656363ad2 Mon Sep 17 00:00:00 2001 From: zhenghaorong Date: Sat, 20 Aug 2022 12:07:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=86=E7=BB=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chain.go | 13 +++++++++++-- db.go | 12 ++++++++---- db_test.go | 2 +- transaction_chain.go | 10 ++++++++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/chain.go b/chain.go index 3a7531b..6b216ce 100644 --- a/chain.go +++ b/chain.go @@ -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 } diff --git a/db.go b/db.go index 6b454b1..af39478 100644 --- a/db.go +++ b/db.go @@ -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) } diff --git a/db_test.go b/db_test.go index 4530f79..2e71c7e 100755 --- a/db_test.go +++ b/db_test.go @@ -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) diff --git a/transaction_chain.go b/transaction_chain.go index d59a8ba..97f01a0 100644 --- a/transaction_chain.go +++ b/transaction_chain.go @@ -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) }