]> saetta.ns0.it Git - libgdaex/commitdiff
SqlBuilder: added ::order (closes #940).
authorAndrea Zagli <azagli@libero.it>
Wed, 22 Jul 2015 14:52:16 +0000 (16:52 +0200)
committerAndrea Zagli <azagli@libero.it>
Wed, 22 Jul 2015 14:52:16 +0000 (16:52 +0200)
src/sqlbuilder.c
src/sqlbuilder.h
tests/sqlbuilder.c

index 975e6d161833c6f0ef822ff4440cf98cffa7e0ca..dc2476346d24a0b3b95a1722d295b7b7ae7257f7 100644 (file)
@@ -4,17 +4,17 @@
  *  Copyright (C) 2010-2014 Andrea Zagli <azagli@libero.it>
  *
  *  This file is part of libgdaex.
- *  
+ *
  *  libgdaex_sql_builder 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.
- *  
+ *
  *  libgdaex_sql_builder 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 libgdaex; if not, write to the Free Software
  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
@@ -147,7 +147,7 @@ static GdaExSqlBuilderField
                                }
                        g_hash_table_insert (table->ht_fields, g_strdup (field_name), f);
                }
-  
+
        return f;
 }
 
@@ -211,7 +211,7 @@ gdaex_sql_builder_from_v (GdaExSqlBuilder *sqlb, ...)
 
        gchar *table_name;
        gchar *table_alias;
-       
+
        GdaExSqlBuilderPrivate *priv = GDAEX_SQLBUILDER_GET_PRIVATE (sqlb);
 
        va_start (ap, sqlb);
@@ -268,7 +268,7 @@ gdaex_sql_builder_join (GdaExSqlBuilder *sqlb,
 
        gchar *tmp;
        GdaSqlOperatorType op;
-               
+
        GdaExSqlBuilderPrivate *priv = GDAEX_SQLBUILDER_GET_PRIVATE (sqlb);
 
        if (priv->stmt_type != GDA_SQL_STATEMENT_SELECT) return;
@@ -282,7 +282,7 @@ gdaex_sql_builder_join (GdaExSqlBuilder *sqlb,
                        field_table_name_right = NULL;
                        field_name_right = NULL;
                        field_alias_right = NULL;
-                       
+
                        field_table_name_left = va_arg (ap, gchar *);
                        if (field_table_name_left != NULL)
                                {
@@ -311,7 +311,7 @@ gdaex_sql_builder_join (GdaExSqlBuilder *sqlb,
                                }
 
                        op = va_arg (ap, guint);
-               
+
                        field_table_name_right = va_arg (ap, gchar *);
                        if (field_table_name_right != NULL)
                                {
@@ -361,7 +361,7 @@ gdaex_sql_builder_fields (GdaExSqlBuilder *sqlb, ...)
        GValue *gval;
 
        GdaExSqlBuilderTable *t;
-       
+
        GdaExSqlBuilderPrivate *priv = GDAEX_SQLBUILDER_GET_PRIVATE (sqlb);
 
        va_start (ap, sqlb);
@@ -371,7 +371,7 @@ gdaex_sql_builder_fields (GdaExSqlBuilder *sqlb, ...)
                        field_name = NULL;
                        field_alias = NULL;
                        gval = NULL;
-                       
+
                        table_name = va_arg (ap, gchar *);
                        if (table_name != NULL)
                                {
@@ -382,7 +382,7 @@ gdaex_sql_builder_fields (GdaExSqlBuilder *sqlb, ...)
                                                        if (field_alias != NULL)
                                                                {
                                                                        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);
                                                                }
@@ -429,14 +429,14 @@ gdaex_sql_builder_where (GdaExSqlBuilder *sqlb, GdaSqlOperatorType op, ...)
 
        GdaSqlBuilderId id_expr;
        GdaSqlBuilderId id_cond;
-               
+
        GdaExSqlBuilderPrivate *priv = GDAEX_SQLBUILDER_GET_PRIVATE (sqlb);
 
        if (priv->stmt_type == GDA_SQL_STATEMENT_INSERT)
                {
                        return;
                }
-       
+
        va_start (ap, op);
        do
                {
@@ -468,7 +468,7 @@ gdaex_sql_builder_where (GdaExSqlBuilder *sqlb, GdaSqlOperatorType op, ...)
                                }
 
                        op_expr = va_arg (ap, guint);
-               
+
                        gval = va_arg (ap, GValue *);
                        if (gval != NULL)
                                {
@@ -495,6 +495,66 @@ gdaex_sql_builder_where (GdaExSqlBuilder *sqlb, GdaSqlOperatorType op, ...)
        return priv->id_where;
 }
 
+/**
+ * gdaex_sql_builder_order:
+ * @sqlb:
+ * @...:
+ */
+void
+gdaex_sql_builder_order (GdaExSqlBuilder *sqlb, ...)
+{
+       va_list ap;
+
+       gchar *table_name;
+       gchar *field_name;
+       gchar *field_alias;
+       gboolean asc;
+
+       GdaExSqlBuilderTable *t;
+       GdaExSqlBuilderField *f;
+
+       GdaExSqlBuilderPrivate *priv = GDAEX_SQLBUILDER_GET_PRIVATE (sqlb);
+
+       va_start (ap, sqlb);
+       do
+               {
+                       table_name = NULL;
+                       field_name = NULL;
+                       field_alias = NULL;
+
+                       table_name = va_arg (ap, gchar *);
+                       if (table_name != NULL)
+                               {
+                                   field_name = va_arg (ap, gchar *);
+                                       if (field_name != NULL)
+                                               {
+                                                       field_alias = va_arg (ap, gchar *);
+                                                       if (field_alias != NULL)
+                                                               {
+                                                                       asc = va_arg (ap, gboolean);
+
+                                                                       t = gdaex_sql_builder_get_table (sqlb, table_name, NULL, TRUE);
+                                                                   f = gdaex_sql_builder_get_field (sqlb, t, field_name, field_alias, NULL, TRUE);
+                                                                       gda_sql_builder_select_order_by (priv->sqlb, f->id, asc, NULL);
+                                                               }
+                                                       else
+                                                               {
+                                                                       break;
+                                                               }
+                                               }
+                                       else
+                                               {
+                                                       break;
+                                               }
+                               }
+                       else
+                               {
+                                       break;
+                               }
+               } while (TRUE);
+       va_end (ap);
+}
+
 /**
  * gaex_sql_builder_get_gda_sql_builder:
  * @sqlb:
index aaa5b5c29ea8fff58c5d233ea3bdf207006c6400..715b87a04da327fc6e9302b8cbfae2558af9a4c9 100644 (file)
@@ -4,21 +4,21 @@
  *  Copyright (C) 2015 Andrea Zagli <azagli@libero.it>
  *
  *  This file is part of libgdaex.
- *  
+ *
  *  libgdaex 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.
- *  
+ *
  *  libgdaex 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 libgdaex; if not, write to the Free Software
  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- *  
+ *
  */
 
 #ifndef __GDAEX_SQL_BUILDER_H__
@@ -70,6 +70,8 @@ void gdaex_sql_builder_fields (GdaExSqlBuilder *sqlb, ...);
 GdaSqlBuilderId gdaex_sql_builder_where (GdaExSqlBuilder *sqln, GdaSqlOperatorType op,
                                                                                 ...);
 
+void gdaex_sql_builder_order (GdaExSqlBuilder *sqlb, ...);
+
 GdaSqlBuilder *gdaex_sql_builder_get_gda_sql_builder (GdaExSqlBuilder *sqlb);
 const gchar *gdaex_sql_builder_get_sql (GdaExSqlBuilder *sqlb, GdaConnection *cnc, GdaSet *params);
 
index 6841221a2df6efbba3b990242c5b121d0402c8d8..be00c45a3acdea25bd9ecefb91406ee7178b6776 100644 (file)
@@ -37,7 +37,7 @@ main (int argc, char **argv)
                                                          "pippo", "id", "", NULL,
                                                          "pippo", "name", "the_name", NULL,
                                                          NULL);
-       
+
        gdaex_sql_builder_join (sqlb, GDA_SQL_SELECT_JOIN_INNER,
                                                        "pippo", "id", "",
                                                        GDA_SQL_OPERATOR_TYPE_EQ,
@@ -53,7 +53,7 @@ main (int argc, char **argv)
                                                         gval,
                                                         NULL);
        g_value_unset (gval);
-       
+
        gval = g_new0 (GValue, 1);
        g_value_init (gval, G_TYPE_STRING);
        g_value_set_string (gval, "ichichc");
@@ -64,6 +64,11 @@ main (int argc, char **argv)
                                                         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));
 
        g_object_unref (sqlb);
@@ -74,7 +79,7 @@ main (int argc, char **argv)
                                                          "pippo", "pluto",
                                                          "topolino", "",
                                                          NULL);
-       
+
        gval = g_new0 (GValue, 1);
        g_value_init (gval, G_TYPE_INT);
        g_value_set_int (gval, 1);
@@ -101,8 +106,8 @@ main (int argc, char **argv)
        g_value_set_string (gval, "il nome di pippo");
        gdaex_sql_builder_fields (sqlb,
                                                          "pippo", "name", "", gval,
-                                                         NULL);        
-       
+                                                         NULL);
+
        gval = g_new0 (GValue, 1);
        g_value_init (gval, G_TYPE_INT);
        g_value_set_int (gval, 1);
@@ -129,8 +134,8 @@ main (int argc, char **argv)
        g_value_set_string (gval, "il nome di pippo");
        gdaex_sql_builder_fields (sqlb,
                                                          "pippo", "name", "", gval,
-                                                         NULL);        
-       
+                                                         NULL);
+
        gval = g_new0 (GValue, 1);
        g_value_init (gval, G_TYPE_INT);
        g_value_set_int (gval, 1);