leo_sobral@1
|
1 |
/**
|
leo_sobral@1
|
2 |
* GMyth Library
|
leo_sobral@1
|
3 |
*
|
leo_sobral@1
|
4 |
* @file gmyth/gmyth_query.c
|
leo_sobral@1
|
5 |
*
|
leo_sobral@1
|
6 |
* @brief <p> GMythQuery class provides a wrapper for accessing
|
leo_sobral@1
|
7 |
* the libmysqlclient funtions.
|
leo_sobral@1
|
8 |
*
|
leo_sobral@1
|
9 |
* Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
|
leo_sobral@1
|
10 |
* @author Leonardo Sobral Cunha <leonardo.cunha@indt.org.br>
|
leo_sobral@1
|
11 |
*
|
leo_sobral@1
|
12 |
*//*
|
leo_sobral@1
|
13 |
*
|
leo_sobral@1
|
14 |
* This program is free software; you can redistribute it and/or modify
|
leo_sobral@1
|
15 |
* it under the terms of the GNU Lesser General Public License as published by
|
leo_sobral@1
|
16 |
* the Free Software Foundation; either version 2 of the License, or
|
leo_sobral@1
|
17 |
* (at your option) any later version.
|
leo_sobral@1
|
18 |
*
|
leo_sobral@1
|
19 |
* This program is distributed in the hope that it will be useful,
|
leo_sobral@1
|
20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
leo_sobral@1
|
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
leo_sobral@1
|
22 |
* GNU General Public License for more details.
|
leo_sobral@1
|
23 |
*
|
leo_sobral@1
|
24 |
* You should have received a copy of the GNU Lesser General Public License
|
leo_sobral@1
|
25 |
* along with this program; if not, write to the Free Software
|
leo_sobral@1
|
26 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
leo_sobral@1
|
27 |
*/
|
leo_sobral@213
|
28 |
|
leo_sobral@213
|
29 |
#ifdef HAVE_CONFIG_H
|
leo_sobral@213
|
30 |
#include "config.h"
|
leo_sobral@213
|
31 |
#endif
|
leo_sobral@1
|
32 |
|
leo_sobral@1
|
33 |
#include <stdlib.h>
|
leo_sobral@1
|
34 |
#include <stdio.h>
|
leo_sobral@1
|
35 |
#include <assert.h>
|
leo_sobral@1
|
36 |
|
leo_sobral@1
|
37 |
#include "gmyth_query.h"
|
renatofilho@131
|
38 |
#include "gmyth_debug.h"
|
leo_sobral@1
|
39 |
|
leo_sobral@1
|
40 |
static void gmyth_query_class_init (GMythQueryClass *klass);
|
leo_sobral@1
|
41 |
static void gmyth_query_init (GMythQuery *object);
|
leo_sobral@1
|
42 |
|
leo_sobral@1
|
43 |
static void gmyth_query_dispose (GObject *object);
|
leo_sobral@1
|
44 |
static void gmyth_query_finalize (GObject *object);
|
leo_sobral@1
|
45 |
|
leo_sobral@1
|
46 |
static void gmyth_query_print_error (MYSQL *conn, char *message);
|
leo_sobral@1
|
47 |
|
leo_sobral@1
|
48 |
G_DEFINE_TYPE(GMythQuery, gmyth_query, G_TYPE_OBJECT)
|
leo_sobral@1
|
49 |
|
leo_sobral@1
|
50 |
static void
|
leo_sobral@1
|
51 |
gmyth_query_class_init (GMythQueryClass *klass)
|
leo_sobral@1
|
52 |
{
|
melunko@117
|
53 |
GObjectClass *gobject_class;
|
leo_sobral@1
|
54 |
|
leo_sobral@1
|
55 |
gobject_class = (GObjectClass *) klass;
|
leo_sobral@1
|
56 |
|
leo_sobral@1
|
57 |
gobject_class->dispose = gmyth_query_dispose;
|
leo_sobral@1
|
58 |
gobject_class->finalize = gmyth_query_finalize;
|
leo_sobral@1
|
59 |
}
|
leo_sobral@1
|
60 |
|
leo_sobral@1
|
61 |
static void
|
leo_sobral@1
|
62 |
gmyth_query_init (GMythQuery *gmyth_query)
|
leo_sobral@1
|
63 |
{
|
melunko@117
|
64 |
gmyth_query->backend_info = NULL;
|
leo_sobral@1
|
65 |
|
leo_sobral@1
|
66 |
/* initialize connection handler */
|
leo_sobral@1
|
67 |
gmyth_query->conn = mysql_init (NULL);
|
leo_sobral@1
|
68 |
|
leo_sobral@1
|
69 |
if (!(gmyth_query->conn))
|
leo_sobral@1
|
70 |
g_warning ("[%s] MSQL structure not initialized", __FUNCTION__);
|
leo_sobral@1
|
71 |
|
leo_sobral@1
|
72 |
}
|
leo_sobral@1
|
73 |
|
leo_sobral@1
|
74 |
static void
|
leo_sobral@1
|
75 |
gmyth_query_dispose (GObject *object)
|
leo_sobral@1
|
76 |
{
|
leo_sobral@1
|
77 |
GMythQuery *gmyth_query = GMYTH_QUERY (object);
|
leo_sobral@1
|
78 |
|
melunko@117
|
79 |
if (gmyth_query->backend_info) {
|
melunko@117
|
80 |
g_object_unref (gmyth_query->backend_info);
|
rosfran@229
|
81 |
//gmyth_query->backend_info = NULL;
|
rosfran@230
|
82 |
}
|
rosfran@230
|
83 |
|
melunko@117
|
84 |
G_OBJECT_CLASS (gmyth_query_parent_class)->dispose (object);
|
leo_sobral@1
|
85 |
}
|
leo_sobral@1
|
86 |
|
leo_sobral@1
|
87 |
static void
|
leo_sobral@1
|
88 |
gmyth_query_finalize (GObject *object)
|
leo_sobral@1
|
89 |
{
|
melunko@117
|
90 |
g_signal_handlers_destroy (object);
|
leo_sobral@1
|
91 |
|
leo_sobral@1
|
92 |
G_OBJECT_CLASS (gmyth_query_parent_class)->finalize (object);
|
leo_sobral@1
|
93 |
}
|
leo_sobral@1
|
94 |
|
leo_sobral@1
|
95 |
/** Creates a new instance of GMythQuery.
|
leo_sobral@1
|
96 |
*
|
leo_sobral@1
|
97 |
* @return a new instance of GMythQuery.
|
leo_sobral@1
|
98 |
*/
|
leo_sobral@1
|
99 |
GMythQuery*
|
leo_sobral@1
|
100 |
gmyth_query_new ()
|
leo_sobral@1
|
101 |
{
|
leo_sobral@1
|
102 |
GMythQuery *sql_query = GMYTH_QUERY (g_object_new(GMYTH_QUERY_TYPE, NULL));
|
leo_sobral@1
|
103 |
|
leo_sobral@1
|
104 |
return sql_query;
|
leo_sobral@1
|
105 |
}
|
leo_sobral@1
|
106 |
|
leo_sobral@1
|
107 |
/** Connects to the Mysql database in the backend. The backend address
|
melunko@117
|
108 |
* is loaded from the GMythBackendInfo instance.
|
leo_sobral@1
|
109 |
*
|
leo_sobral@1
|
110 |
* @param gmyth_query the GMythEPG instance to be connected.
|
leo_sobral@1
|
111 |
* @return true if connection was success, false if failed.
|
leo_sobral@1
|
112 |
*/
|
leo_sobral@1
|
113 |
gboolean
|
melunko@117
|
114 |
gmyth_query_connect (GMythQuery *gmyth_query, GMythBackendInfo *backend_info)
|
leo_sobral@1
|
115 |
{
|
melunko@117
|
116 |
assert(gmyth_query);
|
melunko@117
|
117 |
g_return_val_if_fail (backend_info != NULL, FALSE);
|
melunko@117
|
118 |
g_return_val_if_fail (backend_info->hostname != NULL, FALSE);
|
melunko@117
|
119 |
g_return_val_if_fail (backend_info->username != NULL, FALSE);
|
melunko@117
|
120 |
g_return_val_if_fail (backend_info->password != NULL, FALSE);
|
melunko@117
|
121 |
g_return_val_if_fail (backend_info->db_name != NULL, FALSE);
|
melunko@117
|
122 |
|
melunko@117
|
123 |
g_object_ref (backend_info);
|
melunko@117
|
124 |
gmyth_query->backend_info = backend_info;
|
leo_sobral@1
|
125 |
|
leo_sobral@1
|
126 |
if (gmyth_query->conn == NULL) {
|
leo_sobral@1
|
127 |
gmyth_query_print_error (NULL, "mysql_init() failed (probably out of memory)");
|
leo_sobral@1
|
128 |
return FALSE;
|
leo_sobral@1
|
129 |
}
|
leo_sobral@1
|
130 |
|
leo_sobral@1
|
131 |
/* connect to server */
|
melunko@117
|
132 |
if (mysql_real_connect (gmyth_query->conn,
|
melunko@117
|
133 |
gmyth_query->backend_info->hostname,
|
melunko@117
|
134 |
gmyth_query->backend_info->username,
|
melunko@117
|
135 |
gmyth_query->backend_info->password,
|
melunko@117
|
136 |
gmyth_query->backend_info->db_name,
|
leo_sobral@1
|
137 |
0, NULL, 0) == NULL) {
|
leo_sobral@1
|
138 |
|
leo_sobral@1
|
139 |
gmyth_query_print_error (gmyth_query->conn, "mysql_real_connect() failed");
|
leo_sobral@1
|
140 |
return FALSE;
|
leo_sobral@1
|
141 |
}
|
leo_sobral@1
|
142 |
|
renatofilho@131
|
143 |
gmyth_debug ("[%s] Connection to Mysql server succeeded! (host = %s, user = %s, "\
|
rosfran@122
|
144 |
"password = %s, db name = %s)", __FUNCTION__,
|
rosfran@122
|
145 |
gmyth_query->backend_info->hostname, gmyth_query->backend_info->username,
|
rosfran@122
|
146 |
gmyth_query->backend_info->password, gmyth_query->backend_info->db_name );
|
leo_sobral@1
|
147 |
|
leo_sobral@1
|
148 |
return TRUE;
|
leo_sobral@1
|
149 |
}
|
leo_sobral@1
|
150 |
|
leo_sobral@1
|
151 |
/** Disconnects from the Mysql database in the backend.
|
leo_sobral@1
|
152 |
*
|
leo_sobral@1
|
153 |
* @param gmyth_query the GMythQuery instance to be disconnected
|
leo_sobral@1
|
154 |
* @return true if disconnection was success, false if failed.
|
leo_sobral@1
|
155 |
*/
|
leo_sobral@1
|
156 |
gboolean
|
leo_sobral@1
|
157 |
gmyth_query_disconnect (GMythQuery *gmyth_query)
|
leo_sobral@1
|
158 |
{
|
leo_sobral@1
|
159 |
assert(gmyth_query);
|
leo_sobral@1
|
160 |
|
leo_sobral@1
|
161 |
/* TODO: Check how to return error */
|
renatofilho@131
|
162 |
gmyth_debug ("[%s] Closing gmyth_query->conn", __FUNCTION__);
|
leo_sobral@1
|
163 |
mysql_close (gmyth_query->conn);
|
leo_sobral@1
|
164 |
|
leo_sobral@1
|
165 |
return TRUE;
|
leo_sobral@1
|
166 |
}
|
leo_sobral@1
|
167 |
|
leo_sobral@1
|
168 |
static void
|
leo_sobral@1
|
169 |
gmyth_query_print_error (MYSQL *conn, char *message)
|
leo_sobral@1
|
170 |
{
|
melunko@256
|
171 |
g_debug ("%s", message);
|
leo_sobral@1
|
172 |
|
leo_sobral@1
|
173 |
if (conn != NULL) {
|
leo_sobral@1
|
174 |
#if MYSQL_VERSION_ID >= 40101
|
melunko@256
|
175 |
g_debug ("Error %u (%s): %s\n",
|
leo_sobral@1
|
176 |
mysql_errno (conn), mysql_sqlstate(conn), mysql_error (conn));
|
leo_sobral@1
|
177 |
#else
|
melunko@256
|
178 |
g_debug ("Error %u: %s\n",
|
leo_sobral@1
|
179 |
mysql_errno (conn), mysql_error (conn));
|
leo_sobral@1
|
180 |
#endif
|
leo_sobral@1
|
181 |
}
|
leo_sobral@1
|
182 |
}
|
leo_sobral@1
|
183 |
|
leo_sobral@1
|
184 |
/** Sends the given query to the backend returning the query result as
|
leo_sobral@1
|
185 |
* MYSQL_RES pointer.
|
leo_sobral@1
|
186 |
*
|
leo_sobral@1
|
187 |
* FIXME: this function is returning NULL whether any error happens
|
leo_sobral@1
|
188 |
* or no rows are returned (e.g. UPDATE or REPLACE).
|
leo_sobral@1
|
189 |
*
|
leo_sobral@1
|
190 |
* @param gmyth_query the GMythQuery instance.
|
leo_sobral@1
|
191 |
* @param stmt_str the query text.
|
leo_sobral@1
|
192 |
* @return the MYSQL_RES result pointer or NULL if any error happens.
|
leo_sobral@1
|
193 |
*/
|
leo_sobral@1
|
194 |
MYSQL_RES*
|
leo_sobral@1
|
195 |
gmyth_query_process_statement (GMythQuery *gmyth_query, char *stmt_str)
|
leo_sobral@1
|
196 |
{
|
leo_sobral@1
|
197 |
MYSQL_RES *res_set;
|
leo_sobral@1
|
198 |
|
leo_sobral@1
|
199 |
assert(gmyth_query);
|
leo_sobral@1
|
200 |
|
renatofilho@131
|
201 |
gmyth_debug ("[%s] Running mysql query %s", __FUNCTION__, stmt_str);
|
leo_sobral@1
|
202 |
|
leo_sobral@1
|
203 |
if (gmyth_query == NULL)
|
leo_sobral@1
|
204 |
return NULL;
|
leo_sobral@1
|
205 |
|
leo_sobral@1
|
206 |
/* the statement failed */
|
leo_sobral@1
|
207 |
if (mysql_query (gmyth_query->conn, stmt_str) != 0) {
|
leo_sobral@1
|
208 |
gmyth_query_print_error (gmyth_query->conn, "Could not execute statement");
|
leo_sobral@1
|
209 |
return NULL;
|
leo_sobral@1
|
210 |
}
|
leo_sobral@1
|
211 |
|
leo_sobral@1
|
212 |
/* the statement succeeded; determine whether it returned data */
|
leo_sobral@1
|
213 |
res_set = mysql_store_result (gmyth_query->conn);
|
leo_sobral@1
|
214 |
if (res_set) {
|
leo_sobral@1
|
215 |
return res_set;
|
leo_sobral@1
|
216 |
} else if (mysql_field_count (gmyth_query->conn) == 0) {
|
renatofilho@131
|
217 |
gmyth_debug ("%lu rows affected\n",
|
leo_sobral@49
|
218 |
(unsigned long) mysql_affected_rows (gmyth_query->conn));
|
leo_sobral@1
|
219 |
} else {
|
leo_sobral@49
|
220 |
gmyth_query_print_error (gmyth_query->conn, "Could not retrieve result set");
|
leo_sobral@1
|
221 |
}
|
leo_sobral@1
|
222 |
|
leo_sobral@1
|
223 |
return NULL;
|
leo_sobral@1
|
224 |
}
|