Example #1
0
 public void TestSelect_9()
 {
     _builder = new SqlServerBuilder(new DefaultEntityMatedata());
     _builder.Select <Sample>(x => x.StringValue).Select <Sample2>(x => x.StringValue, "sample2StringValue")
     .From <Sample>("a")
     .Join <Sample2>("b").On <Sample, Sample2>((l, r) => l.StringValue == r.StringValue);
     Output.WriteLine(_builder.ToSql());
 }
Example #2
0
        public void TestSelect_10()
        {
            //结果
            var result = new String();

            result.AppendLine("Select [Sample_Email],[Sample_IntValue] ");
            result.Append("From [b]");

            //执行
            _builder = new SqlServerBuilder(new TestEntityMatedata());
            _builder.Select <Sample>(t => new object[] { t.Email, t.IntValue }).From("b");

            //验证
            Assert.Equal(result.ToString(), _builder.ToSql());
        }
Example #3
0
        public void TestSelect_8()
        {
            //结果
            var result = new String();

            result.AppendLine("Select [s].[StringValue],[s].[IsDeleted] ");
            result.Append("From [Sample3] As [s]");

            //执行
            _builder = new SqlServerBuilder(new DefaultEntityMatedata());
            _builder.Select <Sample3>().From <Sample3>("s");

            //验证
            Assert.Equal(result.ToString(), _builder.ToSql());
        }
Example #4
0
        public async Task <dynamic> FirstOrDefaultAsync()
        {
            var sqlBuilder = new SqlServerBuilder();
            var sql        = sqlBuilder.Select(_table, _fields, _where, _orderBy, 1);

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                var command = new SqlCommand(sql, conn);
                command.Parameters.AddRange(_parameters.Parameters);
                var sdr = await command.ExecuteReaderAsync();

                var handler = new SqlDataReaderSelectConverter();
                return(handler.ConvertToDynamicEntity(sdr));
            }
        }
Example #5
0
        public void Test_RemoveSelect_1()
        {
            //结果
            var result = new Str();

            result.AppendLine("Select [s].[Description],[s].[DisplayName],[s].[StringValue],[s].[IntValue] ");
            result.Append("From [Sample2] As [s]");

            //执行
            _builder = new SqlServerBuilder(new DefaultEntityMatedata());
            _builder.Select <Sample2>()
            .RemoveSelect <Sample2>(x => x.Display)
            .From <Sample2>("s");

            //验证
            Assert.Equal(result.ToString(), _builder.ToSql());
        }
Example #6
0
        public T FirstOrDefault()
        {
            var fields = GetFields();
            var from   = GetFrom();

            var sqlBuilder = new SqlServerBuilder();
            var sql        = sqlBuilder.Select(from, fields, _where, _orderBy, 1);

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                var command = new SqlCommand(sql, conn);
                command.Parameters.AddRange(_parameters.Parameters);
                var sdr = command.ExecuteReader();

                var handler = new SqlDataReaderConverter <T>(_includeProperties.Select(p => p.PropertyName).ToArray());
                return(handler.ConvertToEntity(sdr));
            }
        }
Example #7
0
        public async Task <List <dynamic> > ToListAsync()
        {
            var sqlBuilder = new SqlServerBuilder();
            var sql        = sqlBuilder.Select(_table, _fields, _where, _orderBy);

            List <dynamic> result;
            var            visitor = new SqlDataReaderSelectConverter();

            using (var conn = new SqlConnection(_connectionString))
            {
                var command = new SqlCommand(sql, conn);
                command.Parameters.AddRange(_parameters.Parameters);
                conn.Open();
                using (var sdr = await command.ExecuteReaderAsync())
                {
                    result = visitor.ConvertToDynamicList(sdr);
                }
            }

            return(result);
        }
Example #8
0
        public async Task <List <T> > ToListAsync()
        {
            var fields = GetFields();
            var from   = GetFrom();

            var sqlBuilder = new SqlServerBuilder();
            var sql        = sqlBuilder.Select(from, fields, _where, _orderBy);

            var      visitor = new SqlDataReaderConverter <T>(_includeProperties.Select(p => p.PropertyName));
            List <T> result;

            using (var conn = new SqlConnection(_connectionString))
            {
                var command = new SqlCommand(sql, conn);
                command.Parameters.AddRange(_parameters.Parameters);
                conn.Open();
                using (var sdr = await command.ExecuteReaderAsync())
                {
                    result = visitor.ConvertToEntityList(sdr);
                }
            }
            return(result);
        }
Example #9
0
 public void Test_Validate_1()
 {
     _builder.Select("a");
     AssertHelper.Throws <InvalidOperationException>(() => _builder.ToSql());
 }