From: Andrea Zagli Date: Wed, 22 Jul 2015 14:52:16 +0000 (+0200) Subject: SqlBuilder: added ::order (closes #940). X-Git-Tag: v0.6.0~4 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=1ecc7a7cbf5f8537125651f65a7d8f55f372d02d;p=libgdaex SqlBuilder: added ::order (closes #940). --- diff --git a/src/sqlbuilder.c b/src/sqlbuilder.c index 975e6d1..dc24763 100644 --- a/src/sqlbuilder.c +++ b/src/sqlbuilder.c @@ -4,17 +4,17 @@ * Copyright (C) 2010-2014 Andrea Zagli * * 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: diff --git a/src/sqlbuilder.h b/src/sqlbuilder.h index aaa5b5c..715b87a 100644 --- a/src/sqlbuilder.h +++ b/src/sqlbuilder.h @@ -4,21 +4,21 @@ * Copyright (C) 2015 Andrea Zagli * * 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); diff --git a/tests/sqlbuilder.c b/tests/sqlbuilder.c index 6841221..be00c45 100644 --- a/tests/sqlbuilder.c +++ b/tests/sqlbuilder.c @@ -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);