File: gda-statement-struct-delete.c

package info (click to toggle)
libgda4 4.0.12-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 34,928 kB
  • ctags: 18,543
  • sloc: ansic: 187,884; sh: 10,317; xml: 7,903; yacc: 3,454; makefile: 1,974; java: 1,253; python: 896; sql: 321
file content (165 lines) | stat: -rw-r--r-- 5,029 bytes parent folder | download
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* 
 * Copyright (C) 2007 - 2009 Vivien Malerba
 *
 * This Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <libgda/gda-debug-macros.h>
#include <libgda/sql-parser/gda-statement-struct.h>
#include <libgda/sql-parser/gda-statement-struct-delete.h>
#include <libgda/sql-parser/gda-statement-struct-util.h>
#include <libgda/sql-parser/gda-statement-struct-pspec.h>
#include <string.h>
#include <glib/gi18n-lib.h>

static gpointer  gda_sql_statement_delete_new (void);
static void      gda_sql_statement_delete_free (gpointer stmt);
static gpointer  gda_sql_statement_delete_copy (gpointer src);
static gchar    *gda_sql_statement_delete_serialize (gpointer stmt);
static gboolean  gda_sql_statement_delete_check_structure (GdaSqlAnyPart *stmt, gpointer data, GError **error);

GdaSqlStatementContentsInfo delete_infos = {
	GDA_SQL_STATEMENT_DELETE,
	"DELETE",
	gda_sql_statement_delete_new,
	gda_sql_statement_delete_free,
	gda_sql_statement_delete_copy,
	gda_sql_statement_delete_serialize,

	gda_sql_statement_delete_check_structure,
	NULL
};

GdaSqlStatementContentsInfo *
_gda_sql_statement_delete_get_infos (void)
{
	return &delete_infos;
}

static gpointer
gda_sql_statement_delete_new (void)
{
	GdaSqlStatementDelete *stmt;
	stmt = g_new0 (GdaSqlStatementDelete, 1);
	GDA_SQL_ANY_PART (stmt)->type = GDA_SQL_ANY_STMT_DELETE;
	return (gpointer) stmt;
}

static void
gda_sql_statement_delete_free (gpointer stmt)
{
	GdaSqlStatementDelete *delete = (GdaSqlStatementDelete *) stmt;

	if (delete->table)
		gda_sql_table_free (delete->table);
	if (delete->cond)
		gda_sql_expr_free (delete->cond);
	g_free (delete);
}

static gpointer
gda_sql_statement_delete_copy (gpointer src)
{
	GdaSqlStatementDelete *dest;
	GdaSqlStatementDelete *delete = (GdaSqlStatementDelete *) src;

	dest = gda_sql_statement_delete_new ();

	dest->table = gda_sql_table_copy (delete->table);
	gda_sql_any_part_set_parent (dest->table, dest);

	dest->cond = gda_sql_expr_copy (delete->cond);
	gda_sql_any_part_set_parent (dest->cond, dest);

	return dest;
}

static gchar *
gda_sql_statement_delete_serialize (gpointer stmt)
{
	GString *string;
	gchar *str;
	GdaSqlStatementDelete *delete = (GdaSqlStatementDelete *) stmt;

	g_return_val_if_fail (stmt, NULL);

	string = g_string_new ("\"contents\":{");

	/* table name */
	g_string_append (string, "\"table\":");
	str = gda_sql_table_serialize (delete->table);
	g_string_append (string, str);
	g_free (str);

	/* condition */
	if (delete->cond) {
		g_string_append (string, ",\"condition\":");
		str = gda_sql_expr_serialize (delete->cond);
		g_string_append (string, str);
		g_free (str);
	}
	g_string_append_c (string, '}');
	str = string->str;
	g_string_free (string, FALSE);
	return str;	
}

/**
 * gda_sql_statement_delete_take_table_name
 * @stmt: a #GdaSqlStatement pointer
 * @value: a table name as a G_TYPE_STRING #GValue 
 *
 * Sets the name of the table to delete from in @stmt. @value's responsability is transfered to
 * @stmt (which means @stmt is then responsible to freeing it when no longer needed).
 */
void
gda_sql_statement_delete_take_table_name (GdaSqlStatement *stmt, GValue *value)
{
	GdaSqlStatementDelete *delete = (GdaSqlStatementDelete *) stmt->contents;
	if (value) {
		delete->table = gda_sql_table_new (GDA_SQL_ANY_PART (stmt));
		gda_sql_table_take_name (delete->table, value);
	}
}

/**
 * gda_sql_statement_delete_take_condition
 * @stmt: a #GdaSqlStatement pointer
 * @cond: the WHERE condition of the DELETE statement, as a #GdaSqlExpr 
 *
 * Sets the WHERE condition of @stmt. @cond's responsability is transfered to
 * @stmt (which means @stmt is then responsible to freeing it when no longer needed).
 */
void 
gda_sql_statement_delete_take_condition (GdaSqlStatement *stmt, GdaSqlExpr *cond)
{
	GdaSqlStatementDelete *delete = (GdaSqlStatementDelete *) stmt->contents;
	delete->cond = cond;
	gda_sql_any_part_set_parent (delete->cond, delete);
}

static gboolean
gda_sql_statement_delete_check_structure (GdaSqlAnyPart *stmt, gpointer data, GError **error)
{
	GdaSqlStatementDelete *delete = (GdaSqlStatementDelete *) stmt;
	if (!delete->table) {
		g_set_error (error, GDA_SQL_ERROR, GDA_SQL_STRUCTURE_CONTENTS_ERROR,
			      "%s", _("DELETE statement needs a table to delete from"));
		return FALSE;
	}        

        return TRUE;
}