mirror of
https://github.com/hanzoai/sqlbuilder.git
synced 2026-10-01 00:37:42 +00:00
oracle supports insert multiple values
This commit is contained in:
@@ -120,6 +120,37 @@ func (ib *InsertBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
|
||||
buf := newStringBuilder()
|
||||
ib.injection.WriteTo(buf, insertMarkerInit)
|
||||
|
||||
if len(ib.values) > 1 && ib.args.Flavor == ib.args.Flavor {
|
||||
buf.WriteLeadingString(ib.verb)
|
||||
buf.WriteString(" ALL")
|
||||
|
||||
for _, v := range ib.values {
|
||||
if len(ib.table) > 0 {
|
||||
buf.WriteString(" INTO ")
|
||||
buf.WriteString(ib.table)
|
||||
}
|
||||
ib.injection.WriteTo(buf, insertMarkerAfterInsertInto)
|
||||
if len(ib.cols) > 0 {
|
||||
buf.WriteLeadingString("(")
|
||||
buf.WriteString(strings.Join(ib.cols, ", "))
|
||||
buf.WriteString(")")
|
||||
|
||||
ib.injection.WriteTo(buf, insertMarkerAfterCols)
|
||||
}
|
||||
|
||||
buf.WriteLeadingString("VALUES ")
|
||||
values := make([]string, 0, len(ib.values))
|
||||
values = append(values, fmt.Sprintf("(%v)", strings.Join(v, ", ")))
|
||||
buf.WriteString(strings.Join(values, ", "))
|
||||
}
|
||||
|
||||
buf.WriteString(" SELECT 1 from DUAL")
|
||||
|
||||
ib.injection.WriteTo(buf, insertMarkerAfterValues)
|
||||
|
||||
return ib.args.CompileWithFlavor(buf.String(), flavor, initialArg...)
|
||||
}
|
||||
|
||||
if len(ib.table) > 0 {
|
||||
buf.WriteLeadingString(ib.verb)
|
||||
buf.WriteString(" INTO ")
|
||||
|
||||
@@ -65,6 +65,22 @@ func ExampleInsertBuilder() {
|
||||
// [1 Huan Du 1 2 Charmy Liu 1 1234567890]
|
||||
}
|
||||
|
||||
func ExampleInsertBuilder_Oracle() {
|
||||
ib := Oracle.NewInsertBuilder()
|
||||
ib.InsertInto("demo.user")
|
||||
ib.Cols("id", "name", "status")
|
||||
ib.Values(1, "Huan Du", 1)
|
||||
ib.Values(2, "Charmy Liu", 1)
|
||||
|
||||
sql, args := ib.Build()
|
||||
fmt.Println(sql)
|
||||
fmt.Println(args)
|
||||
|
||||
// Output:
|
||||
// INSERT ALL INTO demo.user (id, name, status) VALUES (:1, :2, :3) INTO demo.user (id, name, status) VALUES (:4, :5, :6) SELECT 1 from DUAL
|
||||
// [1 Huan Du 1 2 Charmy Liu 1]
|
||||
}
|
||||
|
||||
func ExampleInsertBuilder_insertIgnore() {
|
||||
ib := NewInsertBuilder()
|
||||
ib.InsertIgnoreInto("demo.user")
|
||||
|
||||
Reference in New Issue
Block a user