Skip to content

Commit

Permalink
table: Fix use of numbered argument placeholders
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
foxcpp committed Jul 19, 2020
1 parent f720dab commit 78f7713
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions docs/man/maddy-tables.5.scd
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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
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
Expand Down Expand Up @@ -143,17 +143,17 @@ sql_query {
If queries are set to implement corresponding table operations - table becomes
"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
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
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.

'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)

Expand Down
6 changes: 3 additions & 3 deletions internal/table/sql_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (s *SQL) RemoveKey(k string) error {
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 {
return fmt.Errorf("%s: del %s: %w", s.modName, k, err)
}
Expand All @@ -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)
}

if _, err := s.add.Exec(k, v); err != nil {
if _, err := s.set.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(sql.Named("key", k), sql.Named("value", v)); err != nil {
return fmt.Errorf("%s: add %s: %w", s.modName, k, err)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/table/sql_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestSQL(t *testing.T) {
},
{
Name: "lookup",
Args: []string{"SELECT value FROM testTbl WHERE key = $1"},
Args: []string{"SELECT value FROM testTbl WHERE key = $key"},
},
},
}))
Expand Down
8 changes: 4 additions & 4 deletions internal/table/sql_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,23 @@ func (s *SQLTable) Init(cfg *config.Map) error {
},
{
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",
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",
Args: []string{fmt.Sprintf("SELECT %s from %s", keyColumn, tableName)},
},
{
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",
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",
Expand Down

0 comments on commit 78f7713

Please sign in to comment.