2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Npgsql / NpgsqlTypes / NpgsqlTypesHelper.cs
index 63e9ff49bfef2571e858e6eb22bd70b02a51623a..0547242168f94eae38f6e76cabc79ff34bf5f533 100755 (executable)
@@ -160,14 +160,15 @@ namespace NpgsqlTypes
 
                 NativeTypeMapping = new NpgsqlNativeTypeMapping();
 
-                NativeTypeMapping.AddType("text", NpgsqlDbType.Text, DbType.String, true,
-                new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToString));
+                NativeTypeMapping.AddType("text", NpgsqlDbType.Text, DbType.String, true, null);
 
                 NativeTypeMapping.AddDbTypeAlias("text", DbType.StringFixedLength);
                 NativeTypeMapping.AddDbTypeAlias("text", DbType.AnsiString);
                 NativeTypeMapping.AddDbTypeAlias("text", DbType.AnsiStringFixedLength);
                 NativeTypeMapping.AddTypeAlias("text", typeof(String));
 
+               NativeTypeMapping.AddType("varchar", NpgsqlDbType.Varchar, DbType.String, true, null);
+
                 NativeTypeMapping.AddType("bytea", NpgsqlDbType.Bytea, DbType.Binary, true,
                 new ConvertNativeToBackendHandler(BasicNativeToBackendTypeConverter.ToBinary));
 
@@ -295,7 +296,7 @@ namespace NpgsqlTypes
                     new NpgsqlBackendTypeInfo(0, "bpchar", NpgsqlDbType.Text, DbType.String, typeof(String),
                         null),
 
-                    new NpgsqlBackendTypeInfo(0, "varchar", NpgsqlDbType.Text, DbType.String, typeof(String),
+                    new NpgsqlBackendTypeInfo(0, "varchar", NpgsqlDbType.Varchar, DbType.String, typeof(String),
                         null),
 
                     new NpgsqlBackendTypeInfo(0, "text", NpgsqlDbType.Text, DbType.String, typeof(String),
@@ -457,8 +458,7 @@ namespace NpgsqlTypes
         /// <param name="NpgsqlDbType">NpgsqlDbType</param>
         /// <param name="Type">System type to convert fields of this type to.</param>
         /// <param name="ConvertBackendToNative">Data conversion handler.</param>
-        public NpgsqlBackendTypeInfo(Int32 OID, String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Type Type,
-                              ConvertBackendToNativeHandler ConvertBackendToNative)
+        public NpgsqlBackendTypeInfo(Int32 OID, String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Type Type, ConvertBackendToNativeHandler ConvertBackendToNative)
         {
             _OID = OID;
             _Name = Name;
@@ -526,12 +526,20 @@ namespace NpgsqlTypes
     /// </summary>
     internal class NpgsqlNativeTypeInfo
     {
+        private static NumberFormatInfo ni;
+                        
         private ConvertNativeToBackendHandler _ConvertNativeToBackend;
 
         private String           _Name;
         private NpgsqlDbType     _NpgsqlDbType;
         private DbType           _DbType;
         private Boolean          _Quote;
+        
+        static NpgsqlNativeTypeInfo()
+        {
+            ni = new CultureInfo("en-US").NumberFormat;
+            ni.NumberGroupSeparator = "";
+        }
 
         /// <summary>
         /// Construct a new NpgsqlTypeInfo with the given attributes and conversion handlers.
@@ -542,8 +550,7 @@ namespace NpgsqlTypes
         /// <param name="Type">System type to convert fields of this type to.</param>
         /// <param name="ConvertBackendToNative">Data conversion handler.</param>
         /// <param name="ConvertNativeToBackend">Data conversion handler.</param>
-        public NpgsqlNativeTypeInfo(String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Boolean Quote,
-                              ConvertNativeToBackendHandler ConvertNativeToBackend)
+        public NpgsqlNativeTypeInfo(String Name, NpgsqlDbType NpgsqlDbType, DbType DbType, Boolean Quote, ConvertNativeToBackendHandler ConvertNativeToBackend)
         {
             _Name = Name;
             _NpgsqlDbType = NpgsqlDbType;
@@ -588,32 +595,81 @@ namespace NpgsqlTypes
                /// plain queries or extended queries</param>
         public String ConvertToBackend(Object NativeData, Boolean ForExtendedQuery)
         {
+            if (ForExtendedQuery)
+                return ConvertToBackendExtendedQuery(NativeData);
+            else
+                return ConvertToBackendPlainQuery(NativeData);
+            
+        }
+       
+          private String ConvertToBackendPlainQuery(Object NativeData)
+          {
             if ((NativeData == DBNull.Value) || (NativeData == null))
-               if (ForExtendedQuery)
-                       return null;    // Extended query expects null values be represented as null.
-               else
-                       return "NULL";  // Plain queries exptects null values as string NULL. 
-                           
-            else if (_ConvertNativeToBackend != null) {
-                return QuoteString(! ForExtendedQuery, _ConvertNativeToBackend(this, NativeData));
+                return "NULL";  // Plain queries exptects null values as string NULL. 
+            
+            if (_ConvertNativeToBackend != null)
+                return QuoteString(_ConvertNativeToBackend(this, NativeData));
+            else
+            {
+                
+                
+                if (NativeData is System.Enum)
+                {
+                    // Do a special handling of Enum values.
+                    // Translate enum value to its underlying type. 
+                    return QuoteString((String)Convert.ChangeType(Enum.Format(NativeData.GetType(), NativeData, "d"), typeof(String), CultureInfo.InvariantCulture));
+                }
+                else if (NativeData is Double) 
+                {
+                    return QuoteString(((Double)NativeData).ToString("N", ni).Replace("'", "''").Replace("\\", "\\\\"));
+                    
+                }
+                else if (NativeData is Decimal) 
+                {
+                    return QuoteString(((Decimal)NativeData).ToString("N", ni).Replace("'", "''").Replace("\\", "\\\\"));
+                } 
+                // Do special handling of strings when in simple query. Escape quotes and backslashes.
+                return QuoteString(NativeData.ToString().Replace("'", "''").Replace("\\", "\\\\"));
                 
-            } else {
-               // Do a special handling of Enum values.
-               // Translate enum value to its underlying type. 
-               if (NativeData is System.Enum)
-                       return QuoteString(! ForExtendedQuery, (String)Convert.ChangeType(Enum.Format(NativeData.GetType(), NativeData, "d"), typeof(String), CultureInfo.InvariantCulture));
-               
-                return QuoteString(! ForExtendedQuery, (String)Convert.ChangeType(NativeData, typeof(String), CultureInfo.InvariantCulture));
             }
+    
         }
-
-        private static String QuoteString(Boolean Quote, String S)
+        
+        private String ConvertToBackendExtendedQuery(Object NativeData)
         {
-            if (Quote) {
-                return String.Format("'{0}'", S);
-            } else {
-                return S;
+            if ((NativeData == DBNull.Value) || (NativeData == null))
+                return null;    // Extended query expects null values be represented as null.
+            
+            if (_ConvertNativeToBackend != null)
+                return _ConvertNativeToBackend(this, NativeData);
+            else
+            {
+                if (NativeData is System.Enum)
+                {
+                    // Do a special handling of Enum values.
+                    // Translate enum value to its underlying type. 
+                    return (String)Convert.ChangeType(Enum.Format(NativeData.GetType(), NativeData, "d"), typeof(String), CultureInfo.InvariantCulture);
+                }
+                else if (NativeData is Double) 
+                {
+                    return ((Double)NativeData).ToString("N", ni);
+                    
+                }
+                else if (NativeData is Decimal) 
+                {
+                    return ((Decimal)NativeData).ToString("N", ni);
+                } 
+                
+                return NativeData.ToString();
+                
             }
+        
+        }
+
+        private static String QuoteString(String S)
+        {
+            return String.Format("'{0}'", S);
+            
         }
     }