[libgda] SqlBuilder: gda_sql_builder_select_add_target(): Take a name string.



commit 2c17e9de2ecee875a64a82e9b80b0a89634e9f13
Author: Murray Cumming <murrayc murrayc com>
Date:   Fri Dec 4 14:10:27 2009 +0100

    SqlBuilder: gda_sql_builder_select_add_target(): Take a name string.
    
    	* libgda/gda-sql-builder.[h|c] (gda_sql_builder_select_add_target):
    	Rename to gda_sql_builder_select_add_target_id() and add a
    	gda_sql_builder_select_add_target() that takes the table name as a
    	string.
    	* providers/web/gda-web-recordset.c (create_table):
    	* tools/browser/browser-favorites.c (find_favorite_position),
    	(find_favorite), (favorites_reorder), (browser_favorites_list),
    	(browser_favorites_get): Use this simpler form.

 libgda/gda-sql-builder.c          |   44 +++++++++++++++++++++++++++++++++++-
 libgda/gda-sql-builder.h          |    3 +-
 providers/web/gda-web-recordset.c |    2 +-
 tools/browser/browser-favorites.c |   24 +++++---------------
 4 files changed, 51 insertions(+), 22 deletions(-)
---
diff --git a/libgda/gda-sql-builder.c b/libgda/gda-sql-builder.c
index f7923fc..bf15dc0 100644
--- a/libgda/gda-sql-builder.c
+++ b/libgda/gda-sql-builder.c
@@ -1043,7 +1043,7 @@ typedef struct {
 } BuildTarget;
 
 /**
- * gda_sql_builder_select_add_target
+ * gda_sql_builder_select_add_target_id
  * @builder: a #GdaSqlBuilder object
  * @id: the requested ID, or 0 if to be determined by @builder
  * @table_id: the ID of the expression holding a table reference (not %0)
@@ -1056,7 +1056,7 @@ typedef struct {
  * Since: 4.2
  */
 guint
-gda_sql_builder_select_add_target (GdaSqlBuilder *builder, guint id, guint table_id, const gchar *alias)
+gda_sql_builder_select_add_target_id (GdaSqlBuilder *builder, guint id, guint table_id, const gchar *alias)
 {
 	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), 0);
 	g_return_val_if_fail (builder->priv->main_stmt, 0);
@@ -1097,6 +1097,46 @@ gda_sql_builder_select_add_target (GdaSqlBuilder *builder, guint id, guint table
 	return btarget->part_id;
 }
 
+
+/**
+ * gda_sql_builder_select_add_target
+ * @builder: a #GdaSqlBuilder object
+ * @table_name: the name of the target table
+ * @alias: the alias to give to the target, or %NULL
+ *
+ * Adds a new target to a SELECT statement
+ *
+ * Returns: the ID of the new target, or 0 if there was an error
+ *
+ * Since: 4.2
+ */
+guint
+gda_sql_builder_select_add_target (GdaSqlBuilder *builder, const gchar *table_name, const gchar *alias)
+{
+	gchar *tmp;
+	g_return_if_fail (GDA_IS_SQL_BUILDER (builder));
+	g_return_if_fail (builder->priv->main_stmt);
+	if (builder->priv->main_stmt->stmt_type != GDA_SQL_STATEMENT_SELECT) {
+		g_warning (_("Wrong statement type"));
+		return;
+	}
+	g_return_if_fail (table_name && *table_name);
+
+	if (alias && *alias)
+		gda_sql_builder_select_add_target_id (builder,
+					      0, 
+					      gda_sql_builder_add_id (builder, 0, table_name),
+					      gda_sql_builder_add_id (builder, 0, alias));
+	else
+		gda_sql_builder_select_add_target_id (builder,
+					      0, 
+					      gda_sql_builder_add_id (builder, 0, table_name),
+					      0);
+	if (table_name)
+		g_free (tmp);
+}
+
+
 typedef struct {
 	GdaSqlSelectJoin join; /* inheritance! */
 	guint part_id; /* copied from this part ID */
diff --git a/libgda/gda-sql-builder.h b/libgda/gda-sql-builder.h
index 2f76f5e..d3a2ad0 100644
--- a/libgda/gda-sql-builder.h
+++ b/libgda/gda-sql-builder.h
@@ -105,7 +105,8 @@ void              gda_sql_builder_add_field_id (GdaSqlBuilder *builder, guint fi
 /* SELECT Statement API */
 void              gda_sql_builder_select_add_field (GdaSqlBuilder *builder, const gchar *field_name,
 						    const gchar *table_name, const gchar *alias);
-guint             gda_sql_builder_select_add_target (GdaSqlBuilder *builder, guint id,
+guint             gda_sql_builder_select_add_target (GdaSqlBuilder *builder, const gchar *table_name, const gchar *alias);
+guint             gda_sql_builder_select_add_target_id (GdaSqlBuilder *builder, guint id,
 						     guint table_id, const gchar *alias);
 guint             gda_sql_builder_select_join_targets (GdaSqlBuilder *builder, guint id,
 						       guint left_target_id, guint right_target_id,
diff --git a/providers/web/gda-web-recordset.c b/providers/web/gda-web-recordset.c
index 00f3c24..a321fa4 100644
--- a/providers/web/gda-web-recordset.c
+++ b/providers/web/gda-web-recordset.c
@@ -328,7 +328,7 @@ create_table (GdaWebRecordset *rs, GError **error)
 	sb = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
 
 	gda_sql_builder_set_table (ib, TABLE_NAME);
-	gda_sql_builder_select_add_target (sb, 0, gda_sql_builder_add_id (sb, 0, TABLE_NAME), NULL);
+	gda_sql_builder_select_add_target (sb, TABLE_NAME, NULL);
 
 	string = g_string_new ("CREATE table " TABLE_NAME " (");
 	ncols = gda_data_model_get_n_columns ((GdaDataModel*) rs);
diff --git a/tools/browser/browser-favorites.c b/tools/browser/browser-favorites.c
index 4d21999..49d9d89 100644
--- a/tools/browser/browser-favorites.c
+++ b/tools/browser/browser-favorites.c
@@ -244,9 +244,7 @@ find_favorite_position (BrowserFavorites *bfav, gint fav_id, gint order_key)
 	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
 	gda_sql_builder_add_field_id (b,
 				   gda_sql_builder_add_id (b, 0, "rank"), 0);
-	gda_sql_builder_select_add_target (b, 0,
-					   gda_sql_builder_add_id (b, 0, FAVORDER_TABLE_NAME),
-					   NULL);
+	gda_sql_builder_select_add_target (b, FAVORDER_TABLE_NAME, NULL);
 	gda_sql_builder_set_where (b,
 				   gda_sql_builder_add_cond (b, 0, GDA_SQL_OPERATOR_TYPE_AND,
 				   gda_sql_builder_add_cond (b, 0, GDA_SQL_OPERATOR_TYPE_EQ,
@@ -317,9 +315,7 @@ find_favorite (BrowserFavorites *bfav, guint session_id, gint id, const gchar *c
 				   gda_sql_builder_add_id (b, 0, "descr"), 0);
 	gda_sql_builder_add_field_id (b,
 				   gda_sql_builder_add_id (b, 0, "contents"), 0);
-	gda_sql_builder_select_add_target (b, 0,
-					   gda_sql_builder_add_id (b, 0, FAVORITES_TABLE_NAME),
-					   NULL);
+	gda_sql_builder_select_add_target (b, FAVORITES_TABLE_NAME, NULL);
 
 	if (id >= 0) {
 		/* lookup from ID */
@@ -399,9 +395,7 @@ favorites_reorder (BrowserFavorites *bfav, gint order_key, gint id, gint new_pos
 	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
 	gda_sql_builder_add_field_id (b, gda_sql_builder_add_id (b, 0, "fav_id"), 0);
 
-	gda_sql_builder_select_add_target (b, 0,
-					   gda_sql_builder_add_id (b, 0, FAVORDER_TABLE_NAME),
-					   NULL);
+	gda_sql_builder_select_add_target (b, FAVORDER_TABLE_NAME, NULL);
 	
 	gda_sql_builder_set_where (b, gda_sql_builder_add_cond (b, 1, GDA_SQL_OPERATOR_TYPE_EQ,
 				    gda_sql_builder_add_id (b, 0, "order_key"),
@@ -802,13 +796,9 @@ browser_favorites_list (BrowserFavorites *bfav, guint session_id, BrowserFavorit
 	gda_sql_builder_add_field_id (b,
 				   gda_sql_builder_add_id (b, 0, "fav.id"), 0);
 
-	t1 = gda_sql_builder_select_add_target (b, 0,
-						gda_sql_builder_add_id (b, 0, FAVORITES_TABLE_NAME),
-						"fav");
+	t1 = gda_sql_builder_select_add_target (b, FAVORITES_TABLE_NAME, "fav");
 	if (order_key > 0) {
-		t2 = gda_sql_builder_select_add_target (b, 0,
-							gda_sql_builder_add_id (b, 0, FAVORDER_TABLE_NAME),
-							"o");
+		t2 = gda_sql_builder_select_add_target (b, FAVORDER_TABLE_NAME, "o");
 		gda_sql_builder_select_join_targets (b, 0, t1, t2, GDA_SQL_SELECT_JOIN_LEFT,
 						     gda_sql_builder_add_cond (b, 0, GDA_SQL_OPERATOR_TYPE_EQ,
 									   gda_sql_builder_add_id (b, 0, "fav.id"),
@@ -1098,9 +1088,7 @@ browser_favorites_get (BrowserFavorites *bfav, gint fav_id,
 				   gda_sql_builder_add_id (b, 0, "descr"), 0);
 	gda_sql_builder_add_field_id (b,
 				   gda_sql_builder_add_id (b, 0, "contents"), 0);
-	gda_sql_builder_select_add_target (b, 0,
-					   gda_sql_builder_add_id (b, 0, FAVORITES_TABLE_NAME),
-					   NULL);
+	gda_sql_builder_select_add_target (b, FAVORITES_TABLE_NAME, NULL);
 
 	gda_sql_builder_set_where (b,
 				   gda_sql_builder_add_cond (b, 0, GDA_SQL_OPERATOR_TYPE_EQ,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]