glom r1905 - in trunk: . glom glom/libglom/connectionpool_backends glom/libglom/data_structure glom/utility_widgets/db_adddel



Author: murrayc
Date: Wed Feb 18 10:11:34 2009
New Revision: 1905
URL: http://svn.gnome.org/viewvc/glom?rev=1905&view=rev

Log:
2009-02-18  Murray Cumming   <murrayc murrayc com>

* glom/libglom/data_structure/field.[h|cc[: sql(value, format):
Replace this with sql(value, connection). Use 
Gda::DataHolder::get_sql_from_value(), using the connection to get the 
DataHolder. Remove the nasty Postgres escaping code, which is now 
unnecessary. There is now no code in Glom to do escaping or unescaping 
- we use libgda functions instead.
sql(value): Use the active connection.
* glom/libglom/connectionpool_backends/postgres.cc: change_columns():
* glom/libglom/connectionpool_backends/sqlite.cc: recreate_table():
Adapted, though these can probably just call sql(value), using the 
active connection. 

* glom/base_db.[h|cc]: get_connection(): Make this static because it 
can be.

Modified:
   trunk/ChangeLog
   trunk/glom/base_db.cc
   trunk/glom/base_db.h
   trunk/glom/libglom/connectionpool_backends/postgres.cc
   trunk/glom/libglom/connectionpool_backends/sqlite.cc
   trunk/glom/libglom/data_structure/field.cc
   trunk/glom/libglom/data_structure/field.h
   trunk/glom/utility_widgets/db_adddel/db_adddel.cc

Modified: trunk/glom/base_db.cc
==============================================================================
--- trunk/glom/base_db.cc	(original)
+++ trunk/glom/base_db.cc	Wed Feb 18 10:11:34 2009
@@ -1487,7 +1487,7 @@
 }
 #endif
 
-Glib::RefPtr<Gnome::Gda::Connection> Base_DB::get_connection() const
+Glib::RefPtr<Gnome::Gda::Connection> Base_DB::get_connection()
 {
 #ifdef GLIBMM_EXCEPTIONS_ENABLED
   sharedptr<SharedConnection> sharedconnection;

Modified: trunk/glom/base_db.h
==============================================================================
--- trunk/glom/base_db.h	(original)
+++ trunk/glom/base_db.h	Wed Feb 18 10:11:34 2009
@@ -370,7 +370,7 @@
    */
   void update_gda_metastore_for_table(const Glib::ustring& table_name) const;
   
-  Glib::RefPtr<Gnome::Gda::Connection> get_connection() const;
+  static Glib::RefPtr<Gnome::Gda::Connection> get_connection();
 
   static bool get_field_primary_key_index_for_fields(const type_vecFields& fields, guint& field_column);
   static bool get_field_primary_key_index_for_fields(const type_vecLayoutFields& fields, guint& field_column);

Modified: trunk/glom/libglom/connectionpool_backends/postgres.cc
==============================================================================
--- trunk/glom/libglom/connectionpool_backends/postgres.cc	(original)
+++ trunk/glom/libglom/connectionpool_backends/postgres.cc	Wed Feb 18 10:11:34 2009
@@ -340,7 +340,7 @@
       {
         if(old_fields[i]->get_default_value() != new_fields[i]->get_default_value())
         {
-          if(!query_execute(connection, "ALTER TABLE \"" + table_name + "\" ALTER COLUMN \"" + old_fields[i]->get_name() + "\" SET DEFAULT " + new_fields[i]->sql(new_fields[i]->get_default_value(), Field::SQL_FORMAT_POSTGRES), error))
+          if(!query_execute(connection, "ALTER TABLE \"" + table_name + "\" ALTER COLUMN \"" + old_fields[i]->get_name() + "\" SET DEFAULT " + new_fields[i]->sql(new_fields[i]->get_default_value(), connection), error))
             break;
         }
       }

Modified: trunk/glom/libglom/connectionpool_backends/sqlite.cc
==============================================================================
--- trunk/glom/libglom/connectionpool_backends/sqlite.cc	(original)
+++ trunk/glom/libglom/connectionpool_backends/sqlite.cc	Wed Feb 18 10:11:34 2009
@@ -289,7 +289,7 @@
         trans_fields += ",";
       Gnome::Gda::Value default_value = field->get_default_value();
       if(default_value.get_value_type() != G_TYPE_NONE && !default_value.is_null())
-        trans_fields += field->sql(default_value, Field::SQL_FORMAT_SQLITE);
+        trans_fields += field->sql(default_value, connection);
       else
       {
         switch(field->get_glom_type())

Modified: trunk/glom/libglom/data_structure/field.cc
==============================================================================
--- trunk/glom/libglom/data_structure/field.cc	(original)
+++ trunk/glom/libglom/data_structure/field.cc	Wed Feb 18 10:11:34 2009
@@ -180,186 +180,54 @@
   return ( m_lookup_relationship && (!m_strLookupField.empty()) );
 }
 
-namespace { //anonymous
-
-//A copy of PQescapeString from the Postgres source, to avoid linking with libpql directly, 
-//and to use until we can use the latest libgda, which has an equivalent.
-//TODO: Now that we use libgda-3.0, actually use that equivalent.
-
-#define SQL_STR_DOUBLE(ch) ((ch) == '\'' || (ch) == '\\')
-
-/*
- * Escaping arbitrary strings to get valid SQL literal strings.
- *
- * Replaces "\\" with "\\\\" and "'" with "''".
- *
- * length is the length of the source string.  (Note: if a terminating NUL
- * is encountered sooner, PQescapeString stops short of "length"; the behavior
- * is thus rather like strncpy.)
- *
- * For safety the buffer at "to" must be at least 2*length + 1 bytes long.
- * A terminating NUL character is added to the output string, whether the
- * input is NUL-terminated or not.
- *
- * Returns the actual length of the output (not counting the terminating NUL).
- */
-size_t
-Glom_PQescapeString(char *to, const char *from, size_t length)
-{
-	const char *source = from;
-	char	   *target = to;
-	size_t		remaining = length;
-
-	while (remaining > 0 && *source != '\0')
-	{
-		if(SQL_STR_DOUBLE(*source))
-			*target++ = *source;
-
-		*target++ = *source++;
-		remaining--;
-	}
-
-	/* Write the terminating NUL character. */
-	*target = '\0';
-
-	return target - to;
-}
-
-} //anonymous
-
-/// Escape text, including text that is the result of get_escaped_binary_data().
-static std::string glom_escape_text(const std::string& src)
-{
-  if(src.empty())
-    return "''"; //We want to ignore the concept of NULL strings, and deal only with empty strings.
-  else
-  {
-    const size_t len = src.size();
-    char* to = (char*)malloc(sizeof(char) * 2 * (len + 1)); //recommended size for to.
-    //TODO: Use gda_data_handler_get_sql_from_value() instead.
-    //See gda_server_provider_get_data_handler_g_type().
-    const size_t len_escaped = Glom_PQescapeString(to, src.c_str(), len);
-    if(!len_escaped)
-    {
-      std::cerr << "glom_escape_text(): Glom_PQescapeString() failed with text: " << src << std::endl;
-
-      if(to)
-        free(to);
-
-      return "''";
-    }
-    else
-    {
-      std::string escaped(to, len_escaped);
-      free(to);
-
-      //Also escape any ";" characters, because these also cause problems, at least with libgda:
-      //See bug #326325.
-      escaped = Utils::string_replace(escaped, ";", "\\073");
-
-      return ("'" + escaped + "'"); //Add single-quotes. Actually escape it 
-      //std::cout << "glom_escape_text: escaped and quoted: " << str << std::endl;
-    }
-  }
-}
-
-Glib::ustring Field::sql(const Gnome::Gda::Value& value, sql_format format) const
+Glib::ustring Field::sql(const Gnome::Gda::Value& value, const Glib::RefPtr<Gnome::Gda::Connection>& connection) const
 {
   //g_warning("Field::sql: glom_type=%d", get_glom_type());
 
-  if(value.is_null())
+  if(value.is_null() && (get_glom_type() == TYPE_TEXT))
   {
-    switch(get_glom_type())
-    {
-      case(TYPE_TEXT):
-      {
-        return "''"; //We want to ignore the concept of NULL strings, and deal only with empty strings.
-        break;
-      }
-      case(TYPE_DATE):
-      case(TYPE_TIME):
-      case(TYPE_NUMERIC):
-      case(TYPE_IMAGE):
-      {
-        return "NULL";
-        break;
-      }
-      case(TYPE_INVALID):
-      {
-        g_warning("Field::sql(): The field type is INVALID.");
-        return "NULL";
-        break;
-      }
-      default:
-      {
-        //Don't deal with these here.
-        break;
-      }
-    }
+    return "''"; //We want to ignore the concept of NULL strings, and deal only with empty strings.
   }
 
-  Glib::ustring str;
 
-  switch(get_glom_type())
+  //Use libgda's DataHandler to get the string for SQL:
+  Glib::RefPtr<const Gnome::Gda::ServerProvider> provider = connection->get_provider();
+  if(!provider)
+  {
+    std::cerr << "Field::sql(): The ServerProvider was null." << std::endl;
+    return Glib::ustring();
+  } 
+
+  const GType gda_type = get_gda_type_for_glom_type(m_glom_type);
+  Glib::RefPtr<const Gnome::Gda::DataHandler> datahandler = 
+    provider->get_data_handler_g_type(connection, gda_type);
+  if(datahandler)
+  {
+    //Note that this does seems to add the needed quotes too, for instance around strings,
+    //though that is not clearly documented. See bug http://bugzilla.gnome.org/show_bug.cgi?id=572220
+    return datahandler->get_sql_from_value(value);
+  }
+  else
   {
-    case(TYPE_TEXT):
-    {
-      if(value.is_null())
-        return "''"; //We want to ignore the concept of NULL strings, and deal only with empty strings.
-      else
-      {
-        str = value.get_string();
-        str = glom_escape_text(str);
-      }
-
-      break;
-    }
-    case(TYPE_DATE):
-    case(TYPE_TIME):
-    {
-      NumericFormat format_ignored; //Because we use ISO format.
-      str = Conversions::get_text_for_gda_value(m_glom_type, value, std::locale() /* SQL uses the C locale */, format_ignored, true /* ISO standard */);
-      if(str != "NULL")
-         str = "'" + str + "'"; //Add single-quotes.
-
-      break;
-    }
-    case(TYPE_NUMERIC):
-    {
-      str =  Conversions::get_text_for_gda_value(m_glom_type, value, std::locale() /* SQL uses the C locale */); //No quotes for numbers.
-      break;
-    }
-    case(TYPE_BOOLEAN):
-    {
-      if(G_VALUE_TYPE(value.gobj()) == G_TYPE_BOOLEAN)
-        str = (value.get_boolean() ? "TRUE" : "FALSE" );
-      else
-        str = "FALSE";
-
-      break;
-    }
-    case(TYPE_IMAGE):
-    {
-      std::cerr << "Field::sql(): Field type TYPE_IMAGE is not supported. A SQL parameter should be used instead." << std::endl;
-      break;
-    }
-    default:
-    {
-      str = value.to_string();
-      if(str.empty() && (m_glom_type != Field::TYPE_TEXT))
-        str = "NULL"; //This has probably been handled in get_text_for_gda_value() anyway.
-
-      break;
-    }
+    std::cerr << "Field::sql(): The DataHandler was null." << std::endl;
+    return Glib::ustring();
   }
 
-  return str;
+  return Glib::ustring();
 }
 
 Glib::ustring Field::sql(const Gnome::Gda::Value& value) const
 {
-  sql_format format = ConnectionPool::get_instance()->get_sql_format();
-  return sql(value, format);
+  //TODO: Handle exceptions as in BaseDB::connect().
+  sharedptr<SharedConnection> connection = ConnectionPool::get_instance()->connect();
+  if(connection)
+  {
+    Glib::RefPtr<Gnome::Gda::Connection> gda_connection = connection->get_gda_connection();
+    if(gda_connection)
+      return sql(value, gda_connection);
+  }
+
+  return Glib::ustring();
 }
 
 Glib::ustring Field::to_file_format(const Gnome::Gda::Value& value) const
@@ -443,7 +311,7 @@
       if(value.is_null())
         return "''"; //We want to ignore the concept of NULL strings, and deal only with empty strings.
       else
-        return ("'%" + value.to_string() + "%'"); //Add single-quotes. Actually escape it.
+        return ("'%" + value.to_string() + "%'"); //Add single-quotes. TODO: Actually escape it.
         
       break;
     }

Modified: trunk/glom/libglom/data_structure/field.h
==============================================================================
--- trunk/glom/libglom/data_structure/field.h	(original)
+++ trunk/glom/libglom/data_structure/field.h	Wed Feb 18 10:11:34 2009
@@ -167,8 +167,9 @@
 
   /** Escape and quote the value so that it can be used in a SQL command.
    */
-  Glib::ustring sql(const Gnome::Gda::Value& value, sql_format format) const;
+  Glib::ustring sql(const Gnome::Gda::Value& value, const Glib::RefPtr<Gnome::Gda::Connection>& connection) const;
 
+  //TODO: Remove this:
   /** Escape and quote the value so that it can be used in a SQL command.
    * Uses the sql_format of the current connectionpool backend.
    */

Modified: trunk/glom/utility_widgets/db_adddel/db_adddel.cc
==============================================================================
--- trunk/glom/utility_widgets/db_adddel/db_adddel.cc	(original)
+++ trunk/glom/utility_widgets/db_adddel/db_adddel.cc	Wed Feb 18 10:11:34 2009
@@ -2247,8 +2247,8 @@
       const bool bTest = set_field_value_in_database(field_in_record, row, field_value, false /* don't use current calculations */, window);
 
       //Glib::ustring strQuery = "UPDATE \"" + table_name + "\"";
-      //strQuery += " SET " +  /* table_name + "." + postgres does not seem to like the table name here */ strFieldName + " = " + field.sql(field_value);
-      //strQuery += " WHERE " + table_name + "." + primary_key_field.get_name() + " = " + primary_key_field.sql(primary_key_value);
+      //strQuery += " SET " +  /* table_name + "." + postgres does not seem to like the table name here */ strFieldName + " = " + field.sql(field_value, connection);
+      //strQuery += " WHERE " + table_name + "." + primary_key_field.get_name() + " = " + primary_key_field.sql(primary_key_value, connection);
       //bool bTest = query_execute(strQuery);
       if(!bTest)
       {



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