[libgda] GdaSqlBuilder corrections
- From: Vivien Malerba <vivien src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgda] GdaSqlBuilder corrections
- Date: Sat, 27 Feb 2010 14:59:51 +0000 (UTC)
commit 07ab6b27fd3159c256d42e2d4bf6f2adf5928b4e
Author: Vivien Malerba <malerba gnome-db org>
Date: Sat Feb 27 15:58:55 2010 +0100
GdaSqlBuilder corrections
allow NULL of a NULL GValue passed as arguments to
gda_sql_builder_add_field_value() and gda_sql_builder_add_expr_value()
libgda/gda-sql-builder.c | 34 ++++++++++++++++++----------------
tests/test-sql-builder.c | 35 +++++++++++++++++++++++++++++++++--
2 files changed, 51 insertions(+), 18 deletions(-)
---
diff --git a/libgda/gda-sql-builder.c b/libgda/gda-sql-builder.c
index bad1bdf..4bf5427 100644
--- a/libgda/gda-sql-builder.c
+++ b/libgda/gda-sql-builder.c
@@ -591,7 +591,7 @@ gda_sql_builder_add_field (GdaSqlBuilder *builder, const gchar *field_name, GTyp
* gda_sql_builder_add_field_value
* @builder: a #GdaSqlBuilder object
* @field_name: a field name
- * @value: value to set the field to
+ * @value: value to set the field to, or %NULL or a GDA_TYPE_NULL value to represent an SQL NULL
*
* Valid only for: INSERT, UPDATE statements.
*
@@ -606,7 +606,6 @@ gda_sql_builder_add_field_value (GdaSqlBuilder *builder, const gchar *field_name
g_return_if_fail (GDA_IS_SQL_BUILDER (builder));
g_return_if_fail (builder->priv->main_stmt);
g_return_if_fail (field_name && *field_name);
- g_return_if_fail (value);
if ((builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_UPDATE) &&
(builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_INSERT)) {
@@ -739,7 +738,7 @@ gda_sql_builder_add_field_id (GdaSqlBuilder *builder, guint field_id, guint valu
* @builder: a #GdaSqlBuilder object
* @id: the requested ID, or 0 if to be determined by @builder
* @dh: a #GdaDataHandler to use, or %NULL
- * @value: value to set the expression to
+ * @value: value to set the expression to, or %NULL or a GDA_TYPE_NULL value to represent an SQL NULL
*
* Defines an expression in @builder which may be reused to build other parts of a statement.
*
@@ -755,25 +754,27 @@ gda_sql_builder_add_expr_value (GdaSqlBuilder *builder, guint id, GdaDataHandler
{
g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
g_return_val_if_fail (builder->priv->main_stmt, 0);
- g_return_val_if_fail (value != NULL, 0);
- GType type = G_VALUE_TYPE(value);
gchar *str;
- if (!dh)
- dh = gda_get_default_handler (type);
- else {
- if (! gda_data_handler_accepts_g_type (dh, type)) {
+ if (value && (G_VALUE_TYPE (value) != GDA_TYPE_NULL)) {
+ GType type = G_VALUE_TYPE (value);
+ if (!dh)
+ dh = gda_get_default_handler (type);
+ else {
+ if (! gda_data_handler_accepts_g_type (dh, type)) {
+ g_warning (_("Unhandled data type '%s'"), g_type_name (type));
+ return 0;
+ }
+ }
+ if (!dh) {
g_warning (_("Unhandled data type '%s'"), g_type_name (type));
return 0;
}
+ str = gda_data_handler_get_sql_from_value (dh, value);
}
- if (!dh) {
- g_warning (_("Unhandled data type '%s'"), g_type_name (type));
- return 0;
- }
-
- str = gda_data_handler_get_sql_from_value (dh, value);
+ else
+ str = g_strdup ("NULL");
if (str) {
GdaSqlExpr *expr;
@@ -783,7 +784,8 @@ gda_sql_builder_add_expr_value (GdaSqlBuilder *builder, guint id, GdaDataHandler
return add_part (builder, id, (GdaSqlAnyPart *) expr);
}
else {
- g_warning (_("Could not convert value to type '%s'"), g_type_name (type));
+ g_warning (_("Could not convert value to type '%s'"),
+ g_type_name (G_VALUE_TYPE (value)));
return 0;
}
}
diff --git a/tests/test-sql-builder.c b/tests/test-sql-builder.c
index 6ed180a..b95abee 100644
--- a/tests/test-sql-builder.c
+++ b/tests/test-sql-builder.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009 The GNOME Foundation.
+ * Copyright (C) 2009 - 2010 The GNOME Foundation.
*
* AUTHORS:
* Vivien Malerba <malerba gnome-db org>
@@ -39,6 +39,7 @@ static GdaSqlStatement *build5 (void);
static GdaSqlStatement *build6 (void);
static GdaSqlStatement *build7 (void);
static GdaSqlStatement *build8 (void);
+static GdaSqlStatement *build9 (void);
ATest tests[] = {
{"build0", build0, "{\"sql\":null,\"stmt_type\":\"SELECT\",\"contents\":{\"distinct\":\"false\",\"fields\":[{\"expr\":{\"value\":\"*\",\"sqlident\":\"TRUE\"}}],\"from\":{\"targets\":[{\"expr\":{\"value\":\"mytable\",\"sqlident\":\"TRUE\"},\"table_name\":\"mytable\"}]}}}"},
@@ -49,7 +50,8 @@ ATest tests[] = {
{"build5", build5, "{\"sql\":null,\"stmt_type\":\"SELECT\",\"contents\":{\"distinct\":\"false\",\"fields\":[{\"expr\":{\"value\":\"id\",\"sqlident\":\"TRUE\"}}],\"from\":{\"targets\":[{\"expr\":{\"value\":\"mytable\",\"sqlident\":\"TRUE\"},\"table_name\":\"mytable\"}]},\"where\":{\"operation\":{\"operator\":\"AND\",\"operand0\":{\"operation\":{\"operator\":\"=\",\"operand0\":{\"value\":\"sesion\",\"sqlident\":\"TRUE\"},\"operand1\":{\"value\":null,\"param_spec\":{\"name\":\"session\",\"descr\":null,\"type\":\"int\",\"is_param\":true,\"nullok\":false}}}},\"operand1\":{\"operation\":{\"operator\":\"=\",\"operand0\":{\"value\":\"contents\",\"sqlident\":\"TRUE\"},\"operand1\":{\"value\":null,\"param_spec\":{\"name\":\"contents\",\"descr\":null,\"type\":\"int\",\"is_param\":true,\"nullok\":false}}}}}}}}"},
{"build6", build6, "{\"sql\":null,\"stmt_type\":\"SELECT\",\"contents\":{\"distinct\":\"false\",\"fields\":[{\"expr\":{\"value\":\"fav_id\",\"sqlident\":\"TRUE\"}},{\"expr\":{\"value\":\"rank\",\"sqlident\":\"TRUE\"}}],\"from\":{\"targets\":[{\"expr\":{\"value\":\"mytable\",\"sqlident\":\"TRUE\"},\"table_name\":\"mytable\"}]},\"where\":{\"operation\":{\"operator\":\"AND\",\"operand0\":{\"operation\":{\"operator\":\"=\",\"operand0\":{\"value\":\"order_key\",\"sqlident\":\"TRUE\"},\"operand1\":{\"value\":null,\"param_spec\":{\"name\":\"orderkey\",\"descr\":null,\"type\":\"int\",\"is_param\":true,\"nullok\":false}}}},\"operand1\":{\"operation\":{\"operator\":\"!=\",\"operand0\":{\"value\":\"fav_id\",\"sqlident\":\"TRUE\"},\"operand1\":{\"value\":null,\"param_spec\":{\"name\":\"id\",\"descr\":null,\"type\":\"int\",\"is_param\":true,\"nullok\":false}}}}}}}}"},
{"build7", build7, "{\"sql\":null,\"stmt_type\":\"UPDATE\",\"contents\":{\"table\":\"mytable\",\"fields\":[\"rank\"],\"expressions\":[{\"value\":null,\"param_spec\":{\"name\":\"newrank\",\"descr\":null,\"type\":\"int\",\"is_param\":true,\"nullok\":false}}],\"condition\":{\"operation\":{\"operator\":\"AND\",\"operand0\":{\"operation\":{\"operator\":\"=\",\"operand0\":{\"value\":\"fav_id\",\"sqlident\":\"TRUE\"},\"operand1\":{\"value\":null,\"param_spec\":{\"name\":\"id\",\"descr\":null,\"type\":\"int\",\"is_param\":true,\"nullok\":false}}}},\"operand1\":{\"operation\":{\"operator\":\"=\",\"operand0\":{\"value\":\"order_key\",\"sqlident\":\"TRUE\"},\"operand1\":{\"value\":null,\"param_spec\":{\"name\":\"orderkey\",\"descr\":null,\"type\":\"int\",\"is_param\":true,\"nullok\":false}}}},\"operand2\":{\"operation\":{\"operator\":\"=\",\"operand0\":{\"value\":\"rank\",\"sqlident\":\"TRUE\"},\"operand1\":{\"value\":null,\"param_spec\":{\"name\":\"rank\",\"descr\":null,\"type\":\"in
t\",\"is_param\":true,\"nullok\":false}}}}}}}}"},
- {"build8", build8, "{\"sql\":null,\"stmt_type\":\"DELETE\",\"contents\":{\"table\":\"mytable\",\"condition\":{\"operation\":{\"operator\":\"=\",\"operand0\":{\"value\":\"id\",\"sqlident\":\"TRUE\"},\"operand1\":{\"value\":null,\"param_spec\":{\"name\":\"id\",\"descr\":null,\"type\":\"int\",\"is_param\":true,\"nullok\":false}}}}}}"}
+ {"build8", build8, "{\"sql\":null,\"stmt_type\":\"DELETE\",\"contents\":{\"table\":\"mytable\",\"condition\":{\"operation\":{\"operator\":\"=\",\"operand0\":{\"value\":\"id\",\"sqlident\":\"TRUE\"},\"operand1\":{\"value\":null,\"param_spec\":{\"name\":\"id\",\"descr\":null,\"type\":\"int\",\"is_param\":true,\"nullok\":false}}}}}}"},
+ {"build8", build9, "{\"sql\":null,\"stmt_type\":\"INSERT\",\"contents\":{\"table\":\"mytable\",\"fields\":[\"session\",\"name\"],\"values\":[[{\"value\":\"NULL\"},{\"value\":\"NULL\"}]]}}"}
};
int
@@ -409,3 +411,32 @@ build8 (void)
return stmt;
}
+/*
+ * INSERT INTO mytable (session, name) VALUES (NULL, NULL);
+ */
+static GdaSqlStatement *
+build9 (void)
+{
+ GdaSqlBuilder *builder;
+ GdaSqlStatement *stmt;
+ GValue *value;
+
+ builder = gda_sql_builder_new (GDA_SQL_STATEMENT_INSERT);
+ gda_sql_builder_set_table (builder, "mytable");
+
+ gda_sql_builder_add_field_value (builder, "session", NULL);
+ value = gda_value_new_null ();
+ gda_sql_builder_add_field_value (builder, "name", value);
+ gda_value_free (value);
+
+ {
+ GdaStatement *st;
+ st = gda_sql_builder_get_statement (builder, FALSE);
+ g_print ("[%s]\n", gda_statement_to_sql (st, NULL, NULL));
+ g_object_unref (st);
+ }
+ stmt = gda_sql_builder_get_sql_statement (builder, FALSE);
+ g_object_unref (builder);
+
+ return stmt;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]