]> saetta.ns0.it Git - libgdaex/commitdiff
Added GdaExSqlBuilder::fields.
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Fri, 26 Jun 2015 09:07:46 +0000 (11:07 +0200)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Fri, 26 Jun 2015 09:07:46 +0000 (11:07 +0200)
src/sqlbuilder.c
src/sqlbuilder.h
tests/sqlbuilder.c

index d2ebd934fbc827c0e03ef5379b6e24e0b109056a..714958f695ce56c1f067e60e0be76fc291dee25a 100644 (file)
@@ -25,6 +25,8 @@
        #include <config.h>
 #endif
 
+#include <stdarg.h>
+
 #include <glib/gi18n-lib.h>
 
 #include "sqlbuilder.h"
@@ -46,6 +48,15 @@ static void gdaex_sql_builder_get_property (GObject *object,
 
 typedef struct _GdaExSqlBuilderTable GdaExSqlBuilderTable;
 struct _GdaExSqlBuilderTable
+{
+  guint id;
+  gchar *name;
+  gchar *alias;
+  GHashTable *ht_fields;
+};
+
+typedef struct _GdaExSqlBuilderField GdaExSqlBuilderField;
+struct _GdaExSqlBuilderField
 {
   guint id;
   gchar *name;
@@ -92,24 +103,105 @@ GdaExSqlBuilder
        return gdaex_sql_builder;
 }
 
-void
-gdaex_sql_builder_from (GdaExSqlBuilder *sqlb, const gchar *table_name, const gchar *table_alias)
+GdaExSqlBuilderField
+*gdaex_sql_builder_get_field (GdaExSqlBuilder *sqlb, GdaExSqlBuilderTable *table, const gchar *field_name, gboolean add)
+{
+       GdaExSqlBuilderPrivate *priv = GDAEX_SQLBUILDER_GET_PRIVATE (sqlb);
+
+       GdaExSqlBuilderField *f;
+
+       f = g_hash_table_lookup (table->ht_fields, field_name);
+  if (f == NULL && add)
+       {
+         f = g_new0 (GdaExSqlBuilderField, 1);
+         f->id = gda_sql_builder_select_add_field (priv->sqlb, field_name, table->name, NULL);
+         f->name = g_strdup (field_name);
+         f->alias = NULL;
+         g_hash_table_insert (table->ht_fields, g_strdup (field_name), f);
+       }
+  
+  return f;
+}
+
+GdaExSqlBuilderTable
+*gdaex_sql_builder_get_table (GdaExSqlBuilder *sqlb, const gchar *table_name, gboolean add)
 {
        GdaExSqlBuilderPrivate *priv = GDAEX_SQLBUILDER_GET_PRIVATE (sqlb);
 
        GdaExSqlBuilderTable *t;
 
        t = g_hash_table_lookup (priv->ht_tables, table_name);
-       if (t == NULL)
+       if (t == NULL && add)
          {
                t = g_new0 (GdaExSqlBuilderTable, 1);
+               t->id = gda_sql_builder_select_add_target_id (priv->sqlb, gda_sql_builder_add_id (priv->sqlb, table_name), NULL);
                t->name = g_strdup (table_name);
+               t->alias = NULL;
+               t->ht_fields = g_hash_table_new (g_str_hash, g_str_equal);
+               g_hash_table_insert (priv->ht_tables, g_strdup (t->name), t);
+         }
+
+       return t;
+}
+
+void
+gdaex_sql_builder_from (GdaExSqlBuilder *sqlb, const gchar *table_name, const gchar *table_alias)
+{
+       GdaExSqlBuilderPrivate *priv = GDAEX_SQLBUILDER_GET_PRIVATE (sqlb);
+
+       GdaExSqlBuilderTable *t;
+
+       t = gdaex_sql_builder_get_table (sqlb, table_name, TRUE);
+       if (t->alias != NULL)
+         {
+               g_free (t->alias);
                t->alias = g_strdup (table_alias);
-               t->id = gda_sql_builder_select_add_target_id (priv->sqlb, gda_sql_builder_add_id (priv->sqlb, table_name), NULL);
-               g_hash_table_insert (priv->ht_tables, t->name, t);
          }
 }
 
+void
+gdaex_sql_builder_fields (GdaExSqlBuilder *sqlb, ...)
+{
+  va_list ap;
+  
+       GdaExSqlBuilderPrivate *priv = GDAEX_SQLBUILDER_GET_PRIVATE (sqlb);
+
+       va_start (ap, sqlb);
+       do
+         {
+               gchar *table_name = va_arg (ap, gchar *);
+               if (table_name != NULL)
+                 {
+                       gchar *field_name = va_arg (ap, gchar *);
+                       if (field_name != NULL)
+                         {
+                               gchar *field_alias = va_arg (ap, gchar *);
+                               if (field_alias != NULL)
+                                 {
+                                       GdaExSqlBuilderTable *t = gdaex_sql_builder_get_table (sqlb, table_name, TRUE);
+                                       GdaExSqlBuilderField *f = gdaex_sql_builder_get_field (sqlb, t, field_name, TRUE);
+
+                                       gchar *_field_alias = g_strstrip (g_strdup (field_alias));
+                                       gda_sql_builder_select_add_field (priv->sqlb, field_name, table_name, _field_alias);
+                                       g_free (_field_alias);
+                                 }
+                               else
+                                 {
+                                       break;
+                                 }
+                         }
+                       else
+                         {
+                               break;
+                         }
+                 }
+               else
+                 {
+                       break;
+                 }
+         } while (TRUE);
+}
+
 GdaSqlBuilder
 *gdaex_sql_builder_get_gda_sql_builder (GdaExSqlBuilder *sqlb)
 {
@@ -128,7 +220,6 @@ const gchar
 
        ret = NULL;
 
-       gda_sql_builder_select_add_field (priv->sqlb, "*", NULL, NULL);
        stmt = gda_sql_builder_get_statement (priv->sqlb, NULL);
        if (stmt != NULL)
          {
index 6fc6a32acf11757e0b59ea0e8476795ddfdb08b0..fee2331d441bae11e9951b922dadb8f4f7984862 100644 (file)
@@ -59,6 +59,7 @@ GType gdaex_sql_builder_get_type (void) G_GNUC_CONST;
 GdaExSqlBuilder *gdaex_sql_builder_new (GdaSqlStatementType stmt_type);
 
 void gdaex_sql_builder_from (GdaExSqlBuilder *sqlb, const gchar *table_name, const gchar *table_alias);
+void gdaex_sql_builder_fields (GdaExSqlBuilder *sqlb, ...);
 
 GdaSqlBuilder *gdaex_sql_builder_get_gda_sql_builder (GdaExSqlBuilder *sqlb);
 const gchar *gdaex_sql_builder_get_sql (GdaExSqlBuilder *sqlb);
index 0ca035d3dce82d42d10d90ee1b9b067d43405154..8149b95d4326f89c209eeb8aa6eacfb4ef73ba96 100644 (file)
@@ -27,6 +27,10 @@ main (int argc, char **argv)
 
        sqlb = gdaex_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
        gdaex_sql_builder_from (sqlb, "pippo", "pluto");
+       gdaex_sql_builder_fields (sqlb,
+                                                         "pippo", "id", "",
+                                                         "pippo", "name", "the_name",
+                                                         NULL);
 
        g_message ("sql: %s", gdaex_sql_builder_get_sql (sqlb));