[libgdamm] Fix Gda::Value to correctly accept const gchar*, add overload for Gda::SqlBuilder::select_add_target



commit 4d91ad851771587367d5533bd2c966cc50e38e03
Author: Johannes Schmid <jhs gnome org>
Date:   Tue Sep 29 13:46:33 2009 +0200

    Fix Gda::Value to correctly accept const gchar*, add overload for Gda::SqlBuilder::select_add_target(), add example for Gda::SqlBuilder

 ChangeLog                   |   17 ++++++-
 examples/Makefile.am        |    3 +-
 examples/sqlbuilder/main.cc |   94 +++++++++++++++++++++++++++++++++++++++++++
 libgda/libgdamm/value.cc    |    1 +
 libgda/src/sqlbuilder.ccg   |   13 +++++-
 libgda/src/sqlbuilder.hg    |    7 +++
 6 files changed, 130 insertions(+), 5 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 9b3da9f..26c3ac8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,8 +1,19 @@
+2009-09-29  Johannes Schmid  <jschmid openismus com>
+
+	* libgda/libgdamm/value.cc: Fix set(const gchar*) to call
+	g_value_reinit()
+	* libgda/src/sqlbuilder.hg/ccg: Add overload for select_add_target() to
+	be able to skip the alias (NULL in C)
+	
+	* examples/sqlbuilder/main.cc:
+	* examples/Makefile.am:
+	Add example for the SqlBuilder usage based on the libgda example
+
 2009-09-28  Johannes Schmid  <jschmid openismus com>
 
-  * libgda/src/sqlbuilder.hg: Use Value instead of Value& to make the 
-  SqlBuilder::param() much more convenient to use.
-  * libgda/libgdamm.h: Add sqlbuilder.h
+	* libgda/src/sqlbuilder.hg: Use Value instead of Value& to make the 
+	SqlBuilder::param() much more convenient to use.
+	* libgda/libgdamm.h: Add sqlbuilder.h
 
 2009-09-25  Johannes Schmid  <jschmid openismus com>
 
diff --git a/examples/Makefile.am b/examples/Makefile.am
index f8d4e62..aec861e 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -19,7 +19,7 @@ AUTOMAKE_OPTIONS = subdir-objects
 
 dist_noinst_DATA = README.txt
 
-check_PROGRAMS = config/example simple/example
+check_PROGRAMS = config/example simple/example sqlbuilder/example
 
 libgdamm_includes = -I$(top_builddir)/libgda $(if $(srcdir:.=),-I$(top_srcdir)/libgda)
 
@@ -30,3 +30,4 @@ LDADD = $(LIBGDAMM_LIBS) $(top_builddir)/libgda/libgdamm/libgdamm-$(LIBGDAMM_API
 
 config_example_SOURCES = config/main.cc
 simple_example_SOURCES = simple/main.cc
+sqlbuilder_example_SOURCES = sqlbuilder/main.cc
diff --git a/examples/sqlbuilder/main.cc b/examples/sqlbuilder/main.cc
new file mode 100644
index 0000000..c5e6b46
--- /dev/null
+++ b/examples/sqlbuilder/main.cc
@@ -0,0 +1,94 @@
+#include <libgdamm.h>
+#include <iostream>
+#include <vector>
+
+void render_as_sql (const Glib::RefPtr<Gnome::Gda::SqlBuilder>& builder);
+
+int main()
+{
+  Gnome::Gda::init();
+
+  // INSERT INTO customers (e, f, g) VALUES (##p1::string, 15, 'joe')
+  Glib::RefPtr<Gnome::Gda::SqlBuilder> ins_builder =
+    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")));
+  
+  render_as_sql (ins_builder);
+
+  // UPDATE products set ref='A0E''FESP' WHERE id = 14
+  Glib::RefPtr<Gnome::Gda::SqlBuilder> up_builder =
+    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);
+
+  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),
+                                         0));
+  render_as_sql (up_builder);
+
+  /*
+	 * The next statement shows automatic quoting of reserved SQL keywords (DATE and SELECT here)
+	 *
+	 * SELECT c."date", name, date AS person FROM "select" as c, orders
+	 */
+  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"));
+  render_as_sql(sel_builder);
+
+  // reuse the same GdaSqlBuilder object to change the INNER join's condition
+  sel_builder->join_add_field (5, "id");
+  render_as_sql(sel_builder);
+
+  return 0;
+}
+
+void render_as_sql (const Glib::RefPtr<Gnome::Gda::SqlBuilder>& builder)
+{
+  Glib::RefPtr<Gnome::Gda::Statement> stmt;
+  try
+  {
+    stmt = builder->get_statement();
+  }
+  catch(Gnome::Gda::SqlBuilderError& error)
+  {
+    std::cerr << "SqlBuilderError: " << error.what() << std::endl;
+    return;
+  }
+  Glib::ustring sql;
+  try
+  {
+    sql = stmt->to_sql();
+  }
+  catch(Gnome::Gda::StatementError& error)
+  {
+    std::cerr << "StatementError: " << error.what() << std::endl;
+    return;
+  }
+  std::cout << "SQL: " << sql << std::endl;
+}
\ No newline at end of file
diff --git a/libgda/libgdamm/value.cc b/libgda/libgdamm/value.cc
index a9e4ada..8a5374e 100644
--- a/libgda/libgdamm/value.cc
+++ b/libgda/libgdamm/value.cc
@@ -439,6 +439,7 @@ void Value::set(const Glib::ustring& val)
 
 void Value::set(const char* val)
 {
+  value_reinit(gobj(), G_TYPE_STRING);
   g_value_set_string(gobj(), val);
 }
 
diff --git a/libgda/src/sqlbuilder.ccg b/libgda/src/sqlbuilder.ccg
index 4741e76..f3bd244 100644
--- a/libgda/src/sqlbuilder.ccg
+++ b/libgda/src/sqlbuilder.ccg
@@ -30,16 +30,27 @@ namespace Gnome
 namespace Gda
 {
 
+SqlBuilder::SqlBuilder(SqlStatementType type)
+  : _CONSTRUCT("stmt-type", type)
+{
+  
+}
+  
 guint SqlBuilder::cond_v(uint id, SqlOperatorType op, Glib::ArrayHandle<uint> op_ids)
 {
   return gda_sql_builder_cond_v(gobj(), id, (GdaSqlOperatorType) op, op_ids.data(), op_ids.size());
 }
 
-guint SqlBuilder::expr(guint id, Value& value)
+guint SqlBuilder::expr(guint id, Value value)
 {
   return gda_sql_builder_expr_value(gobj(), id, NULL, value.gobj());
 }
 
+guint SqlBuilder::select_add_target(guint id, guint table_id)
+{
+  return gda_sql_builder_select_add_target(gobj(), id, table_id, NULL);
+}
+
 } /* namespace Gda */
 
 } /* namespace Gnome */
diff --git a/libgda/src/sqlbuilder.hg b/libgda/src/sqlbuilder.hg
index 7eb5d3a..91bba90 100644
--- a/libgda/src/sqlbuilder.hg
+++ b/libgda/src/sqlbuilder.hg
@@ -69,6 +69,13 @@ public:
 
   // Statement API
   _WRAP_METHOD(guint select_add_target(guint id, guint table_id, const Glib::ustring& alias), gda_sql_builder_select_add_target)
+  /** 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)
   _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)



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