fix query builder and named params

This commit is contained in:
Simon Vieille 2024-03-19 16:41:39 +01:00
commit 9a5a859c98
Signed by: deblan
GPG key ID: 579388D585F70417

View file

@ -136,16 +136,19 @@ func (a *App) DoAction(c config.SchemaConfigAction, globalColumns map[string]str
for _, row := range rows { for _, row := range rows {
updates := []string{} updates := []string{}
pkeys := []string{} pkeys := []string{}
values := make(map[int]string)
pCounter := 1 pCounter := 1
for col, value := range row { for col, value := range row {
if value.IsUpdated { if value.IsUpdated && !value.IsVirtual {
values[pCounter] = value.Value
updates = append(updates, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter))) updates = append(updates, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter)))
pCounter = pCounter + 1 pCounter = pCounter + 1
} }
} }
for _, col := range c.PrimaryKey { for _, col := range c.PrimaryKey {
values[pCounter] = row[col].Value
pkeys = append(pkeys, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter))) pkeys = append(pkeys, fmt.Sprintf("%s=:p%s", col, strconv.Itoa(pCounter)))
pCounter = pCounter + 1 pCounter = pCounter + 1
} }
@ -161,16 +164,8 @@ func (a *App) DoAction(c config.SchemaConfigAction, globalColumns map[string]str
stmt := nq.NewNamedParameterQuery(sql) stmt := nq.NewNamedParameterQuery(sql)
pCounter = 1 pCounter = 1
for _, value := range row { for i, value := range values {
if value.IsUpdated { stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(i)), value)
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(pCounter)), value.Value)
pCounter = pCounter + 1
}
}
for _, col := range c.PrimaryKey {
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(pCounter)), row[col].Value)
pCounter = pCounter + 1
} }
a.Db.QueryRow(stmt.GetParsedQuery(), (stmt.GetParsedParameters())...).Scan(&scan) a.Db.QueryRow(stmt.GetParsedQuery(), (stmt.GetParsedParameters())...).Scan(&scan)