1 /* 2 * Copyright (C) YEAR The GNOME Foundation 3 * 4 * AUTHORS: 5 * TO_ADD: your name and email 6 * Vivien Malerba <malerba@gnome-db.org> 7 * 8 * This Library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Library General Public License as 10 * published by the Free Software Foundation; either version 2 of the 11 * License, or (at your option) any later version. 12 * 13 * This Library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Library General Public License for more details. 17 * 18 * You should have received a copy of the GNU Library General Public 19 * License along with this Library; see the file COPYING.LIB. If not, 20 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 21 * Boston, MA 02110-1301, USA. 22 */ 23 24 #include <glib/gi18n-lib.h> 25 #include <libgda/libgda.h> 26 #include "gda-capi-ddl.h" 27 28 gchar * 29 gda_capi_render_CREATE_TABLE (GdaServerProvider *provider, GdaConnection *cnc, 30 GdaServerOperation *op, GError **error) 31 { 32 GString *string; 33 const GValue *value; 34 gboolean allok = TRUE; 35 gboolean hasfields = FALSE; 36 gint nrows; 37 gint i; 38 gboolean first; 39 GSList *pkfields = NULL; /* list of GValue* composing the pkey */ 40 gint nbpkfields = 0; 41 gchar *tmp; 42 43 /* CREATE TABLE */ 44 string = g_string_new ("CREATE TABLE "); 45 46 tmp = gda_server_operation_get_sql_identifier_at (op, cnc, provider, "/TABLE_DEF_P/TABLE_NAME"); 47 g_string_append (string, tmp); 48 g_free (tmp); 49 g_string_append (string, " ("); 50 51 /* FIELDS */ 52 if (allok) { 53 GdaServerOperationNode *node; 54 55 node = gda_server_operation_get_node_info (op, "/FIELDS_A"); 56 g_assert (node); 57 58 /* finding if there is a composed primary key */ 59 nrows = gda_data_model_get_n_rows (node->model); 60 for (i = 0; i < nrows; i++) { 61 value = gda_server_operation_get_value_at (op, "/FIELDS_A/@COLUMN_PKEY/%d", i); 62 if (value && G_VALUE_HOLDS (value, G_TYPE_BOOLEAN) && g_value_get_boolean (value)) { 63 tmp = gda_server_operation_get_sql_identifier_at (op, cnc, provider, 64 "/FIELDS_A/@COLUMN_NAME/%d", i); 65 pkfields = g_slist_append (pkfields, tmp); 66 nbpkfields++; 67 } 68 } 69 70 /* manually defined fields */ 71 first = TRUE; 72 for (i = 0; i < nrows; i++) { 73 hasfields = TRUE; 74 if (first) 75 first = FALSE; 76 else 77 g_string_append (string, ", "); 78 79 tmp = gda_server_operation_get_sql_identifier_at (op, cnc, provider, 80 "/FIELDS_A/@COLUMN_NAME/%d", i); 81 g_string_append (string, tmp); 82 g_free (tmp); 83 g_string_append_c (string, ' '); 84 85 value = gda_server_operation_get_value_at (op, "/FIELDS_A/@COLUMN_TYPE/%d", i); 86 g_string_append (string, g_value_get_string (value)); 87 88 value = gda_server_operation_get_value_at (op, "/FIELDS_A/@COLUMN_DEFAULT/%d", i); 89 if (value && G_VALUE_HOLDS (value, G_TYPE_STRING)) { 90 const gchar *str = g_value_get_string (value); 91 if (str && *str) { 92 g_string_append (string, " DEFAULT "); 93 g_string_append (string, str); 94 } 95 } 96 97 value = gda_server_operation_get_value_at (op, "/FIELDS_A/@COLUMN_NNUL/%d", i); 98 if (value && G_VALUE_HOLDS (value, G_TYPE_BOOLEAN) && g_value_get_boolean (value)) 99 g_string_append (string, " NOT NULL"); 100 101 value = gda_server_operation_get_value_at (op, "/FIELDS_A/@COLUMN_UNIQUE/%d", i); 102 if (value && G_VALUE_HOLDS (value, G_TYPE_BOOLEAN) && g_value_get_boolean (value)) 103 g_string_append (string, " UNIQUE"); 104 105 if (nbpkfields == 1) { 106 value = gda_server_operation_get_value_at (op, "/FIELDS_A/@COLUMN_PKEY/%d", i); 107 if (value && G_VALUE_HOLDS (value, G_TYPE_BOOLEAN) && g_value_get_boolean (value)) 108 g_string_append (string, " PRIMARY KEY"); 109 } 110 111 value = gda_server_operation_get_value_at (op, "/FIELDS_A/@COLUMN_CHECK/%d", i); 112 if (value && G_VALUE_HOLDS (value, G_TYPE_STRING)) { 113 const gchar *str = g_value_get_string (value); 114 if (str && *str) { 115 g_string_append (string, " CHECK ("); 116 g_string_append (string, str); 117 g_string_append_c (string, ')'); 118 } 119 } 120 } 121 } 122 123 /* composed primary key */ 124 if (nbpkfields > 1) { 125 GSList *list; 126 127 g_string_append (string, ", PRIMARY KEY ("); 128 for (list = pkfields; list; list = list->next) { 129 if (list != pkfields) 130 g_string_append (string, ", "); 131 g_string_append (string, (gchar*) list->data); 132 } 133 g_string_append_c (string, ')'); 134 } 135 g_slist_foreach (pkfields, (GFunc) g_free, NULL); 136 g_slist_free (pkfields); 137 138 g_string_append (string, ")"); 139 140 if (!hasfields) { 141 allok = FALSE; 142 g_set_error (error, GDA_SERVER_OPERATION_ERROR, 143 GDA_SERVER_OPERATION_INCORRECT_VALUE_ERROR, 144 "%s", _("Table to create must have at least one row")); 145 } 146 147 return g_string_free (string, FALSE); 148 } 149