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
33 #include "gmyth_query.h"
34 #include "gmyth_settings.h"
35 #include "gmyth_context.h"
37 static void gmyth_query_class_init (GMythQueryClass *klass);
38 static void gmyth_query_init (GMythQuery *object);
40 static void gmyth_query_dispose (GObject *object);
41 static void gmyth_query_finalize (GObject *object);
43 static void gmyth_query_print_error (MYSQL *conn, char *message);
45 G_DEFINE_TYPE(GMythQuery, gmyth_query, G_TYPE_OBJECT)
48 gmyth_query_class_init (GMythQueryClass *klass)
50 GObjectClass *gobject_class;
52 gobject_class = (GObjectClass *) klass;
54 gobject_class->dispose = gmyth_query_dispose;
55 gobject_class->finalize = gmyth_query_finalize;
59 gmyth_query_init (GMythQuery *gmyth_query)
61 GMythSettings *gmyth_settings = gmyth_context_get_settings ();
63 gmyth_query->opt_host_name = gmyth_settings_get_backend_hostname(gmyth_settings);
64 gmyth_query->opt_user_name = gmyth_settings_get_username(gmyth_settings);
65 gmyth_query->opt_password = gmyth_settings_get_password(gmyth_settings);
66 gmyth_query->opt_db_name = gmyth_settings_get_dbname(gmyth_settings);
68 /* initialize connection handler */
69 gmyth_query->conn = mysql_init (NULL);
71 if (!(gmyth_query->conn))
72 g_warning ("[%s] MSQL structure not initialized", __FUNCTION__);
77 gmyth_query_dispose (GObject *object)
79 GMythQuery *gmyth_query = GMYTH_QUERY (object);
81 /* disconnect from server */
82 gmyth_query_disconnect (gmyth_query);
84 G_OBJECT_CLASS (gmyth_query_parent_class)->dispose (object);
88 gmyth_query_finalize (GObject *object)
90 g_signal_handlers_destroy (object);
92 G_OBJECT_CLASS (gmyth_query_parent_class)->finalize (object);
95 /** Creates a new instance of GMythQuery.
97 * @return a new instance of GMythQuery.
102 GMythQuery *sql_query = GMYTH_QUERY (g_object_new(GMYTH_QUERY_TYPE, NULL));
107 /** Connects to the Mysql database in the backend. The backend address
108 * is loaded from the GMythSettings instance.
110 * @param gmyth_query the GMythEPG instance to be connected.
111 * @return true if connection was success, false if failed.
114 gmyth_query_connect (GMythQuery *gmyth_query)
123 opt_host_name = (gmyth_query->opt_host_name ? gmyth_query->opt_host_name->str : NULL);
124 opt_db_name = (gmyth_query->opt_db_name ? gmyth_query->opt_db_name->str : NULL);
125 opt_user_name = (gmyth_query->opt_user_name ? gmyth_query->opt_user_name->str : NULL);
126 opt_password = (gmyth_query->opt_password ? gmyth_query->opt_password->str : NULL);
129 if (gmyth_query->conn == NULL) {
130 gmyth_query_print_error (NULL, "mysql_init() failed (probably out of memory)");
134 /* connect to server */
135 if (mysql_real_connect (gmyth_query->conn, opt_host_name,
136 opt_user_name, opt_password, opt_db_name,
137 0, NULL, 0) == NULL) {
139 gmyth_query_print_error (gmyth_query->conn, "mysql_real_connect() failed");
143 g_debug ("[%s] Connection to Mysql server succeeded!", __FUNCTION__);
148 /** Disconnects from the Mysql database in the backend.
150 * @param gmyth_query the GMythQuery instance to be disconnected
151 * @return true if disconnection was success, false if failed.
154 gmyth_query_disconnect (GMythQuery *gmyth_query)
158 /* TODO: Check how to return error */
159 g_debug ("[%s] Closing gmyth_query->conn", __FUNCTION__);
160 mysql_close (gmyth_query->conn);
166 gmyth_query_print_error (MYSQL *conn, char *message)
168 fprintf (stderr, "%s\n", message);
171 #if MYSQL_VERSION_ID >= 40101
172 fprintf (stderr, "Error %u (%s): %s\n",
173 mysql_errno (conn), mysql_sqlstate(conn), mysql_error (conn));
175 fprintf (stderr, "Error %u: %s\n",
176 mysql_errno (conn), mysql_error (conn));
181 /** Sends the given query to the backend returning the query result as
184 * FIXME: this function is returning NULL whether any error happens
185 * or no rows are returned (e.g. UPDATE or REPLACE).
187 * @param gmyth_query the GMythQuery instance.
188 * @param stmt_str the query text.
189 * @return the MYSQL_RES result pointer or NULL if any error happens.
192 gmyth_query_process_statement (GMythQuery *gmyth_query, char *stmt_str)
198 g_debug ("[%s] Running mysql query %s", __FUNCTION__, stmt_str);
200 if (gmyth_query == NULL)
203 /* the statement failed */
204 if (mysql_query (gmyth_query->conn, stmt_str) != 0) {
205 gmyth_query_print_error (gmyth_query->conn, "Could not execute statement");
209 /* the statement succeeded; determine whether it returned data */
210 res_set = mysql_store_result (gmyth_query->conn);
213 } else if (mysql_field_count (gmyth_query->conn) == 0) {
214 g_debug ("%lu rows affected\n",
215 (unsigned long) mysql_affected_rows (gmyth_query->conn));
217 gmyth_query_print_error (gmyth_query->conn, "Could not retrieve result set");