Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / Mono.Data.Sqlite / Mono.Data.Sqlite_2.0 / SQLiteConvert.cs
index 386f912aab117fec9618d956a3d01a2c6a6cb39a..99923147dbbcd0abbe27d020a49bd19e7e8d1cbc 100644 (file)
@@ -640,13 +640,42 @@ namespace Mono.Data.Sqlite
     {\r
       if (String.IsNullOrEmpty(Name)) return DbType.Object;\r
 \r
-      int x = _typeNames.Length;\r
-      for (int n = 0; n < x; n++)\r
+      string nameToCompare = Name;\r
+      int parenthesis = nameToCompare.IndexOf ('(');\r
+      if (parenthesis > 0)\r
+        nameToCompare = nameToCompare.Substring (0, parenthesis);\r
+        \r
+      for (int n = 0; n < _typeNames.Length; n++)\r
       {\r
-        if (String.Compare(Name, _typeNames[n].typeName, true, CultureInfo.InvariantCulture) == 0)\r
+        if (string.Compare(nameToCompare, _typeNames[n].typeName, true, CultureInfo.InvariantCulture) == 0)\r
           return _typeNames[n].dataType; \r
       }\r
-      return DbType.Object;\r
+      \r
+      /* http://www.sqlite.org/datatype3.html\r
+       * 2.1 Determination Of Column Affinity\r
+       * The affinity of a column is determined by the declared type of the column, according to the following rules in the order shown:\r
+       *   1. If the declared type contains the string "INT" then it is assigned INTEGER affinity.\r
+       *   2. If the declared type of the column contains any of the strings "CHAR", "CLOB", or "TEXT" then that column has TEXT affinity. Notice that the type VARCHAR contains the string "CHAR" and is thus assigned TEXT affinity.\r
+       *   3. If the declared type for a column contains the string "BLOB" or if no type is specified then the column has affinity NONE.\r
+       *   4. If the declared type for a column contains any of the strings "REAL", "FLOA", or "DOUB" then the column has REAL affinity.\r
+       *   5. Otherwise, the affinity is NUMERIC.\r
+       */\r
+      \r
+      if (Name.IndexOf ("INT", StringComparison.OrdinalIgnoreCase) >= 0) {\r
+        return DbType.Int64;\r
+      } else if (Name.IndexOf ("CHAR", StringComparison.OrdinalIgnoreCase) >= 0\r
+              || Name.IndexOf ("CLOB", StringComparison.OrdinalIgnoreCase) >= 0\r
+              || Name.IndexOf ("TEXT", StringComparison.OrdinalIgnoreCase) >= 0) {\r
+        return DbType.String;\r
+      } else if (Name.IndexOf ("BLOB", StringComparison.OrdinalIgnoreCase) >= 0 /* || Name == string.Empty // handled at the top of this functin */) {\r
+        return DbType.Object;\r
+      } else if (Name.IndexOf ("REAL", StringComparison.OrdinalIgnoreCase) >= 0\r
+              || Name.IndexOf ("FLOA", StringComparison.OrdinalIgnoreCase) >= 0\r
+              || Name.IndexOf ("DOUB", StringComparison.OrdinalIgnoreCase) >= 0) {\r
+        return DbType.Double;\r
+      } else {\r
+        return DbType.Object; // This can be anything, so use Object instead of Decimal (which we use otherwise where the type affinity is NUMERIC)\r
+      }\r
     }\r
     #endregion\r
 \r
@@ -675,6 +704,7 @@ namespace Mono.Data.Sqlite
       new SQLiteTypeNames("YESNO", DbType.Boolean),\r
       new SQLiteTypeNames("LOGICAL", DbType.Boolean),\r
       new SQLiteTypeNames("BOOL", DbType.Boolean),\r
+      new SQLiteTypeNames("BOOLEAN", DbType.Boolean),\r
       new SQLiteTypeNames("NUMERIC", DbType.Decimal),\r
       new SQLiteTypeNames("DECIMAL", DbType.Decimal),\r
       new SQLiteTypeNames("MONEY", DbType.Decimal),\r
@@ -689,12 +719,14 @@ namespace Mono.Data.Sqlite
       new SQLiteTypeNames("GENERAL", DbType.Binary),\r
       new SQLiteTypeNames("OLEOBJECT", DbType.Binary),\r
       new SQLiteTypeNames("GUID", DbType.Guid),\r
+      new SQLiteTypeNames("GUIDBLOB", DbType.Guid),\r
       new SQLiteTypeNames("UNIQUEIDENTIFIER", DbType.Guid),\r
       new SQLiteTypeNames("MEMO", DbType.String),\r
       new SQLiteTypeNames("NOTE", DbType.String),\r
       new SQLiteTypeNames("SMALLINT", DbType.Int16),\r
       new SQLiteTypeNames("BIGINT", DbType.Int64),\r
       new SQLiteTypeNames("TIMESTAMP", DbType.DateTime),\r
+      new SQLiteTypeNames("DATETIME", DbType.DateTime),\r
     };\r
   }\r
 \r