]> saetta.ns0.it Git - libgdaex/commitdiff
Added macro ZQ_SELECT, ZQ_UPDATE, ZQ_DELETE (WIP).
authorAndrea Zagli <azagli@libero.it>
Sat, 26 Sep 2020 08:06:21 +0000 (10:06 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 26 Sep 2020 08:06:21 +0000 (10:06 +0200)
.gitignore
src/sqlbuilder.h
tests/Makefile.am
tests/sqlbuilder_macro.c [new file with mode: 0644]

index dc1a5f7182385c79dbc2d1885790a20c11ba0b3f..f64b833300ad9df9402068dc707f5100f8c59ef0 100644 (file)
@@ -58,6 +58,7 @@ tests/getsql
 tests/getsqlfromhashtable
 tests/getsqlfromjson
 tests/sqlbuilder
+tests/sqlbuilder_macro
 tests/metastore
 *~
 *.gir
index 39145be0ecd6bf9e494d1c3a7710058b7178648d..b6cefc155a974863ee0aca94ef7f5a0521b76112 100644 (file)
@@ -116,6 +116,25 @@ GdaDataModel *gdaex_sql_builder_query (GdaExSqlBuilder *sqlb, GdaEx *gdaex, GdaS
 gint gdaex_sql_builder_execute  (GdaExSqlBuilder *sqlb, GdaEx *gdaex, GdaSet *params);
 
 
+#define ZQ_SELECT(builder_name, ...) \
+       GdaExSqlBuilder *builder_name; \
+       builder_name = gdaex_sql_builder_new (GDA_SQL_STATEMENT_SELECT); \
+       gdaex_sql_builder_fields (builder_name, __VA_ARGS__);
+
+
+#define ZQ_UPDATE(builder_name, table_name, ...)         \
+       GdaExSqlBuilder *builder_name; \
+       builder_name = gdaex_sql_builder_new (GDA_SQL_STATEMENT_UPDATE); \
+       gdaex_sql_builder_from (builder_name, #table_name, ""); \
+       gdaex_sql_builder_fields (builder_name, __VA_ARGS__);
+
+
+#define ZQ_DELETE(builder_name, table_name)      \
+       GdaExSqlBuilder *builder_name; \
+       builder_name = gdaex_sql_builder_new (GDA_SQL_STATEMENT_DELETE); \
+       gdaex_sql_builder_from (builder_name, #table_name, ""); \
+
+
 G_END_DECLS
 
 #endif /* __GDAEX_SQLBUILDER_H__ */
index cfd672fb793bd72f93ed6b5b842a80c65dc2fd7a..5c9b16b91152fc11894feb38dc0e0887111a320e 100644 (file)
@@ -16,6 +16,7 @@ noinst_PROGRAMS = \
                   query_editor \
                   select \
                   sqlbuilder \
+                  sqlbuilder_macro \
                   test_prefix
 
 LDADD = $(top_builddir)/src/libgdaex.la
diff --git a/tests/sqlbuilder_macro.c b/tests/sqlbuilder_macro.c
new file mode 100644 (file)
index 0000000..1a98034
--- /dev/null
@@ -0,0 +1,264 @@
+/*
+ * Copyright (C) 2015-2019 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)
+{
+       GValue *gval;
+
+       gtk_init (&argc, &argv);
+
+       ZQ_SELECT (sqlb,
+                  "pippo", "id", "", NULL,
+                  "pippo", "name", "the_name", NULL,
+                  NULL);
+
+       gdaex_sql_builder_from_v (sqlb,
+                                                         "pippo", "pluto",
+                                                         "topolino", "",
+                                                         NULL);
+
+       gdaex_sql_builder_field (sqlb, "pippo", "income", NULL, NULL);
+
+       gdaex_sql_builder_join (sqlb, GDA_SQL_SELECT_JOIN_INNER,
+                                                       "pippo", "id", "",
+                                                       GDA_SQL_OPERATOR_TYPE_EQ,
+                                                       "topolino", "id_pippo", "",
+                                                       NULL);
+
+       gval = g_new0 (GValue, 1);
+       g_value_init (gval, G_TYPE_INT);
+       g_value_set_int (gval, 44);
+       gdaex_sql_builder_where (sqlb, 0,
+                                                        "pippo", "id", "",
+                                                        GDA_SQL_OPERATOR_TYPE_EQ,
+                                                        gval,
+                                                        NULL);
+       g_value_unset (gval);
+
+       gval = g_new0 (GValue, 1);
+       g_value_init (gval, G_TYPE_STRING);
+       g_value_set_string (gval, "ichichc");
+       gdaex_sql_builder_where (sqlb, GDA_SQL_OPERATOR_TYPE_OR,
+                                                        "pippo", "name", "",
+                                                        GDA_SQL_OPERATOR_TYPE_EQ,
+                                                        gval,
+                                                        NULL);
+       g_value_unset (gval);
+
+       gdaex_sql_builder_order (sqlb,
+                                                        "pippo", "name", "", TRUE,
+                                                        "pippo", "id", "", FALSE,
+                                                        NULL);
+
+       g_message ("sql: %s", gdaex_sql_builder_get_sql (sqlb, NULL, NULL));
+
+       sqlb = gdaex_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
+
+       gdaex_sql_builder_from_v (sqlb,
+                                 "pippo", "pluto",
+                                 "topolino", "",
+                                 NULL);
+
+       gdaex_sql_builder_fields (sqlb,
+                                 "pippo", "id", "", NULL,
+                                 "pippo", "name", "the_name", NULL,
+                                 NULL);
+
+       gdaex_sql_builder_field (sqlb, "pippo", "income", NULL, NULL);
+
+       gdaex_sql_builder_join (sqlb, GDA_SQL_SELECT_JOIN_INNER,
+                               "pippo", "id", "",
+                               GDA_SQL_OPERATOR_TYPE_EQ,
+                               "topolino", "id_pippo", "",
+                               NULL);
+
+       gdaex_sql_builder_where_int (sqlb, 0,
+                                    "pippo", "id", "",
+                                    GDA_SQL_OPERATOR_TYPE_EQ,
+                                    44, 0);
+
+       gdaex_sql_builder_where_string (sqlb, GDA_SQL_OPERATOR_TYPE_OR,
+                                "pippo", "name", "",
+                                GDA_SQL_OPERATOR_TYPE_DIFF,
+                                "ichichc", NULL);
+
+       gdaex_sql_builder_order (sqlb,
+                                "pippo", "name", "", TRUE,
+                                "pippo", "id", "", FALSE,
+                                NULL);
+
+       g_message ("sql: %s", gdaex_sql_builder_get_sql (sqlb, NULL, NULL));
+
+       g_object_unref (sqlb);
+
+       sqlb = gdaex_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
+
+       gdaex_sql_builder_from (sqlb, "pippo", "");
+
+       gdaex_sql_builder_fields (sqlb,
+                                                         "pippo", "id", "", NULL,
+                                                         "pippo", "name", "the_name", NULL,
+                                                         NULL);
+
+       gval = g_new0 (GValue, 1);
+       g_value_init (gval, G_TYPE_INT);
+       g_value_set_int (gval, 44);
+       gdaex_sql_builder_where (sqlb, 0,
+                                                        "pippo", "id", "",
+                                                        GDA_SQL_OPERATOR_TYPE_ISNULL,
+                                                        gval,
+                                                        NULL);
+       g_value_unset (gval);
+
+       gval = g_new0 (GValue, 1);
+       g_value_init (gval, G_TYPE_INT);
+       g_value_set_int (gval, 44);
+       GValue *gval2 = g_new0 (GValue, 1);
+       g_value_init (gval2, G_TYPE_INT);
+       g_value_set_int (gval2, 8877);
+       gdaex_sql_builder_where (sqlb, 0,
+                                                        "pippo", "id", "",
+                                                        GDA_SQL_OPERATOR_TYPE_BETWEEN,
+                                                        gval, gval2,
+                                                        NULL);
+       g_value_unset (gval);
+       g_value_unset (gval2);
+
+       g_message ("sql: %s", gdaex_sql_builder_get_sql (sqlb, NULL, NULL));
+
+       g_object_unref (sqlb);
+
+       sqlb = gdaex_sql_builder_new (GDA_SQL_STATEMENT_DELETE);
+
+       gdaex_sql_builder_from_v (sqlb,
+                                                         "pippo", "pluto",
+                                                         "topolino", "",
+                                                         NULL);
+
+       gval = g_new0 (GValue, 1);
+       g_value_init (gval, G_TYPE_INT);
+       g_value_set_int (gval, 1);
+       gdaex_sql_builder_where (sqlb, 0,
+                                                        "pippo", "id", "",
+                                                        GDA_SQL_OPERATOR_TYPE_EQ,
+                                                        gval,
+                                                        NULL);
+       g_value_unset (gval);
+
+       g_message ("sql: %s", gdaex_sql_builder_get_sql (sqlb, NULL, NULL));
+
+       g_object_unref (sqlb);
+
+       ZQ_DELETE (sqlb3, pippo);
+
+       gdaex_sql_builder_where_int (sqlb3, 0,
+                                    "pippo", "id", "",
+                                    GDA_SQL_OPERATOR_TYPE_EQ,
+                                    1, 0);
+
+       gdaex_sql_builder_where_int (sqlb3, GDA_SQL_OPERATOR_TYPE_OR,
+                                    "pippo", "id", "",
+                                    GDA_SQL_OPERATOR_TYPE_BETWEEN,
+                                    1, 99);
+
+       gdaex_sql_builder_where_boolean (sqlb3, 0,
+                                        "pippo", "married", "",
+                                        GDA_SQL_OPERATOR_TYPE_EQ,
+                                        TRUE, FALSE);
+
+       gdaex_sql_builder_where_float (sqlb3, 0,
+                                      "pippo", "income", "",
+                                      GDA_SQL_OPERATOR_TYPE_GEQ,
+                                      823.44, 0.0);
+
+       g_message ("sql: %s", gdaex_sql_builder_get_sql (sqlb3, NULL, NULL));
+
+       g_object_unref (sqlb3);
+
+       gval = g_new0 (GValue, 1);
+       g_value_init (gval, G_TYPE_STRING);
+       g_value_set_string (gval, "il nome di pippo");
+
+       ZQ_UPDATE (sqlb2, "pippo",
+                  "pippo", "name", "", gval,
+                  NULL);
+
+       g_value_unset (gval);
+
+       gval = g_new0 (GValue, 1);
+       g_value_init (gval, G_TYPE_INT);
+       g_value_set_int (gval, 1);
+       gdaex_sql_builder_where (sqlb2, 0,
+                                                        "pippo", "id", "",
+                                                        GDA_SQL_OPERATOR_TYPE_EQ,
+                                                        gval,
+                                                        NULL);
+       g_value_unset (gval);
+
+       g_message ("sql: %s", gdaex_sql_builder_get_sql (sqlb2, NULL, NULL));
+
+       g_object_unref (sqlb2);
+
+       sqlb = gdaex_sql_builder_new (GDA_SQL_STATEMENT_INSERT);
+
+       gdaex_sql_builder_from_v (sqlb,
+                                                         "pippo", "pluto",
+                                                         "topolino", "",
+                                                         NULL);
+
+       gval = g_new0 (GValue, 1);
+       g_value_init (gval, G_TYPE_STRING);
+       g_value_set_string (gval, "il nome di pippo");
+       gdaex_sql_builder_fields (sqlb,
+                                                         "pippo", "name", "", gval,
+                                                         NULL);
+
+       gval = g_new0 (GValue, 1);
+       g_value_init (gval, G_TYPE_INT);
+       g_value_set_int (gval, 1);
+       gdaex_sql_builder_where (sqlb, 0,
+                                                        "pippo", "id", "",
+                                                        GDA_SQL_OPERATOR_TYPE_EQ,
+                                                        gval,
+                                                        NULL);
+       g_value_unset (gval);
+
+       g_message ("sql: %s", gdaex_sql_builder_get_sql (sqlb, NULL, NULL));
+
+       g_object_unref (sqlb);
+
+       sqlb = gdaex_sql_builder_new (GDA_SQL_STATEMENT_INSERT);
+
+       gdaex_sql_builder_from_v (sqlb,
+                                 "pippo", "pluto",
+                                 NULL);
+
+       gdaex_sql_builder_field_boolean (sqlb, "pippo", "married", "", FALSE);
+       gdaex_sql_builder_field_int (sqlb, "pippo", "id", "", 44);
+       gdaex_sql_builder_field_string (sqlb, "pippo", "named", "", "the field value'");
+       gdaex_sql_builder_field_float (sqlb, "pippo", "income", "", 4128.65);
+
+       g_message ("sql: %s", gdaex_sql_builder_get_sql (sqlb, NULL, NULL));
+
+       g_object_unref (sqlb);
+
+       return 0;
+}