734fac0d4a3527987fb97772c01a5a3ae579661a
[idzebra-moved-to-github.git] / index / mod_indexplugin_mysql.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2009 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17
18 */
19
20 #include "indexplugin.h"
21 #include <mysql/mysql.h>
22
23 MYSQL mCon;
24
25 static int mysqlConnect(void)
26 {
27     mysql_init(&mCon);
28
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");
32
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))
35     {
36         yaz_log(YLOG_FATAL, "Failed to connect to database: %s\n", mysql_error(&mCon));
37         return ZEBRA_FAIL;
38     }
39     else
40     {
41         yaz_log(YLOG_LOG, "Connected to Mysql Database");
42     }
43         
44     return ZEBRA_OK;
45 }
46
47
48 static int repositoryExtract(ZebraHandle zh, const char *driverCommand, enum zebra_recctrl_action_t action)
49 {
50     /* this doesn't really need to be initialised */
51     int ret = ZEBRA_FAIL;
52
53     assert(driverCommand);
54
55     yaz_log(YLOG_LOG, "Driver command: %s", driverCommand);
56
57     if ((ret = mysqlConnect()) == ZEBRA_OK)
58     {
59         const char *mQuery = driverCommand;
60         if (mysql_real_query(&mCon, mQuery, strlen(mQuery)) == 0)
61         {
62             MYSQL_RES *result;
63             if ((result = mysql_store_result(&mCon)))
64             {
65                 MYSQL_ROW row;
66                 unsigned int num_fields;
67                         
68                 num_fields = mysql_num_fields(result);
69                 while ((row = mysql_fetch_row(result)))
70                 {
71                     unsigned long *lengths;
72                     lengths = mysql_fetch_lengths(result);
73
74                     zebraIndexBuffer(zh, row[1], lengths[1], action, row[0]);
75                 }
76                 mysql_free_result(result);
77             }
78         }
79         else
80         {
81             yaz_log(YLOG_FATAL, "Failed to run query: %s\n", mysql_error(&mCon));
82             ret = ZEBRA_FAIL;
83         }
84     }
85
86     /* Drop our MYSQL connection as we don't need it anymore
87        and deallocate anything allocated */
88     mysql_close(&mCon);
89
90     return ret;
91 }
92
93 void indexPluginRegister(void)
94 {
95     /* register our function that gets called while indexing a document */
96     addDriverFunction(repositoryExtract);
97 }
98 /*
99  * Local variables:
100  * c-basic-offset: 4
101  * c-file-style: "Stroustrup"
102  * indent-tabs-mode: nil
103  * End:
104  * vim: shiftwidth=4 tabstop=8 expandtab
105  */
106