File: gda-postgres-pstmt.c

package info (click to toggle)
libgda5 5.2.10-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 76,168 kB
  • sloc: ansic: 495,319; xml: 10,486; yacc: 5,165; sh: 4,451; makefile: 4,095; php: 1,416; java: 1,300; javascript: 1,298; python: 896; sql: 879; perl: 116
file content (119 lines) | stat: -rw-r--r-- 3,252 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) 2008 - 2013 Vivien Malerba <malerba@gnome-db.org>
 * Copyright (C) 2010 David King <davidk@openismus.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <string.h>
#include <glib/gi18n-lib.h>
#include "gda-postgres-pstmt.h"
#include "gda-postgres-util.h"

static void gda_postgres_pstmt_class_init (GdaPostgresPStmtClass *klass);
static void gda_postgres_pstmt_init       (GdaPostgresPStmt *pstmt, GdaPostgresPStmtClass *klass);
static void gda_postgres_pstmt_finalize    (GObject *object);

static GObjectClass *parent_class = NULL;

/**
 * gda_postgres_pstmt_get_type
 *
 * Returns: the #GType of GdaPostgresPStmt.
 */
GType
gda_postgres_pstmt_get_type (void)
{
	static GType type = 0;

	if (G_UNLIKELY (type == 0)) {
		static GMutex registering;
		static const GTypeInfo info = {
			sizeof (GdaPostgresPStmtClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) gda_postgres_pstmt_class_init,
			NULL,
			NULL,
			sizeof (GdaPostgresPStmt),
			0,
			(GInstanceInitFunc) gda_postgres_pstmt_init,
			0
		};

		g_mutex_lock (&registering);
		if (type == 0)
			type = g_type_register_static (GDA_TYPE_PSTMT, "GdaPostgresPStmt", &info, 0);
		g_mutex_unlock (&registering);
	}
	return type;
}

static void 
gda_postgres_pstmt_class_init (GdaPostgresPStmtClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	parent_class = g_type_class_peek_parent (klass);

	/* virtual functions */
	object_class->finalize = gda_postgres_pstmt_finalize;
}

static void
gda_postgres_pstmt_init (GdaPostgresPStmt *pstmt, G_GNUC_UNUSED GdaPostgresPStmtClass *klass)
{
	g_return_if_fail (GDA_IS_PSTMT (pstmt));
	
	pstmt->prep_name = NULL;
	pstmt->date_format_change = FALSE;
}

static void
gda_postgres_pstmt_finalize (GObject *object)
{
	GdaPostgresPStmt *pstmt = (GdaPostgresPStmt *) object;

	g_return_if_fail (GDA_IS_PSTMT (pstmt));

	/* deallocate statement */
	gchar *sql;
	PGresult *pg_res;

	sql = g_strdup_printf ("DEALLOCATE %s", pstmt->prep_name);
	pg_res = _gda_postgres_PQexec_wrap (pstmt->cnc, pstmt->pconn, sql);
	g_free (sql);
	if (pg_res) 
		PQclear (pg_res);
	
	/* free memory */
	g_free (pstmt->prep_name);

	/* chain to parent class */
	parent_class->finalize (object);
}

GdaPostgresPStmt *
gda_postgres_pstmt_new (GdaConnection *cnc, PGconn *pconn, const gchar *prep_name)
{
	GdaPostgresPStmt *pstmt;

	pstmt = (GdaPostgresPStmt *) g_object_new (GDA_TYPE_POSTGRES_PSTMT, NULL);
	pstmt->prep_name = g_strdup (prep_name);
	pstmt->cnc = cnc;
	pstmt->pconn = pconn;

	return pstmt;
}