wangchuanjin 5 年之前
父节点
当前提交
c8c0c3507c
共有 1 个文件被更改,包括 31 次插入16 次删除
  1. 31 16
      src/qfw/util/mysql/mysql.go

+ 31 - 16
src/qfw/util/mysql/mysql.go

@@ -70,13 +70,13 @@ func (m *Mysql) InsertByTx(tx *sql.Tx, tableName string, data map[string]interfa
 }
 
 //sql语句新增
-func (m *Mysql) InsertBySql(sql string, args ...interface{}) int64 {
-	return m.InsertBySqlByTx(nil, sql, args...)
+func (m *Mysql) InsertBySql(q string, args ...interface{}) int64 {
+	return m.InsertBySqlByTx(nil, q, args...)
 }
 
 //带有事务的sql语句新增
-func (m *Mysql) InsertBySqlByTx(tx *sql.Tx, sql string, args ...interface{}) int64 {
-	result, _ := m.ExecBySqlByTx(tx, sql, args...)
+func (m *Mysql) InsertBySqlByTx(tx *sql.Tx, q string, args ...interface{}) int64 {
+	result, _ := m.ExecBySqlByTx(tx, q, args...)
 	if result == nil {
 		return -1
 	}
@@ -147,8 +147,8 @@ func (m *Mysql) insertOrReplaceBatchByTx(tx *sql.Tx, tp string, afterInsert, tab
 }
 
 //sql语句执行
-func (m *Mysql) ExecBySql(sql string, args ...interface{}) (sql.Result, error) {
-	return m.ExecBySqlByTx(nil, sql, args...)
+func (m *Mysql) ExecBySql(q string, args ...interface{}) (sql.Result, error) {
+	return m.ExecBySqlByTx(nil, q, args...)
 }
 
 //sql语句执行,带有事务
@@ -239,10 +239,13 @@ func (m *Mysql) Find(tableName string, query map[string]interface{}, fields, ord
 }
 
 //sql语句查询
-func (m *Mysql) SelectBySql(sql string, args ...interface{}) *[]map[string]interface{} {
-	return m.SelectBySqlByTx(nil, sql, args...)
+func (m *Mysql) SelectBySql(q string, args ...interface{}) *[]map[string]interface{} {
+	return m.SelectBySqlByTx(nil, q, args...)
 }
 func (m *Mysql) SelectBySqlByTx(tx *sql.Tx, q string, args ...interface{}) *[]map[string]interface{} {
+	return m.Select(0, nil, tx, q, args...)
+}
+func (m *Mysql) Select(bath int, f func(l *[]map[string]interface{}), tx *sql.Tx, q string, args ...interface{}) *[]map[string]interface{} {
 	var stmtOut *sql.Stmt
 	var err error
 	if tx == nil {
@@ -255,7 +258,6 @@ func (m *Mysql) SelectBySqlByTx(tx *sql.Tx, q string, args ...interface{}) *[]ma
 		return nil
 	}
 	defer stmtOut.Close()
-
 	rows, err := stmtOut.Query(args...)
 	if err != nil {
 		log.Println(err)
@@ -282,7 +284,6 @@ func (m *Mysql) SelectBySqlByTx(tx *sql.Tx, q string, args ...interface{}) *[]ma
 			log.Println(err)
 			break
 		}
-
 		for i, col := range values {
 			if v, ok := col.([]uint8); ok {
 				ret[columns[i]] = string(v)
@@ -291,9 +292,23 @@ func (m *Mysql) SelectBySqlByTx(tx *sql.Tx, q string, args ...interface{}) *[]ma
 			}
 		}
 		list = append(list, ret)
+		if bath > 0 {
+			f(&list)
+			list = []map[string]interface{}{}
+		}
+	}
+	if bath > 0 && len(list) > 0 {
+		f(&list)
+		list = []map[string]interface{}{}
 	}
 	return &list
 }
+func (m *Mysql) SelectByBath(bath int, f func(l *[]map[string]interface{}), q string, args ...interface{}) {
+	m.SelectByBathByTx(bath, f, nil, q, args...)
+}
+func (m *Mysql) SelectByBathByTx(bath int, f func(l *[]map[string]interface{}), tx *sql.Tx, q string, args ...interface{}) {
+	m.Select(bath, f, tx, q, args...)
+}
 func (m *Mysql) FindOne(tableName string, query map[string]interface{}, fields, order string) *map[string]interface{} {
 	list := m.Find(tableName, query, fields, order, 0, 1)
 	if list != nil && len(*list) == 1 {
@@ -343,13 +358,13 @@ func (m *Mysql) DeleteByTx(tx *sql.Tx, tableName string, query map[string]interf
 }
 
 //修改或删除
-func (m *Mysql) UpdateOrDeleteBySql(sql string, args ...interface{}) int64 {
-	return m.UpdateOrDeleteBySqlByTx(nil, sql, args...)
+func (m *Mysql) UpdateOrDeleteBySql(q string, args ...interface{}) int64 {
+	return m.UpdateOrDeleteBySqlByTx(nil, q, args...)
 }
 
 //带事务的修改或删除
-func (m *Mysql) UpdateOrDeleteBySqlByTx(tx *sql.Tx, sql string, args ...interface{}) int64 {
-	result, err := m.ExecBySqlByTx(tx, sql, args...)
+func (m *Mysql) UpdateOrDeleteBySqlByTx(tx *sql.Tx, q string, args ...interface{}) int64 {
+	result, err := m.ExecBySqlByTx(tx, q, args...)
 	if err != nil {
 		log.Println(err)
 		return -1
@@ -383,8 +398,8 @@ func (m *Mysql) Count(tableName string, query map[string]interface{}) int64 {
 	log.Println(q, values)
 	return m.CountBySql(q, values...)
 }
-func (m *Mysql) CountBySql(sql string, args ...interface{}) int64 {
-	stmtIns, err := m.DB.Prepare(sql)
+func (m *Mysql) CountBySql(q string, args ...interface{}) int64 {
+	stmtIns, err := m.DB.Prepare(q)
 	if err != nil {
 		log.Println(err)
 		return -1