PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/gmyth-0.7.1/gmyth/gmyth_query.c

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