]> saetta.ns0.it Git - libgdaex/commitdiff
SqlBuilder: added support for UPDATE and INSERT.
authorAndrea Zagli <azagli@libero.it>
Tue, 14 Jul 2015 08:11:07 +0000 (10:11 +0200)
committerAndrea Zagli <azagli@libero.it>
Tue, 14 Jul 2015 08:11:07 +0000 (10:11 +0200)
src/sqlbuilder.c
src/sqlbuilder.h
tests/sqlbuilder.c

index bcf437fe3a4d49f35ac38e31559ff958de7bcae3..efecbed29142556d310672f2cd51abb95ec4ef8f 100644 (file)
@@ -108,7 +108,7 @@ GdaExSqlBuilder
 }
 
 static GdaExSqlBuilderField
-*gdaex_sql_builder_get_field (GdaExSqlBuilder *sqlb, GdaExSqlBuilderTable *table, const gchar *field_name, const gchar *field_alias, gboolean add)
+*gdaex_sql_builder_get_field (GdaExSqlBuilder *sqlb, GdaExSqlBuilderTable *table, const gchar *field_name, const gchar *field_alias, GValue *gval, gboolean add)
 {
        GdaExSqlBuilderPrivate *priv = GDAEX_SQLBUILDER_GET_PRIVATE (sqlb);
 
@@ -125,6 +125,10 @@ static GdaExSqlBuilderField
                        else
                                {
                                        f->id = gda_sql_builder_add_id (priv->sqlb, g_strcmp0 (field_alias, "") != 0 ? field_alias : field_name);
+                                       if (gval != NULL)
+                                               {
+                                                       gda_sql_builder_add_field_value_as_gvalue (priv->sqlb, g_strcmp0 (field_alias, "") != 0 ? field_alias : field_name, gval);
+                                               }
                                }
                        f->name = g_strdup (field_name);
                        if (field_alias != NULL)
@@ -177,6 +181,12 @@ static GdaExSqlBuilderTable
        return t;
 }
 
+/**
+ * gdaex_sql_builder_from:
+ * @sqlb: a #GdaExSqlBuilder object.
+ * @table_name: the table's name.
+ * @table_alias: the table's alias.
+ */
 void
 gdaex_sql_builder_from (GdaExSqlBuilder *sqlb, const gchar *table_name, const gchar *table_alias)
 {
@@ -271,7 +281,7 @@ gdaex_sql_builder_join (GdaExSqlBuilder *sqlb,
                                                        if (field_alias_left != NULL)
                                                                {
                                                                        t_left = gdaex_sql_builder_get_table (sqlb, field_table_name_left, NULL, TRUE);
-                                                                       f_left = gdaex_sql_builder_get_field (sqlb, t_left, field_name_left, field_alias_left, TRUE);
+                                                                       f_left = gdaex_sql_builder_get_field (sqlb, t_left, field_name_left, field_alias_left, NULL, TRUE);
                                                                }
                                                        else
                                                                {
@@ -300,7 +310,7 @@ gdaex_sql_builder_join (GdaExSqlBuilder *sqlb,
                                                        if (field_alias_right != NULL)
                                                                {
                                                                        t_right = gdaex_sql_builder_get_table (sqlb, field_table_name_right, NULL, TRUE);
-                                                                       f_right = gdaex_sql_builder_get_field (sqlb, t_right, field_name_right, field_alias_right, TRUE);
+                                                                       f_right = gdaex_sql_builder_get_field (sqlb, t_right, field_name_right, field_alias_right, NULL, TRUE);
                                                                }
                                                        else
                                                                {
@@ -327,23 +337,37 @@ void
 gdaex_sql_builder_fields (GdaExSqlBuilder *sqlb, ...)
 {
        va_list ap;
-  
+
+       gchar *table_name;
+       gchar *field_name;
+       gchar *field_alias;
+       GValue *gval;
+
+       GdaExSqlBuilderTable *t;
+       
        GdaExSqlBuilderPrivate *priv = GDAEX_SQLBUILDER_GET_PRIVATE (sqlb);
 
        va_start (ap, sqlb);
        do
                {
-                       gchar *table_name = va_arg (ap, gchar *);
+                       table_name = NULL;
+                       field_name = NULL;
+                       field_alias = NULL;
+                       gval = NULL;
+                       
+                       table_name = va_arg (ap, gchar *);
                        if (table_name != NULL)
                                {
-                                       gchar *field_name = va_arg (ap, gchar *);
+                                   field_name = va_arg (ap, gchar *);
                                        if (field_name != NULL)
                                                {
-                                                       gchar *field_alias = va_arg (ap, gchar *);
+                                                       field_alias = va_arg (ap, gchar *);
                                                        if (field_alias != NULL)
                                                                {
-                                                                       GdaExSqlBuilderTable *t = gdaex_sql_builder_get_table (sqlb, table_name, NULL, TRUE);
-                                                                       GdaExSqlBuilderField *f = gdaex_sql_builder_get_field (sqlb, t, field_name, field_alias, TRUE);
+                                                                       gval = va_arg (ap, GValue *);
+                                                                       
+                                                                       t = gdaex_sql_builder_get_table (sqlb, table_name, NULL, TRUE);
+                                                                   gdaex_sql_builder_get_field (sqlb, t, field_name, field_alias, gval, TRUE);
                                                                }
                                                        else
                                                                {
@@ -363,7 +387,7 @@ gdaex_sql_builder_fields (GdaExSqlBuilder *sqlb, ...)
        va_end (ap);
 }
 
-void
+GdaSqlBuilderId
 gdaex_sql_builder_where (GdaExSqlBuilder *sqlb, GdaSqlOperatorType op, ...)
 {
        va_list ap;
@@ -383,6 +407,11 @@ gdaex_sql_builder_where (GdaExSqlBuilder *sqlb, GdaSqlOperatorType op, ...)
                
        GdaExSqlBuilderPrivate *priv = GDAEX_SQLBUILDER_GET_PRIVATE (sqlb);
 
+       if (priv->stmt_type == GDA_SQL_STATEMENT_INSERT)
+               {
+                       return;
+               }
+       
        va_start (ap, op);
        do
                {
@@ -396,7 +425,7 @@ gdaex_sql_builder_where (GdaExSqlBuilder *sqlb, GdaSqlOperatorType op, ...)
                                                        if (field_alias != NULL)
                                                                {
                                                                        t = gdaex_sql_builder_get_table (sqlb, table_name, NULL, TRUE);
-                                                                       f = gdaex_sql_builder_get_field (sqlb, t, field_name, field_alias, TRUE);
+                                                                       f = gdaex_sql_builder_get_field (sqlb, t, field_name, field_alias, NULL, TRUE);
                                                                }
                                                        else
                                                                {
@@ -437,6 +466,8 @@ gdaex_sql_builder_where (GdaExSqlBuilder *sqlb, GdaSqlOperatorType op, ...)
                        gda_sql_builder_set_where (priv->sqlb, priv->id_where);
                } while (TRUE);
        va_end (ap);
+
+       return priv->id_where;
 }
 
 GdaSqlBuilder
index 1276d60f28964d133f34a3a877c087b18fd88597..aaa5b5c29ea8fff58c5d233ea3bdf207006c6400 100644 (file)
@@ -67,8 +67,8 @@ void gdaex_sql_builder_join (GdaExSqlBuilder *sqlb,
 
 void gdaex_sql_builder_fields (GdaExSqlBuilder *sqlb, ...);
 
-void gdaex_sql_builder_where (GdaExSqlBuilder *sqln, GdaSqlOperatorType op,
-                                                         ...);
+GdaSqlBuilderId gdaex_sql_builder_where (GdaExSqlBuilder *sqln, GdaSqlOperatorType op,
+                                                                                ...);
 
 GdaSqlBuilder *gdaex_sql_builder_get_gda_sql_builder (GdaExSqlBuilder *sqlb);
 const gchar *gdaex_sql_builder_get_sql (GdaExSqlBuilder *sqlb, GdaConnection *cnc, GdaSet *params);
index 17ee0ccfc64150c211e728c7a932774d9419ebd8..6841221a2df6efbba3b990242c5b121d0402c8d8 100644 (file)
@@ -34,8 +34,8 @@ main (int argc, char **argv)
                                                          NULL);
 
        gdaex_sql_builder_fields (sqlb,
-                                                         "pippo", "id", "",
-                                                         "pippo", "name", "the_name",
+                                                         "pippo", "id", "", NULL,
+                                                         "pippo", "name", "the_name", NULL,
                                                          NULL);
        
        gdaex_sql_builder_join (sqlb, GDA_SQL_SELECT_JOIN_INNER,
@@ -89,5 +89,61 @@ main (int argc, char **argv)
 
        g_object_unref (sqlb);
 
+       sqlb = gdaex_sql_builder_new (GDA_SQL_STATEMENT_UPDATE);
+
+       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",
+                                                         "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);
+
        return 0;
 }