Merge pull request #2006 from steffen-kiess/posix-sockets-2
[mono.git] / mcs / class / Mono.Data.Sqlite / Mono.Data.Sqlite_2.0 / SQLiteFunction.cs
index f5e9e75969f8b10b99c610c96674cdf20feb9f7f..6aefe804586af9c963af74e3a548331662b97c94 100644 (file)
@@ -5,7 +5,7 @@
  * Released to the public domain, use at your own risk!\r
  ********************************************************/\r
 \r
-namespace System.Data.SQLite\r
+namespace Mono.Data.Sqlite\r
 {\r
   using System;\r
   using System.Collections;\r
@@ -18,7 +18,7 @@ namespace System.Data.SQLite
   /// connection to the database.\r
   /// </summary>\r
   /// <remarks>\r
-  /// Although there is one instance of a class derived from SQLiteFunction per database connection, the derived class has no access\r
+  /// Although there is one instance of a class derived from SqliteFunction per database connection, the derived class has no access\r
   /// to the underlying connection.  This is necessary to deter implementers from thinking it would be a good idea to make database\r
   /// calls during processing.\r
   /// \r
@@ -29,7 +29,7 @@ namespace System.Data.SQLite
   /// For aggregate functions, always create and store your per-statement data in the contextData object on the 1st step.  This data will\r
   /// be automatically freed for you (and Dispose() called if the item supports IDisposable) when the statement completes.\r
   /// </remarks>\r
-  public abstract class SQLiteFunction : IDisposable\r
+  public abstract class SqliteFunction : IDisposable\r
   {\r
     private class AggregateData\r
     {\r
@@ -74,21 +74,21 @@ namespace System.Data.SQLite
     /// <summary>\r
     /// This static list contains all the user-defined functions declared using the proper attributes.\r
     /// </summary>\r
-    private static List<SQLiteFunctionAttribute> _registeredFunctions = new List<SQLiteFunctionAttribute>();\r
+    private static List<SqliteFunctionAttribute> _registeredFunctions = new List<SqliteFunctionAttribute>();\r
 \r
     /// <summary>\r
     /// Internal constructor, initializes the function's internal variables.\r
     /// </summary>\r
-    protected SQLiteFunction()\r
+    protected SqliteFunction()\r
     {\r
       _contextDataList = new Dictionary<long, AggregateData>();\r
     }\r
 \r
     /// <summary>\r
-    /// Returns a reference to the underlying connection's SQLiteConvert class, which can be used to convert\r
+    /// Returns a reference to the underlying connection's SqliteConvert class, which can be used to convert\r
     /// strings and DateTime's into the current connection's encoding schema.\r
     /// </summary>\r
-    public SQLiteConvert SQLiteConvert\r
+    public SqliteConvert SqliteConvert\r
     {\r
       get\r
       {\r
@@ -244,7 +244,7 @@ namespace System.Data.SQLite
         }\r
       }\r
 \r
-      switch (SQLiteConvert.TypeToAffinity(t))\r
+      switch (SqliteConvert.TypeToAffinity(t))\r
       {\r
         case TypeAffinity.Null:\r
           _base.ReturnNull(context);\r
@@ -288,7 +288,7 @@ namespace System.Data.SQLite
     /// than the second.</returns>\r
     internal int CompareCallback(IntPtr ptr, int len1, IntPtr ptr1, int len2, IntPtr ptr2)\r
     {\r
-      return Compare(SQLiteConvert.UTF8ToString(ptr1, len1), SQLiteConvert.UTF8ToString(ptr2, len2));\r
+      return Compare(SqliteConvert.UTF8ToString(ptr1, len1), SqliteConvert.UTF8ToString(ptr2, len2));\r
     }\r
 \r
     internal int CompareCallback16(IntPtr ptr, int len1, IntPtr ptr1, int len2, IntPtr ptr2)\r
@@ -390,17 +390,17 @@ namespace System.Data.SQLite
 \r
     /// <summary>\r
     /// Using reflection, enumerate all assemblies in the current appdomain looking for classes that\r
-    /// have a SQLiteFunctionAttribute attribute, and registering them accordingly.\r
+    /// have a SqliteFunctionAttribute attribute, and registering them accordingly.\r
     /// </summary>\r
 #if !PLATFORM_COMPACTFRAMEWORK\r
-    [Security.Permissions.FileIOPermission(Security.Permissions.SecurityAction.Assert, AllFiles = Security.Permissions.FileIOPermissionAccess.PathDiscovery)]\r
+    [global::System.Security.Permissions.FileIOPermission(global::System.Security.Permissions.SecurityAction.Assert, AllFiles = global::System.Security.Permissions.FileIOPermissionAccess.PathDiscovery)]\r
 #endif\r
-    static SQLiteFunction()\r
+    static SqliteFunction()\r
     {\r
       try\r
       {\r
 #if !PLATFORM_COMPACTFRAMEWORK\r
-        SQLiteFunctionAttribute at;\r
+        SqliteFunctionAttribute at;\r
         System.Reflection.Assembly[] arAssemblies = System.AppDomain.CurrentDomain.GetAssemblies();\r
         int w = arAssemblies.Length;\r
         System.Reflection.AssemblyName sqlite = System.Reflection.Assembly.GetCallingAssembly().GetName();\r
@@ -429,7 +429,7 @@ namespace System.Data.SQLite
 \r
             arTypes = arAssemblies[n].GetTypes();\r
           }\r
-          catch (Reflection.ReflectionTypeLoadException e)\r
+          catch (global::System.Reflection.ReflectionTypeLoadException e)\r
           {\r
             arTypes = e.Types;\r
           }\r
@@ -439,11 +439,11 @@ namespace System.Data.SQLite
           {\r
             if (arTypes[x] == null) continue;\r
 \r
-            object[] arAtt = arTypes[x].GetCustomAttributes(typeof(SQLiteFunctionAttribute), false);\r
+            object[] arAtt = arTypes[x].GetCustomAttributes(typeof(SqliteFunctionAttribute), false);\r
             int u = arAtt.Length;\r
             for (int y = 0; y < u; y++)\r
             {\r
-              at = arAtt[y] as SQLiteFunctionAttribute;\r
+              at = arAtt[y] as SqliteFunctionAttribute;\r
               if (at != null)\r
               {\r
                 at._instanceType = arTypes[x];\r
@@ -459,19 +459,19 @@ namespace System.Data.SQLite
       }\r
     }\r
     /// <summary>\r
-    /// Manual method of registering a function.  The type must still have the SQLiteFunctionAttributes in order to work\r
+    /// Manual method of registering a function.  The type must still have the SqliteFunctionAttributes in order to work\r
     /// properly, but this is a workaround for the Compact Framework where enumerating assemblies is not currently supported.\r
     /// </summary>\r
     /// <param name="typ">The type of the function to register</param>\r
     public static void RegisterFunction(Type typ)\r
     {\r
-      object[] arAtt = typ.GetCustomAttributes(typeof(SQLiteFunctionAttribute), false);\r
+      object[] arAtt = typ.GetCustomAttributes(typeof(SqliteFunctionAttribute), false);\r
       int u = arAtt.Length;\r
-      SQLiteFunctionAttribute at;\r
+      SqliteFunctionAttribute at;\r
 \r
       for (int y = 0; y < u; y++)\r
       {\r
-        at = arAtt[y] as SQLiteFunctionAttribute;\r
+        at = arAtt[y] as SqliteFunctionAttribute;\r
         if (at != null)\r
         {\r
           at._instanceType = typ;\r
@@ -491,14 +491,14 @@ namespace System.Data.SQLite
     /// </remarks>\r
     /// <param name="sqlbase">The base object on which the functions are to bind</param>\r
     /// <returns>Returns an array of functions which the connection object should retain until the connection is closed.</returns>\r
-    internal static SQLiteFunction[] BindFunctions(SQLiteBase sqlbase)\r
+    internal static SqliteFunction[] BindFunctions(SQLiteBase sqlbase)\r
     {\r
-      SQLiteFunction f;\r
-      List<SQLiteFunction> lFunctions = new List<SQLiteFunction>();\r
+      SqliteFunction f;\r
+      List<SqliteFunction> lFunctions = new List<SqliteFunction>();\r
 \r
-      foreach (SQLiteFunctionAttribute pr in _registeredFunctions)\r
+      foreach (SqliteFunctionAttribute pr in _registeredFunctions)\r
       {\r
-        f = (SQLiteFunction)Activator.CreateInstance(pr._instanceType);\r
+        f = (SqliteFunction)Activator.CreateInstance(pr._instanceType);\r
         f._base = sqlbase;\r
         f._InvokeFunc = (pr.FuncType == FunctionType.Scalar) ? new SQLiteCallback(f.ScalarCallback) : null;\r
         f._StepFunc = (pr.FuncType == FunctionType.Aggregate) ? new SQLiteCallback(f.StepCallback) : null;\r
@@ -507,28 +507,52 @@ namespace System.Data.SQLite
         f._CompareFunc16 = (pr.FuncType == FunctionType.Collation) ? new SQLiteCollation(f.CompareCallback16) : null;\r
 \r
         if (pr.FuncType != FunctionType.Collation)\r
-          sqlbase.CreateFunction(pr.Name, pr.Arguments, (f is SQLiteFunctionEx), f._InvokeFunc, f._StepFunc, f._FinalFunc);\r
-        else\r
-          sqlbase.CreateCollation(pr.Name, f._CompareFunc, f._CompareFunc16);\r
+          sqlbase.CreateFunction(pr.Name, pr.Arguments, (f is SqliteFunctionEx), f._InvokeFunc, f._StepFunc, f._FinalFunc);\r
+        else {\r
+#if MONOTOUCH\r
+          GCHandle handle = GCHandle.Alloc (f);\r
+          sqlbase.CreateCollation(pr.Name, collation_callback, collation_callback16, GCHandle.ToIntPtr (handle));\r
+#else\r
+          sqlbase.CreateCollation(pr.Name, f._CompareFunc, f._CompareFunc16, IntPtr.Zero);\r
+#endif\r
+        }\r
 \r
 \r
         lFunctions.Add(f);\r
       }\r
 \r
-      SQLiteFunction[] arFunctions = new SQLiteFunction[lFunctions.Count];\r
+      SqliteFunction[] arFunctions = new SqliteFunction[lFunctions.Count];\r
       lFunctions.CopyTo(arFunctions, 0);\r
 \r
       return arFunctions;\r
     }\r
+\r
+#if MONOTOUCH\r
+    [MonoTouch.MonoPInvokeCallback (typeof (SQLiteCollation))]\r
+    internal static int collation_callback (IntPtr puser, int len1, IntPtr pv1, int len2, IntPtr pv2)\r
+    {\r
+      var handle = GCHandle.FromIntPtr (puser);\r
+      var func = (SqliteFunction) handle.Target;\r
+      return func._CompareFunc (IntPtr.Zero, len1, pv1, len2, pv2);\r
+    }\r
+\r
+    [MonoTouch.MonoPInvokeCallback (typeof (SQLiteCollation))]\r
+    internal static int collation_callback16 (IntPtr puser, int len1, IntPtr pv1, int len2, IntPtr pv2)\r
+    {\r
+      var handle = GCHandle.FromIntPtr (puser);\r
+      var func = (SqliteFunction) handle.Target;\r
+      return func._CompareFunc16 (IntPtr.Zero, len1, pv1, len2, pv2);\r
+    }\r
+#endif\r
   }\r
 \r
   /// <summary>\r
-  /// Extends SQLiteFunction and allows an inherited class to obtain the collating sequence associated with a function call.\r
+  /// Extends SqliteFunction and allows an inherited class to obtain the collating sequence associated with a function call.\r
   /// </summary>\r
   /// <remarks>\r
   /// User-defined functions can call the GetCollationSequence() method in this class and use it to compare strings and char arrays.\r
   /// </remarks>\r
-  public class SQLiteFunctionEx : SQLiteFunction\r
+  public class SqliteFunctionEx : SqliteFunction\r
   {\r
     /// <summary>\r
     /// Obtains the collating sequence in effect for the given function.\r
@@ -659,7 +683,7 @@ namespace System.Data.SQLite
     /// <summary>\r
     /// Context of the function that requested the collating sequence\r
     /// </summary>\r
-    internal SQLiteFunction _func;\r
+    internal SqliteFunction _func;\r
 \r
     /// <summary>\r
     /// Calls the base collating sequence to compare two strings\r