use integer when required in queries

This commit is contained in:
Simon Vieille 2024-03-31 22:17:04 +02:00
commit 038d0a2d6b

View file

@ -60,9 +60,14 @@ func (a *App) TruncateTable(c config.SchemaConfigAction) error {
pCounter := 1 pCounter := 1
for _, col := range c.PrimaryKey { for _, col := range c.PrimaryKey {
if row[col].IsInteger {
value, _ := strconv.Atoi(row[col].Value)
pkeys = append(pkeys, fmt.Sprintf("%s=%d", col, value))
} else {
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
} }
}
sql := fmt.Sprintf( sql := fmt.Sprintf(
"DELETE FROM %s WHERE %s", "DELETE FROM %s WHERE %s",
@ -74,9 +79,11 @@ func (a *App) TruncateTable(c config.SchemaConfigAction) error {
pCounter = 1 pCounter = 1
for _, col := range c.PrimaryKey { for _, col := range c.PrimaryKey {
if !row[col].IsInteger {
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(pCounter)), row[col].Value) stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(pCounter)), row[col].Value)
pCounter = pCounter + 1 pCounter = pCounter + 1
} }
}
a.Db.QueryRow(stmt.GetParsedQuery(), (stmt.GetParsedParameters())...).Scan(&scan) a.Db.QueryRow(stmt.GetParsedQuery(), (stmt.GetParsedParameters())...).Scan(&scan)
} }
@ -148,19 +155,23 @@ func (a *App) UpdateRows(c config.SchemaConfigAction, globalColumns map[string]s
for _, row := range rows { for _, row := range rows {
updates := []string{} updates := []string{}
pkeys := []string{} pkeys := []string{}
values := make(map[int]string) values := make(map[int][]string)
pCounter := 1 pCounter := 1
for col, value := range row { for col, value := range row {
if value.IsUpdated && !value.IsVirtual { if value.IsUpdated && !value.IsVirtual {
values[pCounter] = value.Value if value.IsInteger {
values[pCounter] = []string{value.Value, "int"}
} else {
values[pCounter] = []string{value.Value, "string"}
}
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 values[pCounter] = []string{row[col].Value, "string"}
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
} }
@ -177,7 +188,12 @@ func (a *App) UpdateRows(c config.SchemaConfigAction, globalColumns map[string]s
pCounter = 1 pCounter = 1
for i, value := range values { for i, value := range values {
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(i)), value) if value[1] == "string" {
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(i)), value[0])
} else {
newValue, _ := strconv.Atoi(value[0])
stmt.SetValue(fmt.Sprintf("p%s", strconv.Itoa(i)), newValue)
}
} }
a.Db.QueryRow(stmt.GetParsedQuery(), (stmt.GetParsedParameters())...).Scan(&scan) a.Db.QueryRow(stmt.GetParsedQuery(), (stmt.GetParsedParameters())...).Scan(&scan)