protected override DbCommand GetCommand(QueryCommand query, object[] paramValues)
        {
            DbCommand cmd = base.GetCommand(query, paramValues);

            if (_bindByNameProperty == null)
            {
                _bindByNameProperty = cmd.GetType().GetProperty("BindByName");
                //_notificationAutoEnlistProperty = cmd.GetType().GetProperty("NotificationAutoEnlist");
            }
            _bindByNameProperty.SetValue(cmd, true);
            //_notificationAutoEnlistProperty.SetValue(cmd, false);

            return cmd;
        }
        private IEnumerable<int> ExecuteBatch(QueryCommand query, IEnumerable<object[]> paramSets, int batchSize)
        {
            DbCommand cmd = this.GetCommand(query, null);
            DataTable dataTable = new DataTable();
            for (int i = 0, n = query.Parameters.Count; i < n; i++)
            {
                var qp = query.Parameters[i];
                cmd.Parameters[i].SourceColumn = qp.Name;
                dataTable.Columns.Add(qp.Name, TypeHelper.GetNonNullableType(qp.Type));
            }
            DbDataAdapter dataAdapter = provider.AdoProvider.CreateDataAdapter();
            dataAdapter.InsertCommand = cmd;
            dataAdapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
            dataAdapter.UpdateBatchSize = batchSize;

            this.LogMessage("-- Start SQL Batching --");
            this.LogMessage("");
            this.LogCommand(query, null);

            IEnumerator<object[]> en = paramSets.GetEnumerator();
            using (en)
            {
                bool hasNext = true;
                while (hasNext)
                {
                    int count = 0;
                    for (; count < dataAdapter.UpdateBatchSize && (hasNext = en.MoveNext()); count++)
                    {
                        var paramValues = en.Current;
                        dataTable.Rows.Add(paramValues);
                        this.LogParameters(query, paramValues);
                        this.LogMessage("");
                    }
                    if (count > 0)
                    {
                        int n = dataAdapter.Update(dataTable);
                        for (int i = 0; i < count; i++)
                        {
                            yield return (i < n) ? 1 : 0;
                        }
                        dataTable.Rows.Clear();
                    }
                }
            }

            this.LogMessage(string.Format("-- End SQL Batching --"));
            this.LogMessage("");
        }
 public override IEnumerable<int> ExecuteBatch(QueryCommand query, IEnumerable<object[]> paramSets, int batchSize, bool stream)
 {
     this.StartUsingConnection();
     try
     {
         var result = this.ExecuteBatch(query, paramSets, batchSize);
         if (!stream || this.ActionOpenedConnection)
         {
             return result.ToList();
         }
         else
         {
             return new EnumerateOnce<int>(result);
         }
     }
     finally
     {
         this.StopUsingConnection();
     }
 }
        protected virtual Expression BuildExecuteCommand(DbCommandExpression command)
        {
            var expression  = this.Parameterize(command);
            var commandText = this.linguist.Format(expression);
            var namedValues = DbNamedValueGatherer.Gather(expression);
            var qc          = new QueryCommand(commandText, namedValues.Select(v => new QueryParameter(v.Name, v.Type, v.QueryType)));
            var values      = namedValues.Select(v => Expression.Convert(this.Visit(v.Value), typeof(object))).ToArray() as Expression[];

            var projection = ProjectionFinder.FindProjection(expression);

            if (projection != null)
            {
                return(this.ExecuteProjection(projection, false, qc, values, isTopLevel: true));
            }

            var plan = Expression.Call
                       (
                this.executor, nameof(QueryExecutor.ExecuteCommand), null,
                Expression.Constant(qc),
                Expression.NewArrayInit(typeof(object), values)
                       );

            return(plan);
        }
Example #5
0
        private Expression ExecuteProjection(ProjectionExpression projection, bool okayToDefer, QueryCommand command, Expression[] values)
        {
            okayToDefer &= (this.receivingMember != null && this.policy.IsDeferLoaded(this.receivingMember));

            var saveScope = this.scope;
            ParameterExpression reader = Expression.Parameter(typeof(FieldReader), "r" + nReaders++);

            this.scope = new Scope(this.scope, reader, projection.Select.Alias, projection.Select.Columns);
            LambdaExpression projector = Expression.Lambda(this.Visit(projection.Projector), reader);

            this.scope = saveScope;

            var entity = EntityFinder.Find(projection.Projector);

            string methExecute = okayToDefer
                ? "ExecuteDeferred"
                : "Execute";

            // call low-level execute directly on supplied DbQueryProvider
            Expression result = Expression.Call(this.executor, methExecute, new Type[] { projector.Body.Type },
                                                Expression.Constant(command),
                                                projector,
                                                Expression.Constant(entity, typeof(MappingEntity)),
                                                Expression.NewArrayInit(typeof(object), values)
                                                );

            if (projection.Aggregator != null)
            {
                // apply aggregator
                result = DbExpressionReplacer.Replace(projection.Aggregator.Body, projection.Aggregator.Parameters[0], result);
            }
            return(result);
        }
Example #6
0
 public abstract int ExecuteCommand(QueryCommand query, object[] paramValues);
Example #7
0
 public abstract IEnumerable <T> ExecuteDeferred <T>(QueryCommand query, Func <FieldReader, T> fnProjector, MappingEntity entity, object[] paramValues);
Example #8
0
 public abstract IEnumerable <T> ExecuteBatch <T>(QueryCommand query, IEnumerable <object[]> paramSets, Func <FieldReader, T> fnProjector, MappingEntity entity, int batchSize, bool stream);
Example #9
0
 public abstract IEnumerable <int> ExecuteBatch(QueryCommand query, IEnumerable <object[]> paramSets, int batchSize, bool stream);
        private Expression ExecuteProjection(DbProjectionExpression projection, bool okayToDefer, QueryCommand command, Expression[] values, bool isTopLevel)
        {
            okayToDefer &= this.receivingMember != null && this.policy.IsDeferLoaded(this.receivingMember);

            var saveScope = this.scope;
            var reader    = Expression.Parameter(typeof(FieldReader), "r" + nReaders++);

            this.scope = new Scope(this.scope, reader, projection.Select.Alias, projection.Select.Columns);

            var projector = Expression.Lambda(this.Visit(projection.Projector), reader);

            this.scope = saveScope;

            var entity = EntityFinder.Find(projection.Projector);

            var methExecute = okayToDefer ? nameof(QueryExecutor.ExecuteDeferred) : nameof(QueryExecutor.Execute);

            var result = Expression.Call(this.executor, methExecute, new Type[] { projector.Body.Type }, Expression.Constant(command), projector, Expression.Constant(entity, typeof(MappingEntity)), Expression.NewArrayInit(typeof(object), values)) as Expression;

            if (projection.Aggregator != null)
            {
                result = DbExpressionReplacer.Replace(projection.Aggregator.Body, projection.Aggregator.Parameters[0], result);
            }

            return(result);
        }
Example #11
0
            protected override DbCommand GetCommand(QueryCommand query, object[] paramValues)
            {
                var cmd = (OleDbCommand)this.provider.Connection.CreateCommand();
                cmd.CommandText = query.CommandText;

                this.SetParameterValues(query, cmd, paramValues);

                if (this.provider.Transaction != null)
                {
                    cmd.Transaction = (OleDbTransaction)this.provider.Transaction;
                }

                return cmd;
            }
Example #12
0
            protected override DbCommand GetCommand(QueryCommand query, object[] paramValues)
            {
                SQLiteCommand cmd;
                #if false
                if (!this.provider.commandCache.TryGetValue(query, out cmd))
                {
                    cmd = (SQLiteCommand)this.provider.Connection.CreateCommand();
                    cmd.CommandText = query.CommandText;
                    this.SetParameterValues(query, cmd, paramValues);
                    cmd.Prepare();
                    this.provider.commandCache.Add(query, cmd);
                    if (this.provider.Transaction != null)
                    {
                        cmd = (SQLiteCommand)cmd.Clone();
                        cmd.Transaction = (SQLiteTransaction)this.provider.Transaction;
                    }
                }
                else
                {
                    cmd = (SQLiteCommand)cmd.Clone();
                    cmd.Transaction = (SQLiteTransaction)this.provider.Transaction;
                    this.SetParameterValues(query, cmd, paramValues);
                }
                #else
                cmd = (SQLiteCommand)this.provider.Connection.CreateCommand();
                cmd.CommandText = query.CommandText;
                this.SetParameterValues(query, cmd, paramValues);
                cmd.Prepare();

                if (this.provider.Transaction != null)
                {
                    cmd.Transaction = (SQLiteTransaction)this.provider.Transaction;
                }
                #endif
                return cmd;
            }