[libgda] Minor corrections to GdaSqlBuilder



commit 9b8778a08ffad208d8da159643b38d0f61482c6b
Author: Vivien Malerba <malerba gnome-db org>
Date:   Wed May 12 18:33:54 2010 +0200

    Minor corrections to GdaSqlBuilder
    
    
    * mem leak correction
    * added an example to use a sub select in a FROM

 doc/C/tmpl/gda-sql-builder.sgml |    3 ---
 libgda/gda-sql-builder.c        |   15 +++++++--------
 samples/SqlBuilder/README       |    1 +
 samples/SqlBuilder/example.c    |   36 ++++++++++++++++++++++++++++++------
 4 files changed, 38 insertions(+), 17 deletions(-)
---
diff --git a/doc/C/tmpl/gda-sql-builder.sgml b/doc/C/tmpl/gda-sql-builder.sgml
index 4cdd6bd..0da7571 100644
--- a/doc/C/tmpl/gda-sql-builder.sgml
+++ b/doc/C/tmpl/gda-sql-builder.sgml
@@ -87,7 +87,6 @@ gda_sql_builder_add_cond2 (b, 5, GDA_SQL_OPERATOR_TYPE_AND, 3, 4); // whole expr
 </para>
 
 @builder: 
- copy_it: 
 @Returns: 
 
 
@@ -241,7 +240,6 @@ gda_sql_builder_add_cond2 (b, 5, GDA_SQL_OPERATOR_TYPE_AND, 3, 4); // whole expr
 @builder: 
 @id: 
 @sqlst: 
- steal: 
 @Returns: 
 
 
@@ -396,7 +394,6 @@ gda_sql_builder_add_cond2 (b, 5, GDA_SQL_OPERATOR_TYPE_AND, 3, 4); // whole expr
 
 @builder: 
 @sqlst: 
- steal: 
 
 
 <!-- ##### FUNCTION gda_sql_builder_compound_set_type ##### -->
diff --git a/libgda/gda-sql-builder.c b/libgda/gda-sql-builder.c
index 3391536..9869357 100644
--- a/libgda/gda-sql-builder.c
+++ b/libgda/gda-sql-builder.c
@@ -370,9 +370,7 @@ gda_sql_builder_get_sql_statement (GdaSqlBuilder *builder)
 	if (!builder->priv->main_stmt)
 		return NULL;
 
-	GdaSqlStatement *stmt = builder->priv->main_stmt;
-	builder->priv->main_stmt = NULL;
-	return stmt;
+	return builder->priv->main_stmt;
 }
 
 /**
@@ -1072,6 +1070,7 @@ gda_sql_builder_select_add_target_id (GdaSqlBuilder *builder, guint id, guint ta
 		return 0;
 
 	BuildTarget *btarget;
+	GdaSqlSelectTarget *target;
 	GdaSqlStatementSelect *sel = (GdaSqlStatementSelect*) builder->priv->main_stmt->contents;
 	btarget = g_new0 (BuildTarget, 1);
         GDA_SQL_ANY_PART (btarget)->type = GDA_SQL_ANY_SQL_SELECT_TARGET;
@@ -1081,12 +1080,12 @@ gda_sql_builder_select_add_target_id (GdaSqlBuilder *builder, guint id, guint ta
 	else
 		btarget->part_id = builder->priv->next_assigned_id --;
 
-	((GdaSqlSelectTarget*) btarget)->expr = (GdaSqlExpr*) use_part (p, GDA_SQL_ANY_PART (btarget));
+	target = (GdaSqlSelectTarget*) btarget;
+	target->expr = (GdaSqlExpr*) use_part (p, GDA_SQL_ANY_PART (btarget));
 	if (alias && *alias)
-		((GdaSqlSelectTarget*) btarget)->as = g_strdup (alias);
-	if (g_value_get_string (((GdaSqlSelectTarget*) btarget)->expr->value))
-		((GdaSqlSelectTarget*) btarget)->table_name =
-			g_value_dup_string (((GdaSqlSelectTarget*) btarget)->expr->value);
+		target->as = g_strdup (alias);
+	if (target->expr->value && g_value_get_string (target->expr->value))
+		target->table_name = g_value_dup_string (target->expr->value);
 
 	/* add target to sel->from. NOTE: @btarget is NOT added to the "repository" or GdaSqlAnyPart parts
 	* like others */
diff --git a/samples/SqlBuilder/README b/samples/SqlBuilder/README
index bdb041c..60dccd9 100644
--- a/samples/SqlBuilder/README
+++ b/samples/SqlBuilder/README
@@ -43,6 +43,7 @@ SQL: SELECT people.firstname AS person, people.lastname, "date" AS birthdate, ag
 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)
+SQL: SELECT name FROM (SELECT id, name FROM subdata) AS sub
 
 and for example_cnc:
 SQL: INSERT INTO "customers" ("e", "f", "g") VALUES (##p1::string, 15, 'joe')
diff --git a/samples/SqlBuilder/example.c b/samples/SqlBuilder/example.c
index 0e4d9f0..ecdccab 100644
--- a/samples/SqlBuilder/example.c
+++ b/samples/SqlBuilder/example.c
@@ -8,7 +8,6 @@ main (int argc, char *argv[])
 	gda_init ();
 
 	GdaSqlBuilder *b;
-	
 
 	/* INSERT INTO customers (e, f, g) VALUES (##p1::string, 15, 'joe') */
 	b = gda_sql_builder_new (GDA_SQL_STATEMENT_INSERT);
@@ -153,7 +152,7 @@ main (int argc, char *argv[])
 	gda_sql_builder_select_add_target_id (b, 0,
 					   gda_sql_builder_add_id (b, 0, "subdata"),
 					   NULL);
-	sub = gda_sql_builder_get_sql_statement (b);
+	sub = gda_sql_statement_copy (gda_sql_builder_get_sql_statement (b));
 	g_object_unref (b);
 
 	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
@@ -163,6 +162,7 @@ main (int argc, char *argv[])
 					   NULL);
 	gda_sql_builder_add_id (b, 1, "id");
 	gda_sql_builder_add_sub_select (b, 2, sub);
+	gda_sql_statement_free (sub);
 	gda_sql_builder_add_cond (b, 3, GDA_SQL_OPERATOR_TYPE_IN, 1, 2, 0);
 	gda_sql_builder_set_where (b, 3);
 	render_as_sql (b);
@@ -180,7 +180,7 @@ main (int argc, char *argv[])
 				   gda_sql_builder_add_cond (b, 0, GDA_SQL_OPERATOR_TYPE_EQ,
 							     gda_sql_builder_add_id (b, 0, "t.id"),
 							     gda_sql_builder_add_id (b, 0, "d.topic"), 0));
-	sub = gda_sql_builder_get_sql_statement (b);
+	sub = gda_sql_statement_copy (gda_sql_builder_get_sql_statement (b));
 	g_object_unref (b);
 
 	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
@@ -189,6 +189,7 @@ main (int argc, char *argv[])
 	gda_sql_builder_add_field_id (b, gda_sql_builder_add_id (b, 0, "id"), 0);
 	gda_sql_builder_add_field_id (b, gda_sql_builder_add_id (b, 0, "name"), 0);
 	gda_sql_builder_add_field_id (b, gda_sql_builder_add_sub_select (b, 0, sub), 0);
+	gda_sql_statement_free (sub);
 
 	render_as_sql (b);
 	g_object_unref (b);
@@ -201,7 +202,7 @@ main (int argc, char *argv[])
 	gda_sql_builder_select_add_target_id (b, 0,
 					   gda_sql_builder_add_id (b, 0, "subdate"),
 					   NULL);
-	sub = gda_sql_builder_get_sql_statement (b);
+	sub = gda_sql_statement_copy (gda_sql_builder_get_sql_statement (b));
 	g_object_unref (b);
 
 	b = gda_sql_builder_new (GDA_SQL_STATEMENT_INSERT);
@@ -211,6 +212,7 @@ main (int argc, char *argv[])
 	gda_sql_builder_add_field_id (b, gda_sql_builder_add_id (b, 0, "f"), 0);
 	gda_sql_builder_add_field_id (b, gda_sql_builder_add_id (b, 0, "g"), 0);
 	gda_sql_builder_add_field_id (b, gda_sql_builder_add_sub_select (b, 0, sub), 0);
+	gda_sql_statement_free (sub);
 	
 	render_as_sql (b);
 	g_object_unref (b);
@@ -224,7 +226,7 @@ main (int argc, char *argv[])
 	gda_sql_builder_select_add_target_id (b, 0,
 					   gda_sql_builder_add_id (b, 0, "subdata1"),
 					   NULL);
-	sub1 = gda_sql_builder_get_sql_statement (b);
+	sub1 = gda_sql_statement_copy (gda_sql_builder_get_sql_statement (b));
 	g_object_unref (b);
 
 	b = gda_sql_builder_new (GDA_SQL_STATEMENT_SELECT);
@@ -233,12 +235,14 @@ main (int argc, char *argv[])
 	gda_sql_builder_select_add_target_id (b, 0,
 					   gda_sql_builder_add_id (b, 0, "subdata2"),
 					   NULL);
-	sub2 = gda_sql_builder_get_sql_statement (b);
+	sub2 = gda_sql_statement_copy (gda_sql_builder_get_sql_statement (b));
 	g_object_unref (b);
 
 	b = gda_sql_builder_new (GDA_SQL_STATEMENT_COMPOUND);
 	gda_sql_builder_compound_add_sub_select (b, sub1);
 	gda_sql_builder_compound_add_sub_select (b, sub2);
+	gda_sql_statement_free (sub1);
+	gda_sql_statement_free (sub2);
 	render_as_sql (b);
 	g_object_unref (b);
 
@@ -341,6 +345,26 @@ main (int argc, char *argv[])
 	render_as_sql (b);
 	g_object_unref (b);
 
+	/* Subselect: SELECT name FROM (SELECT id FROM subdata) as sub */
+	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_add_field_id (b, gda_sql_builder_add_id (b, 0, "name"), 0);
+	gda_sql_builder_select_add_target_id (b, 0,
+					      gda_sql_builder_add_id (b, 0, "subdata"),
+					      NULL);
+	sub = gda_sql_statement_copy (gda_sql_builder_get_sql_statement (b));
+	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, "name"), 0);
+	gda_sql_builder_select_add_target_id (b, 0,
+					      gda_sql_builder_add_sub_select (b, 0, sub), "sub");
+	gda_sql_statement_free (sub);
+	
+	render_as_sql (b);
+	g_object_unref (b);
+
+
 	return 0;
 }
 



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