Re: [gnome-db] GdaSqlBuilder: Specifying the full field name in a WHERE clause.

Murray Cumming <[email protected]>
Newsgroups gmane.comp.gnome.db
Message-ID <1276523681.2805.45.camel@murrayc-desktop>
On Sun, 2010-06-13 at 14:56 +0200, Murray Cumming wrote:
> On Tue, 2010-05-25 at 16:15 +0200, Murray Cumming wrote:
> > When specifying the fields to get, gda_sql_builder_select_add_field()
> > lets me specify both the table name and the field name, to avoid
> > ambiguity:
> > http://library.gnome.org/devel/libgda/unstable/GdaSqlBuilder.html#gda-sql-builder-select-add-field
> > 
> > But when I want to specify the field in a WHERE clause, or a JOIN, or a
> > function parameer, I must manually create that string, such as
> > "mytable"."myfield", with the quotes if necessary, when passing it to
> > gda_sql_builder_add_id():
> > http://library.gnome.org/devel/libgda/unstable/GdaSqlBuilder.html#gda-sql-builder-add-id
> > 
> > I'd like to have a function for field IDs that takes both strings. But
> > gda_sql_builder_add_field_id() already exists and has a very different
> > purpose:
> > http://library.gnome.org/devel/libgda/unstable/GdaSqlBuilder.html#gda-sql-builder-add-field-id
> > 
> > Maybe gda_sql_builder_add_id_for_field()?
> 
> Thoughts? I might go ahead and implement this.

May I commit the attached patch?

GdaSqlBuilder: Add gda_sql_builder_add_field_id(builder, field, table).
    
    * libgda/gda-sql-builder.[h|c]: Added gda_sql_builder_add_field_id()
    as an easier way to call gda_sql_builder_add_id() when you need to
    specify the field name with a table prefix to avoid ambiguity.
    Note that there was previously a gda_sql_builder_add_field_id()
    which has been renamed to gda_sql_builder_add_field_value_id().
    Luckily it's signature is quite different.
    * doc/C/libgda-sections.txt:
    * libgda/libgda.symbols: Mention the new function here.



-- 
[email protected]
www.murrayc.com
www.openismus.com

_______________________________________________
gnome-db-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/gnome-db-list
0001-GdaSqlBuilder-Add-gda_sql_builder_add_field_id-build.patch (text/x-patch, 5 KB)
From a294bd7310b52591e351d40a6c7efd758bb7b687 Mon Sep 17 00:00:00 2001
From: Murray Cumming <[email protected]>
Date: Mon, 14 Jun 2010 15:50:41 +0200
Subject: [PATCH] GdaSqlBuilder: Add gda_sql_builder_add_field_id(builder, field, table).

* libgda/gda-sql-builder.[h|c]: Added gda_sql_builder_add_field_id()
as an easier way to call gda_sql_builder_add_id() when you need to
specify the field name with a table prefix to avoid ambiguity.
Note that there was previously a gda_sql_builder_add_field_id()
which has been renamed to gda_sql_builder_add_field_value_id().
Luckily it's signature is quite different.
* doc/C/libgda-sections.txt:
* libgda/libgda.symbols: Mention the new function here.
---
 doc/C/libgda-sections.txt |    1 +
 libgda/gda-sql-builder.c  |   44 +++++++++++++++++++++++++++++++++++++++++++-
 libgda/gda-sql-builder.h  |    1 +
 libgda/libgda.symbols     |    1 +
 4 files changed, 46 insertions(+), 1 deletions(-)

diff --git a/doc/C/libgda-sections.txt b/doc/C/libgda-sections.txt
index a358910..7a8272c 100644
--- a/doc/C/libgda-sections.txt
+++ b/doc/C/libgda-sections.txt
@@ -1681,6 +1681,7 @@ gda_sql_builder_add_function
 gda_sql_builder_add_function_v
 <SUBSECTION>
 gda_sql_builder_add_id
+gda_sql_builder_add_field_id
 gda_sql_builder_add_expr
 gda_sql_builder_add_expr_value
 gda_sql_builder_add_param
diff --git a/libgda/gda-sql-builder.c b/libgda/gda-sql-builder.c
index 0bd59e1..533e4e5 100644
--- a/libgda/gda-sql-builder.c
+++ b/libgda/gda-sql-builder.c
@@ -488,6 +488,8 @@ gda_sql_builder_set_where (GdaSqlBuilder *builder, GdaSqlBuilderId cond_id)
  *
  * Add a selected selected item to the SELECT statement.
  *
+ * For non-SELECT statements, see gda_sql_builder_add_field_id().
+ *
  * Returns: the ID of the added field, or 0 if there was an error
  *
  * Since: 4.2
@@ -846,7 +848,9 @@ gda_sql_builder_add_expr (GdaSqlBuilder *builder, GdaDataHandler *dh, GType type
  * @string: a string
  *
  * Defines an expression representing an identifier in @builder,
- * which may be reused to build other parts of a statement.
+ * which may be reused to build other parts of a statement,
+ * for instance as a parameter to gda_sql_builder_add_cond() or
+ * gda_sql_builder_add_field_value_id().
  *
  * The new expression will contain the @string literal.
  * For example:
@@ -863,6 +867,8 @@ gda_sql_builder_add_expr (GdaSqlBuilder *builder, GdaDataHandler *dh, GType type
  *
  * because "date" is an SQL reserved keyword.
  *
+ * For fields, see gda_sql_builder_add_field_id().
+ *
  * Returns: the ID of the new expression, or 0 if there was an error
  *
  * Since: 4.2
@@ -885,6 +891,42 @@ gda_sql_builder_add_id (GdaSqlBuilder *builder, const gchar *string)
 }
 
 /**
+ * gda_sql_builder_add_field_id
+ * @builder: a #GdaSqlBuilder object
+ * @field_name: a field name
+ * @table_name: a table name, or %NULL
+ *
+ * Defines an expression representing a field in @builder,
+ * which may be reused to build other parts of a statement,
+ * for instance as a parameter to gda_sql_builder_add_cond() or
+ * gda_sql_builder_add_field_value_id().
+ *
+ * Calling this with a %NULL @table_name is equivalent to calling gda_sql_builder_add_id().
+ *
+ * For SELECT queries, see gda_sql_builder_select_add_field().
+ *
+ * Returns: the ID of the new expression, or 0 if there was an error
+ *
+ * Since: 4.2
+ */
+GdaSqlBuilderId
+gda_sql_builder_add_field_id (GdaSqlBuilder *builder, const gchar *field_name, const gchar *table_name)
+{
+	gchar* tmp = 0;
+	if (table_name && *table_name)
+		tmp = g_strdup_printf ("%s.%s", table_name, field_name);
+	else
+		tmp = (gchar*) field_name;
+
+	guint field_id = gda_sql_builder_add_id (builder, tmp);
+
+	if (table_name)
+		g_free (tmp);
+
+	return field_id;
+}
+
+/**
  * gda_sql_builder_add_param
  * @builder: a #GdaSqlBuilder object
  * @param_name: parameter's name
diff --git a/libgda/gda-sql-builder.h b/libgda/gda-sql-builder.h
index 955d7fe..8c07794 100644
--- a/libgda/gda-sql-builder.h
+++ b/libgda/gda-sql-builder.h
@@ -75,6 +75,7 @@ GdaSqlStatement  *gda_sql_builder_get_sql_statement (GdaSqlBuilder *builder);
 
 /* Expression API */
 GdaSqlBuilderId gda_sql_builder_add_id (GdaSqlBuilder *builder, const gchar *string);
+GdaSqlBuilderId gda_sql_builder_add_field_id (GdaSqlBuilder *builder, const gchar *field_name, const gchar *table_name);
 GdaSqlBuilderId gda_sql_builder_add_expr (GdaSqlBuilder *builder, GdaDataHandler *dh, GType type, ...);
 GdaSqlBuilderId gda_sql_builder_add_expr_value (GdaSqlBuilder *builder, GdaDataHandler *dh, const GValue *value);
 GdaSqlBuilderId gda_sql_builder_add_param (GdaSqlBuilder *builder, const gchar *param_name, GType type, gboolean nullok);
diff --git a/libgda/libgda.symbols b/libgda/libgda.symbols
index 34159d1..df247aa 100644
--- a/libgda/libgda.symbols
+++ b/libgda/libgda.symbols
@@ -579,6 +579,7 @@
 	gda_sql_builder_add_cond_v
 	gda_sql_builder_add_expr
 	gda_sql_builder_add_expr_value
+	gda_sql_builder_add_field_id
 	gda_sql_builder_add_field_value
 	gda_sql_builder_add_field_value_id
 	gda_sql_builder_add_field_value_as_gvalue
-- 
1.6.3.3
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.