1 /*
2 * Copyright (C) 2008 Murray Cumming <murrayc@murrayc.com>
3 * Copyright (C) 2008 - 2011 Vivien Malerba <malerba@gnome-db.org>
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 <glib/gi18n-lib.h>
22 #include <libgda/libgda.h>
23 #include "gda-firebird-ddl.h"
24
25 gchar *
gda_firebird_render_CREATE_TABLE(GdaServerProvider * provider,GdaConnection * cnc,GdaServerOperation * op,GError ** error)26 gda_firebird_render_CREATE_TABLE (GdaServerProvider *provider, GdaConnection *cnc,
27 GdaServerOperation *op, GError **error)
28 {
29 GString *string;
30 const GValue *value;
31 gboolean allok = TRUE;
32 gboolean hasfields = FALSE;
33 gint nrows;
34 gint i;
35 gboolean first;
36 GSList *pkfields = NULL; /* list of GValue* composing the pkey */
37 gint nbpkfields = 0;
38 gchar *sql = NULL;
39
40 /* CREATE TABLE */
41 string = g_string_new ("CREATE TABLE ");
42
43 value = gda_server_operation_get_value_at (op, "/TABLE_DEF_P/TABLE_NAME");
44 g_assert (value && G_VALUE_HOLDS (value, G_TYPE_STRING));
45 g_string_append (string, g_value_get_string (value));
46 g_string_append (string, " (");
47
48 /* FIELDS */
49 if (allok) {
50 GdaServerOperationNode *node;
51
52 node = gda_server_operation_get_node_info (op, "/FIELDS_A");
53 g_assert (node);
54
55 /* finding if there is a composed primary key */
56 nrows = gda_data_model_get_n_rows (node->model);
57 for (i = 0; i < nrows; i++) {
58 value = gda_server_operation_get_value_at (op, "/FIELDS_A/@COLUMN_PKEY/%d", i);
59 if (value && G_VALUE_HOLDS (value, G_TYPE_BOOLEAN) && g_value_get_boolean (value))
60 pkfields = g_slist_append (pkfields,
61 (GValue *) gda_server_operation_get_value_at (op, "/FIELDS_A/@COLUMN_NAME/%d", i));
62 }
63 nbpkfields = g_slist_length (pkfields);
64
65 /* manually defined fields */
66 first = TRUE;
67 for (i = 0; i < nrows; i++) {
68 hasfields = TRUE;
69 if (first)
70 first = FALSE;
71 else
72 g_string_append (string, ", ");
73
74 value = gda_server_operation_get_value_at (op, "/FIELDS_A/@COLUMN_NAME/%d", i);
75 g_string_append (string, g_value_get_string (value));
76 g_string_append_c (string, ' ');
77
78 value = gda_server_operation_get_value_at (op, "/FIELDS_A/@COLUMN_TYPE/%d", i);
79 g_string_append (string, g_value_get_string (value));
80
81 value = gda_server_operation_get_value_at (op, "/FIELDS_A/@COLUMN_DEFAULT/%d", i);
82 if (value && G_VALUE_HOLDS (value, G_TYPE_STRING)) {
83 const gchar *str = g_value_get_string (value);
84 if (str && *str) {
85 g_string_append (string, " DEFAULT ");
86 g_string_append (string, str);
87 }
88 }
89
90 value = gda_server_operation_get_value_at (op, "/FIELDS_A/@COLUMN_NNUL/%d", i);
91 if (value && G_VALUE_HOLDS (value, G_TYPE_BOOLEAN) && g_value_get_boolean (value))
92 g_string_append (string, " NOT NULL");
93
94 value = gda_server_operation_get_value_at (op, "/FIELDS_A/@COLUMN_UNIQUE/%d", i);
95 if (value && G_VALUE_HOLDS (value, G_TYPE_BOOLEAN) && g_value_get_boolean (value))
96 g_string_append (string, " UNIQUE");
97
98 if (nbpkfields == 1) {
99 value = gda_server_operation_get_value_at (op, "/FIELDS_A/@COLUMN_PKEY/%d", i);
100 if (value && G_VALUE_HOLDS (value, G_TYPE_BOOLEAN) && g_value_get_boolean (value))
101 g_string_append (string, " PRIMARY KEY");
102 }
103
104 value = gda_server_operation_get_value_at (op, "/FIELDS_A/@COLUMN_CHECK/%d", i);
105 if (value && G_VALUE_HOLDS (value, G_TYPE_STRING)) {
106 const gchar *str = g_value_get_string (value);
107 if (str && *str) {
108 g_string_append (string, " CHECK (");
109 g_string_append (string, str);
110 g_string_append_c (string, ')');
111 }
112 }
113 }
114 }
115
116 /* composed primary key */
117 if (nbpkfields > 1) {
118 GSList *list = pkfields;
119
120 g_string_append (string, ", PRIMARY KEY (");
121 while (list) {
122 if (list != pkfields)
123 g_string_append (string, ", ");
124 g_string_append (string, g_value_get_string ((GValue*) list->data));
125 list = list->next;
126 }
127 g_string_append_c (string, ')');
128 }
129
130 g_string_append (string, ")");
131
132 if (!hasfields) {
133 allok = FALSE;
134 g_set_error (error, GDA_SERVER_OPERATION_ERROR,
135 GDA_SERVER_OPERATION_INCORRECT_VALUE_ERROR,
136 "%s", _("Table to create must have at least one row"));
137 }
138 g_slist_free (pkfields);
139
140 sql = string->str;
141 g_string_free (string, FALSE);
142
143 return sql;
144 }
145