コード例 #1
0
        public void where_allows_abstract_condition_if_not_requiresConcreteCondition()
        {
            Expression <Func <Artist, Artist, bool> > filter = (x, y) => x.Id == y.Id;

            _subject = new WhereBuilder(filter, false, 0);
            _subject.ToString().Should().Be($"(\"Artists\".\"Id\" = \"Artists\".\"Id\")");
        }
コード例 #2
0
        public void where_throws_without_concrete_condition_if_requiresConcreteCondition()
        {
            Expression <Func <Artist, Artist, bool> > filter = (x, y) => x.Id == y.Id;

            _subject = new WhereBuilder(filter, true, 0);
            Assert.Throws <InvalidOperationException>(() => _subject.ToString());
        }
コード例 #3
0
        public void SqlServerSelectQuery_MethodExpression_BinaryExpression()
        {
            // Arrange
            var command = new System.Data.SqlClient.SqlCommand();
            ColumnMapCollection columns       = MapRepository.Instance.GetColumns(typeof(Person));
            MappingHelper       mappingHelper = new MappingHelper(command);

            Person person = new Person();

            person.ID        = 1;
            person.Name      = "Jordan";
            person.Age       = 33;
            person.IsHappy   = true;
            person.BirthDate = new DateTime(1977, 1, 22);

            List <Person> list = new List <Person>();

            var where = new WhereBuilder <Person>(command, p => p.Name.Contains("John") && p.Age > 5, false);
            IQuery query = new SelectQuery(columns, "dbo.People", where.ToString(), "", false);

            // Act
            string queryText = query.Generate();

            // Assert
            Assert.IsNotNull(queryText);
            Assert.AreEqual(command.Parameters["@P0"].Value, "John");
            Assert.AreEqual(command.Parameters["@P1"].Value, 5);
            Assert.IsTrue(queryText.Contains("[Name] LIKE '%' + @P0 + '%'"));
            Assert.IsTrue(queryText.Contains("[Age] > @P1"));
        }
コード例 #4
0
        public void where_equal_const()
        {
            _subject = Where(x => x.Id == 10);

            _subject.ToString().Should().Be($"(\"Artists\".\"Id\" = @Clause1_P1)");
            _subject.Parameters.Get <int>("Clause1_P1").Should().Be(10);
        }
コード例 #5
0
        public void enum_in_array()
        {
            var allowed = new ArtistStatusType[] { ArtistStatusType.Continuing, ArtistStatusType.Ended };

            _subject = WhereMetadata(x => allowed.Contains(x.Status));

            _subject.ToString().Should().Be($"(\"ArtistMetadata\".\"Status\" IN @Clause1_P1)");
        }
コード例 #6
0
        public void where_string_is_null_value()
        {
            string imdb = null;

            _subject = Where(x => x.CleanName == imdb);

            _subject.ToString().Should().Be($"(\"Artists\".\"CleanName\" IS NULL)");
        }
コード例 #7
0
        public void enum_in_array()
        {
            var allowed = new MovieStatusType[] { MovieStatusType.Announced, MovieStatusType.InCinemas };

            _subject = Where(x => allowed.Contains(x.Status));

            _subject.ToString().Should().Be($"(\"Movies\".\"Status\" IN @Clause1_P1)");
        }
コード例 #8
0
 /// <summary>
 ///  获取Where条件SQL
 /// </summary>
 public string GetWhereSql()
 {
     if (string.IsNullOrWhiteSpace(WhereBuilder.ToString().Trim()))
     {
         WhereBuilder.Append(" 1=1");
     }
     return(WhereBuilder.ToString().Trim());
 }
コード例 #9
0
        public void where_string_is_null_value()
        {
            string cleanTitle = null;

            _subject = Where(x => x.CleanTitle == cleanTitle);

            _subject.ToString().Should().Be($"(\"Movies\".\"CleanTitle\" IS NULL)");
        }
コード例 #10
0
        public void where_equal_lazy_property()
        {
            _subject = Where(x => x.QualityProfile.Value.Id == 1);

            _subject.Parameters.ParameterNames.Should().HaveCount(1);
            _subject.ToString().Should().Be($"(\"QualityProfiles\".\"Id\" = @Clause1_P1)");
            _subject.Parameters.Get <int>("Clause1_P1").Should().Be(1);
        }
コード例 #11
0
        public void where_column_ends_with_string()
        {
            var test = "small";

            _subject = Where(x => x.CleanName.EndsWith(test));

            _subject.ToString().Should().Be($"(\"Artists\".\"CleanName\" LIKE '%' || @Clause1_P1)");
            _subject.Parameters.Get <string>("Clause1_P1").Should().Be(test);
        }
コード例 #12
0
        public void where_string_contains_column()
        {
            var test = "small";

            _subject = Where(x => test.Contains(x.CleanName));

            _subject.ToString().Should().Be($"(@Clause1_P1 LIKE '%' || \"Artists\".\"CleanName\" || '%')");
            _subject.Parameters.Get <string>("Clause1_P1").Should().Be(test);
        }
コード例 #13
0
        public void where_column_starts_with_string()
        {
            var test = "small";

            _subject = Where(x => x.CleanTitle.StartsWith(test));

            _subject.ToString().Should().Be($"(\"Movies\".\"CleanTitle\" LIKE @Clause1_P1 || '%')");
            _subject.Parameters.Get <string>("Clause1_P1").Should().Be(test);
        }
コード例 #14
0
        public void where_column_contains_string()
        {
            var test = "small";

            _subject = Where(x => x.CleanName.Contains(test));

            _subject.ToString().Should().Be($"(\"Authors\".\"CleanName\" LIKE '%' || @Clause1_P1 || '%')");
            _subject.Parameters.Get <string>("Clause1_P1").Should().Be(test);
        }
コード例 #15
0
        public void where_equal_variable()
        {
            var id = 10;

            _subject = Where(x => x.Id == id);

            _subject.ToString().Should().Be($"(\"Artists\".\"Id\" = @Clause1_P1)");
            _subject.Parameters.Get <int>("Clause1_P1").Should().Be(id);
        }
コード例 #16
0
        public void where_equal_null_property()
        {
            var movie = new Movie {
                CleanTitle = null
            };

            _subject = Where(x => x.CleanTitle == movie.CleanTitle);

            _subject.ToString().Should().Be($"(\"Movies\".\"CleanTitle\" IS NULL)");
        }
コード例 #17
0
        public void where_in_list_2()
        {
            var list = new List <int> {
                1, 2, 3
            };

            _subject = Where(x => x.CleanName == "test" && list.Contains(x.Id));

            _subject.ToString().Should().Be($"((\"Artists\".\"CleanName\" = @Clause1_P1) AND (\"Artists\".\"Id\" IN (1, 2, 3)))");
        }
コード例 #18
0
        public void where_in_list()
        {
            var list = new List <int> {
                1, 2, 3
            };

            _subject = Where(x => list.Contains(x.Id));

            _subject.ToString().Should().Be($"(\"Artists\".\"Id\" IN (1, 2, 3))");
        }
コード例 #19
0
        public void where_equal_null_property()
        {
            var artist = new Artist {
                CleanName = null
            };

            _subject = Where(x => x.CleanName == artist.CleanName);

            _subject.ToString().Should().Be($"(\"Artists\".\"CleanName\" IS NULL)");
        }
コード例 #20
0
        public void enum_in_list()
        {
            var allowed = new List <AuthorStatusType> {
                AuthorStatusType.Continuing, AuthorStatusType.Ended
            };

            _subject = WhereMetadata(x => allowed.Contains(x.Status));

            _subject.ToString().Should().Be($"(\"AuthorMetadata\".\"Status\" IN @Clause1_P1)");
        }
コード例 #21
0
        public void where_in_string_list()
        {
            var list = new List <string> {
                "first", "second", "third"
            };

            _subject = Where(x => list.Contains(x.CleanName));

            _subject.ToString().Should().Be($"(\"Artists\".\"CleanName\" IN @Clause1_P1)");
        }
コード例 #22
0
        public void where_equal_null_property()
        {
            var author = new Author {
                CleanName = null
            };

            _subject = Where(x => x.CleanName == author.CleanName);

            _subject.ToString().Should().Be($"(\"Authors\".\"CleanName\" IS NULL)");
        }
コード例 #23
0
        public void where_in_list()
        {
            var list = new List <int> {
                1, 2, 3
            };

            _subject = Where(x => list.Contains(x.Id));

            _subject.ToString().Should().Be($"(\"Movies\".\"Id\" IN (1, 2, 3))");

            _subject.Parameters.ParameterNames.Should().BeEmpty();
        }
コード例 #24
0
        public void where_equal_property()
        {
            var movie = new Movie {
                Id = 10
            };

            _subject = Where(x => x.Id == movie.Id);

            _subject.Parameters.ParameterNames.Should().HaveCount(1);
            _subject.ToString().Should().Be($"(\"Movies\".\"Id\" = @Clause1_P1)");
            _subject.Parameters.Get <int>("Clause1_P1").Should().Be(movie.Id);
        }
コード例 #25
0
        public void where_equal_property()
        {
            var artist = new Artist {
                Id = 10
            };

            _subject = Where(x => x.Id == artist.Id);

            _subject.Parameters.ParameterNames.Should().HaveCount(1);
            _subject.ToString().Should().Be($"(\"Artists\".\"Id\" = @Clause1_P1)");
            _subject.Parameters.Get <int>("Clause1_P1").Should().Be(artist.Id);
        }
コード例 #26
0
        public void SqlServerDeleteQuery_ShouldGenQuery()
        {
            // Arrange
            var command = new System.Data.SqlClient.SqlCommand();

            var where = new WhereBuilder <Person>(command, p => p.ID == 5, false);
            IQuery query = new DeleteQuery("dbo.People", where.ToString());

            // Act
            string queryText = query.Generate();

            // Assert
            Assert.IsNotNull(queryText);
            Assert.IsTrue(queryText.Contains("DELETE FROM dbo.People"));
            Assert.IsTrue(queryText.Contains("WHERE ([ID] = @P0)"));
            Assert.AreEqual(command.Parameters["@P0"].Value, 5);
        }
コード例 #27
0
ファイル: QGenTest.cs プロジェクト: vawen/Marr.DataMapper
        public void SqlServerUpdateQuery_ShouldGenQuery()
        {
            // Arrange
            var command = new System.Data.SqlClient.SqlCommand();
            var db = MockRepository.GenerateStub<IDataMapper>();
            db.Expect(d => d.Command).Return(command);
            ColumnMapCollection columns = MapRepository.Instance.GetColumns(typeof(Person));
            MappingHelper mappingHelper = new MappingHelper(db);

            Person person = new Person();
            person.ID = 1;
            person.Name = "Jordan";
            person.Age = 33;
            person.IsHappy = true;
            person.BirthDate = new DateTime(1977, 1, 22);

            mappingHelper.CreateParameters<Person>(person, columns, true);

            int idValue = 7;
            TableCollection tables = new TableCollection { new Table(typeof(Person)) };
            Expression<Func<Person, bool>> filter =  p => p.ID == person.ID || p.ID == idValue || p.Name == person.Name && p.Name == "Bob";
            var where = new WhereBuilder<Person>(command, new SqlServerDialect(), filter, tables, false, true);

            IQuery query = new UpdateQuery(new SqlServerDialect(), columns, command, "dbo.People", where.ToString());

            // Act
            string queryText = query.Generate();

            // Assert
            Assert.IsNotNull(queryText);
            Assert.IsTrue(queryText.Contains("UPDATE [dbo].[People]"));
            Assert.IsTrue(queryText.Contains("[Name]"));
            Assert.IsTrue(queryText.Contains("[Age]"));
            Assert.IsTrue(queryText.Contains("[IsHappy]"));
            Assert.IsTrue(queryText.Contains("[BirthDate]"));
            Assert.IsTrue(queryText.Contains("[ID] = @P4"));
            Assert.IsTrue(queryText.Contains("[ID] = @P5"));
            Assert.IsTrue(queryText.Contains("[Name] = @P6"));
            Assert.IsTrue(queryText.Contains("[Name] = @P7"));
            Assert.AreEqual(command.Parameters["@P4"].Value, 1);
            Assert.AreEqual(command.Parameters["@P5"].Value, 7);
            Assert.AreEqual(command.Parameters["@P6"].Value, "Jordan");
            Assert.AreEqual(command.Parameters["@P7"].Value, "Bob");
        }
コード例 #28
0
        public void SqlServerSelectQuery_ShouldGenQuery()
        {
            // Arrange
            var command = new System.Data.SqlClient.SqlCommand();
            var db      = MockRepository.GenerateStub <IDataMapper>();

            db.Expect(d => d.Command).Return(command);
            ColumnMapCollection columns       = MapRepository.Instance.GetColumns(typeof(Person));
            MappingHelper       mappingHelper = new MappingHelper(db);
            var orderBy = MockRepository.GenerateStub <ISortQueryBuilder>();

            orderBy.Expect(o => o.BuildQuery()).Return("");

            Person person = new Person();

            person.ID        = 1;
            person.Name      = "Jordan";
            person.Age       = 33;
            person.IsHappy   = true;
            person.BirthDate = new DateTime(1977, 1, 22);

            List <Person> list = new List <Person>();

            TableCollection tables = new TableCollection {
                new Table(typeof(Person))
            };
            Expression <Func <Person, bool> > filter = p => p.Name == "John" && p.Age > 15 || p.Age < 5 && p.Age > 1;

            var where = new WhereBuilder <Person>(command, new SqlServerDialect(), filter, tables, false, true);
            IQuery query = new SelectQuery(new SqlServerDialect(), tables, where.ToString(), orderBy, false);

            // Act
            string queryText = query.Generate();

            // Assert
            Assert.IsNotNull(queryText);
            Assert.AreEqual(command.Parameters["@P0"].Value, "John");
            Assert.AreEqual(command.Parameters["@P1"].Value, 15);
            Assert.AreEqual(command.Parameters["@P2"].Value, 5);
            Assert.AreEqual(command.Parameters["@P3"].Value, 1);
            Assert.IsTrue(queryText.Contains("([t0].[Name] = @P0) AND ([t0].[Age] > @P1))"));
            Assert.IsTrue(queryText.Contains("([t0].[Age] < @P2) AND ([t0].[Age] > @P3))"));
        }
コード例 #29
0
        public void SqlServerUpdateQuery_ShouldGenQuery()
        {
            // Arrange
            var command = new System.Data.SqlClient.SqlCommand();
            ColumnMapCollection columns       = MapRepository.Instance.GetColumns(typeof(Person));
            MappingHelper       mappingHelper = new MappingHelper(command);

            Person person = new Person();

            person.ID        = 1;
            person.Name      = "Jordan";
            person.Age       = 33;
            person.IsHappy   = true;
            person.BirthDate = new DateTime(1977, 1, 22);

            mappingHelper.CreateParameters <Person>(person, columns, false, true);

            int idValue = 7;

            var where = new WhereBuilder <Person>(command, p => p.ID == person.ID || p.ID == idValue || p.Name == person.Name && p.Name == "Bob", false);

            IQuery query = new UpdateQuery(columns, command, "dbo.People", where.ToString());

            // Act
            string queryText = query.Generate();

            // Assert
            Assert.IsNotNull(queryText);
            Assert.IsTrue(queryText.Contains("UPDATE dbo.People"));
            Assert.IsTrue(queryText.Contains("[Name]"));
            Assert.IsTrue(queryText.Contains("[Age]"));
            Assert.IsTrue(queryText.Contains("[IsHappy]"));
            Assert.IsTrue(queryText.Contains("[BirthDate]"));
            Assert.IsTrue(queryText.Contains("[ID] = @P5"));
            Assert.IsTrue(queryText.Contains("[ID] = @P6"));
            Assert.IsTrue(queryText.Contains("[Name] = @P7"));
            Assert.IsTrue(queryText.Contains("[Name] = @P8"));
            Assert.AreEqual(command.Parameters["@P5"].Value, 1);
            Assert.AreEqual(command.Parameters["@P6"].Value, 7);
            Assert.AreEqual(command.Parameters["@P7"].Value, "Jordan");
            Assert.AreEqual(command.Parameters["@P8"].Value, "Bob");
        }
コード例 #30
0
        public void SqlServerDeleteQuery_ShouldGenQuery()
        {
            // Arrange
            var             command = new System.Data.SqlClient.SqlCommand();
            TableCollection tables  = new TableCollection {
                new Table(typeof(Person))
            };
            Expression <Func <Person, bool> > filter = p => p.ID == 5;

            var where = new WhereBuilder <Person>(command, new SqlServerDialect(), filter, tables, false, false);
            IQuery query = new DeleteQuery(new Dialect(), tables[0], where.ToString());

            // Act
            string queryText = query.Generate();

            // Assert
            Assert.IsNotNull(queryText);
            Assert.IsTrue(queryText.Contains("DELETE FROM [PersonTable]"));
            Assert.IsTrue(queryText.Contains("WHERE ([ID] = @P0)"));
            Assert.AreEqual(command.Parameters["@P0"].Value, 5);
        }
コード例 #31
0
ファイル: QGenTest.cs プロジェクト: vawen/Marr.DataMapper
        public void SqlServerUpdateQuery_ShouldGenQuery()
        {
            // Arrange
            var command = new System.Data.SqlClient.SqlCommand();
            ColumnMapCollection columns = MapRepository.Instance.GetColumns(typeof(Person));
            MappingHelper mappingHelper = new MappingHelper(command);

            Person person = new Person();
            person.ID = 1;
            person.Name = "Jordan";
            person.Age = 33;
            person.IsHappy = true;
            person.BirthDate = new DateTime(1977, 1, 22);

            mappingHelper.CreateParameters<Person>(person, columns, false, true);

            int idValue = 7;
            var where = new WhereBuilder<Person>(command, p => p.ID == person.ID || p.ID == idValue || p.Name == person.Name && p.Name == "Bob", false);

            IQuery query = new UpdateQuery(columns, command, "dbo.People", where.ToString());

            // Act
            string queryText = query.Generate();

            // Assert
            Assert.IsNotNull(queryText);
            Assert.IsTrue(queryText.Contains("UPDATE dbo.People"));
            Assert.IsTrue(queryText.Contains("[Name]"));
            Assert.IsTrue(queryText.Contains("[Age]"));
            Assert.IsTrue(queryText.Contains("[IsHappy]"));
            Assert.IsTrue(queryText.Contains("[BirthDate]"));
            Assert.IsTrue(queryText.Contains("[ID] = @P5"));
            Assert.IsTrue(queryText.Contains("[ID] = @P6"));
            Assert.IsTrue(queryText.Contains("[Name] = @P7"));
            Assert.IsTrue(queryText.Contains("[Name] = @P8"));
            Assert.AreEqual(command.Parameters["@P5"].Value, 1);
            Assert.AreEqual(command.Parameters["@P6"].Value, 7);
            Assert.AreEqual(command.Parameters["@P7"].Value, "Jordan");
            Assert.AreEqual(command.Parameters["@P8"].Value, "Bob");
        }
コード例 #32
0
        public int Delete <T>(string tableName, Expression <Func <T, bool> > filter)
        {
            // Remember sql mode
            var previousSqlMode = this.SqlMode;

            SqlMode = SqlModes.Text;

            var mappingHelper = new MappingHelper(this);

            if (tableName == null)
            {
                tableName = MapRepository.Instance.GetTableName(typeof(T));
            }
            var             dialect = QGen.QueryFactory.CreateDialect(this);
            TableCollection tables  = new TableCollection();

            tables.Add(new Table(typeof(T)));
            var where = new WhereBuilder <T>(Command, dialect, filter, tables, false, false);
            IQuery query = QueryFactory.CreateDeleteQuery(dialect, tables[0], where.ToString());

            Command.CommandText = query.Generate();

            int rowsAffected = 0;

            try
            {
                OpenConnection();
                rowsAffected = Command.ExecuteNonQuery();
            }
            finally
            {
                CloseConnection();
            }

            // Return to previous sql mode
            SqlMode = previousSqlMode;

            return(rowsAffected);
        }
コード例 #33
0
ファイル: QGenTest.cs プロジェクト: vawen/Marr.DataMapper
        public void SqlServerDeleteQuery_ShouldGenQuery()
        {
            // Arrange
            var command = new System.Data.SqlClient.SqlCommand();
            var where = new WhereBuilder<Person>(command, p => p.ID == 5, false);
            IQuery query = new DeleteQuery("dbo.People", where.ToString());

            // Act
            string queryText = query.Generate();

            // Assert
            Assert.IsNotNull(queryText);
            Assert.IsTrue(queryText.Contains("DELETE FROM dbo.People"));
            Assert.IsTrue(queryText.Contains("WHERE ([ID] = @P0)"));
            Assert.AreEqual(command.Parameters["@P0"].Value, 5);
        }
コード例 #34
0
ファイル: QGenTest.cs プロジェクト: vawen/Marr.DataMapper
        public void SqlServerSelectQuery_MethodExpression_BinaryExpression()
        {
            // Arrange
            var command = new System.Data.SqlClient.SqlCommand();
            ColumnMapCollection columns = MapRepository.Instance.GetColumns(typeof(Person));
            MappingHelper mappingHelper = new MappingHelper(command);

            Person person = new Person();
            person.ID = 1;
            person.Name = "Jordan";
            person.Age = 33;
            person.IsHappy = true;
            person.BirthDate = new DateTime(1977, 1, 22);

            List<Person> list = new List<Person>();

            var where = new WhereBuilder<Person>(command, p => p.Name.Contains("John") && p.Age > 5, false);
            IQuery query = new SelectQuery(columns, "dbo.People", where.ToString(), "", false);

            // Act
            string queryText = query.Generate();

            // Assert
            Assert.IsNotNull(queryText);
            Assert.AreEqual(command.Parameters["@P0"].Value, "John");
            Assert.AreEqual(command.Parameters["@P1"].Value, 5);
            Assert.IsTrue(queryText.Contains("[Name] LIKE '%' + @P0 + '%'"));
            Assert.IsTrue(queryText.Contains("[Age] > @P1"));            
        }
コード例 #35
0
ファイル: QGenTest.cs プロジェクト: vawen/Marr.DataMapper
        public void SqlServerDeleteQuery_ShouldGenQuery()
        {
            // Arrange
            var command = new System.Data.SqlClient.SqlCommand();
            TableCollection tables = new TableCollection { new Table(typeof(Person)) };
            Expression<Func<Person, bool>> filter = p => p.ID == 5;
            var where = new WhereBuilder<Person>(command, new SqlServerDialect(), filter, tables, false, false);
            IQuery query = new DeleteQuery(new Dialect(), tables[0], where.ToString());

            // Act
            string queryText = query.Generate();

            // Assert
            Assert.IsNotNull(queryText);
            Assert.IsTrue(queryText.Contains("DELETE FROM [PersonTable]"));
            Assert.IsTrue(queryText.Contains("WHERE ([ID] = @P0)"));
            Assert.AreEqual(command.Parameters["@P0"].Value, 5);
        }
コード例 #36
0
ファイル: QGenTest.cs プロジェクト: vawen/Marr.DataMapper
        public void SqlServerSelectQuery_MethodExpression_BinaryExpression()
        {
            // Arrange
            var command = new System.Data.SqlClient.SqlCommand();
            var db = MockRepository.GenerateStub<IDataMapper>();
            db.Expect(d => d.Command).Return(command);
            ColumnMapCollection columns = MapRepository.Instance.GetColumns(typeof(Person));
            MappingHelper mappingHelper = new MappingHelper(db);
            var orderBy = MockRepository.GenerateStub<ISortQueryBuilder>();
            orderBy.Expect(o => o.BuildQuery()).Return("");

            Person person = new Person();
            person.ID = 1;
            person.Name = "Jordan";
            person.Age = 33;
            person.IsHappy = true;
            person.BirthDate = new DateTime(1977, 1, 22);

            List<Person> list = new List<Person>();

            TableCollection tables = new TableCollection { new Table(typeof(Person)) };
            Expression<Func<Person, bool>> filter = p => p.Name.Contains("John") && p.Age > 5;
            var where = new WhereBuilder<Person>(command, new SqlServerDialect(), filter, tables, false, true);
            IQuery query = new SelectQuery(new SqlServerDialect(), tables, where.ToString(), orderBy, false);

            // Act
            string queryText = query.Generate();

            // Assert
            Assert.IsNotNull(queryText);
            Assert.AreEqual(command.Parameters["@P0"].Value, "John");
            Assert.AreEqual(command.Parameters["@P1"].Value, 5);
            Assert.IsTrue(queryText.Contains("[Name] LIKE '%' + @P0 + '%'"));
            Assert.IsTrue(queryText.Contains("[Age] > @P1"));
        }