table: Fix use of numbered argument placeholders

go-sqlite3 does not implement them properly (in fact, the proper support
was just removed, wtf, mattn). Additionally, go-sqlite3 does not handle
$name or @name properly despite these being supported by SQLite, only
:name works.

Closes #241.
This commit is contained in:
fox.cpp 2020-06-19 14:09:48 +03:00
parent 3fc8a54924
commit 071d06dbff
No known key found for this signature in database
GPG key ID: 5B991F6215D2FCC0
4 changed files with 12 additions and 12 deletions

View file

@ -111,7 +111,7 @@ https://pkg.go.dev/github.com/lib/pq?tab=doc#hdr-Connection_String_Parameters
SQL query to use to obtain the lookup result. SQL query to use to obtain the lookup result.
It will get one positional argument containing the lookup key. Use $1 It will get one named argument containing the lookup key. Use :key
placeholder to access it in SQL. The result row set should contain one row, one placeholder to access it in SQL. The result row set should contain one row, one
column with the string that will be used as a lookup result. If there are more column with the string that will be used as a lookup result. If there are more
rows, they will be ignored. If there are more columns, lookup will fail. If rows, they will be ignored. If there are more columns, lookup will fail. If
@ -143,17 +143,17 @@ sql_query {
If queries are set to implement corresponding table operations - table becomes If queries are set to implement corresponding table operations - table becomes
"mutable" and can be used in contexts that require writable key-value store. "mutable" and can be used in contexts that require writable key-value store.
'add' query gets two ordered arguments - key and value strings to store. 'add' query gets :key, :value named arguments - key and value strings to store.
They should be added to the store. The query *should* not add multiple values They should be added to the store. The query *should* not add multiple values
for the same key and *should* fail if the key already exists. for the same key and *should* fail if the key already exists.
'list' query gets no arguments and should return a column with all keys in 'list' query gets no arguments and should return a column with all keys in
the store. the store.
'set' query gets two arguments - key and value and should replace the existing 'set' query gets :key, :value named arguments - key and value and should replace the existing
entry in the database. entry in the database.
'del' query gets one argument - key and should remove it from the database. 'del' query gets :key argument - key and should remove it from the database.
# Static table (static) # Static table (static)

View file

@ -150,7 +150,7 @@ func (s *SQL) RemoveKey(k string) error {
return fmt.Errorf("%s: table is not mutable (no 'del' query)", s.modName) return fmt.Errorf("%s: table is not mutable (no 'del' query)", s.modName)
} }
_, err := s.del.Exec(k) _, err := s.del.Exec(sql.Named("key", k))
if err != nil { if err != nil {
return fmt.Errorf("%s: del %s: %w", s.modName, k, err) return fmt.Errorf("%s: del %s: %w", s.modName, k, err)
} }
@ -165,8 +165,8 @@ func (s *SQL) SetKey(k, v string) error {
return fmt.Errorf("%s: table is not mutable (no 'add' query)", s.modName) return fmt.Errorf("%s: table is not mutable (no 'add' query)", s.modName)
} }
if _, err := s.add.Exec(k, v); err != nil { if _, err := s.add.Exec(sql.Named("key", k), sql.Named("value", v)); err != nil {
if _, err := s.set.Exec(k, v); err != nil { if _, err := s.set.Exec(sql.Named("key", k), sql.Named("value", v)); err != nil {
return fmt.Errorf("%s: add %s: %w", s.modName, k, err) return fmt.Errorf("%s: add %s: %w", s.modName, k, err)
} }
return nil return nil

View file

@ -37,7 +37,7 @@ func TestSQL(t *testing.T) {
}, },
{ {
Name: "lookup", Name: "lookup",
Args: []string{"SELECT value FROM testTbl WHERE key = $1"}, Args: []string{"SELECT value FROM testTbl WHERE key = $key"},
}, },
}, },
})) }))

View file

@ -67,11 +67,11 @@ func (s *SQLTable) Init(cfg *config.Map) error {
}, },
{ {
Name: "lookup", Name: "lookup",
Args: []string{fmt.Sprintf("SELECT %s FROM %s WHERE %s = $1", valueColumn, tableName, keyColumn)}, Args: []string{fmt.Sprintf("SELECT %s FROM %s WHERE %s = :key", valueColumn, tableName, keyColumn)},
}, },
{ {
Name: "add", Name: "add",
Args: []string{fmt.Sprintf("INSERT INTO %s(%s, %s) VALUES($1, $2)", tableName, keyColumn, valueColumn)}, Args: []string{fmt.Sprintf("INSERT INTO %s(%s, %s) VALUES(:key, :value)", tableName, keyColumn, valueColumn)},
}, },
{ {
Name: "list", Name: "list",
@ -79,11 +79,11 @@ func (s *SQLTable) Init(cfg *config.Map) error {
}, },
{ {
Name: "set", Name: "set",
Args: []string{fmt.Sprintf("UPDATE %s SET %s = $2 WHERE %s = $1", tableName, valueColumn, keyColumn)}, Args: []string{fmt.Sprintf("UPDATE %s SET %s = :value WHERE %s = :key", tableName, valueColumn, keyColumn)},
}, },
{ {
Name: "del", Name: "del",
Args: []string{fmt.Sprintf("DELETE FROM %s WHERE %s = $1", tableName, keyColumn)}, Args: []string{fmt.Sprintf("DELETE FROM %s WHERE %s = :key", tableName, keyColumn)},
}, },
{ {
Name: "init", Name: "init",