[svn r367] Itsn't mandatory to have database information in the GMyth URI.
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);
115 /* sets connection timeout */
116 mysql_options (gmyth_query->conn, MYSQL_OPT_CONNECT_TIMEOUT, (gchar*) &timeout);
119 return gmyth_query_connect (gmyth_query, backend_info);
122 /** Connects to the Mysql database in the backend. The backend address
123 * is loaded from the GMythBackendInfo instance.
125 * @param gmyth_query the GMythEPG instance to be connected.
126 * @return true if connection was success, false if failed.
129 gmyth_query_connect (GMythQuery *gmyth_query, GMythBackendInfo *backend_info)
132 g_return_val_if_fail (backend_info != NULL, FALSE);
133 g_return_val_if_fail (backend_info->hostname != NULL, FALSE);
134 g_return_val_if_fail (backend_info->username != NULL, FALSE);
135 g_return_val_if_fail (backend_info->password != NULL, FALSE);
136 g_return_val_if_fail (backend_info->db_name != NULL, FALSE);
138 g_object_ref (backend_info);
139 gmyth_query->backend_info = backend_info;
141 if (gmyth_query->conn == NULL) {
142 gmyth_query_print_error (NULL, "mysql_init() failed (probably out of memory)");
146 /* connect to server */
147 if (mysql_real_connect (gmyth_query->conn,
148 gmyth_query->backend_info->hostname,
149 gmyth_query->backend_info->username,
150 gmyth_query->backend_info->password,
151 gmyth_query->backend_info->db_name,
152 0, NULL, 0) == NULL) {
154 gmyth_query_print_error (gmyth_query->conn, "mysql_real_connect() failed");
158 g_debug ("[%s] Connection to Mysql server succeeded! (host = %s, user = %s, "\
159 "password = %s, db name = %s)", __FUNCTION__,
160 gmyth_query->backend_info->hostname, gmyth_query->backend_info->username,
161 gmyth_query->backend_info->password, gmyth_query->backend_info->db_name );
166 /** Disconnects from the Mysql database in the backend.
168 * @param gmyth_query the GMythQuery instance to be disconnected
169 * @return true if disconnection was success, false if failed.
172 gmyth_query_disconnect (GMythQuery *gmyth_query)
176 /* TODO: Check how to return error */
177 g_debug ("[%s] Closing gmyth_query->conn", __FUNCTION__);
178 mysql_close (gmyth_query->conn);
184 gmyth_query_print_error (MYSQL *conn, char *message)
186 g_debug ("%s", message);
189 #if MYSQL_VERSION_ID >= 40101
190 g_debug ("Error %u (%s): %s\n",
191 mysql_errno (conn), mysql_sqlstate(conn), mysql_error (conn));
193 g_debug ("Error %u: %s\n",
194 mysql_errno (conn), mysql_error (conn));
199 /** Sends the given query to the backend returning the query result as
202 * FIXME: this function is returning NULL whether any error happens
203 * or no rows are returned (e.g. UPDATE or REPLACE).
205 * @param gmyth_query the GMythQuery instance.
206 * @param stmt_str the query text.
207 * @return the MYSQL_RES result pointer or NULL if any error happens.
210 gmyth_query_process_statement (GMythQuery *gmyth_query, char *stmt_str)
216 g_debug ("[%s] Running mysql query %s", __FUNCTION__, stmt_str);
218 if (gmyth_query == NULL)
221 /* the statement failed */
222 if (mysql_query (gmyth_query->conn, stmt_str) != 0) {
223 gmyth_query_print_error (gmyth_query->conn, "Could not execute statement");
227 /* the statement succeeded; determine whether it returned data */
228 res_set = mysql_store_result (gmyth_query->conn);
231 } else if (mysql_field_count (gmyth_query->conn) == 0) {
232 g_debug ("%lu rows affected\n",
233 (unsigned long) mysql_affected_rows (gmyth_query->conn));
235 gmyth_query_print_error (gmyth_query->conn, "Could not retrieve result set");