|
|
@ -4,6 +4,7 @@ import (
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -133,6 +134,49 @@ func (star *StarDB) ExecXS(ins interface{}, args ...interface{}) ([]sql.Result,
|
|
|
|
func (star *StarDB) ExecX(ins interface{}, args ...interface{}) (sql.Result, error) {
|
|
|
|
func (star *StarDB) ExecX(ins interface{}, args ...interface{}) (sql.Result, error) {
|
|
|
|
return star.execX(nil, ins, args...)
|
|
|
|
return star.execX(nil, ins, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getUpdateSentence(ins interface{}, sheetName string, primaryKey ...string) (string, []string, error) {
|
|
|
|
|
|
|
|
Keys, err := getAllRefKey(ins, "db")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "", []string{}, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var mystr string
|
|
|
|
|
|
|
|
for k, v := range Keys {
|
|
|
|
|
|
|
|
mystr += fmt.Sprintf("%s=? ", v)
|
|
|
|
|
|
|
|
Keys[k] = ":" + v
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
mystr = fmt.Sprintf("update %s set %s where ", sheetName, mystr)
|
|
|
|
|
|
|
|
var whereSlice []string
|
|
|
|
|
|
|
|
for _, v := range primaryKey {
|
|
|
|
|
|
|
|
whereSlice = append(whereSlice, v+"=?")
|
|
|
|
|
|
|
|
Keys = append(Keys, ":"+v)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
mystr += strings.Join(whereSlice, " and ")
|
|
|
|
|
|
|
|
return mystr, Keys, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getInsertSentence(ins interface{}, sheetName string, autoIncrease ...string) (string, []string, error) {
|
|
|
|
|
|
|
|
Keys, err := getAllRefKey(ins, "db")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "", []string{}, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var mystr, rps string
|
|
|
|
|
|
|
|
var rtnKeys []string
|
|
|
|
|
|
|
|
cns:
|
|
|
|
|
|
|
|
for _, v := range Keys {
|
|
|
|
|
|
|
|
for _, vs := range autoIncrease {
|
|
|
|
|
|
|
|
if v == vs {
|
|
|
|
|
|
|
|
rps += "null,"
|
|
|
|
|
|
|
|
continue cns
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
rtnKeys = append(rtnKeys, ":"+v)
|
|
|
|
|
|
|
|
rps += "?,"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
mystr = fmt.Sprintf("insert into %s (%s) values (%s) ", sheetName, strings.Join(Keys, ","), rps[:len(rps)-1])
|
|
|
|
|
|
|
|
return mystr, rtnKeys, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarDB) execX(ctx context.Context, ins interface{}, args ...interface{}) (sql.Result, error) {
|
|
|
|
func (star *StarDB) execX(ctx context.Context, ins interface{}, args ...interface{}) (sql.Result, error) {
|
|
|
|
kvMap, err := getAllRefValue(ins, "db")
|
|
|
|
kvMap, err := getAllRefValue(ins, "db")
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -161,10 +205,44 @@ func (star *StarDB) execX(ctx context.Context, ins interface{}, args ...interfac
|
|
|
|
return star.exec(ctx, args...)
|
|
|
|
return star.exec(ctx, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarDB) QueryXContext(ctx context.Context,ins interface{}, args ...interface{}) (*StarRows, error) {
|
|
|
|
func (star *StarDB) Update(ins interface{}, sheetName string, primaryKey ...string) (sql.Result, error) {
|
|
|
|
|
|
|
|
return star.updateinsert(nil, true, ins, sheetName, primaryKey...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarDB) UpdateContext(ctx context.Context, ins interface{}, sheetName string, primaryKey ...string) (sql.Result, error) {
|
|
|
|
|
|
|
|
return star.updateinsert(ctx, true, ins, sheetName, primaryKey...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarDB) Insert(ins interface{}, sheetName string, primaryKey ...string) (sql.Result, error) {
|
|
|
|
|
|
|
|
return star.updateinsert(nil, false, ins, sheetName, primaryKey...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarDB) InsertContext(ctx context.Context, ins interface{}, sheetName string, primaryKey ...string) (sql.Result, error) {
|
|
|
|
|
|
|
|
return star.updateinsert(ctx, false, ins, sheetName, primaryKey...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarDB) updateinsert(ctx context.Context, isUpdate bool, ins interface{}, sheetName string, primaryKey ...string) (sql.Result, error) {
|
|
|
|
|
|
|
|
var sqlStr string
|
|
|
|
|
|
|
|
var para []string
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if isUpdate {
|
|
|
|
|
|
|
|
sqlStr, para, err = getUpdateSentence(ins, sheetName, primaryKey...)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
sqlStr, para, err = getInsertSentence(ins, sheetName, primaryKey...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpStr := append([]interface{}{}, sqlStr)
|
|
|
|
|
|
|
|
for _, v := range para {
|
|
|
|
|
|
|
|
tmpStr = append(tmpStr, v)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return star.execX(ctx, ins, tmpStr...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (star *StarDB) QueryXContext(ctx context.Context, ins interface{}, args ...interface{}) (*StarRows, error) {
|
|
|
|
return star.queryX(ctx, ins, args)
|
|
|
|
return star.queryX(ctx, ins, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (star *StarDB) QueryXSContext(ctx context.Context,ins interface{}, args ...interface{}) ([]*StarRows, error) {
|
|
|
|
func (star *StarDB) QueryXSContext(ctx context.Context, ins interface{}, args ...interface{}) ([]*StarRows, error) {
|
|
|
|
var starRes []*StarRows
|
|
|
|
var starRes []*StarRows
|
|
|
|
t := reflect.TypeOf(ins)
|
|
|
|
t := reflect.TypeOf(ins)
|
|
|
|
v := reflect.ValueOf(ins)
|
|
|
|
v := reflect.ValueOf(ins)
|
|
|
@ -191,7 +269,7 @@ func (star *StarDB) QueryXSContext(ctx context.Context,ins interface{}, args ...
|
|
|
|
return starRes, nil
|
|
|
|
return starRes, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarDB) ExecXSContext(ctx context.Context,ins interface{}, args ...interface{}) ([]sql.Result, error) {
|
|
|
|
func (star *StarDB) ExecXSContext(ctx context.Context, ins interface{}, args ...interface{}) ([]sql.Result, error) {
|
|
|
|
var starRes []sql.Result
|
|
|
|
var starRes []sql.Result
|
|
|
|
t := reflect.TypeOf(ins)
|
|
|
|
t := reflect.TypeOf(ins)
|
|
|
|
v := reflect.ValueOf(ins)
|
|
|
|
v := reflect.ValueOf(ins)
|
|
|
@ -218,12 +296,10 @@ func (star *StarDB) ExecXSContext(ctx context.Context,ins interface{}, args ...i
|
|
|
|
return starRes, nil
|
|
|
|
return starRes, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarDB) ExecXContext(ctx context.Context,ins interface{}, args ...interface{}) (sql.Result, error) {
|
|
|
|
func (star *StarDB) ExecXContext(ctx context.Context, ins interface{}, args ...interface{}) (sql.Result, error) {
|
|
|
|
return star.execX(ctx, ins, args...)
|
|
|
|
return star.execX(ctx, ins, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) queryX(ctx context.Context, ins interface{}, args ...interface{}) (*StarRows, error) {
|
|
|
|
func (star *StarTx) queryX(ctx context.Context, ins interface{}, args ...interface{}) (*StarRows, error) {
|
|
|
|
kvMap, err := getAllRefValue(ins, "db")
|
|
|
|
kvMap, err := getAllRefValue(ins, "db")
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -254,6 +330,7 @@ func (star *StarTx) queryX(ctx context.Context, ins interface{}, args ...interfa
|
|
|
|
func (star *StarTx) QueryX(ins interface{}, args ...interface{}) (*StarRows, error) {
|
|
|
|
func (star *StarTx) QueryX(ins interface{}, args ...interface{}) (*StarRows, error) {
|
|
|
|
return star.queryX(nil, ins, args)
|
|
|
|
return star.queryX(nil, ins, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) QueryXS(ins interface{}, args ...interface{}) ([]*StarRows, error) {
|
|
|
|
func (star *StarTx) QueryXS(ins interface{}, args ...interface{}) ([]*StarRows, error) {
|
|
|
|
var starRes []*StarRows
|
|
|
|
var starRes []*StarRows
|
|
|
|
t := reflect.TypeOf(ins)
|
|
|
|
t := reflect.TypeOf(ins)
|
|
|
@ -280,7 +357,40 @@ func (star *StarTx) QueryXS(ins interface{}, args ...interface{}) ([]*StarRows,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return starRes, nil
|
|
|
|
return starRes, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (star *StarTx) Update(ins interface{}, sheetName string, primaryKey ...string) (sql.Result, error) {
|
|
|
|
|
|
|
|
return star.updateinsert(nil, true, ins, sheetName, primaryKey...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) UpdateContext(ctx context.Context, ins interface{}, sheetName string, primaryKey ...string) (sql.Result, error) {
|
|
|
|
|
|
|
|
return star.updateinsert(ctx, true, ins, sheetName, primaryKey...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) Insert(ins interface{}, sheetName string, primaryKey ...string) (sql.Result, error) {
|
|
|
|
|
|
|
|
return star.updateinsert(nil, false, ins, sheetName, primaryKey...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) InsertContext(ctx context.Context, ins interface{}, sheetName string, primaryKey ...string) (sql.Result, error) {
|
|
|
|
|
|
|
|
return star.updateinsert(ctx, false, ins, sheetName, primaryKey...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) updateinsert(ctx context.Context, isUpdate bool, ins interface{}, sheetName string, primaryKey ...string) (sql.Result, error) {
|
|
|
|
|
|
|
|
var sqlStr string
|
|
|
|
|
|
|
|
var para []string
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if isUpdate {
|
|
|
|
|
|
|
|
sqlStr, para, err = getUpdateSentence(ins, sheetName, primaryKey...)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
sqlStr, para, err = getInsertSentence(ins, sheetName, primaryKey...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpStr := append([]interface{}{}, sqlStr)
|
|
|
|
|
|
|
|
for _, v := range para {
|
|
|
|
|
|
|
|
tmpStr = append(tmpStr, v)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return star.execX(ctx, ins, tmpStr...)
|
|
|
|
|
|
|
|
}
|
|
|
|
func (star *StarTx) ExecXS(ins interface{}, args ...interface{}) ([]sql.Result, error) {
|
|
|
|
func (star *StarTx) ExecXS(ins interface{}, args ...interface{}) ([]sql.Result, error) {
|
|
|
|
var starRes []sql.Result
|
|
|
|
var starRes []sql.Result
|
|
|
|
t := reflect.TypeOf(ins)
|
|
|
|
t := reflect.TypeOf(ins)
|
|
|
@ -339,10 +449,10 @@ func (star *StarTx) execX(ctx context.Context, ins interface{}, args ...interfac
|
|
|
|
return star.exec(ctx, args...)
|
|
|
|
return star.exec(ctx, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) QueryXContext(ctx context.Context,ins interface{}, args ...interface{}) (*StarRows, error) {
|
|
|
|
func (star *StarTx) QueryXContext(ctx context.Context, ins interface{}, args ...interface{}) (*StarRows, error) {
|
|
|
|
return star.queryX(ctx, ins, args)
|
|
|
|
return star.queryX(ctx, ins, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (star *StarTx) QueryXSContext(ctx context.Context,ins interface{}, args ...interface{}) ([]*StarRows, error) {
|
|
|
|
func (star *StarTx) QueryXSContext(ctx context.Context, ins interface{}, args ...interface{}) ([]*StarRows, error) {
|
|
|
|
var starRes []*StarRows
|
|
|
|
var starRes []*StarRows
|
|
|
|
t := reflect.TypeOf(ins)
|
|
|
|
t := reflect.TypeOf(ins)
|
|
|
|
v := reflect.ValueOf(ins)
|
|
|
|
v := reflect.ValueOf(ins)
|
|
|
@ -369,7 +479,7 @@ func (star *StarTx) QueryXSContext(ctx context.Context,ins interface{}, args ...
|
|
|
|
return starRes, nil
|
|
|
|
return starRes, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) ExecXSContext(ctx context.Context,ins interface{}, args ...interface{}) ([]sql.Result, error) {
|
|
|
|
func (star *StarTx) ExecXSContext(ctx context.Context, ins interface{}, args ...interface{}) ([]sql.Result, error) {
|
|
|
|
var starRes []sql.Result
|
|
|
|
var starRes []sql.Result
|
|
|
|
t := reflect.TypeOf(ins)
|
|
|
|
t := reflect.TypeOf(ins)
|
|
|
|
v := reflect.ValueOf(ins)
|
|
|
|
v := reflect.ValueOf(ins)
|
|
|
@ -396,6 +506,6 @@ func (star *StarTx) ExecXSContext(ctx context.Context,ins interface{}, args ...i
|
|
|
|
return starRes, nil
|
|
|
|
return starRes, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarTx) ExecXContext(ctx context.Context,ins interface{}, args ...interface{}) (sql.Result, error) {
|
|
|
|
func (star *StarTx) ExecXContext(ctx context.Context, ins interface{}, args ...interface{}) (sql.Result, error) {
|
|
|
|
return star.execX(ctx, ins, args...)
|
|
|
|
return star.execX(ctx, ins, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|