diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2009-02-17 18:36:54 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2009-02-17 18:36:54 +0200 |
commit | 317095181674a01651602ddf6817e888d0ad5280 (patch) | |
tree | da9a84a50223918408dfb578813c6fb098c1870c /src/sql.c | |
parent | 3277cd4abfa20e8b2499f052a6eb3792d8aa6cce (diff) | |
download | wydawca-317095181674a01651602ddf6817e888d0ad5280.tar.gz wydawca-317095181674a01651602ddf6817e888d0ad5280.tar.bz2 |
Begin rewriting method system
* src/builtin.c, src/builtin.h: New files.
* src/Makefile.am (wydawca_SOURCES): Add builtin.c and builtin.h
* src/config.c: New keyword access-method.query
* src/update-2.0.awk: Update.
* src/meta.c: Remove quote_string (replaced by method_quote_string).
(meta_escape): Add `handle' argument.
* src/method.c (struct method_descr): New methods: open, close, quote.
Updated methods: free, run, get. All callers updated.
(method_quote_string): New function.
* src/process.c (scan_directory_pair): Init all access methods at once.
* src/sql.c: Take name of the SQL connection from method->parmv[0].
Adapt to changes to the method subsystem.
* src/sql.h: Likewise.
* src/wydawca.h (struct access_method): New members id, parmc, parmv,
storage.
Remove union v.
(meta_escape): Take 3 arguments.
(method_new): Take 2 arguments.
(method_open, method_close, method_quote_string): New proto.
(method_run, method_result): Change signature.
* src/mail.c, src/triplet.c, src/verify.c
* etc/wydawca.rc: Update.
Diffstat (limited to 'src/sql.c')
-rw-r--r-- | src/sql.c | 55 |
1 files changed, 43 insertions, 12 deletions
@@ -58,12 +58,12 @@ sql_connection_exists_p (const char *ident) int sql_init_method (struct access_method *method) { - struct sqlconn *conn = sql_find_connection (method->param[0]); + struct sqlconn *conn = sql_find_connection (method->parmv[0]); if (!conn) { logmsg (LOG_EMERG, "INTERNAL ERROR: cannot find SQL connection %s", - method->param[0]); + method->parmv[0]); abort (); } @@ -75,18 +75,24 @@ sql_init_method (struct access_method *method) conn->socket, 0)) { logmsg (LOG_ERR, "Failed to connect to database %s: Error: %s\n", - method->param[0], mysql_error (&conn->mysql)); + method->parmv[0], mysql_error (&conn->mysql)); return 1; } } - method->v.sqlconn = conn; + method->storage = conn; return 0; } +void * +sql_open (struct access_method *method) +{ + return method->storage; +} + int -sql_free_result (struct access_method *method) +sql_free_result (struct access_method *method, void *handle) { - struct sqlconn *conn = method->v.sqlconn; + struct sqlconn *conn = handle; if (conn->result) { mysql_free_result (conn->result); @@ -99,22 +105,29 @@ sql_free_result (struct access_method *method) int sql_done_method (struct access_method *method) { - struct sqlconn *conn = method->v.sqlconn; + struct sqlconn *conn = method->storage; if (!conn || conn->initcount == 0) return 0; if (--conn->initcount) return 0; - sql_free_result (method); + sql_free_result (method, conn); /* FIXME: Not needed */ mysql_close (&conn->mysql); + method->storage = NULL; return 0; } /* Execute QUERY using the given access METHOD. Return 0 on success. */ int -sql_run_method (struct access_method *method, const char *query) +sql_run_method (struct access_method *method, void *handle, const char *query) { - struct sqlconn *conn = method->v.sqlconn; + struct sqlconn *conn = handle; MYSQL *mysql = &conn->mysql; + + if (!query) + { + logmsg (LOG_ERR, "No query supplied for method %s", "sql"); + return 1; + } if (mysql_query (mysql, query)) { @@ -144,9 +157,10 @@ sql_run_method (struct access_method *method, const char *query) } int -sql_get_method (struct access_method *method, unsigned nrow, unsigned ncol) +sql_get_method (struct access_method *method, void *handle, + unsigned nrow, unsigned ncol) { - struct sqlconn *conn = method->v.sqlconn; + struct sqlconn *conn = handle; MYSQL_ROW row; size_t len; @@ -163,3 +177,20 @@ sql_get_method (struct access_method *method, unsigned nrow, unsigned ncol) return 0; } +int +sql_quote (struct access_method *method, void *handle, const char *input, + char **poutput, size_t *psize) +{ + struct sqlconn *conn = handle; + size_t len, size; + char *output; + + len = strlen (input); + size = 2 * len + 1; + output = xmalloc (size); + mysql_real_escape_string (&conn->mysql, output, input, len); + *poutput = output; + if (psize) + *psize = strlen (output); + return 0; +} |