[libgdamm] Updated GdaSqlBuilder bindings to use new method names from libgda



commit 1f54174bd9b395713205ed0090bcc0b77c9211e2
Author: Johannes Schmid <jhs gnome org>
Date:   Thu Oct 1 21:39:36 2009 +0200

    Updated GdaSqlBuilder bindings to use new method names from libgda
    
    All methods now return an automatically generated id which should be stored
    somewhere when it needs to reused. Manual id definition like in the C
    API has been removed.

 ChangeLog                      |   12 +++++
 examples/sqlbuilder/main.cc    |   75 +++++++++++++++----------------
 libgda/src/libgda_methods.defs |   24 +++++-----
 libgda/src/sqlbuilder.ccg      |   48 ++++++++++++++++----
 libgda/src/sqlbuilder.hg       |   96 +++++++++++++++++++++++++++++++---------
 5 files changed, 175 insertions(+), 80 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 237f296..223cbc4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2009-10-01  Johannes Schmid  <jschmid openismus com>
+
+	* libgda/src/sqlbuilder.hg/ccg:
+	* libgda/src/libgda_methods.defs:
+	Updated GdaSqlBuilder bindings to use new method names from libgda
+	
+	All methods now return an automatically generated id which should be stored
+	somewhere when it needs to reused. Manual id definition like in the C
+	API has been removed.
+	
+	* examples/sqlbuilder/main.cc: Updated example to new API
+
 2009-09-30  Johannes Schmid  <jschmid openismis com>
   
 	* libgda/src/row.hg: 
diff --git a/examples/sqlbuilder/main.cc b/examples/sqlbuilder/main.cc
index 5444b18..52a3f0d 100644
--- a/examples/sqlbuilder/main.cc
+++ b/examples/sqlbuilder/main.cc
@@ -13,12 +13,12 @@ int main()
     Gnome::Gda::SqlBuilder::create(Gnome::Gda::SQL_STATEMENT_INSERT);
 
   ins_builder->set_table("customer");
-  ins_builder->add_field(ins_builder->ident(0, "e"),
-                         ins_builder->param(0, "p1", G_TYPE_STRING, false));
-  ins_builder->add_field(ins_builder->ident(0, "f"),
-                         ins_builder->expr(0, Gnome::Gda::Value(15)));
-  ins_builder->add_field(ins_builder->ident(0, "g"),
-                         ins_builder->expr(0, Gnome::Gda::Value("joe")));
+  ins_builder->add_field(ins_builder->add_id("e"),
+                         ins_builder->add_param("p1", G_TYPE_STRING, false));
+  ins_builder->add_field(ins_builder->add_id("f"),
+                         ins_builder->add_expr(Gnome::Gda::Value(15)));
+  ins_builder->add_field(ins_builder->add_id("g"),
+                         ins_builder->add_expr(Gnome::Gda::Value("joe")));
   
   render_as_sql (ins_builder);
 
@@ -27,19 +27,19 @@ int main()
     Gnome::Gda::SqlBuilder::create(Gnome::Gda::SQL_STATEMENT_UPDATE);
   
   up_builder->set_table("customer");
-  up_builder->add_field(up_builder->ident(0, "ref"),
-                        up_builder->expr(0, Gnome::Gda::Value("A0E'FESP")));
-  up_builder->ident(1, "id");
-  up_builder->expr(2, Gnome::Gda::Value(14));
-  up_builder->cond(3, Gnome::Gda::SQL_OPERATOR_TYPE_EQ, 1, 2, 0);
-  up_builder->set_where(3);
+  up_builder->add_field(up_builder->add_id("ref"),
+                        up_builder->add_expr(Gnome::Gda::Value("A0E'FESP")));
+  guint id = up_builder->add_id("id");
+  guint value = up_builder->add_expr(Gnome::Gda::Value(14));
+  guint cond = up_builder->add_cond(Gnome::Gda::SQL_OPERATOR_TYPE_EQ, id, value, 0);
+  up_builder->set_where(cond);
 
   render_as_sql (up_builder);
   
   // reuse the same GdaSqlBuilder object to change the WHERE condition to: WHERE id = ##theid::int
-  up_builder->set_where(up_builder->cond(0, Gnome::Gda::SQL_OPERATOR_TYPE_EQ,
-                                         1,
-                                         up_builder->param (0, "theid", G_TYPE_INT, false),
+  up_builder->set_where(up_builder->add_cond(Gnome::Gda::SQL_OPERATOR_TYPE_EQ,
+                                         id,
+                                         up_builder->add_param ("theid", G_TYPE_INT, false),
                                          0));
   render_as_sql (up_builder);
 
@@ -51,46 +51,45 @@ int main()
   Glib::RefPtr<Gnome::Gda::SqlBuilder> sel_builder =
     Gnome::Gda::SqlBuilder::create(Gnome::Gda::SQL_STATEMENT_SELECT);
 
-  sel_builder->ident(1, "select"); // SELECT is an sql keyword
-  sel_builder->select_add_target(1, 1, "c");
-  sel_builder->select_add_target(2,
-                                 sel_builder->ident(0, "orders"));
-  sel_builder->select_join_targets(5, 1, 2, Gnome::Gda::SQL_SELECT_JOIN_INNER, 0);
-  sel_builder->add_field(sel_builder->ident(0, "c.date"), 0);
-  sel_builder->add_field(sel_builder->ident(0, "name"),
-                          sel_builder->ident(0, "person"));
+  id = sel_builder->add_id("select"); // SELECT is an sql keyword
+  guint target_c = sel_builder->select_add_target(id, "c");
+  guint target_orders = sel_builder->select_add_target(sel_builder->add_id("orders"));
+  guint join = sel_builder->select_join_targets(target_c, target_orders, Gnome::Gda::SQL_SELECT_JOIN_INNER);
+  sel_builder->add_field(sel_builder->add_id("c.date"));
+  sel_builder->add_field(sel_builder->add_id("name"),
+                         sel_builder->add_id("person"));
   render_as_sql(sel_builder);
 
   // reuse the same GdaSqlBuilder object to change the INNER join's condition
-  sel_builder->join_add_field (5, "id");
+  sel_builder->join_add_field (join, "id");
   render_as_sql(sel_builder);
 
   // SELECT myfunc (a, 5, 'Joe') FROM mytable
   Glib::RefPtr<Gnome::Gda::SqlBuilder> func_builder = 
     Gnome::Gda::SqlBuilder::create(Gnome::Gda::SQL_STATEMENT_SELECT);
   
-  func_builder->select_add_target(0, func_builder->ident(0, "mytable"));
+  func_builder->select_add_target(func_builder->add_id("mytable"));
 
   std::vector<guint> args;
-  args.push_back(func_builder->ident(0, "a"));
-  args.push_back(func_builder->expr(0, Gnome::Gda::Value(5)));
-  args.push_back(func_builder->expr(0, Gnome::Gda::Value("Joe")));
-  func_builder->add_function(1, "myfunc",
-                             args);
-	func_builder->add_field (1, 0);
+  args.push_back(func_builder->add_id("a"));
+  args.push_back(func_builder->add_expr(Gnome::Gda::Value(5)));
+  args.push_back(func_builder->add_expr(Gnome::Gda::Value("Joe")));
+  guint func = func_builder->add_function("myfunc",
+                                          args);
+	func_builder->add_field (func);
 	render_as_sql (func_builder);
   
   /* reuse the same GdaSqlBuilder object to have:
    * SELECT myfunc (a, 5, 'Joe'), MAX (myfunc (a, 5, 'Joe'), b, 10) FROM mytable */
+	guint id_b = func_builder->add_id("b");
+	guint expr_ten = func_builder->add_expr (Gnome::Gda::Value(10));
   std::vector<guint> args2;
-  args2.push_back(1);
-  args2.push_back(3);
-  args2.push_back(4);
-	func_builder->ident(3, "b");
-	func_builder->expr (4, Gnome::Gda::Value(10));
+  args2.push_back(func);
+  args2.push_back(id_b);
+  args2.push_back(expr_ten);
 
-	func_builder->add_function (5, "MAX", args2);
-	func_builder->add_field (5, 0);
+	guint func_max = func_builder->add_function ("MAX", args2);
+	func_builder->add_field (func_max);
 
 	render_as_sql (func_builder);
   
diff --git a/libgda/src/libgda_methods.defs b/libgda/src/libgda_methods.defs
index 505fb8c..43a5fbc 100644
--- a/libgda/src/libgda_methods.defs
+++ b/libgda/src/libgda_methods.defs
@@ -5029,9 +5029,9 @@
   )
 )
 
-(define-method ident
+(define-method add_id
   (of-object "GdaSqlBuilder")
-  (c-name "gda_sql_builder_ident")
+  (c-name "gda_sql_builder_add_id")
   (return-type "guint")
   (parameters
     '("guint" "id")
@@ -5039,9 +5039,9 @@
   )
 )
 
-(define-method expr
+(define-method add_expr
   (of-object "GdaSqlBuilder")
-  (c-name "gda_sql_builder_expr")
+  (c-name "gda_sql_builder_add_expr")
   (return-type "guint")
   (parameters
     '("guint" "id")
@@ -5051,9 +5051,9 @@
   (varargs #t)
 )
 
-(define-method expr_value
+(define-method add_expr_value
   (of-object "GdaSqlBuilder")
-  (c-name "gda_sql_builder_expr_value")
+  (c-name "gda_sql_builder_add_expr_value")
   (return-type "guint")
   (parameters
     '("guint" "id")
@@ -5062,9 +5062,9 @@
   )
 )
 
-(define-method param
+(define-method add_param
   (of-object "GdaSqlBuilder")
-  (c-name "gda_sql_builder_param")
+  (c-name "gda_sql_builder_add_param")
   (return-type "guint")
   (parameters
     '("guint" "id")
@@ -5074,9 +5074,9 @@
   )
 )
 
-(define-method cond
+(define-method add_cond
   (of-object "GdaSqlBuilder")
-  (c-name "gda_sql_builder_cond")
+  (c-name "gda_sql_builder_add_cond")
   (return-type "guint")
   (parameters
     '("guint" "id")
@@ -5087,9 +5087,9 @@
   )
 )
 
-(define-method cond_v
+(define-method add_cond_v
   (of-object "GdaSqlBuilder")
-  (c-name "gda_sql_builder_cond_v")
+  (c-name "gda_sql_builder_add_cond_v")
   (return-type "guint")
   (parameters
     '("guint" "id")
diff --git a/libgda/src/sqlbuilder.ccg b/libgda/src/sqlbuilder.ccg
index 1bd4ee5..0c26652 100644
--- a/libgda/src/sqlbuilder.ccg
+++ b/libgda/src/sqlbuilder.ccg
@@ -35,25 +35,55 @@ SqlBuilder::SqlBuilder(SqlStatementType type)
 {
   
 }
-  
-guint SqlBuilder::cond_v(uint id, SqlOperatorType op, Glib::ArrayHandle<uint> op_ids)
+
+guint SqlBuilder::add_id(const Glib::ustring& string)
+{
+  return gda_sql_builder_add_id(gobj(), 0, string.c_str());
+}
+
+guint SqlBuilder::add_expr(const Glib::RefPtr<DataHandler>& dh, Value value)
+{
+  return gda_sql_builder_add_expr_value(gobj(), 0, NULL, value.gobj());
+}
+
+guint SqlBuilder::add_expr(Value value)
+{
+  return gda_sql_builder_add_expr_value(gobj(), 0, NULL, value.gobj());
+}
+
+guint SqlBuilder::add_param(const Glib::ustring& param_name, GType type, bool nullok)
+{
+  return gda_sql_builder_add_param(gobj(), 0, param_name.c_str(), type, nullok);
+}
+
+guint SqlBuilder::add_cond(SqlOperatorType op, guint op1, guint op2, guint op3)
+{
+  return gda_sql_builder_add_cond(gobj(), 0, (GdaSqlOperatorType) op, op1, op2, op3);
+}
+
+guint SqlBuilder::add_cond_v(SqlOperatorType op, Glib::ArrayHandle<uint> op_ids)
+{
+  return gda_sql_builder_add_cond_v(gobj(), 0, (GdaSqlOperatorType) op, op_ids.data(), op_ids.size());
+}
+
+guint SqlBuilder::add_function(const Glib::ustring& function_name, Glib::ArrayHandle<guint> args)
 {
-  return gda_sql_builder_cond_v(gobj(), id, (GdaSqlOperatorType) op, op_ids.data(), op_ids.size());
+  return gda_sql_builder_add_function_v(gobj(), 0, function_name.c_str(), args.data(), args.size());
 }
 
-guint SqlBuilder::add_function(guint id, const Glib::ustring& function_name, Glib::ArrayHandle<guint> args)
+guint SqlBuilder::select_add_target(guint table_id, const Glib::ustring& alias)
 {
-  return gda_sql_builder_add_function_v(gobj(), id, function_name.c_str(), args.data(), args.size());
+  return gda_sql_builder_select_add_target(gobj(), 0, table_id, alias.c_str());
 }
 
-guint SqlBuilder::expr(guint id, Value value)
+guint SqlBuilder::select_add_target(guint table_id)
 {
-  return gda_sql_builder_expr_value(gobj(), id, NULL, value.gobj());
+  return gda_sql_builder_select_add_target(gobj(), 0, table_id, NULL);
 }
 
-guint SqlBuilder::select_add_target(guint id, guint table_id)
+guint SqlBuilder::select_join_targets(guint left_target_id, guint right_target_id, SqlSelectJoinType join_type, guint join_expr)
 {
-  return gda_sql_builder_select_add_target(gobj(), id, table_id, NULL);
+  return gda_sql_builder_select_join_targets(gobj(), 0, left_target_id, right_target_id, (GdaSqlSelectJoinType) join_type, join_expr);
 }
 
 } /* namespace Gda */
diff --git a/libgda/src/sqlbuilder.hg b/libgda/src/sqlbuilder.hg
index 2f64466..1400871 100644
--- a/libgda/src/sqlbuilder.hg
+++ b/libgda/src/sqlbuilder.hg
@@ -49,51 +49,105 @@ public:
   // TODO: Wrap SqlStatement
   //_WRAP_METHOD(Glib::RefPtr<SqlStatement> get_sql_statement(bool copy_it), gda_sql_builder_get_sql_statement, errthrow)
 
+  _IGNORE(gda_sql_builder_add_expr, gda_sql_builder_add_param, gda_sql_builder_add_cond)
+  _IGNORE(gda_sql_builder_add_cond_v, gda_sql_builder_add_id)
+  _IGNORE(gda_sql_builder_add_expr_value)
   // Expression API
-  _WRAP_METHOD(guint ident(guint id, const Glib::ustring& string), gda_sql_builder_ident)
-  _IGNORE(gda_sql_builder_expr)
-  _WRAP_METHOD(guint expr(guint id, const Glib::RefPtr<DataHandler>& dh, Value value), gda_sql_builder_expr_value)
-   /** Defines an expression in Builder which may be reused to build other parts of a statement.
-   * @param id: the requested ID, or 0 if to be determined automatically
+  /** Defines an expression which may be reused to build other parts of a statement.
+   * @param string: string to use in the SQL
+   *
+   * @returns: Returns: the ID of the new expression, or 0 if there was an error
+   */
+  guint add_id(const Glib::ustring& string);
+
+  /** Defines an expression in Builder which may be reused to build other parts of a statement.
+   * @param dh: the datahandler to use
    * @param value: value to set the expression to
    * 
    * See expr() except that no custom datahandler is given 
    *
    * @return the ID of the new expression, or 0 if there was an error
    */
-  guint expr(guint id, Value value);
-  _WRAP_METHOD(guint param(guint id, const Glib::ustring& param_name, GType type, bool nullok), gda_sql_builder_param)
-  _WRAP_METHOD(guint cond(guint id, SqlOperatorType op, guint op1, guint op2, guint op3), gda_sql_builder_cond)
-  _WRAP_METHOD_DOCS_ONLY(gda_sql_builder_cond_v)
-  guint cond_v(guint id, SqlOperatorType op, Glib::ArrayHandle<guint> op_ids);
+  guint add_expr(const Glib::RefPtr<DataHandler>& dh, Value value);
+  /** Defines an expression in Builder which may be reused to build other parts of a statement.
+   * @param value: value to set the expression to
+   * 
+   * See expr() except that no custom datahandler is given 
+   *
+   * @return the ID of the new expression, or 0 if there was an error
+   */
+  guint add_expr(Value value);
+
+  /** Defines a parameter which may be reused to build other parts of a statement.
+   * @param: param_name: name of the parameter
+   * @type: GType of the parameter
+   * @nullok: true if the parameter can be set to %NULL
+   *
+   * @return the ID of the new expression, or 0 if there was an error
+   */
+  guint add_param(const Glib::ustring& param_name, GType type, bool nullok);
+
+  /** Builds a new expression which reprenents a condition (or operation).
+   * @param op: type of condition
+   * @param op1: the ID of the 1st argument (not 0)
+   * @param op2: the ID of the 2st argument (maybe 0 if op only requires one argument)
+   * @param op3: the ID of the 3st argument (maybe 0 if op only requires one or two arguments)
+   *
+   * @return the ID of the new expression, or 0 if there was an error
+   */
+  guint add_cond(SqlOperatorType op, guint op1, guint op2, guint op3);
+
+  /** Builds a new expression which reprenents a condition (or operation).
+   * @param op: type of condition
+   * @param op_ids: list of IDs of the operands of the condition
+   *
+   * @return the ID of the new expression, or 0 if there was an error
+   */
+  guint add_cond_v(SqlOperatorType op, Glib::ArrayHandle<guint> op_ids);
 
   _IGNORE(gda_sql_builder_add_function, gda_sql_builder_add_function_v)
+  _IGNORE(gda_sql_builder_select_add_target, gda_sql_builder_select_join_targets)
   /** add_function
-    * @param id: the requested ID, or 0 if to be determined automatically
     * @param function_name: the name of the function to embed (e.g. MAX, MIN, ...)
     * @param args: list of ids of the arguments to the function
     *
     * @return the ID of the new target, or 0 if there was an error
     */
-  guint add_function(guint id, const Glib::ustring& function_name, Glib::ArrayHandle<guint> args);
+  guint add_function(const Glib::ustring& function_name, Glib::ArrayHandle<guint> args);
 
   // Statement API
-  _WRAP_METHOD(guint select_add_target(guint id, guint table_id, const Glib::ustring& alias), gda_sql_builder_select_add_target)
+  /** Adds a new target to a SELECT statement
+   * @param table_id: the ID of the expression holding a table reference
+   * @param alias: the alias to give to the target
+   *
+   * @return the ID of the new target, or 0 if there was an error
+   */
+  guint select_add_target(guint table_id, const Glib::ustring& alias);
+
   /** select_add_target with no alias given
-    * @param id: the requested ID, or 0 if to be determined automatically
-    * @param table_id: the ID of the expression holding a table reference
-    *
-    * @return the ID of the new target, or 0 if there was an error
-    */
-  guint select_add_target(guint id, guint table_id);
-  _WRAP_METHOD(guint select_join_targets(guint id, guint left_target_id, guint right_target_id, SqlSelectJoinType join_type, guint join_expr), gda_sql_builder_select_join_targets)
+   * @param id: the requested ID, or 0 if to be determined automatically
+   * @param table_id: the ID of the expression holding a table reference
+   *
+   * @return the ID of the new target, or 0 if there was an error
+   */
+  guint select_add_target(guint table_id);
+
+  /** Joins two targets in a SELECT statement
+   * @param left_target_id: the ID of the left target to use (not %0)
+   * @param right_target_id: the ID of the right target to use (not %0)
+   * @param join_type: the type of join
+   * @param join_expr: joining expression's ID, or %0
+   *
+   * @return the ID of the new join, or 0 if there was an error
+   */
+  guint select_join_targets(guint left_target_id, guint right_target_id, SqlSelectJoinType join_type, guint join_expr = 0);
   _WRAP_METHOD(void join_add_field(guint join_id, const Glib::ustring& field_name), gda_sql_builder_join_add_field)
   _WRAP_METHOD(void select_order_by(guint expr_id, bool asc, const Glib::ustring& collation_name), gda_sql_builder_select_order_by)
   
   // General Statement API
   _WRAP_METHOD(void set_table(const Glib::ustring& table_name), gda_sql_builder_set_table)
   _WRAP_METHOD(void set_where(guint cond_id), gda_sql_builder_set_where)
-  _WRAP_METHOD(void add_field(guint field_id, guint value_id), gda_sql_builder_add_field)
+  _WRAP_METHOD(void add_field(guint field_id, guint value_id = 0), gda_sql_builder_add_field)
   
 };
 



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