Example #1
0
        public void SelectLambdaParameter()
        {
            using (var fsql = new FreeSql.FreeSqlBuilder()
                              .UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;min pool size=1;Max Pool Size=51")
                              .UseAutoSyncStructure(true)
                              .UseGenerateCommandParameterWithLambda(true)
                              .UseMonitorCommand(
                       cmd => Trace.WriteLine("\r\n线程" + Thread.CurrentThread.ManagedThreadId + ": " + cmd.CommandText) //监听SQL命令对象,在执行前
                       //, (cmd, traceLog) => Console.WriteLine(traceLog)
                       )
                              .Build())
            {
                fsql.Delete <ts_slp01>().Where("1=1").ExecuteAffrows();

                var testItem = new ts_slp01();
                var sql1     = fsql.Select <ts_slp01>().Where(a => a.name == testItem.GetTestName1()).ToSql();
                var sql2     = fsql.Select <ts_slp01>().Where(a => a.name == testItem.GetTestName2()).ToSql();

                Assert.Equal(@"SELECT a.[id], a.[name] 
FROM [ts_slp01] a 
WHERE (a.[name] = @exp_0)", sql1);
                Assert.Equal(@"SELECT a.[id], a.[name] 
FROM [ts_slp01] a 
WHERE (a.[name]  IS  NULL)", sql2);
            }
        }
Example #2
0
        public void WithLambdaParameter01()
        {
            using (var fsql = new FreeSql.FreeSqlBuilder()
                              .UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=6")
                              .UseAutoSyncStructure(true)
                              .UseGenerateCommandParameterWithLambda(true)
                              .UseMonitorCommand(
                       cmd => Trace.WriteLine("\r\n线程" + Thread.CurrentThread.ManagedThreadId + ": " + cmd.CommandText) //监听SQL命令对象,在执行前
                       //, (cmd, traceLog) => Console.WriteLine(traceLog)
                       )
                              .UseLazyLoading(true)
                              .Build())
            {
                fsql.Delete <ts_wlp01>().Where("1=1").ExecuteAffrows();

                var id1     = Guid.NewGuid();
                var id2     = Guid.NewGuid();
                var affrows = fsql.Insert(new[] {
                    new ts_wlp01
                    {
                        id     = id1,
                        status = ts_wlp01_status.completed,
                    },
                    new ts_wlp01
                    {
                        id     = id2,
                        status = ts_wlp01_status.pending,
                    }
                }).ExecuteAffrows();
                Assert.Equal(2, affrows);
                var items = fsql.Select <ts_wlp01>().OrderBy(a => a.status).ToList();
                Assert.Equal(2, items.Count);
                Assert.Equal(id1, items[0].id);
                Assert.Equal(ts_wlp01_status.completed, items[0].status);
                Assert.Equal(id2, items[1].id);
                Assert.Equal(ts_wlp01_status.pending, items[1].status);

                var item1Select = fsql.Select <ts_wlp01>().Where(a => a.status == ts_wlp01_status.completed);
                var item1S0p    = item1Select as Select0Provider;
                Assert.Single(item1S0p._params);
                var p0 = item1S0p._params[0];
                Assert.Equal(p0.DbType, System.Data.DbType.String);
                Assert.Equal(p0.Value, "completed");
                items = item1Select.ToList();
                Assert.Single(items);
                Assert.Equal(id1, items[0].id);
                Assert.Equal(ts_wlp01_status.completed, items[0].status);

                var item2Select = fsql.Select <ts_wlp01>().Where(a => a.status == ts_wlp01_status.pending);
                var item2S0p    = item2Select as Select0Provider;
                Assert.Single(item2S0p._params);
                p0 = item2S0p._params[0];
                Assert.Equal(p0.DbType, System.Data.DbType.String);
                Assert.Equal(p0.Value, "pending");
                items = item2Select.ToList();
                Assert.Single(items);
                Assert.Equal(id2, items[0].id);
                Assert.Equal(ts_wlp01_status.pending, items[0].status);

                // use var
                var item1status = ts_wlp01_status.completed;
                item1Select = fsql.Select <ts_wlp01>().Where(a => a.status == item1status);
                item1S0p    = item1Select as Select0Provider;
                Assert.Single(item1S0p._params);
                p0 = item1S0p._params[0];
                Assert.Equal(p0.DbType, System.Data.DbType.String);
                Assert.Equal(p0.Value, "completed");
                items = item1Select.ToList();
                Assert.Single(items);
                Assert.Equal(id1, items[0].id);
                Assert.Equal(item1status, items[0].status);

                var item2status = ts_wlp01_status.pending;
                item2Select = fsql.Select <ts_wlp01>().Where(a => a.status == item2status);
                item2S0p    = item2Select as Select0Provider;
                Assert.Single(item2S0p._params);
                p0 = item2S0p._params[0];
                Assert.Equal(p0.DbType, System.Data.DbType.String);
                Assert.Equal(p0.Value, "pending");
                items = item2Select.ToList();
                Assert.Single(items);
                Assert.Equal(id2, items[0].id);
                Assert.Equal(item2status, items[0].status);
            }
        }