aboutsummaryrefslogtreecommitdiff
path: root/src/sql.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2007-08-19 13:30:35 +0000
committerSergey Poznyakoff <gray@gnu.org.ua>2007-08-19 13:30:35 +0000
commit32d5cff1b7c2266779b63fb97eb6b91cab34e7ad (patch)
treea6334d3ceff71716e9e1399fb376963782a5404b /src/sql.c
parent106472e9039e46a5468ed15f118a2cf7d74f6ec8 (diff)
downloadwydawca-32d5cff1b7c2266779b63fb97eb6b91cab34e7ad.tar.gz
wydawca-32d5cff1b7c2266779b63fb97eb6b91cab34e7ad.tar.bz2
New module: wydawca
git-svn-id: file:///svnroot/wydawca/trunk@279 6bb4bd81-ecc2-4fd4-a2d4-9571d19c0d33
Diffstat (limited to 'src/sql.c')
-rw-r--r--src/sql.c150
1 files changed, 150 insertions, 0 deletions
diff --git a/src/sql.c b/src/sql.c
new file mode 100644
index 0000000..2ceb724
--- /dev/null
+++ b/src/sql.c
@@ -0,0 +1,150 @@
+/* wydawca - FTP release synchronisation daemon
+ Copyright (C) 2007 Sergey Poznyakoff
+
+ This program is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation; either version 3 of the License, or (at your
+ option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "wydawca.h"
+#include "sql.h"
+
+struct sql_list
+{
+ struct sql_list *next;
+ struct sqlconn conn;
+};
+
+static struct sql_list *sql_list;
+
+void
+sql_register_conn (struct sqlconn *conn)
+{
+ struct sql_list *ent = xmalloc (sizeof *ent);
+ ent->conn = *conn;
+ ent->next = sql_list;
+ sql_list = ent;
+}
+
+struct sqlconn *
+sql_find_connection (const char *ident)
+{
+ struct sql_list *p;
+ for (p = sql_list; p; p = p->next)
+ if (strcmp (p->conn.ident, ident) == 0)
+ return &p->conn;
+ return NULL;
+}
+
+int
+sql_connection_exists_p (const char *ident)
+{
+ return sql_find_connection (ident) != NULL;
+}
+
+int
+sql_init_method (struct access_method *method)
+{
+ struct sqlconn *conn = sql_find_connection (method->param[0]);
+
+ if (!conn)
+ {
+ logmsg (LOG_EMERG, "INTERNAL ERROR: cannot find SQL connection %s",
+ method->param[0]);
+ abort ();
+ }
+
+ mysql_init (&conn->mysql);
+ if (!mysql_real_connect (&conn->mysql, conn->host, conn->user,
+ conn->password, conn->database, conn->port,
+ conn->socket, 0))
+ {
+ logmsg (LOG_ERR, "Failed to connect to database %s: Error: %s\n",
+ method->param[0], mysql_error (&conn->mysql));
+ return 1;
+ }
+ method->v.sqlconn = conn;
+ return 0;
+}
+
+int
+sql_done_method (struct access_method *method)
+{
+ mysql_close (&method->v.sqlconn->mysql);
+ return 0;
+}
+
+/* Return the length of the string without trailing whitespace */
+static size_t
+trim_length (const char *str)
+{
+ size_t len;
+
+ for (len = strlen (str); len > 0 && isspace (str[len-1]); len--)
+ ;
+ return len;
+}
+
+int
+sql_run_method (struct access_method *method, const char *query)
+{
+ MYSQL *mysql = &method->v.sqlconn->mysql;
+ MYSQL_RES *result;
+ MYSQL_ROW row;
+ long n;
+ size_t len;
+
+ if (mysql_query (mysql, query))
+ {
+ logmsg (LOG_ERR, "Query failed: %s", mysql_error (mysql));
+ logmsg (LOG_NOTICE, "The failed query was: %s", query);
+ return 1;
+ }
+
+ result = mysql_store_result (mysql);
+ if (!result)
+ {
+ logmsg (LOG_ERR, "Query returned 0 tuples");
+ logmsg (LOG_NOTICE, "The failed query was: %s", query);
+ return 1;
+ }
+
+ n = mysql_num_rows (result);
+ if (n != 1)
+ {
+ logmsg (LOG_NOTICE, "Query returned %ld tuples", n);
+ logmsg (LOG_NOTICE, "The query was: %s", query);
+ if (n == 0)
+ return 1;
+ }
+
+ n = mysql_num_fields (result);
+ if (n != 1)
+ {
+ logmsg (LOG_NOTICE, "Query returned %ld fields", n);
+ logmsg (LOG_NOTICE, "The query was: %s", query);
+ }
+
+ row = mysql_fetch_row (result);
+ if (row[0] == NULL)
+ len = 0;
+ else
+ len = trim_length (row[0]);
+
+ method_copy_result (method, row[0], len);
+
+ mysql_free_result (result);
+
+ return 0;
+}
+
+
+

Return to:

Send suggestions and report system problems to the System administrator.