fixing leak
[oweals/gnunet.git] / src / monkey / edb_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file monkey/edb_api.c
23  * @brief Monkey API for accessing the Expression Database (edb)
24  */
25
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_monkey_edb.h"
29 #include <sqlite3.h>
30
31
32 /**
33  * Context for Database connection and Expressions
34  */
35 struct GNUNET_MONKEY_EDB_Context
36 {
37   /**
38    *  Database connection 
39    */
40   sqlite3 *db_handle;
41 };
42
43
44 /**
45  * Establish a connection to the Expression Database
46  *
47  * @param db_file_name path the Expression Database file
48  * @return context to use for Accessing the Expression Database, NULL on error
49  */
50 struct GNUNET_MONKEY_EDB_Context *
51 GNUNET_MONKEY_EDB_connect (const char *db_file_name)
52 {
53   int err;
54   struct GNUNET_MONKEY_EDB_Context *ctxt =
55     GNUNET_malloc (sizeof (struct GNUNET_MONKEY_EDB_Context));
56
57   err = sqlite3_open (db_file_name, &ctxt->db_handle);
58   if (err)
59     {
60       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
61                   "Cannot open Expression Database. `%s'\n",
62                   sqlite3_errmsg (ctxt->db_handle));
63       return NULL;
64     }
65   return ctxt;
66 }
67
68
69 /**
70  * Disconnect from Database, and cleanup resources
71  *
72  * @param context context containing the Expression Database handle
73  * @return GNUNET_OK on success, GNUNET_NO on failure
74  */
75 int
76 GNUNET_MONKEY_EDB_disconnect (struct GNUNET_MONKEY_EDB_Context *cntxt)
77 {
78   sqlite3_close (cntxt->db_handle);
79   GNUNET_free (cntxt);
80   return GNUNET_OK;
81 }
82
83
84 /**
85  * Return the line number of the end-of-scope for the expression indicated by start_line_no
86  *
87  * @param cntxt context containing the Expression Database handle
88  * @param file_name path to the file in which the expression in question exists
89  * @param start_line_no expression's line
90  * @param iter callback function, iterator for values returned from the Database
91  * @param iter_cls closure for the expression iterator, will contain the scope-end line number
92  * @return GNUNET_OK on success, GNUNET_NO on failure
93  */
94 int
95 GNUNET_MONKEY_EDB_get_expression_scope_end(struct GNUNET_MONKEY_EDB_Context *cntxt,
96                                   const char *file_name, int start_line_no,
97                                   GNUNET_MONKEY_ExpressionIterator iter,
98                                   void *iter_cls)
99 {
100         int err;
101         char *errMsg;
102         char *query;
103
104         if (asprintf(&query, "select end_lineno from Expression where file_name LIKE \'%%/%s\' and start_lineno = %d", file_name, start_line_no) == -1) {
105           GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Memory allocation problem occurred during creating database query!\n");
106                 return GNUNET_NO;
107         }
108
109         err = sqlite3_exec(cntxt->db_handle, query, iter, iter_cls, &errMsg);
110         if (err) {
111                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
112                   "Error occurred while executing Database query. `%s'",
113                   errMsg);
114                 return GNUNET_NO;
115         }
116         return GNUNET_OK;
117 }
118
119
120 /**
121  * Run an SQLite query to retrieve those expressions that are previous to
122  * given expression and are in the same scope of the given expression
123  * 
124  * @param cntxt context containing the Expression Database handle
125  * @param file_name path to the file in which the expression in question exists
126  * @param start_line_no expression beginning line
127  * @param end_line_no line number for the expression's scope end
128  * @param iter callback function, iterator for expressions returned from the Database
129  * @param iter_cls closure for the expression iterator
130  * @return GNUNET_OK success, GNUNET_NO failure
131  */
132 int
133 GNUNET_MONKEY_EDB_get_expressions (struct GNUNET_MONKEY_EDB_Context *cntxt,
134                                    const char *file_name, int start_line_no,
135                                    int end_line_no,
136                                    GNUNET_MONKEY_ExpressionIterator iter,
137                                    void *iter_cls)
138 {
139   int err;
140   char *errMsg;
141   char *query;
142   if (asprintf
143       (&query,
144        "select expr_syntax, start_lineno from Expression where file_name LIKE \'%%/%s\' and start_lineno <= %d and end_lineno = %d",
145        file_name, start_line_no, end_line_no) == -1)
146     {
147       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
148                   "Memory allocation problem occurred!\n");
149       return GNUNET_NO;
150     }
151
152   err = sqlite3_exec (cntxt->db_handle, query, iter, iter_cls, &errMsg);
153   if (err)
154     {
155       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
156                   "Error occurred while executing Database query. `%s'",
157                   errMsg);
158       return GNUNET_NO;
159     }
160   return GNUNET_OK;
161 }