gmyth/src/gmyth_query.c
author renatofilho
Thu Sep 28 15:41:06 2006 +0100 (2006-09-28)
branchtrunk
changeset 18 79f6da40f6e9
parent 1 gmyth/src/libgmyth/gmyth_query.c@ffdf467315ec
child 49 6deb6330eaca
permissions -rw-r--r--
[svn r19] - splited libgmyth;
     1 /**
     2  * GMyth Library
     3  *
     4  * @file gmyth/gmyth_query.c
     5  * 
     6  * @brief <p> GMythQuery class provides a wrapper for accessing
     7  * the libmysqlclient funtions.
     8  *
     9  * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
    10  * @author Leonardo Sobral Cunha <leonardo.cunha@indt.org.br>
    11  *
    12  *//*
    13  * 
    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.
    18  *
    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.
    23  *
    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
    27  */
    28 
    29 #include <stdlib.h>
    30 #include <stdio.h>
    31 #include <assert.h>
    32 
    33 #include "gmyth_query.h"
    34 #include "gmyth_settings.h"
    35 #include "gmyth_context.h"
    36 
    37 static void gmyth_query_class_init          (GMythQueryClass *klass);
    38 static void gmyth_query_init                (GMythQuery *object);
    39 
    40 static void gmyth_query_dispose  (GObject *object);
    41 static void gmyth_query_finalize (GObject *object);
    42 
    43 static void gmyth_query_print_error (MYSQL *conn, char *message);
    44 
    45 G_DEFINE_TYPE(GMythQuery, gmyth_query, G_TYPE_OBJECT)
    46     
    47 static void
    48 gmyth_query_class_init (GMythQueryClass *klass)
    49 {
    50 	GObjectClass *gobject_class;
    51 
    52     gobject_class = (GObjectClass *) klass;
    53 	
    54     gobject_class->dispose  = gmyth_query_dispose;
    55     gobject_class->finalize = gmyth_query_finalize;	
    56 }
    57 
    58 static void
    59 gmyth_query_init (GMythQuery *gmyth_query)
    60 {
    61 	GMythSettings *gmyth_settings = gmyth_context_get_settings ();
    62 	
    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);
    67 
    68     /* initialize connection handler */
    69     gmyth_query->conn = mysql_init (NULL);
    70     
    71     if (!(gmyth_query->conn))
    72     	g_warning ("[%s] MSQL structure not initialized", __FUNCTION__);
    73 
    74 }
    75 
    76 static void
    77 gmyth_query_dispose  (GObject *object)
    78 {
    79     GMythQuery *gmyth_query = GMYTH_QUERY (object);
    80     
    81     /* disconnect from server */
    82 	gmyth_query_disconnect (gmyth_query);
    83 	
    84 	G_OBJECT_CLASS (gmyth_query_parent_class)->dispose (object);
    85 }
    86 
    87 static void
    88 gmyth_query_finalize (GObject *object)
    89 {
    90 	g_signal_handlers_destroy (object);
    91 
    92     G_OBJECT_CLASS (gmyth_query_parent_class)->finalize (object);
    93 }
    94 
    95 /** Creates a new instance of GMythQuery.
    96  * 
    97  * @return a new instance of GMythQuery.
    98  */
    99 GMythQuery*
   100 gmyth_query_new ()
   101 {
   102     GMythQuery *sql_query = GMYTH_QUERY (g_object_new(GMYTH_QUERY_TYPE, NULL));
   103 
   104     return sql_query;
   105 }
   106 
   107 /** Connects to the Mysql database in the backend. The backend address
   108  * is loaded from the GMythSettings instance.
   109  * 
   110  * @param gmyth_query the GMythEPG instance to be connected.
   111  * @return true if connection was success, false if failed.
   112  */
   113 gboolean
   114 gmyth_query_connect (GMythQuery *gmyth_query) 
   115 {
   116     char *opt_host_name;
   117     char *opt_user_name;
   118     char *opt_password;
   119     char *opt_db_name;
   120 
   121     assert(gmyth_query);
   122     
   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);
   127 
   128     
   129     if (gmyth_query->conn == NULL) {
   130         gmyth_query_print_error (NULL, "mysql_init() failed (probably out of memory)");
   131         return FALSE;
   132     }
   133     
   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) {
   138             	
   139         gmyth_query_print_error (gmyth_query->conn, "mysql_real_connect() failed");
   140 		return FALSE;
   141     }
   142 
   143     g_debug ("[%s] Connection to Mysql server succeeded!", __FUNCTION__);
   144     
   145     return TRUE;
   146 }
   147 
   148 /** Disconnects from the Mysql database in the backend.
   149  * 
   150  * @param gmyth_query the GMythQuery instance to be disconnected
   151  * @return true if disconnection was success, false if failed.
   152  */
   153 gboolean
   154 gmyth_query_disconnect (GMythQuery *gmyth_query) 
   155 {
   156     assert(gmyth_query);
   157     
   158     /* TODO: Check how to return error */
   159     g_debug ("[%s] Closing gmyth_query->conn", __FUNCTION__);
   160     mysql_close (gmyth_query->conn);
   161 
   162 	return TRUE;	
   163 }
   164 
   165 static void
   166 gmyth_query_print_error (MYSQL *conn, char *message)
   167 {
   168     fprintf (stderr, "%s\n", message);
   169     
   170     if (conn != NULL) {
   171 #if MYSQL_VERSION_ID >= 40101
   172         fprintf (stderr, "Error %u (%s): %s\n",
   173                 mysql_errno (conn), mysql_sqlstate(conn), mysql_error (conn));
   174 #else
   175         fprintf (stderr, "Error %u: %s\n",
   176                mysql_errno (conn), mysql_error (conn));
   177 #endif
   178     }
   179 }
   180 
   181 /** Sends the given query to the backend returning the query result as
   182  * MYSQL_RES pointer.
   183  * 
   184  * FIXME: this function is returning NULL whether any error happens
   185  * or no rows are returned (e.g. UPDATE or REPLACE).
   186  * 
   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.
   190  */
   191 MYSQL_RES*
   192 gmyth_query_process_statement (GMythQuery *gmyth_query, char *stmt_str)
   193 {
   194     MYSQL_RES *res_set;
   195     
   196     assert(gmyth_query);
   197     
   198     g_debug ("[%s] Running mysql query %s", __FUNCTION__, stmt_str);
   199 
   200     if (gmyth_query == NULL)
   201     	return NULL;
   202     
   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");
   206         return NULL;
   207     }
   208 
   209     /* the statement succeeded; determine whether it returned data */
   210     res_set = mysql_store_result (gmyth_query->conn);
   211     if (res_set) {
   212         return res_set;
   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));
   216     } else {
   217             gmyth_query_print_error (gmyth_query->conn, "Could not retrieve result set");
   218     }
   219     
   220     return NULL;
   221 }