File: gda-statement-struct-update.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 (294 lines) | stat: -rw-r--r-- 9,219 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* 
 * Copyright (C) 2007 - 2008 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-update.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_update_new (void);
static void      gda_sql_statement_update_free (gpointer stmt);
static gpointer  gda_sql_statement_update_copy (gpointer src);
static gchar    *gda_sql_statement_update_serialize (gpointer stmt);
static gboolean  gda_sql_statement_update_check_structure (GdaSqlAnyPart *stmt, gpointer data, GError **error);

GdaSqlStatementContentsInfo update_infos = {
	GDA_SQL_STATEMENT_UPDATE,
	"UPDATE",
	gda_sql_statement_update_new,
	gda_sql_statement_update_free,
	gda_sql_statement_update_copy,
	gda_sql_statement_update_serialize,

	gda_sql_statement_update_check_structure,
	NULL
};

GdaSqlStatementContentsInfo *
_gda_sql_statement_update_get_infos (void)
{
	return &update_infos;
}

static gpointer
gda_sql_statement_update_new (void)
{
	GdaSqlStatementUpdate *stmt;
	stmt = g_new0 (GdaSqlStatementUpdate, 1);
	GDA_SQL_ANY_PART (stmt)->type = GDA_SQL_ANY_STMT_UPDATE;
	return (gpointer) stmt;
}

static void
gda_sql_statement_update_free (gpointer stmt)
{
	GdaSqlStatementUpdate *update = (GdaSqlStatementUpdate *) stmt;
	GSList *list;

	if (update->table)
		gda_sql_table_free (update->table);
	for (list = update->fields_list; list; list = list->next) {
		if (list->data)
			gda_sql_field_free ((GdaSqlField *) list->data);
	}
	for (list = update->expr_list; list; list = list->next) {
		if (list->data)
			gda_sql_expr_free ((GdaSqlExpr *) list->data);
	}
	if (update->cond)
		gda_sql_expr_free (update->cond);
	g_free (update);
}

static gpointer
gda_sql_statement_update_copy (gpointer src)
{
	GdaSqlStatementUpdate *dest;
	GdaSqlStatementUpdate *update = (GdaSqlStatementUpdate *) src;
	GSList *list;

	dest = gda_sql_statement_update_new ();
	if (update->on_conflict)
                dest->on_conflict = g_strdup (update->on_conflict);

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

	for (list = update->fields_list; list; list = list->next) {
		dest->fields_list = g_slist_prepend (dest->fields_list,
						     gda_sql_field_copy ((GdaSqlField *) list->data));
		gda_sql_any_part_set_parent (dest->fields_list->data, dest);
	}
	dest->fields_list = g_slist_reverse (dest->fields_list);

	for (list = update->expr_list; list; list = list->next) {
		dest->expr_list = g_slist_prepend (dest->expr_list,
						   gda_sql_expr_copy ((GdaSqlExpr *) list->data));
		gda_sql_any_part_set_parent (dest->expr_list->data, dest);
	}
	dest->expr_list = g_slist_reverse (dest->expr_list);

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

	return dest;
}

static gchar *
gda_sql_statement_update_serialize (gpointer stmt)
{
	GString *string;
	gchar *str;
	GSList *list;
	GdaSqlStatementUpdate *update = (GdaSqlStatementUpdate *) 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 (update->table);
	g_string_append (string, str);
	g_free (str);

	/* fields */
	g_string_append (string, ",\"fields\":");
	if (update->fields_list) {
		g_string_append_c (string, '[');
		for (list = update->fields_list; list; list = list->next) {
			if (list != update->fields_list)
				g_string_append_c (string, ',');
			str = gda_sql_field_serialize ((GdaSqlField *) list->data);
			g_string_append (string, str);
			g_free (str);
		}
		g_string_append_c (string, ']');
	}
	else
		g_string_append (string, "null");

	/* expressions */
	g_string_append (string, ",\"expressions\":");
	if (update->expr_list) {
		g_string_append_c (string, '[');
		for (list = update->expr_list; list; list = list->next) {
			if (list != update->expr_list)
				g_string_append_c (string, ',');
			str = gda_sql_expr_serialize ((GdaSqlExpr *) list->data);
			g_string_append (string, str);
			g_free (str);
		}
		g_string_append_c (string, ']');
	}
	else
		g_string_append (string, "null");	

	/* condition */
	if (update->cond) {
		g_string_append (string, ",\"condition\":");
		str = gda_sql_expr_serialize (update->cond);
		g_string_append (string, str);
		g_free (str);
	}

	/* conflict clause */
        if (update->on_conflict) {
                g_string_append (string, ",\"on_conflict\":");
                str = _json_quote_string (update->on_conflict);
                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_update_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_update_take_table_name (GdaSqlStatement *stmt, GValue *value)
{
	GdaSqlStatementUpdate *update = (GdaSqlStatementUpdate *) stmt->contents;
	if (value) {
		update->table = gda_sql_table_new (GDA_SQL_ANY_PART (update));
		gda_sql_table_take_name (update->table, value);
	}
}

/**
 * gda_sql_statement_update_take_on_conflict
 * @stmt: a #GdaSqlStatement pointer
 * @value: name of the resolution conflict algotithm, as a G_TYPE_STRING #GValue
 *
 * Sets the name of the resolution conflict algotithm used by @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_update_take_on_conflict (GdaSqlStatement *stmt, GValue *value)
{
	GdaSqlStatementUpdate *update = (GdaSqlStatementUpdate *) stmt->contents;
        if (value) {
                update->on_conflict = g_value_dup_string (value);
                g_value_reset (value);
                g_free (value);
        }
}

/**
 * gda_sql_statement_update_take_condition
 * @stmt: a #GdaSqlStatement pointer
 * @cond: a #GdaSqlExpr pointer
 *
 * Sets the WHERE clause of @stmt
 *
 * @expr's responsability is transfered to
 * @stmt (which means @stmt is then responsible to freeing it when no longer needed).
 */
void 
gda_sql_statement_update_take_condition (GdaSqlStatement *stmt, GdaSqlExpr *cond)
{
	GdaSqlStatementUpdate *update = (GdaSqlStatementUpdate *) stmt->contents;
	update->cond = cond;
	gda_sql_any_part_set_parent (cond, update);
}


/**
 * gda_sql_statement_update_take_set_value
 * @stmt: a #GdaSqlStatement pointer
 * @fname: a field name, as a G_TYPE_STRING #GValue
 * @expr: a #GdaSqlExpr pointer
 *
 * Specifies that the field named @fname will be updated with the expression @expr.
 *
 * @fname and @expr's responsability are transfered to
 * @stmt (which means @stmt is then responsible to freeing them when no longer needed).
 */
void
gda_sql_statement_update_take_set_value (GdaSqlStatement *stmt, GValue *fname, GdaSqlExpr *expr)
{
	GdaSqlStatementUpdate *update = (GdaSqlStatementUpdate *) stmt->contents;
	GdaSqlField *sf;

	sf = gda_sql_field_new (GDA_SQL_ANY_PART (update));
	gda_sql_field_take_name (sf, fname);
	update->fields_list = g_slist_append (update->fields_list, sf);

	update->expr_list = g_slist_append (update->expr_list, expr);
	gda_sql_any_part_set_parent (expr, update);
}

static gboolean
gda_sql_statement_update_check_structure (GdaSqlAnyPart *stmt, gpointer data, GError **error)
{
	GdaSqlStatementUpdate *update = (GdaSqlStatementUpdate *) stmt;

	if (!update->table) {
		g_set_error (error, GDA_SQL_ERROR, GDA_SQL_STRUCTURE_CONTENTS_ERROR,
			      "%s", _("UPDATE statement needs a table to update data"));
		return FALSE;
	}

	if (g_slist_length (update->fields_list) != g_slist_length (update->expr_list)) {
		g_set_error (error, GDA_SQL_ERROR, GDA_SQL_STRUCTURE_CONTENTS_ERROR,
			      "%s", _("UPDATE statement does not have the same number of target columns and expressions"));
		return FALSE;
	}

	if (!update->fields_list) {
		g_set_error (error, GDA_SQL_ERROR, GDA_SQL_STRUCTURE_CONTENTS_ERROR,
			      "%s", _("UPDATE statement does not have any target columns to update"));
		return FALSE;
	}

        return TRUE;
}