1 /*
2  * Copyright (C) 2008 - 2011 Vivien Malerba <malerba@gnome-db.org>
3  * Copyright (C) 2011 Murray Cumming <murrayc@murrayc.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA  02110-1301, USA.
19  */
20 
21 #include <string.h>
22 #include <glib/gi18n-lib.h>
23 #include "gda-mysql-pstmt.h"
24 
25 static void
26 gda_mysql_pstmt_class_init (GdaMysqlPStmtClass  *klass);
27 static void
28 gda_mysql_pstmt_init (GdaMysqlPStmt       *pstmt,
29 		      GdaMysqlPStmtClass  *klass);
30 static void
31 gda_mysql_pstmt_finalize (GObject  *object);
32 
33 static GObjectClass *parent_class = NULL;
34 
35 /**
36  * gda_mysql_pstmt_get_type
37  *
38  * Returns: the #GType of GdaMysqlPStmt.
39  */
40 GType
gda_mysql_pstmt_get_type(void)41 gda_mysql_pstmt_get_type (void)
42 {
43 	static GType type = 0;
44 
45 	if (G_UNLIKELY (type == 0)) {
46 		static GMutex registering;
47 		static const GTypeInfo info = {
48 			sizeof (GdaMysqlPStmtClass),
49 			(GBaseInitFunc) NULL,
50 			(GBaseFinalizeFunc) NULL,
51 			(GClassInitFunc) gda_mysql_pstmt_class_init,
52 			NULL,
53 			NULL,
54 			sizeof (GdaMysqlPStmt),
55 			0,
56 			(GInstanceInitFunc) gda_mysql_pstmt_init,
57 			NULL
58 		};
59 
60 		g_mutex_lock (&registering);
61 		if (type == 0)
62 			type = g_type_register_static (GDA_TYPE_PSTMT, "GdaMysqlPStmt", &info, 0);
63 		g_mutex_unlock (&registering);
64 	}
65 	return type;
66 }
67 
68 static void
gda_mysql_pstmt_class_init(GdaMysqlPStmtClass * klass)69 gda_mysql_pstmt_class_init (GdaMysqlPStmtClass  *klass)
70 {
71 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
72 	parent_class = g_type_class_peek_parent (klass);
73 
74 	/* virtual functions */
75 	object_class->finalize = gda_mysql_pstmt_finalize;
76 }
77 
78 static void
gda_mysql_pstmt_init(GdaMysqlPStmt * pstmt,G_GNUC_UNUSED GdaMysqlPStmtClass * klass)79 gda_mysql_pstmt_init (GdaMysqlPStmt       *pstmt,
80 		      G_GNUC_UNUSED GdaMysqlPStmtClass  *klass)
81 {
82 	g_return_if_fail (GDA_IS_PSTMT (pstmt));
83 
84 	/* initialize specific parts of @pstmt */
85 	// TO_IMPLEMENT;
86 	pstmt->mysql_bind_result = NULL;
87 }
88 
89 static void
gda_mysql_pstmt_finalize(GObject * object)90 gda_mysql_pstmt_finalize (GObject  *object)
91 {
92 	GdaMysqlPStmt *pstmt = (GdaMysqlPStmt *) object;
93 
94 	g_return_if_fail (GDA_IS_PSTMT (pstmt));
95 
96 	/* free memory */
97 	if (pstmt->mysql_stmt)
98 		mysql_stmt_close (pstmt->mysql_stmt);
99 
100 	gint i;
101 	for (i = 0; i < ((GdaPStmt *) pstmt)->ncols; ++i) {
102 		g_free (pstmt->mysql_bind_result[i].buffer);
103 		g_free (pstmt->mysql_bind_result[i].is_null);
104 		g_free (pstmt->mysql_bind_result[i].length);
105 	}
106 	g_free (pstmt->mysql_bind_result);
107 	pstmt->mysql_bind_result = NULL;
108 
109 	/* chain to parent class */
110 	parent_class->finalize (object);
111 }
112 
113 /*
114  * Steals @mysql_stmt
115  */
116 GdaMysqlPStmt *
gda_mysql_pstmt_new(GdaConnection * cnc,MYSQL * mysql,MYSQL_STMT * mysql_stmt)117 gda_mysql_pstmt_new (GdaConnection  *cnc,
118 		     MYSQL          *mysql,
119 		     MYSQL_STMT     *mysql_stmt)
120 {
121 	GdaMysqlPStmt *ps = (GdaMysqlPStmt *) g_object_new (GDA_TYPE_MYSQL_PSTMT,
122 							    NULL);
123 	ps->cnc = cnc;
124 	ps->mysql = mysql;
125 	ps->mysql_stmt = mysql_stmt;
126 	ps->stmt_used = FALSE;
127 
128 	return ps;
129 }
130 
131