1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "indexplugin.h"
21 #include <mysql/mysql.h>
25 static int mysqlConnect(void)
29 /* Set the default encoding to utf-8 so that zebra
30 doesn't gribe that the XML conflicts with it's encoding */
31 mysql_options(&mCon, MYSQL_SET_CHARSET_NAME, "utf8");
33 mysql_options(&mCon, MYSQL_READ_DEFAULT_GROUP, "indexplugin_mysql");
34 if (!mysql_real_connect(&mCon, "127.0.0.1", "test", "test", "newDatabase", 0, NULL, 0))
36 yaz_log(YLOG_FATAL, "Failed to connect to database: %s\n", mysql_error(&mCon));
41 yaz_log(YLOG_LOG, "Connected to Mysql Database");
48 static int repositoryExtract(ZebraHandle zh, const char *driverCommand, enum zebra_recctrl_action_t action)
50 /* this doesn't really need to be initialised */
53 assert(driverCommand);
55 yaz_log(YLOG_LOG, "Driver command: %s", driverCommand);
57 if ((ret = mysqlConnect()) == ZEBRA_OK)
59 const char *mQuery = driverCommand;
60 if (mysql_real_query(&mCon, mQuery, strlen(mQuery)) == 0)
63 if ((result = mysql_store_result(&mCon)))
66 unsigned int num_fields;
68 num_fields = mysql_num_fields(result);
69 while ((row = mysql_fetch_row(result)))
71 unsigned long *lengths;
72 lengths = mysql_fetch_lengths(result);
74 zebraIndexBuffer(zh, row[1], lengths[1], action, row[0]);
76 mysql_free_result(result);
81 yaz_log(YLOG_FATAL, "Failed to run query: %s\n", mysql_error(&mCon));
86 /* Drop our MYSQL connection as we don't need it anymore
87 and deallocate anything allocated */
93 void indexPluginRegister(void)
95 /* register our function that gets called while indexing a document */
96 addDriverFunction(repositoryExtract);
101 * c-file-style: "Stroustrup"
102 * indent-tabs-mode: nil
104 * vim: shiftwidth=4 tabstop=8 expandtab