Skip to content

Commit

Permalink
1.Replace方法更新,与Wiki中描述一致
Browse files Browse the repository at this point in the history
2.Replace方法使用性能更好的拼接法降低微乎其微的延迟
3.Replace方法开放无Where()查询状态的插入功能,不建议使用,因为Oracle没有主键检查,插入出错后,只能使用ret返回中的int判断是否有插入,插入了几条数据,有一定的插入失败风险,且Err不会报错
  • Loading branch information
tobycroft committed Oct 30, 2023
1 parent 0f33373 commit 2637721
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions builder_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,42 +184,44 @@ func (b *BuilderOracle) BuildExecuteOra(operType string) (sqlStr string, args []
b.IOrm.GetISession().GetIEngin().GetLogger().Error(err.Error())
return
}
if where == "" && b.IOrm.GetForce() == false {
err = errors.New("出于安全考虑, update时where条件不能为空, 如果真的不需要where条件, 请使用Force()(如: db.xxx.Force().Update())")
b.IOrm.GetISession().GetIEngin().GetLogger().Error(err.Error())
return
}
select_sql, multiWhere, update_sql, insert_keys, insert_vals, err := b.BuildReplace(update, where)
select_sql, on_sql, update_sql, insert_keys, insert_vals, err := b.BuildReplace(update, where)
if err != nil {
return "", nil, err
}
sqlStr = fmt.Sprintf("MERGE INTO %s t USING (select %s FROM dual) d on (%s) WHEN matched THEN UPDATE SET%s WHEN NOT matched THEN INSERT (%s) VALUES (%s)",
b.BuildTable(), select_sql, multiWhere, update_sql, insert_keys, insert_vals)
sqlStr = "MERGE INTO " + b.BuildTable() + " t "
sqlStr += "USING (select " + select_sql + " FROM dual) d on (" + on_sql + ") "
if update_sql != "" {
sqlStr += "WHEN matched THEN UPDATE SET " + update_sql + " "
}
sqlStr += "WHEN NOT matched THEN INSERT (" + insert_keys + ") VALUES (" + insert_vals + ")"
//sqlStr = fmt.Sprintf("MERGE INTO %s t USING (select %s FROM dual) d on (%s) WHEN matched THEN UPDATE SET%s WHEN NOT matched THEN INSERT (%s) VALUES (%s)", b.BuildTable(), select_sql, on_sql, update_sql, insert_keys, insert_vals)
break
}

args = b.IOrm.GetBindValues()
return
}

func (b *BuilderOracle) BuildReplace(update, where string) (select_sql, multiWhere, update_sql, insert_keys, insert_vals string, err error) {
func (b *BuilderOracle) BuildReplace(update, where string) (select_sql, on_sql, update_sql, insert_keys, insert_vals string, err error) {
var reg *regexp.Regexp
reg, err = regexp.Compile(`\(([^\)]+)\)`)
if err != nil {
return
}

wheres := reg.FindAllString(where, -1)
warr := []string{}
wheres := reg.FindAllString(where, -1)

for i, ws := range wheres {
ws = strings.ReplaceAll(ws, "(", "")
ws = strings.ReplaceAll(ws, ")", "")
if i > 0 {
multiWhere += " and"
on_sql += " and"
}
warr = append(warr, ws)
multiWhere += " t.\"" + ws + "\"=" + "d.\"" + ws + "\""
on_sql += " t.\"" + ws + "\"=" + "d.\"" + ws + "\""
}

data1 := strings.Split(update, ",")
for i, data := range data1 {
data_kv := strings.Split(data, "=")
Expand All @@ -229,10 +231,17 @@ func (b *BuilderOracle) BuildReplace(update, where string) (select_sql, multiWhe
insert_vals += ","
}
if !inArray(data_kv[0], warr) {
if update_sql != "" && i > 0 {
update_sql += ","
if len(warr) > 0 {
if update_sql != "" && i > 0 {
update_sql += ","
}
update_sql += " t." + b.AddFieldQuotesOracle(data_kv[0]) + "=" + "d." + b.AddFieldQuotesOracle(data_kv[0]) + ""
} else {
if i > 0 {
on_sql += " and"
}
on_sql += " t." + b.AddFieldQuotesOracle(data_kv[0]) + "=" + "d." + b.AddFieldQuotesOracle(data_kv[0]) + ""
}
update_sql += " t." + b.AddFieldQuotesOracle(data_kv[0]) + "=" + "d." + b.AddFieldQuotesOracle(data_kv[0]) + ""
}
insert_keys += b.AddFieldQuotesOracle(data_kv[0])
insert_vals += "d." + b.AddFieldQuotesOracle(data_kv[0]) + ""
Expand Down

0 comments on commit 2637721

Please sign in to comment.