mirror of
https://github.com/hanzoai/sqlbuilder.git
synced 2026-10-01 00:37:42 +00:00
added From support for UpdateBuilder
This commit is contained in:
@@ -15,6 +15,7 @@ const (
|
||||
updateMarkerAfterWith
|
||||
updateMarkerAfterUpdate
|
||||
updateMarkerAfterSet
|
||||
updateMarkerAfterFrom
|
||||
updateMarkerAfterWhere
|
||||
updateMarkerAfterOrderBy
|
||||
updateMarkerAfterLimit
|
||||
@@ -71,6 +72,7 @@ type UpdateBuilder struct {
|
||||
cteBuilder *CTEBuilder
|
||||
|
||||
tables []string
|
||||
fromTables []string
|
||||
assignments []string
|
||||
orderByCols []string
|
||||
order string
|
||||
@@ -140,6 +142,13 @@ func (ub *UpdateBuilder) SetMore(assignment ...string) *UpdateBuilder {
|
||||
return ub
|
||||
}
|
||||
|
||||
// From sets table names of FROM in UPDATE.
|
||||
func (ub *UpdateBuilder) From(table ...string) *UpdateBuilder {
|
||||
ub.fromTables = table
|
||||
ub.marker = updateMarkerAfterFrom
|
||||
return ub
|
||||
}
|
||||
|
||||
// Where sets expressions of WHERE in UPDATE.
|
||||
func (ub *UpdateBuilder) Where(andExpr ...string) *UpdateBuilder {
|
||||
if len(andExpr) == 0 || estimateStringsBytes(andExpr) == 0 {
|
||||
@@ -336,7 +345,7 @@ func (ub *UpdateBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
|
||||
buf.WriteStringsPrefixed("INSERTED.", ub.returning, ", ")
|
||||
}
|
||||
|
||||
ub.injection.WriteTo(buf, insertMarkerAfterReturning)
|
||||
ub.injection.WriteTo(buf, updateMarkerAfterReturning)
|
||||
}
|
||||
|
||||
if flavor != MySQL {
|
||||
@@ -351,6 +360,20 @@ func (ub *UpdateBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
|
||||
}
|
||||
}
|
||||
|
||||
if flavor == PostgreSQL || flavor == SQLite || flavor == SQLServer {
|
||||
if len(ub.fromTables) > 0 {
|
||||
|
||||
if ub.cteBuilder == nil || len(ub.cteBuilder.tableNamesForFrom()) == 0 {
|
||||
buf.WriteLeadingString("FROM ")
|
||||
} else {
|
||||
buf.WriteString(", ")
|
||||
}
|
||||
|
||||
buf.WriteStrings(ub.fromTables, ", ")
|
||||
ub.injection.WriteTo(buf, updateMarkerAfterFrom)
|
||||
}
|
||||
}
|
||||
|
||||
if ub.WhereClause != nil {
|
||||
ub.whereClauseProxy.WhereClause = ub.WhereClause
|
||||
defer func() {
|
||||
|
||||
Reference in New Issue
Block a user