internal static agg_function_hook_info from_ptr(IntPtr p) { GCHandle h = (GCHandle)p; agg_function_hook_info hi = h.Target as agg_function_hook_info; // TODO assert(hi._h == h) return(hi); }
static void agg_function_hook_bridge_final(IntPtr context) { IntPtr agg = new IntPtr(SQLite3RuntimeProvider.sqlite3_aggregate_context(context.ToInt64(), 8)); // TODO error check agg nomem IntPtr p = new IntPtr(SQLite3RuntimeProvider.sqlite3_user_data(context.ToInt64())); agg_function_hook_info hi = agg_function_hook_info.from_ptr(p); hi.call_final(context, agg); }
int ISQLite3Provider.sqlite3_create_function(IntPtr db, string name, int nargs, object v, delegate_function_aggregate_step func_step, delegate_function_aggregate_final func_final) { string key = string.Format("{0}.{1}", nargs, name); if (_agg_functions.ContainsKey(key)) { agg_function_hook_info hi = _agg_functions[key]; // TODO maybe turn off the hook here, for now hi.free(); _agg_functions.Remove(key); } GCHandle pinned = GCHandle.Alloc(util.to_utf8(name), GCHandleType.Pinned); IntPtr ptr = pinned.AddrOfPinnedObject(); int rc; // 1 is SQLITE_UTF8 if (func_step != null) { // TODO both func_step and func_final must be non-null agg_function_hook_info hi = new agg_function_hook_info(func_step, func_final, v); rc = SQLite3RuntimeProvider.sqlite3_create_function_v2(db.ToInt64(), ptr.ToInt64(), nargs, 1, hi.ptr.ToInt64(), 0, Marshal.GetFunctionPointerForDelegate(new callback_agg_function_step(agg_function_hook_bridge_step)).ToInt64(), Marshal.GetFunctionPointerForDelegate(new callback_agg_function_final(agg_function_hook_bridge_final)).ToInt64(), 0); if (rc == 0) { _agg_functions[key] = hi; } } else { rc = SQLite3RuntimeProvider.sqlite3_create_function_v2(db.ToInt64(), ptr.ToInt64(), nargs, 1, 0, 0, 0, 0, 0); } pinned.Free(); return(rc); }
int ISQLite3Provider.sqlite3_create_function (IntPtr db, string name, int nargs, object v, delegate_function_aggregate_step func_step, delegate_function_aggregate_final func_final) { // the keys for this dictionary are nargs.name, not just the name string key = string.Format ("{0}.{1}", nargs, name); var info = hooks.getOrCreateFor (db); if (info.agg.ContainsKey (key)) { agg_function_hook_info hi = info.agg [key]; // TODO maybe turn off the hook here, for now hi.free (); info.agg.Remove (key); } // 1 is SQLITE_UTF8 if (func_step != null) { // TODO both func_step and func_final must be non-null agg_function_hook_info hi = new agg_function_hook_info (func_step, func_final, v); int rc = NativeMethods.sqlite3_create_function_v2 (db, util.to_utf8 (name), nargs, 1, hi.ptr, null, agg_function_hook_bridge_step, agg_function_hook_bridge_final, null); if (rc == 0) { info.agg [key] = hi; } return rc; } else { return NativeMethods.sqlite3_create_function_v2 (db, util.to_utf8 (name), nargs, 1, IntPtr.Zero, null, null, null, null); } }
int ISQLite3Provider.sqlite3_create_function(IntPtr db, string name, int nargs, object v, delegate_function_aggregate_step func_step, delegate_function_aggregate_final func_final) { // the keys for this dictionary are nargs.name, not just the name string key = string.Format("{0}.{1}", nargs, name); var info = hooks.getOrCreateFor(db); if (info.agg.ContainsKey(key)) { agg_function_hook_info hi = info.agg[key]; // TODO maybe turn off the hook here, for now hi.free(); info.agg.Remove(key); } GCHandle pinned = GCHandle.Alloc(util.to_utf8(name), GCHandleType.Pinned); IntPtr ptr = pinned.AddrOfPinnedObject(); int rc; // 1 is SQLITE_UTF8 if (func_step != null) { // TODO both func_step and func_final must be non-null agg_function_hook_info hi = new agg_function_hook_info(func_step, func_final, v); rc = SQLite3RuntimeProvider.sqlite3_create_function_v2(db.ToInt64(), ptr.ToInt64(), nargs, 1, hi.ptr.ToInt64(), 0, Marshal.GetFunctionPointerForDelegate(agg_function_hook_bridge_step).ToInt64(), Marshal.GetFunctionPointerForDelegate(agg_function_hook_bridge_final).ToInt64(), 0); if (rc == 0) { info.agg[key] = hi; } } else { rc = SQLite3RuntimeProvider.sqlite3_create_function_v2(db.ToInt64(), ptr.ToInt64(), nargs, 1, 0, 0, 0, 0, 0); } pinned.Free(); return rc; }