1# SQL query mapping23The table.sql_query module implements table interface using SQL queries.45Definition:67```8table.sql_query {9 driver <driver name>10 dsn <data source name>11 lookup <lookup query>1213 # Optional:14 init <init query list>15 list <list query>16 add <add query>17 del <del query>18 set <set query>19}20```2122Usage example:2324```25# Resolve SMTP address aliases using PostgreSQL DB.26modify {27 replace_rcpt sql_query {28 driver postgres29 dsn "dbname=maddy user=maddy"30 lookup "SELECT alias FROM aliases WHERE address = $1"31 }32}33```3435## Configuration directives3637### driver _driver name_38**Required.**3940Driver to use to access the database.4142Supported drivers: `postgres`, `sqlite3` (if compiled with C support)4344---4546### dsn _data source name_47**Required.**4849Data Source Name to pass to the driver. For SQLite3 this is just a path to DB50file. For Postgres, see51[https://pkg.go.dev/github.com/lib/pq?tab=doc#hdr-Connection\_String\_Parameters](https://pkg.go.dev/github.com/lib/pq?tab=doc#hdr-Connection\_String\_Parameters)5253---5455### lookup _query_56**Required.**5758SQL query to use to obtain the lookup result.5960It will get one named argument containing the lookup key. Use :key61placeholder to access it in SQL. The result row set should contain one row, one62column with the string that will be used as a lookup result. If there are more63rows, they will be ignored. If there are more columns, lookup will fail. If64there are no rows, lookup returns "no results". If there are any error - lookup65will fail.6667---6869### init _queries..._70Default: empty7172List of queries to execute on initialization. Can be used to configure RDBMS.7374Example, to improve SQLite3 performance:7576```77table.sql_query {78 driver sqlite379 dsn whatever.db80 init "PRAGMA journal_mode=WAL" \81 "PRAGMA synchronous=NORMAL"82 lookup "SELECT alias FROM aliases WHERE address = $1"83}84```8586---8788### named_args _boolean_89Default: `yes`9091Whether to use named parameters binding when executing SQL queries92or not.9394Note that maddy's PostgreSQL driver does not support named parameters and95SQLite3 driver has issues handling numbered parameters:96[https://github.com/mattn/go-sqlite3/issues/472](https://github.com/mattn/go-sqlite3/issues/472)9798---99100### add _query_<br>list _query_<br>set _query_ <br>del _query_101Default: none102103If queries are set to implement corresponding table operations - table becomes104"mutable" and can be used in contexts that require writable key-value store.105106'add' query gets :key, :value named arguments - key and value strings to store.107They should be added to the store. The query **should** not add multiple values108for the same key and **should** fail if the key already exists.109110'list' query gets no arguments and should return a column with all keys in111the store.112113'set' query gets :key, :value named arguments - key and value and should replace the existing114entry in the database.115116'del' query gets :key argument - key and should remove it from the database.117118If `named_args` is set to `no` - key is passed as the first numbered parameter119($1), value is passed as the second numbered parameter ($2).120