[hyena] Add BinaryFunction class



commit 1c031f5bbfc3ef8063052cd57b0b38afe3aaa3cd
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Tue Feb 16 15:30:56 2010 -0800

    Add BinaryFunction class
    
    Lets extensions etc call custom methods from SQL even if they're not
    loaded when the DbConnection is created.

 .../Hyena.Data.Sqlite/SqliteUtils.cs               |   34 ++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)
---
diff --git a/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/SqliteUtils.cs b/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/SqliteUtils.cs
index b0acfb7..cbe5269 100644
--- a/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/SqliteUtils.cs
+++ b/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/SqliteUtils.cs
@@ -30,6 +30,7 @@ using System;
 using System.Reflection;
 using System.Text;
 using Mono.Data.Sqlite;
+using System.Collections.Generic;
 
 namespace Hyena.Data.Sqlite
 {
@@ -126,6 +127,39 @@ namespace Hyena.Data.Sqlite
         }
     }
 
+    [SqliteFunction (Name = "HYENA_BINARY_FUNCTION", FuncType = FunctionType.Scalar, Arguments = 3)]
+    public sealed class BinaryFunction : SqliteFunction
+    {
+        private static Dictionary<string, Func<object, object, object>> funcs = new Dictionary<string, Func<object, object, object>> ();
+
+        public static void Add (string functionId, Func<object, object, object> func)
+        {
+            lock (funcs) {
+                if (funcs.ContainsKey (functionId)) {
+                    throw new ArgumentException (String.Format ("{0} is already taken", functionId), "functionId");
+                }
+
+                funcs[functionId] = func;
+            }
+        }
+
+        public static void Remove (string functionId)
+        {
+            lock (funcs) {
+                if (!funcs.ContainsKey (functionId)) {
+                    throw new ArgumentException (String.Format ("{0} does not exist", functionId), "functionId");
+                }
+
+                funcs.Remove (functionId);
+            }
+        }
+
+        public override object Invoke (object[] args)
+        {
+            return funcs[args[0] as string] (args[1], args[2]);
+        }
+    }
+
     [SqliteFunction (Name = "HYENA_COLLATION_KEY", FuncType = FunctionType.Scalar, Arguments = 1)]
     internal class CollationKeyFunction : SqliteFunction
     {



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