From 9613000e1b8aaac450f91323217e0858a57704e7 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 21 Feb 2016 17:20:48 +0100 Subject: [PATCH] SqlBuilder: enabled number of arguments different from 2. --- src/sqlbuilder.c | 58 +++++++++++++++++++++++++++++++++++++++------- tests/sqlbuilder.c | 37 +++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 9 deletions(-) diff --git a/src/sqlbuilder.c b/src/sqlbuilder.c index 180a455..939ca36 100644 --- a/src/sqlbuilder.c +++ b/src/sqlbuilder.c @@ -1,7 +1,7 @@ /* * sql_builder.c * - * Copyright (C) 2010-2014 Andrea Zagli + * Copyright (C) 2010-2016 Andrea Zagli * * This file is part of libgdaex. * @@ -445,7 +445,8 @@ gdaex_sql_builder_where (GdaExSqlBuilder *sqlb, GdaSqlOperatorType op, ...) GdaSqlOperatorType op_expr; - GdaSqlBuilderId id_expr; + GdaSqlBuilderId id_expr1; + GdaSqlBuilderId id_expr2; GdaSqlBuilderId id_cond; GdaExSqlBuilderPrivate *priv = GDAEX_SQLBUILDER_GET_PRIVATE (sqlb); @@ -487,17 +488,56 @@ gdaex_sql_builder_where (GdaExSqlBuilder *sqlb, GdaSqlOperatorType op, ...) op_expr = va_arg (ap, guint); - gval = va_arg (ap, GValue *); - if (gval != NULL) - { - id_expr = gda_sql_builder_add_expr_value (priv->sqlb, NULL, gval); - } - else + id_expr1 = 0; + id_expr2 = 0; + switch (op_expr) { + case GDA_SQL_OPERATOR_TYPE_EQ: + case GDA_SQL_OPERATOR_TYPE_IS: + case GDA_SQL_OPERATOR_TYPE_LIKE: + case GDA_SQL_OPERATOR_TYPE_NOTLIKE: + case GDA_SQL_OPERATOR_TYPE_ILIKE: + case GDA_SQL_OPERATOR_TYPE_NOTILIKE: + case GDA_SQL_OPERATOR_TYPE_GT: + case GDA_SQL_OPERATOR_TYPE_LT: + case GDA_SQL_OPERATOR_TYPE_GEQ: + case GDA_SQL_OPERATOR_TYPE_LEQ: + case GDA_SQL_OPERATOR_TYPE_DIFF: + case GDA_SQL_OPERATOR_TYPE_REGEXP: + case GDA_SQL_OPERATOR_TYPE_REGEXP_CI: + case GDA_SQL_OPERATOR_TYPE_NOT_REGEXP: + case GDA_SQL_OPERATOR_TYPE_NOT_REGEXP_CI: + case GDA_SQL_OPERATOR_TYPE_SIMILAR: + case GDA_SQL_OPERATOR_TYPE_REM: + case GDA_SQL_OPERATOR_TYPE_DIV: + case GDA_SQL_OPERATOR_TYPE_BITAND: + case GDA_SQL_OPERATOR_TYPE_BITOR: + gval = va_arg (ap, GValue *); + if (gval != NULL) + { + id_expr1 = gda_sql_builder_add_expr_value (priv->sqlb, NULL, gval); + } + break; + + case GDA_SQL_OPERATOR_TYPE_ISNULL: + case GDA_SQL_OPERATOR_TYPE_ISNOTNULL: + break; + + case GDA_SQL_OPERATOR_TYPE_BETWEEN: + gval = va_arg (ap, GValue *); + if (gval != NULL) + { + id_expr1 = gda_sql_builder_add_expr_value (priv->sqlb, NULL, gval); + } + gval = va_arg (ap, GValue *); + if (gval != NULL) + { + id_expr2 = gda_sql_builder_add_expr_value (priv->sqlb, NULL, gval); + } break; } - id_cond = gda_sql_builder_add_cond (priv->sqlb, op_expr, f->id, id_expr, 0); + id_cond = gda_sql_builder_add_cond (priv->sqlb, op_expr, f->id, id_expr1, id_expr2); if (priv->id_where != 0) { priv->id_where = gda_sql_builder_add_cond (priv->sqlb, op, priv->id_where, id_cond, 0); diff --git a/tests/sqlbuilder.c b/tests/sqlbuilder.c index e12c229..2a29520 100644 --- a/tests/sqlbuilder.c +++ b/tests/sqlbuilder.c @@ -75,6 +75,43 @@ main (int argc, char **argv) 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, -- 2.49.0