Пример #1
0
        private static TResult ExecuteComplex <TResult>(
            ImpatientQueryProvider queryProvider,
            Action <DbCommand> commandBuilder,
            Func <DbDataReader, TResult> materializer)
        {
            using (var connection = queryProvider.ConnectionFactory.CreateConnection())
                using (var command = connection.CreateCommand())
                {
                    commandBuilder(command);

                    queryProvider.DbCommandInterceptor?.Invoke(command);

                    connection.Open();

                    using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        // TODO: Can the logic really be this simple?
                        // - Related to 'DefaultIfEmpty' and '___OrDefault' behavior
                        // - Currently relying on the queries themselves to supply:
                        //   - The default value, through the materializer
                        //   - Exceptions for First/Single/SingleOrDefault/Last/ElementAt

                        reader.Read();

                        return(materializer(reader));
                    }
                }
        }
Пример #2
0
        static NullableMemberTests()
        {
            var impatient
                = new ImpatientQueryProvider(
                      new TestImpatientConnectionFactory(
                          @"Server=.\sqlexpress; Database=NORTHWND; Trusted_Connection=True"),
                      new DefaultImpatientQueryCache(),
                      new DefaultImpatientExpressionVisitorProvider());

            context = new NorthwindQueryContext(impatient);
        }
        public NorthwindQueryContext(ImpatientQueryProvider impatient)
        {
            this.impatient = impatient;

            impatient.DbCommandInterceptor = command =>
            {
                if (commandLog.Length > 0)
                {
                    commandLog.AppendLine().AppendLine();
                }

                commandLog.Append(command.CommandText);
            };
        }
Пример #4
0
        private static TResult ExecuteScalar <TResult>(
            ImpatientQueryProvider queryProvider,
            Action <DbCommand> commandBuilder)
        {
            using (var connection = queryProvider.ConnectionFactory.CreateConnection())
                using (var command = connection.CreateCommand())
                {
                    commandBuilder(command);

                    queryProvider.DbCommandInterceptor?.Invoke(command);

                    connection.Open();

                    return((TResult)command.ExecuteScalar());
                }
        }
Пример #5
0
        private static IEnumerable <TElement> ExecuteEnumerable <TElement>(
            ImpatientQueryProvider queryProvider,
            Action <DbCommand> commandBuilder,
            Func <DbDataReader, TElement> materializer)
        {
            using (var connection = queryProvider.ConnectionFactory.CreateConnection())
                using (var command = connection.CreateCommand())
                {
                    commandBuilder(command);

                    queryProvider.DbCommandInterceptor?.Invoke(command);

                    connection.Open();

                    using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (reader.Read())
                        {
                            yield return(materializer(reader));
                        }
                    }
                }
        }
 public ImpatientQueryable(Expression expression, ImpatientQueryProvider provider)
 {
     Expression = expression ?? throw new ArgumentNullException(nameof(expression));
     Provider   = provider ?? throw new ArgumentNullException(nameof(provider));
 }
Пример #7
0
 public NorthwindQueryContext(ImpatientQueryProvider impatient, TestDbCommandExecutorFactory executor)
 {
     this.impatient = impatient;
     this.executor  = executor;
 }
 public QueryActivatingExpressionVisitor(ImpatientQueryProvider provider)
 {
     this.provider = provider;
 }
        public TablePerTypeInheritanceTests()
        {
            impatient = new ImpatientQueryProvider(
                new TestImpatientConnectionFactory(@"Server=.\sqlexpress; Database=tempdb; Trusted_Connection=true"),
                new DefaultImpatientQueryCache(),
                new DefaultImpatientExpressionVisitorProvider())
            {
                DbCommandInterceptor = command =>
                {
                    if (commandLog.Length > 0)
                    {
                        commandLog.AppendLine().AppendLine();
                    }

                    commandLog.Append(command.CommandText);
                }
            };

            using (var connection = impatient.ConnectionFactory.CreateConnection())
            {
                connection.Execute(@"
DROP TABLE IF EXISTS DerivedConcreteType3;
DROP TABLE IF EXISTS DerivedConcreteType2;
DROP TABLE IF EXISTS DerivedConcreteType1;
DROP TABLE IF EXISTS DerivedAbstractType1;
DROP TABLE IF EXISTS BaseAbstractType1;

CREATE TABLE BaseAbstractType1
(
    [Id] int not null primary key,
    [Property_BaseAbstractType1] nvarchar(max) null
);

CREATE TABLE DerivedAbstractType1
(
    [Id] int not null primary key,
    [Property_DerivedAbstractType1] nvarchar(max) null,
    CONSTRAINT FK_DerivedAbstractType1_BaseAbstractType1
        FOREIGN KEY (Id) REFERENCES BaseAbstractType1 (Id)
);

CREATE TABLE DerivedConcreteType1
(
    [Id] int not null primary key,
    [Property_DerivedConcreteType1] nvarchar(max) null,
    CONSTRAINT FK_DerivedConcreteType1_BaseAbstractType1
        FOREIGN KEY (Id) REFERENCES BaseAbstractType1 (Id)
);

CREATE TABLE DerivedConcreteType2
(
    [Id] int not null primary key,
    [Property_DerivedConcreteType2] nvarchar(max) null,
    CONSTRAINT FK_DerivedConcreteType2_DerivedAbstractType1
        FOREIGN KEY (Id) REFERENCES DerivedAbstractType1 (Id)
);

CREATE TABLE DerivedConcreteType3
(
    [Id] int not null primary key,
    [Property_DerivedConcreteType3] nvarchar(max) null,
    CONSTRAINT FK_DerivedConcreteType3_DerivedAbstractType1
        FOREIGN KEY (Id) REFERENCES DerivedAbstractType1 (Id)
);

INSERT INTO BaseAbstractType1 (Id, Property_BaseAbstractType1)
VALUES
(1, 'a'),
(2, 'b'),
(3, 'c'),
(4, 'a'),
(5, 'b'),
(6, 'c'),
(7, 'a'),
(8, 'b'),
(9, 'c');

INSERT INTO DerivedAbstractType1 (Id, Property_DerivedAbstractType1)
VALUES
(4, 'a'),
(5, 'b'),
(6, 'c'),
(7, 'a'),
(8, 'b'),
(9, 'c');

INSERT INTO DerivedConcreteType1(Id, Property_DerivedConcreteType1)
VALUES
(1, 'a'),
(2, 'b'),
(3, 'c');

INSERT INTO DerivedConcreteType2(Id, Property_DerivedConcreteType2)
VALUES
(4, 'a'),
(5, 'b'),
(6, 'c');

INSERT INTO DerivedConcreteType3(Id, Property_DerivedConcreteType3)
VALUES
(7, 'a'),
(8, 'b'),
(9, 'c');
");
            }
        }