Exemplo n.º 1
0
        internal override IExecuteResult Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, object[] parentArgs, object[] userArgs, ICompiledSubQuery[] subQueries, object lastResult)
        {
            IExecuteResult result3;

            InitializeProviderMode();
            DbConnection connection = conManager.UseConnection(this);

            try
            {
#if FREE
                var count = GetTabesCount(connection);
                Debug.Assert(count > 0);

                if (count > Constants.LimitedTablesCount)
                {
                    throw ALinq.SqlClient.Error.TablesLimited();
                }
#endif

                DbCommand cmd = connection.CreateCommand();

                cmd.CommandText    = queryInfo.CommandText;
                cmd.Transaction    = conManager.Transaction;
                cmd.CommandTimeout = CommandTimeout;
                cmd.CommandType    = (queryInfo.Query is SqlStoredProcedureCall) ? CommandType.StoredProcedure : CommandType.Text;
                AssignParameters(cmd, queryInfo.Parameters, userArgs, lastResult);
                //if (queryInfo.Query is SqlStoredProcedureCall)

                LogCommand(Log, cmd);
                queryCount++;
                switch (queryInfo.ResultShape)
                {
                case ResultShape.Singleton:
                {
                    DbDataReader  reader  = CreateRereader(cmd.ExecuteReader());
                    IObjectReader reader2 = factory.Create(reader, true, this, parentArgs, userArgs, subQueries);
                    conManager.UseConnection(reader2.Session);
                    try
                    {
                        var objType  = typeof(OneTimeEnumerable <>).MakeGenericType(new[] { queryInfo.ResultType });
                        var bf1      = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
                        var args     = new object[] { reader2 };
                        var sequence =
                            (IEnumerable)Activator.CreateInstance(objType, bf1, null, args, null);
                        object     obj2       = null;
                        var        expression = query as MethodCallExpression;
                        MethodInfo info;
                        if ((expression != null) && ((expression.Method.DeclaringType == typeof(Queryable)) ||
                                                     (expression.Method.DeclaringType == typeof(Enumerable))))
                        {
                            string name = expression.Method.Name;
                            if (name != null)
                            {
                                if ((!(name == "First") && !(name == "FirstOrDefault")) &&
                                    !(name == "SingleOrDefault"))
                                {
                                    if (name == "Single")
                                    {
                                    }
                                }
                                else
                                {
                                    info = TypeSystem.FindSequenceMethod(expression.Method.Name, sequence);
                                    goto Label_01DE;
                                }
                            }
                            info = TypeSystem.FindSequenceMethod("Single", sequence);
                        }
                        else
                        {
                            info = TypeSystem.FindSequenceMethod("SingleOrDefault", sequence);
                        }
Label_01DE:
                        if (info != null)
                        {
                            try
                            {
                                obj2 = info.Invoke(null, new object[] { sequence });
                            }
                            catch (TargetInvocationException exception)
                            {
                                if (exception.InnerException != null)
                                {
                                    throw exception.InnerException;
                                }
                                throw;
                            }
                        }
                        return(CreateExecuteResult(cmd, queryInfo.Parameters, reader2.Session, obj2));
                    }
                    finally
                    {
                        reader2.Dispose();
                    }
                }

                case ResultShape.Sequence:
                    break;

                case ResultShape.MultipleResults:
                {
                    DbDataReader         reader5 = CreateRereader(cmd.ExecuteReader());
                    IObjectReaderSession user    = readerCompiler.CreateSession(reader5, this, parentArgs,
                                                                                userArgs, subQueries);
                    conManager.UseConnection(user);
                    MetaFunction function2     = GetFunction(query);
                    var          executeResult = CreateExecuteResult(cmd, queryInfo.Parameters, user);
                    executeResult.ReturnValue = new MultipleResults(this, function2, user, executeResult);
                    return(executeResult);
                }

                default:
                    return(CreateExecuteResult(cmd, queryInfo.Parameters, null, cmd.ExecuteNonQuery(), true));
                }

                DbDataReader  reader3 = CreateRereader(cmd.ExecuteReader());
                IObjectReader reader4 = factory.Create(reader3, true, this, parentArgs, userArgs, subQueries);
                conManager.UseConnection(reader4.Session);
                var t = typeof(OneTimeEnumerable <>).MakeGenericType(new[] {
                    TypeSystem.GetElementType(queryInfo.ResultType)
                });
                var flags  = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
                var objs   = new object[] { reader4 };
                var source = (IEnumerable)Activator.CreateInstance(t, flags, null, objs, null);
                if (typeof(IQueryable).IsAssignableFrom(queryInfo.ResultType))
                {
                    source = source.AsQueryable();
                }

                var          result   = CreateExecuteResult(cmd, queryInfo.Parameters, reader4.Session);
                MetaFunction function = GetFunction(query);
                if ((function != null) && !function.IsComposable)
                {
                    t = typeof(SingleResult <>).MakeGenericType(new[] {
                        TypeSystem.GetElementType(queryInfo.ResultType)
                    });
                    flags  = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
                    objs   = new object[] { source, result, services.Context };
                    source = (IEnumerable)Activator.CreateInstance(t, flags, null, objs, null);
                }
                result.ReturnValue = source;
                result3            = result;
            }
            finally
            {
                conManager.ReleaseConnection(this);
            }
            return(result3);
        }