]> saetta.ns0.it Git - zakform/gdaex/commitdiff
In extension added ability to return an SQL string.
authorAndrea Zagli <azagli@libero.it>
Sat, 30 Jun 2018 10:32:29 +0000 (12:32 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 30 Jun 2018 10:32:29 +0000 (12:32 +0200)
src/form.c
src/formelementextension.c
src/formelementextension.h

index 41df4c576ff190873f130876c1ab8fb9f03067e8..08452709e903479af7b548819924559bd803236c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015-2017 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2018 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
@@ -138,6 +138,10 @@ gchar
 
        GValue *value;
 
+       gchar *str;
+
+       GString *where;
+
        ZakFormGdaexProviderPrivate *priv = ZAK_FORM_GDAEX_PROVIDER_GET_PRIVATE (provider);
 
        sqlb = gdaex_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
@@ -150,6 +154,8 @@ gchar
                                 "",
                                 NULL);
 
+       where = g_string_new ("");
+
        for (i = 0; i < elements->len; i++)
                {
                        ZakFormElement *element = (ZakFormElement *)g_ptr_array_index (elements, i);
@@ -169,56 +175,88 @@ gchar
 
                                        ZakFormGdaExElementExtension *extension = (ZakFormGdaExElementExtension *)g_ptr_array_index (extensions, l);
 
-                                       table = zak_form_gdaex_element_extension_get_where_table (extension);
-                                       table_alias = zak_form_gdaex_element_extension_get_where_table_alias (extension);
-                                       field = zak_form_gdaex_element_extension_get_where_field (extension);
-                                       field_alias = zak_form_gdaex_element_extension_get_where_field_alias (extension);
-                                       type = zak_form_gdaex_element_extension_get_where_type (extension);
-
-                                       if (table != NULL)
-                                               {
-                                                       gdaex_sql_builder_from (sqlb, table, table_alias == NULL ? "" : table_alias);
-                                               }
-
-                                       if (type == GDA_SQL_OPERATOR_TYPE_LIKE
-                                           || type == GDA_SQL_OPERATOR_TYPE_ILIKE
-                                           || type == GDA_SQL_OPERATOR_TYPE_NOTLIKE
-                                           || type == GDA_SQL_OPERATOR_TYPE_NOTILIKE)
+                                       str = zak_form_gdaex_element_extension_get_str (extension);
+                                       if (str == NULL)
                                                {
-                                                       gchar *str;
-                                                       gint like_type;
-
-                                                       str = g_value_dup_string (value);
+                                                       table = zak_form_gdaex_element_extension_get_where_table (extension);
+                                                       table_alias = zak_form_gdaex_element_extension_get_where_table_alias (extension);
+                                                       field = zak_form_gdaex_element_extension_get_where_field (extension);
+                                                       field_alias = zak_form_gdaex_element_extension_get_where_field_alias (extension);
+                                                       type = zak_form_gdaex_element_extension_get_where_type (extension);
 
-                                                       like_type = zak_form_gdaex_element_extension_get_where_like_type (extension);
-                                                       if (like_type < 0)
+                                                       if (table != NULL)
                                                                {
-                                                                       g_value_set_string (value, g_strdup_printf ("%%%s", str));
+                                                                       gdaex_sql_builder_from (sqlb, table, table_alias == NULL ? "" : table_alias);
                                                                }
-                                                       else if (like_type > 0)
+
+                                                       if (type == GDA_SQL_OPERATOR_TYPE_LIKE
+                                                           || type == GDA_SQL_OPERATOR_TYPE_ILIKE
+                                                           || type == GDA_SQL_OPERATOR_TYPE_NOTLIKE
+                                                           || type == GDA_SQL_OPERATOR_TYPE_NOTILIKE)
                                                                {
-                                                                       g_value_set_string (value, g_strdup_printf ("%s%%", str));
+                                                                       gint like_type;
+
+                                                                       str = g_value_dup_string (value);
+
+                                                                       like_type = zak_form_gdaex_element_extension_get_where_like_type (extension);
+                                                                       if (like_type < 0)
+                                                                               {
+                                                                                       g_value_set_string (value, g_strdup_printf ("%%%s", str));
+                                                                               }
+                                                                       else if (like_type > 0)
+                                                                               {
+                                                                                       g_value_set_string (value, g_strdup_printf ("%s%%", str));
+                                                                               }
+                                                                       else
+                                                                               {
+                                                                                       g_value_set_string (value, g_strdup_printf ("%%%s%%", str));
+                                                                               }
                                                                }
-                                                       else
+
+                                                       gdaex_sql_builder_where (sqlb,
+                                                                                0,
+                                                                                table == NULL ? priv->table : table,
+                                                                                field == NULL ? zak_form_element_get_name (element) : field,
+                                                                                field_alias == NULL ? "" : field_alias,
+                                                                                type,
+                                                                                value,
+                                                                                NULL);
+                                               }
+                                       else
+                                               {
+                                                       if (g_strcmp0 (str, "") != 0
+                                                           && g_strcmp0 (str, g_value_get_string (value)) == 0)
                                                                {
-                                                                       g_value_set_string (value, g_strdup_printf ("%%%s%%", str));
+                                                                       g_string_append_printf (where, "%s%s", where->len > 0 ? " AND " : "", str);
+                                                                       g_free (str);
                                                                }
                                                }
-
-                                       gdaex_sql_builder_where (sqlb,
-                                                                0,
-                                                                table == NULL ? priv->table : table,
-                                                                field == NULL ? zak_form_element_get_name (element) : field,
-                                                                field_alias == NULL ? "" : field_alias,
-                                                                type,
-                                                                value,
-                                                                NULL);
                                }
 
                        g_value_unset (value);
                }
 
-       return gdaex_sql_builder_get_sql_where (sqlb, (GdaConnection *)gdaex_get_gdaconnection (priv->gdaex), NULL);
+       str = gdaex_sql_builder_get_sql_where (sqlb, (GdaConnection *)gdaex_get_gdaconnection (priv->gdaex), NULL);
+       if (str != NULL)
+               {
+                       if (where->len > 0)
+                               {
+                                       g_string_prepend (where, " AND ");
+                               }
+                       g_string_prepend (where, str);
+
+                       g_free (str);
+               }
+
+       /* str = g_strconcat (gdaex_sql_builder_get_sql_where (sqlb, (GdaConnection *)gdaex_get_gdaconnection (priv->gdaex), NULL) != NULL ? gdaex_sql_builder_get_sql_where (sqlb, (GdaConnection *)gdaex_get_gdaconnection (priv->gdaex), NULL) : "", */
+       /*                    gdaex_sql_builder_get_sql_where (sqlb, (GdaConnection *)gdaex_get_gdaconnection (priv->gdaex), NULL) != NULL && g_strcmp0 (gdaex_sql_builder_get_sql_where (sqlb, (GdaConnection *)gdaex_get_gdaconnection (priv->gdaex), NULL), "") != 0 && where->len > 0 ? " AND " : "", */
+       /*                    where->str, */
+       /*                    NULL); */
+
+       str = g_strdup (where->str);
+       g_string_free (where, TRUE);
+
+       return str;
 }
 
 /* PRIVATE */
index d75bd8b3f28c8faf9fbb197c60d98fd3704ab63a..d789d0522fa2430a40099887ae6829a54dc15324 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2017-2018 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
@@ -52,6 +52,7 @@ typedef struct
                gchar *field_alias;
                GdaSqlOperatorType type;
                gint like_type;
+               gchar *str;
        } ZakFormGdaExElementExtensionPrivate;
 
 G_DEFINE_TYPE_WITH_PRIVATE (ZakFormGdaExElementExtension, zak_form_gdaex_element_extension, ZAK_FORM_TYPE_ELEMENT_EXTENSION)
@@ -81,6 +82,7 @@ zak_form_gdaex_element_extension_init (ZakFormGdaExElementExtension *zak_form_gd
        priv->field_alias = NULL;
        priv->type = 0;
        priv->like_type = 0;
+       priv->str = NULL;
 }
 
 ZakFormElementExtension
@@ -110,86 +112,95 @@ zak_form_gdaex_element_extension_xml_parsing (ZakFormElementExtension *extension
                {
                        if (g_strcmp0 ((gchar *)xnodetmp->name, "where") == 0)
                                {
-                                       zak_form_gdaex_element_extension_set_where_table ((ZakFormGdaExElementExtension *)extension,
-                                                                                         (const gchar *)xmlGetProp (xnodetmp, (const xmlChar *)"table"));
-                                       zak_form_gdaex_element_extension_set_where_table_alias ((ZakFormGdaExElementExtension *)extension,
-                                                                                               (const gchar *)xmlGetProp (xnodetmp, (const xmlChar *)"table_alias"));
-
-                                       zak_form_gdaex_element_extension_set_where_field ((ZakFormGdaExElementExtension *)extension,
-                                                                                         (const gchar *)xmlGetProp (xnodetmp, (const xmlChar *)"field"));
-                                       zak_form_gdaex_element_extension_set_where_field_alias ((ZakFormGdaExElementExtension *)extension,
-                                                                                               (const gchar *)xmlGetProp (xnodetmp, (const xmlChar *)"field_alias"));
-
-                                       prop = xmlGetProp (xnodetmp, (const xmlChar *)"type");
-                                       if (g_strcmp0 (prop, "lt") == 0)
+                                       if (g_strcmp0 ((gchar *)xmlNodeGetContent (xnodetmp), "") == 0)
                                                {
-                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
-                                                                                                        GDA_SQL_OPERATOR_TYPE_LT);
-                                               }
-                                       else if (g_strcmp0 (prop, "let") == 0)
-                                               {
-                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
-                                                                                                        GDA_SQL_OPERATOR_TYPE_LEQ);
-                                               }
-                                       else if (g_strcmp0 (prop, "e") == 0)
-                                               {
-                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
-                                                                                                        GDA_SQL_OPERATOR_TYPE_EQ);
-                                               }
-                                       else if (g_strcmp0 (prop, "ne") == 0)
-                                               {
-                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
-                                                                                                        GDA_SQL_OPERATOR_TYPE_DIFF);
-                                               }
-                                       else if (g_strcmp0 (prop, "gt") == 0)
-                                               {
-                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
-                                                                                                        GDA_SQL_OPERATOR_TYPE_GT);
-                                               }
-                                       else if (g_strcmp0 (prop, "get") == 0)
-                                               {
-                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
-                                                                                                        GDA_SQL_OPERATOR_TYPE_GEQ);
-                                               }
-                                       else if (g_strcmp0 (prop, "like") == 0)
-                                               {
-                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
-                                                                                                        GDA_SQL_OPERATOR_TYPE_LIKE);
-                                               }
-                                       else if (g_strcmp0 (prop, "nlike") == 0)
-                                               {
-                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
-                                                                                                        GDA_SQL_OPERATOR_TYPE_NOTLIKE);
-                                               }
-                                       else if (g_strcmp0 (prop, "ilike") == 0)
-                                               {
-                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
-                                                                                                        GDA_SQL_OPERATOR_TYPE_ILIKE);
-                                               }
-                                       else if (g_strcmp0 (prop, "nilike") == 0)
-                                               {
-                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
-                                                                                                        GDA_SQL_OPERATOR_TYPE_NOTILIKE);
-                                               }
-
-                                       if (g_strcmp0 (prop, "like") == 0
-                                               || g_strcmp0 (prop, "nlike") == 0
-                                           || g_strcmp0 (prop, "ilike") == 0
-                                               || g_strcmp0 (prop, "nilike") == 0)
-                                               {
-                                                       prop = xmlGetProp (xnodetmp, (const xmlChar *)"like");
-                                                       if (g_strcmp0 (prop, "start") == 0)
+                                                       zak_form_gdaex_element_extension_set_where_table ((ZakFormGdaExElementExtension *)extension,
+                                                                                                         (const gchar *)xmlGetProp (xnodetmp, (const xmlChar *)"table"));
+                                                       zak_form_gdaex_element_extension_set_where_table_alias ((ZakFormGdaExElementExtension *)extension,
+                                                                                                               (const gchar *)xmlGetProp (xnodetmp, (const xmlChar *)"table_alias"));
+
+                                                       zak_form_gdaex_element_extension_set_where_field ((ZakFormGdaExElementExtension *)extension,
+                                                                                                         (const gchar *)xmlGetProp (xnodetmp, (const xmlChar *)"field"));
+                                                       zak_form_gdaex_element_extension_set_where_field_alias ((ZakFormGdaExElementExtension *)extension,
+                                                                                                               (const gchar *)xmlGetProp (xnodetmp, (const xmlChar *)"field_alias"));
+
+                                                       prop = xmlGetProp (xnodetmp, (const xmlChar *)"type");
+                                                       if (g_strcmp0 (prop, "lt") == 0)
+                                                               {
+                                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
+                                                                                                                        GDA_SQL_OPERATOR_TYPE_LT);
+                                                               }
+                                                       else if (g_strcmp0 (prop, "let") == 0)
+                                                               {
+                                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
+                                                                                                                        GDA_SQL_OPERATOR_TYPE_LEQ);
+                                                               }
+                                                       else if (g_strcmp0 (prop, "e") == 0)
+                                                               {
+                                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
+                                                                                                                        GDA_SQL_OPERATOR_TYPE_EQ);
+                                                               }
+                                                       else if (g_strcmp0 (prop, "ne") == 0)
+                                                               {
+                                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
+                                                                                                                        GDA_SQL_OPERATOR_TYPE_DIFF);
+                                                               }
+                                                       else if (g_strcmp0 (prop, "gt") == 0)
+                                                               {
+                                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
+                                                                                                                        GDA_SQL_OPERATOR_TYPE_GT);
+                                                               }
+                                                       else if (g_strcmp0 (prop, "get") == 0)
+                                                               {
+                                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
+                                                                                                                        GDA_SQL_OPERATOR_TYPE_GEQ);
+                                                               }
+                                                       else if (g_strcmp0 (prop, "like") == 0)
+                                                               {
+                                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
+                                                                                                                        GDA_SQL_OPERATOR_TYPE_LIKE);
+                                                               }
+                                                       else if (g_strcmp0 (prop, "nlike") == 0)
                                                                {
-                                                                       zak_form_gdaex_element_extension_set_where_like_type ((ZakFormGdaExElementExtension *)extension, -1);
+                                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
+                                                                                                                        GDA_SQL_OPERATOR_TYPE_NOTLIKE);
                                                                }
-                                                       else if (g_strcmp0 (prop, "end") == 0)
+                                                       else if (g_strcmp0 (prop, "ilike") == 0)
                                                                {
-                                                                       zak_form_gdaex_element_extension_set_where_like_type ((ZakFormGdaExElementExtension *)extension, 1);
+                                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
+                                                                                                                        GDA_SQL_OPERATOR_TYPE_ILIKE);
                                                                }
-                                                       else
+                                                       else if (g_strcmp0 (prop, "nilike") == 0)
                                                                {
-                                                                       zak_form_gdaex_element_extension_set_where_like_type ((ZakFormGdaExElementExtension *)extension, 0);
+                                                                       zak_form_gdaex_element_extension_set_where_type ((ZakFormGdaExElementExtension *)extension,
+                                                                                                                        GDA_SQL_OPERATOR_TYPE_NOTILIKE);
                                                                }
+
+                                                       if (g_strcmp0 (prop, "like") == 0
+                                                           || g_strcmp0 (prop, "nlike") == 0
+                                                           || g_strcmp0 (prop, "ilike") == 0
+                                                           || g_strcmp0 (prop, "nilike") == 0)
+                                                               {
+                                                                       prop = xmlGetProp (xnodetmp, (const xmlChar *)"like");
+                                                                       if (g_strcmp0 (prop, "start") == 0)
+                                                                               {
+                                                                                       zak_form_gdaex_element_extension_set_where_like_type ((ZakFormGdaExElementExtension *)extension, -1);
+                                                                               }
+                                                                       else if (g_strcmp0 (prop, "end") == 0)
+                                                                               {
+                                                                                       zak_form_gdaex_element_extension_set_where_like_type ((ZakFormGdaExElementExtension *)extension, 1);
+                                                                               }
+                                                                       else
+                                                                               {
+                                                                                       zak_form_gdaex_element_extension_set_where_like_type ((ZakFormGdaExElementExtension *)extension, 0);
+                                                                               }
+                                                               }
+
+                                                       zak_form_gdaex_element_extension_set_str ((ZakFormGdaExElementExtension *)extension, NULL);
+                                               }
+                                       else
+                                               {
+                                                       zak_form_gdaex_element_extension_set_str ((ZakFormGdaExElementExtension *)extension, (gchar *)xmlNodeGetContent (xnodetmp));
                                                }
                                }
 
@@ -391,6 +402,51 @@ zak_form_gdaex_element_extension_set_where_like_type (ZakFormGdaExElementExtensi
        priv->like_type = type;
 }
 
+/**
+ * zak_form_gdaex_element_extension_get_str:
+ * @extension:
+ *
+ * Returns: the where string.
+ */
+gchar
+*zak_form_gdaex_element_extension_get_str (ZakFormGdaExElementExtension *extension)
+{
+       ZakFormGdaExElementExtensionPrivate *priv = zak_form_gdaex_element_extension_get_instance_private (extension);
+
+       if (priv->str != NULL)
+               {
+                       return g_strdup (priv->str);
+               }
+       else
+               {
+                       return NULL;
+               }
+}
+
+/**
+ * zak_form_gdaex_element_extension_set_str:
+ * @extension:
+ * @str:
+ */
+void
+zak_form_gdaex_element_extension_set_str (ZakFormGdaExElementExtension *extension, const gchar *str)
+{
+       ZakFormGdaExElementExtensionPrivate *priv = zak_form_gdaex_element_extension_get_instance_private (extension);
+
+       if (str == NULL)
+               {
+                       if (priv->str != NULL)
+                               {
+                                       g_free (priv->str);
+                               }
+                       priv->str = NULL;
+               }
+       else
+               {
+                       priv->str = g_strdup (str);
+               }
+}
+
 /* PRIVATE */
 static void
 zak_form_gdaex_element_extension_set_property (GObject *object,
index dcc827e15fd40896ec614332cde806f11f719aec..6774fa967062fcd25425dc83795631d77a080fa0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2017-2018 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
@@ -59,6 +59,9 @@ void zak_form_gdaex_element_extension_set_where_type (ZakFormGdaExElementExtensi
 gint zak_form_gdaex_element_extension_get_where_like_type (ZakFormGdaExElementExtension *extension);
 void zak_form_gdaex_element_extension_set_where_like_type (ZakFormGdaExElementExtension *extension, gint where_like_type);
 
+gchar *zak_form_gdaex_element_extension_get_str (ZakFormGdaExElementExtension *extension);
+void zak_form_gdaex_element_extension_set_str (ZakFormGdaExElementExtension *extension, const gchar *str);
+
 
 G_END_DECLS