glom r1865 - in trunk: . glom



Author: jhs
Date: Thu Jan 29 10:44:51 2009
New Revision: 1865
URL: http://svn.gnome.org/viewvc/glom?rev=1865&view=rev

Log:
2009-01-29  Johannes Schmid  <jschmid openismus com>

	* glom/base_db.cc:
	* glom/base_db_table_data.cc:
	Converted more queries to use parameters

Modified:
   trunk/ChangeLog
   trunk/glom/base_db.cc
   trunk/glom/base_db_table_data.cc

Modified: trunk/glom/base_db.cc
==============================================================================
--- trunk/glom/base_db.cc	(original)
+++ trunk/glom/base_db.cc	Thu Jan 29 10:44:51 2009
@@ -2799,11 +2799,13 @@
   {
     //Convert the value, in case the from and to fields have different types:
     const Gnome::Gda::Value value_to_key_field = Conversions::convert_value(key_value, to_key_field->get_glom_type());
+    Glib::RefPtr<Gnome::Gda::Set> params = Gnome::Gda::Set::create();
+    params->add_holder("key", value_to_key_field);
 
     Glib::ustring strQuery = "SELECT \"" + relationship->get_to_table() + "\".\"" + source_field->get_name() + "\" FROM \"" +  relationship->get_to_table() + "\"";
-    strQuery += " WHERE \"" + to_key_field->get_name() + "\" = " + to_key_field->sql(value_to_key_field);
+    strQuery += " WHERE \"" + to_key_field->get_name() + "\" = ##key::" + to_key_field->get_gda_type();
 
-    Glib::RefPtr<Gnome::Gda::DataModel> data_model = query_execute_select(strQuery);
+    Glib::RefPtr<Gnome::Gda::DataModel> data_model = query_execute_select(strQuery, params);
     if(data_model && data_model->get_n_rows())
     {
       //There should be only 1 row. Well, there could be more but we will ignore them.
@@ -2823,10 +2825,14 @@
   bool result = true;  //Arbitrarily default to saying it's unique if we can't get any result.
 
   const Glib::ustring table_name_used = field->get_table_used(table_name); 
+  Glib::RefPtr<Gnome::Gda::Set> params = Gnome::Gda::Set::create();
+  Field glom_field = *field->get_full_field_details();
+  glom_field.set_data(value);
+  params->add_holder(glom_field.get_holder());
   Glib::ustring strQuery = "SELECT \"" + table_name_used + "\".\"" + field->get_name() + "\" FROM \"" + table_name_used + "\"";
-  strQuery += " WHERE \"" + field->get_name() + "\" = " + field->get_full_field_details()->sql(value);
+  strQuery += " WHERE \"" + field->get_name() + "\" = " + glom_field.get_gda_holder_string();
 
-  Glib::RefPtr<Gnome::Gda::DataModel> data_model = query_execute_select(strQuery);
+  Glib::RefPtr<Gnome::Gda::DataModel> data_model = query_execute_select(strQuery, params);
   if(data_model)
   {
     //std::cout << "debug: Base_DB::get_field_value_is_unique(): table_name=" << table_name << ", field name=" << field->get_name() << ", value=" << value.to_string() << ", rows count=" << data_model->get_n_rows() << std::endl;
@@ -3019,15 +3025,17 @@
   layout_item->set_full_field_details(primary_key);
   fieldsToGet.push_back(layout_item);
 
-
+  Glib::RefPtr<Gnome::Gda::Set> params = Gnome::Gda::Set::create();
+  params->add_holder("primary_key", primary_key_value);
+  
   Glib::ustring where_clause;
   if(!found_set.m_where_clause.empty())
     where_clause = "(" + found_set.m_where_clause + ") AND ";
 
-  where_clause += "(\"" + primary_key->get_name() + "\"=" + primary_key->sql(primary_key_value) + ")";
+  where_clause += "(\"" + primary_key->get_name() + "\" = ##primary_key::" + primary_key->get_gda_type() + ")";
 
   const Glib::ustring query = Utils::build_sql_select_with_where_clause(found_set.m_table_name, fieldsToGet, where_clause);
-  Glib::RefPtr<Gnome::Gda::DataModel> data_model = query_execute_select(query);
+  Glib::RefPtr<Gnome::Gda::DataModel> data_model = query_execute_select(query, params);
 
   if(data_model && data_model->get_n_rows())
   {
@@ -3160,7 +3168,7 @@
  
     //std::cout << "extra_join where_clause_to_key_field=" << where_clause_to_key_field->get_name() << std::endl;
   }
-
+  // TODO: Where is this used? Should we use parameters for this query?
   if(where_clause_to_key_field)
     found_set.m_where_clause = "\"" + where_clause_to_table_name + "\".\"" + relationship->get_to_field() + "\" = " + where_clause_to_key_field->sql(foreign_key_value);
 }

Modified: trunk/glom/base_db_table_data.cc
==============================================================================
--- trunk/glom/base_db_table_data.cc	(original)
+++ trunk/glom/base_db_table_data.cc	Thu Jan 29 10:44:51 2009
@@ -147,7 +147,8 @@
   //Avoid specifying the same field twice:
   typedef std::map<Glib::ustring, bool> type_map_added;
   type_map_added map_added;
-
+  Glib::RefPtr<Gnome::Gda::Set> params = Gnome::Gda::Set::create();
+  
   for(type_vecLayoutFields::const_iterator iter = fieldsToAdd.begin(); iter != fieldsToAdd.end(); ++iter)
   {
     sharedptr<LayoutItem_Field> layout_item = *iter;
@@ -162,6 +163,7 @@
         const sharedptr<const Field>& field = layout_item->get_full_field_details();
         if(field)
         {
+          Field gda_field = *field;
           //Use the specified (generated) primary key value, if there is one:
           if(primary_key_name == field_name && !Conversions::value_is_empty(primary_key_value))
           {
@@ -172,6 +174,7 @@
             if(use_entered_data)
               value = get_entered_field_data(layout_item);
           }
+          gda_field.set_data(value);
 
           /* //TODO: This would be too many small queries when adding one record.
           //Check whether the value meets uniqueness constraints:
@@ -183,9 +186,7 @@
             } 
           }
           */
-
-          const Glib::ustring strFieldValue = field->sql(value);
-          if(!strFieldValue.empty())
+          if(!gda_field.get_data().is_null())
           {
             if(!strNames.empty())
             {
@@ -194,7 +195,10 @@
             }
   
             strNames += "\"" + field_name + "\"";
-            strValues += strFieldValue;
+            strValues += gda_field.get_gda_holder_string();
+            Glib::RefPtr<Gnome::Gda::Holder> holder = gda_field.get_holder();
+            holder->set_not_null(false);
+            params->add_holder(holder);
   
             map_added[field_name] = true;
           }
@@ -207,7 +211,7 @@
   if(!strNames.empty() && !strValues.empty())
   {
     const Glib::ustring strQuery = "INSERT INTO \"" + m_table_name + "\" (" + strNames + ") VALUES (" + strValues + ")";
-    const bool test = query_execute(strQuery);
+    const bool test = query_execute(strQuery, params);
     if(!test)
       std::cerr << "Box_Data::record_new(): INSERT failed." << std::endl;
     else



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