Ejemplo n.º 1
0
 Delegate IPlatformMarshal.ApplyNativeCallingConventionToAggregateFinal(AggregateFinalNative final)
 {
     return(new AggregateFinalNativeCdecl(final));
 }
        public void CreateAggregate(string name, int numberOfArguments, AggregateStep step, AggregateFinal final)
        {
            name = name.ToUpperInvariant();

            var nativeAggregateStep = new AggregateStepNative((context, numberArguments, nativeArguments) =>
            {
                var contextGuidPtr = this.sqlite3Provider.Sqlite3AggregateContext(context, 16);

                if (contextGuidPtr != IntPtr.Zero)
                {
                    var contextGuidBytes = new byte[16];
                    this.platformMarshal.Copy(contextGuidPtr, contextGuidBytes, 0, 16);
                    var contextGuid = new Guid(contextGuidBytes);

                    if (contextGuid == Guid.Empty)
                    {
                        contextGuid = Guid.NewGuid();
                        contextGuidBytes = contextGuid.ToByteArray();
                        this.platformMarshal.Copy(contextGuidBytes, contextGuidPtr, 0, 16);
                    }

                    object[] mangedArguments = ObtainManagedArguments(nativeArguments);

                    // TODO
                    if (!this.aggregateContextDataDic.ContainsKey(contextGuid))
                    {
                        this.aggregateContextDataDic[contextGuid] = new Dictionary<string, object>();
                    }

                    var aggregateContextData = this.aggregateContextDataDic[contextGuid];
                    try
                    {
                        step.Invoke(aggregateContextData, mangedArguments);
                    }
                    catch (Exception ex)
                    {
                        SetError(context, ex);
                    }
                }
                else
                {
                    SetError(context, new Exception("Unable to initialize aggregate context."));
                }
            });

            var aggregateStepDelegate = this.platformMarshal.ApplyNativeCallingConventionToAggregateStep(nativeAggregateStep);
            this.aggregateStepDelegates[name] = aggregateStepDelegate;

            var aggregateStepPtr = this.platformMarshal.MarshalDelegateToNativeFunctionPointer(aggregateStepDelegate);

            var nativeAggregateFinal = new AggregateFinalNative((context) =>
            {
                IDictionary<string, object> aggregateContextData = new Dictionary<string, object>();

                var contextGuidPtr = this.sqlite3Provider.Sqlite3AggregateContext(context, 0);

                if (contextGuidPtr != IntPtr.Zero)
                {
                    var contextGuidBytes = new byte[16];
                    this.platformMarshal.Copy(contextGuidPtr, contextGuidBytes, 0, 16);
                    var contextGuid = new Guid(contextGuidBytes);

                    aggregateContextData = this.aggregateContextDataDic[contextGuid];
                }

                try
                {
                    var result = final.Invoke(aggregateContextData);
                    SetResult(context, result);
                }
                catch (Exception ex)
                {
                    SetError(context, ex);
                }
            });

            var aggregateFinalDelegate = this.platformMarshal.ApplyNativeCallingConventionToAggregateFinal(nativeAggregateFinal);
            this.aggregateFinalDelegates[name] = aggregateFinalDelegate;

            var aggregateFinalPtr = this.platformMarshal.MarshalDelegateToNativeFunctionPointer(aggregateFinalDelegate);

            int nameLength;
            var namePtr = this.platformMarshal.MarshalStringManagedToNativeUTF8(name, out nameLength);

            try
            {
                this.sqlite3Provider.Sqlite3CreateAggregate(this.db, namePtr, numberOfArguments, aggregateStepPtr, aggregateFinalPtr);
            }
            finally
            {
                if (namePtr != IntPtr.Zero)
                {
                    this.platformMarshal.CleanUpStringNativeUTF8(namePtr);
                }
            }
        }
Ejemplo n.º 3
0
        public void CreateAggregate(string name, int numberOfArguments, AggregateStep step, AggregateFinal final)
        {
            name = name.ToUpperInvariant();

            var nativeAggregateStep = new AggregateStepNative((context, numberArguments, nativeArguments) =>
            {
                var contextGuidPtr = this.sqlite3Provider.Sqlite3AggregateContext(context, 16);

                if (contextGuidPtr != IntPtr.Zero)
                {
                    var contextGuidBytes = new byte[16];
                    this.platformMarshal.Copy(contextGuidPtr, contextGuidBytes, 0, 16);
                    var contextGuid = new Guid(contextGuidBytes);

                    if (contextGuid == Guid.Empty)
                    {
                        contextGuid      = Guid.NewGuid();
                        contextGuidBytes = contextGuid.ToByteArray();
                        this.platformMarshal.Copy(contextGuidBytes, contextGuidPtr, 0, 16);
                    }

                    object[] mangedArguments = ObtainManagedArguments(nativeArguments);

                    // TODO
                    if (!this.aggregateContextDataDic.ContainsKey(contextGuid))
                    {
                        this.aggregateContextDataDic[contextGuid] = new Dictionary <string, object>();
                    }

                    var aggregateContextData = this.aggregateContextDataDic[contextGuid];
                    try
                    {
                        step.Invoke(aggregateContextData, mangedArguments);
                    }
                    catch (Exception ex)
                    {
                        SetError(context, ex);
                    }
                }
                else
                {
                    SetError(context, new Exception("Unable to initialize aggregate context."));
                }
            });

            var aggregateStepDelegate = this.platformMarshal.ApplyNativeCallingConventionToAggregateStep(nativeAggregateStep);

            this.aggregateStepDelegates[name] = aggregateStepDelegate;

            var aggregateStepPtr = this.platformMarshal.MarshalDelegateToNativeFunctionPointer(aggregateStepDelegate);

            var nativeAggregateFinal = new AggregateFinalNative((context) =>
            {
                IDictionary <string, object> aggregateContextData = new Dictionary <string, object>();

                var contextGuidPtr = this.sqlite3Provider.Sqlite3AggregateContext(context, 0);

                if (contextGuidPtr != IntPtr.Zero)
                {
                    var contextGuidBytes = new byte[16];
                    this.platformMarshal.Copy(contextGuidPtr, contextGuidBytes, 0, 16);
                    var contextGuid = new Guid(contextGuidBytes);

                    aggregateContextData = this.aggregateContextDataDic[contextGuid];
                }

                try
                {
                    var result = final.Invoke(aggregateContextData);
                    SetResult(context, result);
                }
                catch (Exception ex)
                {
                    SetError(context, ex);
                }
            });

            var aggregateFinalDelegate = this.platformMarshal.ApplyNativeCallingConventionToAggregateFinal(nativeAggregateFinal);

            this.aggregateFinalDelegates[name] = aggregateFinalDelegate;

            var aggregateFinalPtr = this.platformMarshal.MarshalDelegateToNativeFunctionPointer(aggregateFinalDelegate);

            int nameLength;
            var namePtr = this.platformMarshal.MarshalStringManagedToNativeUTF8(name, out nameLength);

            try
            {
                this.sqlite3Provider.Sqlite3CreateAggregate(this.db, namePtr, numberOfArguments, aggregateStepPtr, aggregateFinalPtr);
            }
            finally
            {
                if (namePtr != IntPtr.Zero)
                {
                    this.platformMarshal.CleanUpStringNativeUTF8(namePtr);
                }
            }
        }
 Delegate IPlatformMarshal.ApplyNativeCallingConventionToAggregateFinal(AggregateFinalNative final)
 {
     return new AggregateFinalNativeCdecl(final);
 }