|
|
@ -20,7 +20,8 @@ type Query struct { |
|
|
|
title string |
|
|
|
where []string |
|
|
|
where_or []string |
|
|
|
join [][]string //[["tablea as a","a.id=b.id","left"]]
|
|
|
|
join [][]string //[["tablea as a","a.id=b.id","left"]]
|
|
|
|
save_data []map[string]interface{} //[["title":"a","num":1,],["title":"a","num":1,]]
|
|
|
|
data []string |
|
|
|
value []interface{} |
|
|
|
orderby string |
|
|
@ -111,6 +112,14 @@ func (this *Query) WhereOr(where string) *Query { |
|
|
|
this.where_or = append(this.where_or, where) |
|
|
|
return this |
|
|
|
} |
|
|
|
func (this *Query) saveData(value map[string]interface{}) *Query { |
|
|
|
this.save_data = append(this.save_data, value) |
|
|
|
return this |
|
|
|
} |
|
|
|
func (this *Query) saveDatas(value []map[string]interface{}) *Query { |
|
|
|
this.save_data = append(this.save_data, value...) |
|
|
|
return this |
|
|
|
} |
|
|
|
func (this *Query) Value(value interface{}) *Query { |
|
|
|
this.value = append(this.value, value) |
|
|
|
return this |
|
|
@ -328,22 +337,42 @@ func (this *Query) CreateAllStmt() error { |
|
|
|
dbName := getTableName(this.dbname, this.table) |
|
|
|
|
|
|
|
var sql string |
|
|
|
var dataSql = []string{} |
|
|
|
var valSql = []string{} |
|
|
|
|
|
|
|
if len(this.save_data) > 0 { |
|
|
|
this.data = []string{} |
|
|
|
this.value = []interface{}{} |
|
|
|
for i, datum := range this.save_data { |
|
|
|
for s, v := range datum { |
|
|
|
if i == 0 { |
|
|
|
this.data = append(this.data, s) |
|
|
|
dataSql = append(dataSql, "?") |
|
|
|
} |
|
|
|
this.value = append(this.value, v) |
|
|
|
} |
|
|
|
valSql = append(valSql, "("+strings.Join(dataSql, " , ")+")") |
|
|
|
} |
|
|
|
} else { |
|
|
|
for i := 0; i < len(this.data); i++ { |
|
|
|
dataSql = append(dataSql, "?") |
|
|
|
} |
|
|
|
valSql = append(valSql, "("+strings.Join(dataSql, " , ")+")") |
|
|
|
} |
|
|
|
|
|
|
|
sql = helper.StringJoin("insert into ", dbName, " (", strings.Join(this.data, " , ")+")") |
|
|
|
|
|
|
|
if len(this.data) == 0 { |
|
|
|
return errors.New("参数错误,没有数据表") |
|
|
|
} |
|
|
|
if len(this.value) == 0 { |
|
|
|
return errors.New("参数错误,条件值错误") |
|
|
|
} |
|
|
|
var valueSql = []string{} |
|
|
|
var dataSql = []string{} |
|
|
|
for i := 0; i < len(this.data); i++ { |
|
|
|
dataSql = append(valueSql, "?") |
|
|
|
} |
|
|
|
dataSqlText := "(" + strings.Join(dataSql, " , ") + ")" |
|
|
|
for i := 0; i < len(this.value); i++ { |
|
|
|
valueSql = append(valueSql, dataSqlText) |
|
|
|
|
|
|
|
setText := " values " |
|
|
|
if len(valSql) > 1 { |
|
|
|
setText = " value " |
|
|
|
} |
|
|
|
sql = sql + " " + strings.Join(valueSql, " , ") |
|
|
|
sql = helper.StringJoin("insert into ", dbName, " (", strings.Join(this.data, " , "), ")", setText, strings.Join(valSql, ",")) |
|
|
|
|
|
|
|
if this.debug { |
|
|
|
log.Println("insert sql:", sql, this.value) |
|
|
|
} |
|
|
|