[svn r258] Added gmyth_query and gmyth_scheduler connection methods with timeout. Besides, configura.ac now checks libmysqlclient in the right way
4 * @file gmyth/gmyth_query.c
6 * @brief <p> GMythQuery class provides a wrapper for accessing
7 * the libmysqlclient funtions.
9 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
10 * @author Leonardo Sobral Cunha <leonardo.cunha@indt.org.br>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 #include "gmyth_query.h"
38 #include "gmyth_debug.h"
40 static void gmyth_query_class_init (GMythQueryClass *klass);
41 static void gmyth_query_init (GMythQuery *object);
43 static void gmyth_query_dispose (GObject *object);
44 static void gmyth_query_finalize (GObject *object);
46 static void gmyth_query_print_error (MYSQL *conn, char *message);
48 G_DEFINE_TYPE(GMythQuery, gmyth_query, G_TYPE_OBJECT)
51 gmyth_query_class_init (GMythQueryClass *klass)
53 GObjectClass *gobject_class;
55 gobject_class = (GObjectClass *) klass;
57 gobject_class->dispose = gmyth_query_dispose;
58 gobject_class->finalize = gmyth_query_finalize;
62 gmyth_query_init (GMythQuery *gmyth_query)
64 gmyth_query->backend_info = NULL;
66 /* initialize connection handler */
67 gmyth_query->conn = mysql_init (NULL);
70 if (!(gmyth_query->conn))
71 g_warning ("[%s] MSQL structure not initialized", __FUNCTION__);
76 gmyth_query_dispose (GObject *object)
78 GMythQuery *gmyth_query = GMYTH_QUERY (object);
80 if (gmyth_query->backend_info) {
81 g_object_unref (gmyth_query->backend_info);
82 //gmyth_query->backend_info = NULL;
85 G_OBJECT_CLASS (gmyth_query_parent_class)->dispose (object);
89 gmyth_query_finalize (GObject *object)
91 g_signal_handlers_destroy (object);
93 G_OBJECT_CLASS (gmyth_query_parent_class)->finalize (object);
96 /** Creates a new instance of GMythQuery.
98 * @return a new instance of GMythQuery.
103 GMythQuery *sql_query = GMYTH_QUERY (g_object_new(GMYTH_QUERY_TYPE, NULL));
109 gmyth_query_connect_with_timeout (GMythQuery *gmyth_query,
110 GMythBackendInfo *backend_info, guint timeout)
113 g_return_val_if_fail (gmyth_query->conn != NULL, FALSE);
114 printf ("XXXXXXXXX timeout %d\n", timeout);
116 /* sets connection timeout */
117 mysql_options (gmyth_query->conn, MYSQL_OPT_CONNECT_TIMEOUT, (gchar*) &timeout);
120 return gmyth_query_connect (gmyth_query, backend_info);
123 /** Connects to the Mysql database in the backend. The backend address
124 * is loaded from the GMythBackendInfo instance.
126 * @param gmyth_query the GMythEPG instance to be connected.
127 * @return true if connection was success, false if failed.
130 gmyth_query_connect (GMythQuery *gmyth_query, GMythBackendInfo *backend_info)
133 g_return_val_if_fail (backend_info != NULL, FALSE);
134 g_return_val_if_fail (backend_info->hostname != NULL, FALSE);
135 g_return_val_if_fail (backend_info->username != NULL, FALSE);
136 g_return_val_if_fail (backend_info->password != NULL, FALSE);
137 g_return_val_if_fail (backend_info->db_name != NULL, FALSE);
139 g_object_ref (backend_info);
140 gmyth_query->backend_info = backend_info;
142 if (gmyth_query->conn == NULL) {
143 gmyth_query_print_error (NULL, "mysql_init() failed (probably out of memory)");
147 /* connect to server */
148 if (mysql_real_connect (gmyth_query->conn,
149 gmyth_query->backend_info->hostname,
150 gmyth_query->backend_info->username,
151 gmyth_query->backend_info->password,
152 gmyth_query->backend_info->db_name,
153 0, NULL, 0) == NULL) {
155 gmyth_query_print_error (gmyth_query->conn, "mysql_real_connect() failed");
159 gmyth_debug ("[%s] Connection to Mysql server succeeded! (host = %s, user = %s, "\
160 "password = %s, db name = %s)", __FUNCTION__,
161 gmyth_query->backend_info->hostname, gmyth_query->backend_info->username,
162 gmyth_query->backend_info->password, gmyth_query->backend_info->db_name );
167 /** Disconnects from the Mysql database in the backend.
169 * @param gmyth_query the GMythQuery instance to be disconnected
170 * @return true if disconnection was success, false if failed.
173 gmyth_query_disconnect (GMythQuery *gmyth_query)
177 /* TODO: Check how to return error */
178 gmyth_debug ("[%s] Closing gmyth_query->conn", __FUNCTION__);
179 mysql_close (gmyth_query->conn);
185 gmyth_query_print_error (MYSQL *conn, char *message)
187 g_debug ("%s", message);
190 #if MYSQL_VERSION_ID >= 40101
191 g_debug ("Error %u (%s): %s\n",
192 mysql_errno (conn), mysql_sqlstate(conn), mysql_error (conn));
194 g_debug ("Error %u: %s\n",
195 mysql_errno (conn), mysql_error (conn));
200 /** Sends the given query to the backend returning the query result as
203 * FIXME: this function is returning NULL whether any error happens
204 * or no rows are returned (e.g. UPDATE or REPLACE).
206 * @param gmyth_query the GMythQuery instance.
207 * @param stmt_str the query text.
208 * @return the MYSQL_RES result pointer or NULL if any error happens.
211 gmyth_query_process_statement (GMythQuery *gmyth_query, char *stmt_str)
217 gmyth_debug ("[%s] Running mysql query %s", __FUNCTION__, stmt_str);
219 if (gmyth_query == NULL)
222 /* the statement failed */
223 if (mysql_query (gmyth_query->conn, stmt_str) != 0) {
224 gmyth_query_print_error (gmyth_query->conn, "Could not execute statement");
228 /* the statement succeeded; determine whether it returned data */
229 res_set = mysql_store_result (gmyth_query->conn);
232 } else if (mysql_field_count (gmyth_query->conn) == 0) {
233 gmyth_debug ("%lu rows affected\n",
234 (unsigned long) mysql_affected_rows (gmyth_query->conn));
236 gmyth_query_print_error (gmyth_query->conn, "Could not retrieve result set");