示例#1
0
        public void FetchOnWithSecondGenericType()
        {
            var s       = new DefaultSqlExpression <User>(Database, true);
            var joinexp = s.On <CustomerUser>((x, y) => x.Name == y.CustomerName);

            Assert.AreEqual(joinexp, "([U].[Name] = [CU].[CustomerName])");
        }
示例#2
0
        public virtual int Update(Expression <Func <T, bool> > whereExpression, Func <T, T> funcUpdate)
        {
            using (var db = GetDatabase())
            {
                SqlExpression <T> express = new DefaultSqlExpression <T>(db);
                express      = express.Where(whereExpression);
                string where = express.Context.ToWhereStatement();
                var poco = db.SingleOrDefault <T>(where, express.Context.Params);
                if (poco == null)
                {
                    return(0);
                }

                var snapshot = db.StartSnapshot(poco);
                poco = funcUpdate(poco);
                //return db.Update(poco, columns: snapshot.UpdatedColumns().ToArray());

                var columns = snapshot.UpdatedColumns().ToArray();
                var result  = db.Update(poco, columns: columns);
                if (result == 0 && columns.Length == 0)
                {
                    return(1); // 缓存中的数据和要更新的数据对应的字段值相等
                }
                return(result);
            }
        }
示例#3
0
        public void WhereExpressionWithNullableEnum()
        {
            var sqlExpression = new DefaultSqlExpression <UserWithNullableId>(Database);

            sqlExpression.Where(x => x.NameEnum == NameEnum.Bobby);
            Assert.AreEqual(sqlExpression.Context.Params[0], "Bobby");
        }
 public void WhereExpressionWithNullableEnumContains()
 {
     var sqlExpression = new DefaultSqlExpression<UserWithNullableId>(Database);
     sqlExpression.Where(x => new List<NameEnum?> { NameEnum.Bill, NameEnum.Bobby }.Contains(x.NameEnum));
     Assert.AreEqual(sqlExpression.Context.Params[0], "Bill");
     Assert.AreEqual(sqlExpression.Context.Params[1], "Bobby");
 }
示例#5
0
        public void WhereExpressionWithNullableEnumAndValueIsNull()
        {
            var sqlExpression = new DefaultSqlExpression <UserWithNullableId>(Database);

            sqlExpression.Where(x => x.NameEnum == null);
            Assert.AreEqual(sqlExpression.Context.Params.Length, 0);
        }
示例#6
0
 public void NullableExpressionHasValueWithAnotherExpression_ReturnsIsNotNull()
 {
     var sqlExpression = new DefaultSqlExpression<NullableProperty>(Database);
     sqlExpression.Where(x => x.Age.HasValue && x.Age > 2);
     var whereStatement = sqlExpression.Context.ToWhereStatement();
     string expected = string.Format("WHERE (({0} is not null) AND ([Age] > @0))", escapedAgeIdentifier);
     Assert.AreEqual(expected, whereStatement);
 }
示例#7
0
 public void NullableExpressionNotEqualsNull_ReturnsIsNotNull()
 {
     var sqlExpression = new DefaultSqlExpression<NullableProperty>(Database);
     sqlExpression.Where(x => x.Age != null);
     var whereStatement = sqlExpression.Context.ToWhereStatement();
     string expected = string.Format("WHERE ({0} is not null)", escapedAgeIdentifier);
     Assert.AreEqual(expected, whereStatement);
 }
示例#8
0
        public void SelectStatementDoesNotRenderPropertyNameAsAlias()
        {
            var sqlExpression = new DefaultSqlExpression <UserDecorated>(Database);

            sqlExpression.Select(x => new { x.IsMale, x.Name });
            var selectStatement = sqlExpression.Context.ToSelectStatement();

            Assert.AreEqual("SELECT [is_male], [Name] \nFROM [Users] UD", selectStatement);
        }
示例#9
0
        public void NullableExpressionNotEqualsNull_ReturnsIsNotNull()
        {
            var sqlExpression = new DefaultSqlExpression <NullableProperty>(Database);

            sqlExpression.Where(x => x.Age != null);
            var whereStatement = sqlExpression.Context.ToWhereStatement();

            Assert.AreEqual("WHERE ([Age] is not null)", whereStatement);
        }
示例#10
0
        public void NullableExpressionHasValueEqualFalse_ReturnsIsNull()
        {
            var sqlExpression = new DefaultSqlExpression <NullableProperty>(Database);

            sqlExpression.Where(x => x.Age.HasValue == false);
            var whereStatement = sqlExpression.Context.ToWhereStatement();

            Assert.AreEqual("WHERE ([Age] is null)", whereStatement);
        }
示例#11
0
        public void NullableExpressionNotEqualsNull_ReturnsIsNotNull()
        {
            var sqlExpression = new DefaultSqlExpression <NullableProperty>(Database);

            sqlExpression.Where(x => x.Age != null);
            var    whereStatement = sqlExpression.Context.ToWhereStatement();
            string expected       = string.Format("WHERE ({0} is not null)", escapedAgeIdentifier);

            Assert.AreEqual(expected, whereStatement);
        }
示例#12
0
        public void ExpressionWithNullableExpressionNotHasValue_ReturnsNotIsNotNull()
        {
            var sqlExpression = new DefaultSqlExpression <NullableProperty>(Database);

            sqlExpression.Where(x => x.Age > 2 && x.Age.HasValue);
            var    whereStatement = sqlExpression.Context.ToWhereStatement();
            string expected       = string.Format("WHERE (([Age] > @0) AND ({0} is not null))", escapedAgeIdentifier);

            Assert.AreEqual(expected, whereStatement);
        }
示例#13
0
 public virtual T SingleOrDefault(Expression <Func <T, bool> > whereExpression)
 {
     using (var db = GetDatabase())
     {
         SqlExpression <T> express = new DefaultSqlExpression <T>(db);
         express      = express.Where(whereExpression);
         string where = express.Context.ToWhereStatement();
         var poco = db.FirstOrDefault <T>(where, express.Context.Params);
         return(poco);
     }
 }
        //[Test, NUnit.Framework.Ignore("Not Supported For Now")]
        public void FetchOnWithSecondGenericType()
        {
            var s       = new DefaultSqlExpression <CustomerUserJoin>(Database, true);
            var joinexp = s.On <CustomerUser>((x, y) => x.Name == y.CustomerName);

            string expected = string.Format("({0}.{1} = {2}.{3})",
                                            TestDatabase.DbType.EscapeTableName("CUJ"),
                                            TestDatabase.DbType.EscapeTableName("Name"),
                                            TestDatabase.DbType.EscapeTableName("CU"),
                                            TestDatabase.DbType.EscapeTableName("CustomerName"));

            Assert.AreEqual(expected, joinexp);
        }
示例#15
0
        public void FetchOnWithSecondGenericType()
        {
            var s = new DefaultSqlExpression<User>(Database, true);
            var joinexp = s.On<CustomerUser>((x, y) => x.Name == y.CustomerName);
            
            string expected = string.Format("({0}.{1} = {2}.{3})", 
                TestDatabase.DbType.EscapeTableName("U"),
                TestDatabase.DbType.EscapeTableName("Name"),
                TestDatabase.DbType.EscapeTableName("CU"),
                TestDatabase.DbType.EscapeTableName("CustomerName"));

            Assert.AreEqual(expected, joinexp);
        }
        public void SelectStatementDoesNotRenderPropertyNameAsAlias()
        {
            var sqlExpression = new DefaultSqlExpression <UserDecorated>(Database);

            sqlExpression.Select(x => new { x.IsMale, x.Name });
            var selectStatement = sqlExpression.Context.ToSelectStatement();

            string expected = string.Format("SELECT {0}, {1} \nFROM {2}",
                                            TestDatabase.DbType.EscapeSqlIdentifier("is_male"),
                                            TestDatabase.DbType.EscapeSqlIdentifier("Name"),
                                            TestDatabase.DbType.EscapeTableName("Users"));

            Assert.AreEqual(expected, selectStatement);
        }
示例#17
0
 public virtual int Delete(Expression <Func <T, bool> > whereExpression)
 {
     using (var db = GetDatabase())
     {
         /**
          *生成对应的where sql
          */
         SqlExpression <T> express = new DefaultSqlExpression <T>(db);
         express      = express.Where(whereExpression);
         string where = express.Context.ToWhereStatement();
         if (where.StartsWith("WHERE"))
         {
             where = where.Remove(0, 5);
         }
         return(db.DeleteWhere <T>(where, express.Context.Params));
     }
 }
 public void WhereExpressionWithNullableEnumContains()
 {
     var sqlExpression = new DefaultSqlExpression<UserWithNullableId>(Database);
     sqlExpression.Where(x => new List<NameEnum?> { NameEnum.Bill, NameEnum.Bobby }.Contains(x.NameEnum));
     Assert.AreEqual(sqlExpression.Context.Params[0], "Bill");
     Assert.AreEqual(sqlExpression.Context.Params[1], "Bobby");
 }
 public void WhereExpressionWithNullableEnumAndValueIsNull()
 {
     var sqlExpression = new DefaultSqlExpression<UserWithNullableId>(Database);
     sqlExpression.Where(x => x.NameEnum == null);
     Assert.AreEqual(sqlExpression.Context.Params.Length, 0);
 }
 public void WhereExpressionWithNullableEnum()
 {
     var sqlExpression = new DefaultSqlExpression<UserWithNullableId>(Database);
     sqlExpression.Where(x => x.NameEnum == NameEnum.Bobby);
     Assert.AreEqual(sqlExpression.Context.Params[0], "Bobby");
 }
示例#21
0
        public void SelectStatementDoesNotRenderPropertyNameAsAlias()
        {
            var sqlExpression = new DefaultSqlExpression<UserDecorated>(Database);
            sqlExpression.Select(x => new {x.IsMale, x.Name});
            var selectStatement = sqlExpression.Context.ToSelectStatement();

            string expected = string.Format("SELECT {0}, {1} \nFROM {2}",
                                            TestDatabase.DbType.EscapeSqlIdentifier("is_male"),
                                            TestDatabase.DbType.EscapeSqlIdentifier("Name"),
                                            TestDatabase.DbType.EscapeTableName("Users"));

            Assert.AreEqual(expected, selectStatement);
        }
示例#22
0
 public void SelectStatementDoesNotRenderPropertyNameAsAlias()
 {
     var sqlExpression = new DefaultSqlExpression<UserDecorated>(Database);
     sqlExpression.Select(x => new {x.IsMale, x.Name});
     var selectStatement = sqlExpression.Context.ToSelectStatement();
     Assert.AreEqual("SELECT [is_male],[Name] \nFROM [Users]", selectStatement);
 }