|
@@ -44,21 +44,16 @@ func (m *Mysql) Init() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-//新增
|
|
|
+// 新增
|
|
|
func (m *Mysql) Insert(tableName string, data map[string]interface{}) int64 {
|
|
|
return m.InsertByTx(nil, tableName, data)
|
|
|
}
|
|
|
|
|
|
-//带有事务的新增
|
|
|
+// 带有事务的新增
|
|
|
func (m *Mysql) InsertByTx(tx *sql.Tx, tableName string, data map[string]interface{}) int64 {
|
|
|
fields := []string{}
|
|
|
values := []interface{}{}
|
|
|
placeholders := []string{}
|
|
|
- if tableName == "dataexport_order" {
|
|
|
- if _, ok := data["user_nickname"]; ok {
|
|
|
- data["user_nickname"] = ""
|
|
|
- }
|
|
|
- }
|
|
|
for k, v := range data {
|
|
|
fields = append(fields, k)
|
|
|
values = append(values, v)
|
|
@@ -69,12 +64,12 @@ func (m *Mysql) InsertByTx(tx *sql.Tx, tableName string, data map[string]interfa
|
|
|
return m.InsertBySqlByTx(tx, q, values...)
|
|
|
}
|
|
|
|
|
|
-//sql语句新增
|
|
|
+// sql语句新增
|
|
|
func (m *Mysql) InsertBySql(q string, args ...interface{}) int64 {
|
|
|
return m.InsertBySqlByTx(nil, q, args...)
|
|
|
}
|
|
|
|
|
|
-//带有事务的sql语句新增
|
|
|
+// 带有事务的sql语句新增
|
|
|
func (m *Mysql) InsertBySqlByTx(tx *sql.Tx, q string, args ...interface{}) int64 {
|
|
|
result, _ := m.ExecBySqlByTx(tx, q, args...)
|
|
|
if result == nil {
|
|
@@ -88,32 +83,32 @@ func (m *Mysql) InsertBySqlByTx(tx *sql.Tx, q string, args ...interface{}) int64
|
|
|
return id
|
|
|
}
|
|
|
|
|
|
-//批量新增
|
|
|
+// 批量新增
|
|
|
func (m *Mysql) InsertIgnoreBatch(tableName string, fields []string, values []interface{}) (int64, int64) {
|
|
|
return m.InsertIgnoreBatchByTx(nil, tableName, fields, values)
|
|
|
}
|
|
|
|
|
|
-//带事务的批量新增
|
|
|
+// 带事务的批量新增
|
|
|
func (m *Mysql) InsertIgnoreBatchByTx(tx *sql.Tx, tableName string, fields []string, values []interface{}) (int64, int64) {
|
|
|
return m.insertOrReplaceBatchByTx(tx, "INSERT", "IGNORE", tableName, fields, values)
|
|
|
}
|
|
|
|
|
|
-//批量新增
|
|
|
+// 批量新增
|
|
|
func (m *Mysql) InsertBatch(tableName string, fields []string, values []interface{}) (int64, int64) {
|
|
|
return m.InsertBatchByTx(nil, tableName, fields, values)
|
|
|
}
|
|
|
|
|
|
-//带事务的批量新增
|
|
|
+// 带事务的批量新增
|
|
|
func (m *Mysql) InsertBatchByTx(tx *sql.Tx, tableName string, fields []string, values []interface{}) (int64, int64) {
|
|
|
return m.insertOrReplaceBatchByTx(tx, "INSERT", "", tableName, fields, values)
|
|
|
}
|
|
|
|
|
|
-//批量更新
|
|
|
+// 批量更新
|
|
|
func (m *Mysql) ReplaceBatch(tableName string, fields []string, values []interface{}) (int64, int64) {
|
|
|
return m.ReplaceBatchByTx(nil, tableName, fields, values)
|
|
|
}
|
|
|
|
|
|
-//带事务的批量更新
|
|
|
+// 带事务的批量更新
|
|
|
func (m *Mysql) ReplaceBatchByTx(tx *sql.Tx, tableName string, fields []string, values []interface{}) (int64, int64) {
|
|
|
return m.insertOrReplaceBatchByTx(tx, "REPLACE", "", tableName, fields, values)
|
|
|
}
|
|
@@ -184,12 +179,12 @@ func (m *Mysql) InsertBulk(tableName string, fields []string, doc ...map[string]
|
|
|
return v1, v2
|
|
|
}
|
|
|
|
|
|
-//sql语句执行
|
|
|
+// sql语句执行
|
|
|
func (m *Mysql) ExecBySql(q string, args ...interface{}) (sql.Result, error) {
|
|
|
return m.ExecBySqlByTx(nil, q, args...)
|
|
|
}
|
|
|
|
|
|
-//sql语句执行,带有事务
|
|
|
+// sql语句执行,带有事务
|
|
|
func (m *Mysql) ExecBySqlByTx(tx *sql.Tx, q string, args ...interface{}) (sql.Result, error) {
|
|
|
var stmtIns *sql.Stmt
|
|
|
var err error
|
|
@@ -289,7 +284,7 @@ func (m *Mysql) Find(tableName string, query map[string]interface{}, fields, ord
|
|
|
return m.SelectBySql(q, vs...)
|
|
|
}
|
|
|
|
|
|
-//sql语句查询
|
|
|
+// sql语句查询
|
|
|
func (m *Mysql) SelectBySql(q string, args ...interface{}) *[]map[string]interface{} {
|
|
|
return m.SelectBySqlByTx(nil, q, args...)
|
|
|
}
|
|
@@ -369,12 +364,12 @@ func (m *Mysql) FindOne(tableName string, query map[string]interface{}, fields,
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-//修改
|
|
|
+// 修改
|
|
|
func (m *Mysql) Update(tableName string, query, update map[string]interface{}) bool {
|
|
|
return m.UpdateByTx(nil, tableName, query, update)
|
|
|
}
|
|
|
|
|
|
-//带事务的修改
|
|
|
+// 带事务的修改
|
|
|
func (m *Mysql) UpdateByTx(tx *sql.Tx, tableName string, query, update map[string]interface{}) bool {
|
|
|
q_fs := []string{}
|
|
|
u_fs := []string{}
|
|
@@ -391,7 +386,7 @@ func (m *Mysql) UpdateByTx(tx *sql.Tx, tableName string, query, update map[strin
|
|
|
return m.UpdateOrDeleteBySqlByTx(tx, q, values...) >= 0
|
|
|
}
|
|
|
|
|
|
-//删除
|
|
|
+// 删除
|
|
|
func (m *Mysql) Delete(tableName string, query map[string]interface{}) bool {
|
|
|
return m.DeleteByTx(nil, tableName, query)
|
|
|
}
|
|
@@ -407,12 +402,12 @@ func (m *Mysql) DeleteByTx(tx *sql.Tx, tableName string, query map[string]interf
|
|
|
return m.UpdateOrDeleteBySqlByTx(tx, q, values...) > 0
|
|
|
}
|
|
|
|
|
|
-//修改或删除
|
|
|
+// 修改或删除
|
|
|
func (m *Mysql) UpdateOrDeleteBySql(q string, args ...interface{}) int64 {
|
|
|
return m.UpdateOrDeleteBySqlByTx(nil, q, args...)
|
|
|
}
|
|
|
|
|
|
-//带事务的修改或删除
|
|
|
+// 带事务的修改或删除
|
|
|
func (m *Mysql) UpdateOrDeleteBySqlByTx(tx *sql.Tx, q string, args ...interface{}) int64 {
|
|
|
result, err := m.ExecBySqlByTx(tx, q, args...)
|
|
|
if err != nil {
|
|
@@ -427,7 +422,7 @@ func (m *Mysql) UpdateOrDeleteBySqlByTx(tx *sql.Tx, q string, args ...interface{
|
|
|
return count
|
|
|
}
|
|
|
|
|
|
-//总数
|
|
|
+// 总数
|
|
|
func (m *Mysql) Count(tableName string, query map[string]interface{}) int64 {
|
|
|
fields := []string{}
|
|
|
values := []interface{}{}
|
|
@@ -503,7 +498,7 @@ func (m *Mysql) CountBySql(q string, args ...interface{}) int64 {
|
|
|
return count
|
|
|
}
|
|
|
|
|
|
-//执行事务
|
|
|
+// 执行事务
|
|
|
func (m *Mysql) ExecTx(msg string, f func(tx *sql.Tx) bool) bool {
|
|
|
tx, err := m.DB.Begin()
|
|
|
if err != nil {
|