]> saetta.ns0.it Git - zakform/gdaex/commitdiff
Added functions ::get_create_table_*. develop
authorAndrea Zagli <azagli@libero.it>
Fri, 24 Jul 2020 11:45:27 +0000 (13:45 +0200)
committerAndrea Zagli <azagli@libero.it>
Fri, 24 Jul 2020 11:45:27 +0000 (13:45 +0200)
src/form.c
src/form.h

index 08452709e903479af7b548819924559bd803236c..09fd92aff6dec0f0064cc98a5d511edaa274bb9e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015-2018 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2020 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -259,6 +259,194 @@ gchar
        return str;
 }
 
+GdaServerOperation
+*zak_form_gdaex_provider_get_create_table_op (ZakFormGdaexProvider *provider, GPtrArray *elements)
+{
+       GdaServerOperation *op;
+
+       GdaConnection *cnc;
+       GdaServerProvider *gda_provider;
+
+       GError *error;
+
+       guint i;
+       guint l;
+
+       gchar *type;
+
+       ZakFormGdaexProviderPrivate *priv = ZAK_FORM_GDAEX_PROVIDER_GET_PRIVATE (provider);
+
+       cnc = (GdaConnection *)gdaex_get_gdaconnection (priv->gdaex);
+       gda_provider = gda_connection_get_provider (cnc);
+
+       error = NULL;
+       op = gda_server_provider_create_operation (gda_provider, cnc, GDA_SERVER_OPERATION_CREATE_TABLE, NULL, &error);
+       if (op == NULL)
+               {
+                       g_warning  ("CREATE TABLE operation is not supported by the provider: %s\n",
+                                   error && error->message ? error->message : "no detail");
+                       return NULL;
+               }
+
+       error = NULL;
+       if (!gda_server_operation_set_value_at (op, priv->table, &error, "/TABLE_DEF_P/TABLE_NAME")
+           || error != NULL)
+               {
+                       g_warning ("Error on server_operation_set_value (table name): %s",
+                                  error != NULL && error->message != NULL ? error->message : "no detail");
+               }
+
+       l = -1;
+       for (i = 0; i < elements->len; i++)
+               {
+                       ZakFormElement *element = (ZakFormElement *)g_ptr_array_index (elements, i);
+
+                       if (!zak_form_element_get_to_save (element)) continue;
+
+                       l++;
+
+                       error = NULL;
+                       if (!gda_server_operation_set_value_at (op, zak_form_element_get_name (element), &error, "/FIELDS_A/@COLUMN_NAME/%d", l)
+                           || error != NULL)
+                               {
+                                       g_warning ("Error on server_operation_set_value (column name): %s",
+                                                  error != NULL && error->message != NULL ? error->message : "no detail");
+                               }
+
+                       type = zak_form_element_get_provider_type (element);
+
+                       if (g_strcmp0 (type, "integer") == 0
+                           || g_strcmp0 (type, "float") == 0
+                           || g_strcmp0 (type, "boolean") == 0
+                           || g_strcmp0 (type, "date") == 0
+                           || g_strcmp0 (type, "time") == 0)
+                               {
+                                       gda_server_operation_set_value_at (op, type, &error, "/FIELDS_A/@COLUMN_TYPE/%d", l);
+                               }
+                       else if (g_strcmp0 (type, "string") == 0)
+                               {
+                                       gda_server_operation_set_value_at (op, "varchar", &error, "/FIELDS_A/@COLUMN_TYPE/%d", l);
+                                       //gda_server_operation_set_value_at (op, "maxlength", &error, "/FIELDS_A/@COLUMN_SIZE/%d", i);
+                               }
+                       else if (g_strcmp0 (type, "datetime") == 0)
+                               {
+                                       gda_server_operation_set_value_at (op, "timestamp", &error, "/FIELDS_A/@COLUMN_TYPE/%d", l);
+                               }
+                       else
+                               {
+                                       g_warning ("Type not supported: %s", type);
+                                       continue;
+                               }
+
+                       error = NULL;
+                       if (zak_form_element_get_is_key (element))
+                               {
+                                       if (!gda_server_operation_set_value_at (op, "TRUE", &error, "/FIELDS_A/@COLUMN_PKEY/%d", l)
+                                           || error != NULL)
+                                               {
+                                                       g_warning ("Error on server_operation_set_value (column pkey): %s",
+                                                                  error != NULL && error->message != NULL ? error->message : "no detail");
+                                               }
+                               }
+
+                       error = NULL;
+                       if (g_strcmp0 (type, "string") == 0
+                           || g_strcmp0 (type, "boolean") == 0
+                           || g_strcmp0 (type, "date") == 0
+                           || g_strcmp0 (type, "time") == 0
+                           || g_strcmp0 (type, "datetime") == 0)
+                               {
+                                       if (g_strcmp0 (type, "string") != 0)
+                                               {
+                                                       if (g_strcmp0 (zak_form_element_get_default_value (element), "") != 0)
+                                                               {
+                                                                       if (g_strcmp0 (type, "date") == 0
+                                                                           || g_strcmp0 (type, "time") == 0
+                                                                           || g_strcmp0 (type, "datetime") == 0)
+                                                                               {
+                                                                                       if (g_strcmp0 (zak_form_element_get_default_value (element), "@now") == 0)
+                                                                                               {
+                                                                                                       if (!gda_server_operation_set_value_at (op, "CURRENT DATE", &error, "/FIELDS_A/@COLUMN_DEFAULT/%d", l)
+                                                                                                           || error != NULL)
+                                                                                                               {
+                                                                                                                       g_warning ("Error on server_operation_set_value (column default): %s",
+                                                                                                                                  error != NULL && error->message != NULL ? error->message : "no detail");
+                                                                                                               }
+                                                                                               }
+                                                                                       else
+                                                                                               {
+                                                                                                       if (!gda_server_operation_set_value_at (op, g_strdup_printf ("'%s'", gdaex_strescape (zak_form_element_get_default_value (element), NULL)), &error, "/FIELDS_A/@COLUMN_DEFAULT/%d", l)
+                                                                                                           || error != NULL)
+                                                                                                               {
+                                                                                                                       g_warning ("Error on server_operation_set_value (column default): %s",
+                                                                                                                                  error != NULL && error->message != NULL ? error->message : "no detail");
+                                                                                                               }
+                                                                                               }
+                                                                               }
+                                                                       else
+                                                                               {
+                                                                                       if (!gda_server_operation_set_value_at (op, g_strdup_printf ("'%s'", gdaex_strescape (zak_form_element_get_default_value (element), NULL)), &error, "/FIELDS_A/@COLUMN_DEFAULT/%d", l)
+                                                                                           || error != NULL)
+                                                                                               {
+                                                                                                       g_warning ("Error on server_operation_set_value (column default): %s",
+                                                                                                                  error != NULL && error->message != NULL ? error->message : "no detail");
+                                                                                               }
+                                                                               }
+                                                               }
+                                               }
+                                       else
+                                               {
+                                                       if (!gda_server_operation_set_value_at (op, g_strdup_printf ("'%s'", gdaex_strescape (zak_form_element_get_default_value (element), NULL)), &error, "/FIELDS_A/@COLUMN_DEFAULT/%d", l)
+                                                           || error != NULL)
+                                                               {
+                                                                       g_warning ("Error on server_operation_set_value (column default): %s",
+                                                                                  error != NULL && error->message != NULL ? error->message : "no detail");
+                                                               }
+                                               }
+                               }
+                       else
+                               {
+                                       error = NULL;
+                                       if (!gda_server_operation_set_value_at (op, gdaex_strescape (zak_form_element_get_default_value (element), NULL), &error, "/FIELDS_A/@COLUMN_DEFAULT/%d", l)
+                                           || error != NULL)
+                                               {
+                                                       g_warning ("Error on server_operation_set_value (column default): %s",
+                                                                  error != NULL && error->message != NULL ? error->message : "no detail");
+                                               }
+                               }
+               }
+
+       return op;
+}
+
+gchar
+*zak_form_gdaex_provider_get_create_table_str (ZakFormGdaexProvider *provider, GPtrArray *elements)
+{
+       ZakFormGdaexProviderPrivate *priv = ZAK_FORM_GDAEX_PROVIDER_GET_PRIVATE (provider);
+
+       GdaConnection *cnc;
+
+       GError *error;
+       gchar *sql;
+
+       cnc = (GdaConnection *)gdaex_get_gdaconnection (priv->gdaex);
+
+       error = NULL;
+
+       sql = gda_server_provider_render_operation (gda_connection_get_provider (cnc),
+                                                   cnc,
+                                                   zak_form_gdaex_provider_get_create_table_op (provider, elements),
+                                                   &error);
+       if (sql == NULL
+           || error != NULL)
+               {
+                       g_warning ("Error on rendering server operation: %s",
+                                  error != NULL && error->message != NULL ? error->message : "no detail");
+               }
+
+       return sql;
+}
+
 /* PRIVATE */
 static void
 zak_form_gdaex_provider_set_property (GObject *object,
index 670095eb91635df5ec026fcc46dd66ff1b4b8678..3ed06a4c6720d93d22ff38ae858963fa0d1d9827 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015-2017 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2020 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -42,6 +42,9 @@ ZakFormGdaexProvider *zak_form_gdaex_provider_new (GdaEx *gdaex, const gchar *ta
 
 gchar *zak_form_gdaex_provider_get_where_str (ZakFormGdaexProvider *provider, GPtrArray *elements);
 
+GdaServerOperation *zak_form_gdaex_provider_get_create_table_op (ZakFormGdaexProvider *provider, GPtrArray *elements);
+gchar *zak_form_gdaex_provider_get_create_table_str (ZakFormGdaexProvider *provider, GPtrArray *elements);
+
 
 G_END_DECLS