[libgda] Improved GdaSqlBuilder with import/export features



commit 12b40ace74565cf60f59eee5b10811c1b0d38452
Author: Vivien Malerba <malerba gnome-db org>
Date:   Wed May 5 15:31:05 2010 +0200

    Improved GdaSqlBuilder with import/export features
    
    
    added: gda_sql_builder_export_expression() and gda_sql_builder_import_expression()

 doc/C/libgda-sections.txt        |    3 ++
 doc/C/tmpl/gda-sql-builder.sgml  |   25 +++++++++++++++++++
 libgda/gda-sql-builder.c         |   50 ++++++++++++++++++++++++++++++++++++++
 libgda/gda-sql-builder.h         |    4 +++
 libgda/libgda.symbols            |    2 +
 samples/SqlBuilder/README        |   11 +++++++-
 samples/SqlBuilder/example.c     |   36 +++++++++++++++++++++++++++
 samples/SqlBuilder/example_cnc.c |   36 +++++++++++++++++++++++++++
 8 files changed, 166 insertions(+), 1 deletions(-)
---
diff --git a/doc/C/libgda-sections.txt b/doc/C/libgda-sections.txt
index e33406e..8abeb24 100644
--- a/doc/C/libgda-sections.txt
+++ b/doc/C/libgda-sections.txt
@@ -1702,6 +1702,9 @@ gda_sql_builder_select_group_by
 <SUBSECTION>
 gda_sql_builder_compound_add_sub_select
 gda_sql_builder_compound_set_type
+<SUBSECTION>
+gda_sql_builder_export_expression
+gda_sql_builder_import_expression
 <SUBSECTION Standard>
 GDA_SQL_BUILDER
 GDA_SQL_BUILDER_GET_CLASS
diff --git a/doc/C/tmpl/gda-sql-builder.sgml b/doc/C/tmpl/gda-sql-builder.sgml
index b3e678a..4cdd6bd 100644
--- a/doc/C/tmpl/gda-sql-builder.sgml
+++ b/doc/C/tmpl/gda-sql-builder.sgml
@@ -48,6 +48,9 @@ gda_sql_builder_add_cond2 (b, 5, GDA_SQL_OPERATOR_TYPE_AND, 3, 4); // whole expr
 <!-- ##### SECTION Stability_Level ##### -->
 
 
+<!-- ##### SECTION Image ##### -->
+
+
 <!-- ##### STRUCT GdaSqlBuilder ##### -->
 <para>
 
@@ -310,6 +313,7 @@ gda_sql_builder_add_cond2 (b, 5, GDA_SQL_OPERATOR_TYPE_AND, 3, 4); // whole expr
 @field_name: 
 @table_name: 
 @alias: 
+ Returns: 
 
 
 <!-- ##### FUNCTION gda_sql_builder_select_join_targets ##### -->
@@ -404,3 +408,24 @@ gda_sql_builder_add_cond2 (b, 5, GDA_SQL_OPERATOR_TYPE_AND, 3, 4); // whole expr
 @compound_type: 
 
 
+<!-- ##### FUNCTION gda_sql_builder_export_expression ##### -->
+<para>
+
+</para>
+
+ builder: 
+ id: 
+ Returns: 
+
+
+<!-- ##### FUNCTION gda_sql_builder_import_expression ##### -->
+<para>
+
+</para>
+
+ builder: 
+ id: 
+ expr: 
+ Returns: 
+
+
diff --git a/libgda/gda-sql-builder.c b/libgda/gda-sql-builder.c
index 9d35f75..d250c25 100644
--- a/libgda/gda-sql-builder.c
+++ b/libgda/gda-sql-builder.c
@@ -1811,3 +1811,53 @@ gda_sql_builder_add_case_v (GdaSqlBuilder *builder, guint id,
 	gda_sql_expr_free (expr);
 	return 0;
 }
+
+/**
+ * gda_sql_builder_export_expression
+ * @builder: a #GdaSqlBuilder object
+ * @id: the ID of the expression to be exported, (must be a valid ID in @builder, not %0)
+ *
+ * Exports a part managed by @builder as a new #GdaSqlExpr, which can represent any expression
+ * in a statement.
+ *
+ * Returns: a pointer to a new #GdaSqlExpr structure, free using gda_sql_expr_free() when not
+ * needed anymore. If the part with @id as ID cannot be found, the returned value is %NULL.
+ *
+ * Since: 4.2
+ */
+GdaSqlExpr *
+gda_sql_builder_export_expression (GdaSqlBuilder *builder, guint id)
+{
+	g_return_val_if_fail (GDA_IS_SQL_BUILDER (builder), NULL);
+	g_return_val_if_fail (builder->priv->main_stmt, NULL);
+
+	SqlPart *part;
+	part = get_part (builder, id, GDA_SQL_ANY_EXPR);
+	if (! part)
+		return NULL;
+	g_return_val_if_fail (part->part->type == GDA_SQL_ANY_EXPR, NULL);
+	return gda_sql_expr_copy ((GdaSqlExpr*) part->part);
+}
+
+/**
+ * gda_sql_builder_import_expression
+ * @builder: a #GdaSqlBuilder object
+ * @id: the requested ID, or 0 if to be determined by @builder
+ * @expr: a #GdaSqlExpr obtained using gda_sql_builder_export_expression()
+ *
+ * Imports the @expr into @builder.
+ *
+ * Returns: the ID of the new expression, or 0 if there was an error
+ *
+ * Since: 4.2
+ */
+guint
+gda_sql_builder_import_expression (GdaSqlBuilder *builder, guint id, GdaSqlExpr *expr)
+{
+	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 (expr, 0);
+
+	g_return_val_if_fail (GDA_SQL_ANY_PART (expr)->type == GDA_SQL_ANY_EXPR, 0);
+	return add_part (builder, id, (GdaSqlAnyPart *) gda_sql_expr_copy (expr));	
+}
diff --git a/libgda/gda-sql-builder.h b/libgda/gda-sql-builder.h
index 17666f6..60984fe 100644
--- a/libgda/gda-sql-builder.h
+++ b/libgda/gda-sql-builder.h
@@ -127,6 +127,10 @@ void              gda_sql_builder_select_group_by (GdaSqlBuilder *builder, guint
 void              gda_sql_builder_compound_set_type (GdaSqlBuilder *builder, GdaSqlStatementCompoundType compound_type);
 void              gda_sql_builder_compound_add_sub_select (GdaSqlBuilder *builder, GdaSqlStatement *sqlst, gboolean steal);
 
+/* import/Export API */
+GdaSqlExpr       *gda_sql_builder_export_expression (GdaSqlBuilder *builder, guint id);
+guint             gda_sql_builder_import_expression (GdaSqlBuilder *builder, guint id, GdaSqlExpr *expr);
+
 G_END_DECLS
 
 #endif
diff --git a/libgda/libgda.symbols b/libgda/libgda.symbols
index 2ba2b87..d1cbf8a 100644
--- a/libgda/libgda.symbols
+++ b/libgda/libgda.symbols
@@ -590,9 +590,11 @@
 	gda_sql_builder_compound_set_type
 	gda_sql_builder_error_get_type
 	gda_sql_builder_error_quark
+	gda_sql_builder_export_expression
 	gda_sql_builder_get_sql_statement
 	gda_sql_builder_get_statement
 	gda_sql_builder_get_type
+	gda_sql_builder_import_expression
 	gda_sql_builder_join_add_field
 	gda_sql_builder_new
 	gda_sql_builder_select_add_field
diff --git a/samples/SqlBuilder/README b/samples/SqlBuilder/README
index da52319..bdb041c 100644
--- a/samples/SqlBuilder/README
+++ b/samples/SqlBuilder/README
@@ -34,10 +34,15 @@ SQL: SELECT myfunc (a, 5, 'Joe'), MAX (myfunc (a, 5, 'Joe'), b, 10) FROM mytable
 SQL: UPDATE "select" SET "date"='2009-05-27' WHERE id = 14
 SQL: SELECT date (a, 5, 'Joe') FROM "date", MyTable
 SQL: SELECT name FROM master WHERE id IN (SELECT id FROM subdata)
+SQL: SELECT id, name, (SELECT MAX (ts) FROM documents AS d WHERE t.id = d.topic) FROM topics AS t
 SQL: INSERT INTO customers (e, f, g) SELECT id, name, location FROM subdate
 SQL: SELECT id, name FROM subdata1 UNION SELECT ident, lastname FROM subdata2
 SQL: SELECT CASE WHEN price < 1.200000 THEN 2 ELSE 1 END FROM data
 SQL: SELECT CASE tag WHEN 'Alpha' THEN 1 WHEN 'Bravo' THEN 2 WHEN 'Charlie' THEN 3 END FROM data
+SQL: SELECT people.firstname AS person, people.lastname, "date" AS birthdate, age FROM people
+SQL: INSERT INTO customers (f, g) VALUES (15, 'joe')
+SQL: DELETE FROM items WHERE id = ##theid::int
+SQL: SELECT id FROM mytable WHERE (name = ##thename::string) AND (id = ##theid::int)
 
 and for example_cnc:
 SQL: INSERT INTO "customers" ("e", "f", "g") VALUES (##p1::string, 15, 'joe')
@@ -49,4 +54,8 @@ SQL: SELECT "c"."date", "name" AS person FROM "select" AS c INNER JOIN "orders"
 SQL: SELECT myfunc ("a", 5, 'Joe') FROM "mytable"
 SQL: SELECT myfunc ("a", 5, 'Joe'), MAX (myfunc ("a", 5, 'Joe'), "b", 10) FROM "mytable"
 SQL: UPDATE "select" SET "date"='2009-05-27' WHERE "id" = 14
-SQL: SELECT date ("a", 5, 'Joe') FROM "date", "MyTable"
\ No newline at end of file
+SQL: SELECT date ("a", 5, 'Joe') FROM "date", "MyTable"
+SQL: SELECT "people"."firstname" AS person, "people"."lastname", "date" AS birthdate, "age" FROM "people"
+SQL: INSERT INTO "customers" ("f", "g") VALUES (15, 'joe')
+SQL: DELETE FROM "items" WHERE "id" = ##theid::int
+SQL: SELECT "id" FROM "mytable" WHERE ("name" = ##thename::string) AND ("id" = ##theid::int)
diff --git a/samples/SqlBuilder/example.c b/samples/SqlBuilder/example.c
index 35aa5ad..ff06873 100644
--- a/samples/SqlBuilder/example.c
+++ b/samples/SqlBuilder/example.c
@@ -305,6 +305,42 @@ main (int argc, char *argv[])
 	render_as_sql (b);
 	g_object_unref (b);
 
+	/* Create a WHERE clause in a statement and reuse it in another one:
+	 *
+	 * - the first SQL built is DELETE FROM items WHERE id = ##theid::int
+	 * - the "id = ##theid::int" is exported from the first build and imported into the final build
+	 * - the final SQL is: SELECT id FROM mytable WHERE (name = ##thename::string) AND (id = ##theid::int)
+	 */
+	GdaSqlExpr *expr;
+	b = gda_sql_builder_new (GDA_SQL_STATEMENT_DELETE);
+
+	gda_sql_builder_set_table (b, "items");
+	gda_sql_builder_add_id (b, 1, "id");
+	gda_sql_builder_add_param (b, 2, "theid", G_TYPE_INT, FALSE);
+	gda_sql_builder_add_cond (b, 3, GDA_SQL_OPERATOR_TYPE_EQ, 1, 2, 0);
+	gda_sql_builder_set_where (b, 3);
+
+	render_as_sql (b);
+	expr = gda_sql_builder_export_expression (b, 3);
+	g_object_unref (b);
+
+	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
+	gda_sql_builder_add_field_id (b, gda_sql_builder_add_id (b, 0, "id"), 0);
+	gda_sql_builder_select_add_target_id (b, 0,
+					      gda_sql_builder_add_id (b, 0, "mytable"),
+					      NULL);
+	gda_sql_builder_add_id (b, 1, "name");
+	gda_sql_builder_add_param (b, 2, "thename", G_TYPE_STRING, FALSE);
+	gda_sql_builder_add_cond (b, 3, GDA_SQL_OPERATOR_TYPE_EQ, 1, 2, 0);
+
+	gda_sql_builder_set_where (b,
+				   gda_sql_builder_add_cond (b, 0, GDA_SQL_OPERATOR_TYPE_AND, 3,
+							     gda_sql_builder_import_expression (b, 0, expr),
+							     0));
+	gda_sql_expr_free (expr);
+	render_as_sql (b);
+	g_object_unref (b);
+
 	return 0;
 }
 
diff --git a/samples/SqlBuilder/example_cnc.c b/samples/SqlBuilder/example_cnc.c
index 5f5f60e..51555cf 100644
--- a/samples/SqlBuilder/example_cnc.c
+++ b/samples/SqlBuilder/example_cnc.c
@@ -172,6 +172,42 @@ main (int argc, char *argv[])
 	
 	render_as_sql (b);
 	g_object_unref (b);
+
+	/* Create a WHERE clause in a statement and reuse it in another one:
+	 *
+	 * - the first SQL built is DELETE FROM items WHERE id = ##theid::int
+	 * - the "id = ##theid::int" is exported from the first build and imported into the final build
+	 * - the final SQL is: SELECT id FROM mytable WHERE (name = ##thename::string) AND (id = ##theid::int)
+	 */
+	GdaSqlExpr *expr;
+	b = gda_sql_builder_new (GDA_SQL_STATEMENT_DELETE);
+
+	gda_sql_builder_set_table (b, "items");
+	gda_sql_builder_add_id (b, 1, "id");
+	gda_sql_builder_add_param (b, 2, "theid", G_TYPE_INT, FALSE);
+	gda_sql_builder_add_cond (b, 3, GDA_SQL_OPERATOR_TYPE_EQ, 1, 2, 0);
+	gda_sql_builder_set_where (b, 3);
+
+	render_as_sql (b);
+	expr = gda_sql_builder_export_expression (b, 3);
+	g_object_unref (b);
+
+	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
+	gda_sql_builder_add_field_id (b, gda_sql_builder_add_id (b, 0, "id"), 0);
+	gda_sql_builder_select_add_target_id (b, 0,
+					      gda_sql_builder_add_id (b, 0, "mytable"),
+					      NULL);
+	gda_sql_builder_add_id (b, 1, "name");
+	gda_sql_builder_add_param (b, 2, "thename", G_TYPE_STRING, FALSE);
+	gda_sql_builder_add_cond (b, 3, GDA_SQL_OPERATOR_TYPE_EQ, 1, 2, 0);
+
+	gda_sql_builder_set_where (b,
+				   gda_sql_builder_add_cond (b, 0, GDA_SQL_OPERATOR_TYPE_AND, 3,
+							     gda_sql_builder_import_expression (b, 0, expr),
+							     0));
+	gda_sql_expr_free (expr);
+	render_as_sql (b);
+	g_object_unref (b);
 	
 	return 0;
 }



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