]> saetta.ns0.it Git - libgdaex/commitdiff
Ended implementation of tables name prefix. 0.2.1
authorAndrea Zagli <azagli@libero.it>
Wed, 25 Aug 2010 16:15:04 +0000 (18:15 +0200)
committerAndrea Zagli <azagli@libero.it>
Wed, 25 Aug 2010 16:15:04 +0000 (18:15 +0200)
Tagged as 0.2.1.

.gitignore
Makefile.am
configure.ac
src/gdaex.c
tests/Makefile.am [new file with mode: 0644]
tests/test_prefix.c [new file with mode: 0644]
tests/test_prefix.db [new file with mode: 0644]

index 7f42ddded7df97a766199babdb28b1b2d32234a4..ace013a13dbc2bf7816c21f41d8c7773b3e583f2 100644 (file)
@@ -19,14 +19,17 @@ libtool
 ltmain.sh
 m4/
 missing
-src/.deps/
 stamp-h1
 *.stamp
 docs/reference/html/
 docs/reference/xml/
 src/.libs/
+src/.deps/
+tests/.libs/
+tests/.deps/
 *.lo
 *.o
 *.la
 *.bak
 libgdaex*tar*
+tests/test_prefix
index 77e1b69c3bd72ab5539a20d8938780fe753eddaa..d0a7c6fc1e68ac81326c874fe20a6cdcee943efb 100644 (file)
@@ -1,6 +1,6 @@
 DISTCHECK_CONFIGURE_FLAGS = --enable-gtk-doc
 
-SUBDIRS = src docs
+SUBDIRS = src docs tests
 
 ACLOCAL_AMFLAGS = -I m4
 
index 37d7f910c4354cbf0c374aacba93920f90334a46..572e5f18fadc925d22ed22ba52a545307c0d43af 100644 (file)
@@ -49,5 +49,6 @@ AC_CONFIG_FILES([
   docs/Makefile
   docs/reference/Makefile
   docs/reference/version.xml
+  tests/Makefile
 ])
 AC_OUTPUT
index 9bd70a53041d354bb23ce8fddf2a6a1b1d2c90c9..10e83c31dd2c2ed0891713f58d56ff99cddcb1ba 100644 (file)
@@ -362,6 +362,141 @@ gdaex_set_tables_name_prefix (GdaEx *gdaex, const gchar *tables_name_prefix)
                }
 }
 
+static void
+gdaex_set_tables_name_prefix_into_statement (GdaEx *gdaex, GdaStatement **stmt)
+{
+       GdaStatement *stmp;
+       GdaSqlStatement *sstmt;
+
+       GdaExPrivate *priv = GDAEX_GET_PRIVATE (gdaex);
+
+       stmp = *stmt;
+       g_object_get (G_OBJECT (stmp), "structure", &sstmt, NULL);
+       if (sstmt == NULL)
+               {
+                       g_warning ("Unable to get the GdaSqlStatement from the GdaStatement.");
+                       return;
+               }
+
+       switch (sstmt->stmt_type)
+               {
+                       case GDA_SQL_STATEMENT_SELECT:
+                               {
+                                       GdaSqlStatementSelect *select;
+                                       GSList *tables;
+                                       GSList *joins;
+                                       GdaSqlSelectTarget *target;
+                                       GdaSqlExpr *expr;
+                                       GSList *operands;
+                                       gchar **splits;
+                                       gchar *field_name;
+                                       gchar *tables_names;
+
+                                       select = (GdaSqlStatementSelect *)sstmt->contents;
+
+                                       tables_names = g_strdup ("");
+                                       tables = NULL;
+                                       tables = select->from->targets;
+                                       while (tables != NULL)
+                                               {
+                                                       target = (GdaSqlSelectTarget *)tables->data;
+                                                       if (g_ascii_strncasecmp (g_value_get_string (target->expr->value), priv->tables_name_prefix, strlen (priv->tables_name_prefix)) != 0)
+                                                               {
+                                                                       g_value_set_string (target->expr->value,
+                                                                                                   g_strdup_printf ("%s%s",
+                                                                                                                    priv->tables_name_prefix,
+                                                                                                                    g_value_get_string (target->expr->value)));
+                                                               }
+                                                       tables_names = g_strconcat (tables_names, g_value_get_string (target->expr->value), "|", NULL);
+                                                       tables = g_slist_next (tables);
+                                               }
+
+                                       joins = NULL;
+                                       joins = select->from->joins;
+                                       while (joins != NULL)
+                                               {
+                                                       expr = ((GdaSqlSelectJoin *)joins->data)->expr;
+                                                       operands = ((GdaSqlOperation *)expr->cond)->operands;
+                                                       while (operands != NULL)
+                                                               {
+                                                                       splits = gda_sql_identifier_split (g_value_get_string (((GdaSqlExpr *)operands->data)->value));
+                                                                       if (g_strv_length (splits) > 1)
+                                                                               {
+                                                                                       if (g_strrstr (tables_names, g_strdup_printf ("%s|", splits[0])) != NULL
+                                                                                           && g_ascii_strncasecmp (splits[0], priv->tables_name_prefix, strlen (priv->tables_name_prefix)) != 0)
+                                                                                               {
+                                                                                                       field_name = g_strdup_printf ("%s%s",
+                                                                                                                                             priv->tables_name_prefix,
+                                                                                                                                             g_value_get_string (((GdaSqlExpr *)operands->data)->value));
+                                                                                               }
+                                                                                       else
+                                                                                               {
+                                                                                                       field_name = g_strdup (g_value_get_string (((GdaSqlExpr *)operands->data)->value));
+                                                                                               }
+                                                                               }
+                                                                       else
+                                                                               {
+                                                                                       field_name = g_strdup (splits[0]);
+                                                                               }
+
+                                                                       g_value_set_string (((GdaSqlExpr *)operands->data)->value, field_name);
+                                                                       g_free (field_name);
+                                                                       operands = g_slist_next (operands);
+                                                               }
+
+                                                       joins = g_slist_next (joins);
+                                               }
+                               }
+                               break;
+
+                       case GDA_SQL_STATEMENT_INSERT:
+                               {
+                                       GdaSqlStatementInsert *insert;
+                                       GValue *gval;
+
+                                       insert = (GdaSqlStatementInsert *)sstmt->contents;
+                                       gval = gda_value_new_from_string (g_strdup_printf ("%s%s",
+                                                                                          priv->tables_name_prefix,
+                                                                                          insert->table->table_name),
+                                                                         G_TYPE_STRING);
+                                       gda_sql_statement_insert_take_table_name (sstmt, gval);
+                               }
+                               break;
+
+                       case GDA_SQL_STATEMENT_UPDATE:
+                               {
+                                       GdaSqlStatementUpdate *update;
+                                       GValue *gval;
+
+                                       update = (GdaSqlStatementUpdate *)sstmt->contents;
+                                       gval = gda_value_new_from_string (g_strdup_printf ("%s%s", priv->tables_name_prefix, update->table->table_name),
+                                                                         G_TYPE_STRING);
+                                       gda_sql_statement_update_take_table_name (sstmt, gval);
+                               }
+                               break;
+
+                       case GDA_SQL_STATEMENT_DELETE:
+                               {
+                                       GdaSqlStatementDelete *delete;
+                                       GValue *gval;
+
+                                       delete = (GdaSqlStatementDelete *)sstmt->contents;
+                                       gval = gda_value_new_from_string (g_strdup_printf ("%s%s", priv->tables_name_prefix, delete->table->table_name),
+                                                                         G_TYPE_STRING);
+                                       gda_sql_statement_delete_take_table_name (sstmt, gval);
+                               }
+                               break;
+
+                       default:
+                               g_warning ("Statement type %s not implemented.",
+                                          gda_sql_statement_type_to_string (sstmt->stmt_type));
+                               return;
+               }
+
+       g_object_set (G_OBJECT (stmp), "structure", sstmt, NULL);
+       g_free (sstmt);
+}
+
 /**
  * gdaex_query:
  * @gdaex: a #GdaEx object.
@@ -390,6 +525,8 @@ GdaDataModel
                        return NULL;
                }
 
+       gdaex_set_tables_name_prefix_into_statement (gdaex, &stmt);
+
        error = NULL;
        GdaDataModel *dm = gda_connection_statement_execute_select (priv->gda_conn, stmt, NULL, &error);
        if (!GDA_IS_DATA_MODEL (dm))
@@ -1103,6 +1240,8 @@ gdaex_execute (GdaEx *gdaex, const gchar *sql)
 
        g_signal_emit (gdaex, klass->before_execute_signal_id, 0, stmt);
 
+       gdaex_set_tables_name_prefix_into_statement (gdaex, &stmt);
+
        error = NULL;
        nrecs = gda_connection_statement_execute_non_select (priv->gda_conn, stmt, NULL, NULL, &error);
 
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644 (file)
index 0000000..403f842
--- /dev/null
@@ -0,0 +1,12 @@
+LIBS = $(GDAEX_LIBS) \
+       -export-dynamic
+
+AM_CPPFLAGS = $(GDAEX_CFLAGS) \
+              -I$(top_srcdir)/src \
+              -DTESTSDIR="\"@abs_builddir@\""
+
+noinst_PROGRAMS = test_prefix
+
+LDADD = $(top_builddir)/src/libgdaex.la
+
+EXTRA_DIST = test_prefix.db
diff --git a/tests/test_prefix.c b/tests/test_prefix.c
new file mode 100644 (file)
index 0000000..0772088
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2010 Andrea Zagli <azagli@libero.it>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "libgdaex.h"
+
+int
+main (int argc, char **argv)
+{
+       GdaEx *gdaex;
+
+       g_type_init ();
+
+       gdaex = gdaex_new_from_string (g_strdup_printf ("SQLite://DB_DIR=%s;DB_NAME=test_prefix.db", TESTSDIR));
+
+       gdaex_set_tables_name_prefix (gdaex, "paperinik_");
+
+       gdaex_execute (gdaex, "INSERT INTO table1 (id) VALUES (1)");
+
+       gdaex_execute (gdaex, "UPDATE table1 SET id = 1 WHERE id = 1");
+
+       gdaex_execute (gdaex, "DELETE FROM table1 WHERE id = 1");
+
+       gdaex_query (gdaex, "SELECT * FROM table1 WHERE id = 1");
+
+       gdaex_query (gdaex, "SELECT * FROM paperinik_table1 WHERE id = 1");
+
+       gdaex_query (gdaex, "SELECT * FROM table_sel1 INNER JOIN table_sel2 ON table_sel1.id = table_sel2.id2");
+
+       gdaex_query (gdaex, "SELECT * FROM table_sel1 AS t1 INNER JOIN table_sel2 ON t1.id = table_sel2.id2");
+
+       return 0;
+}
diff --git a/tests/test_prefix.db b/tests/test_prefix.db
new file mode 100644 (file)
index 0000000..fe82559
Binary files /dev/null and b/tests/test_prefix.db differ