예제 #1
0
        void IResultWriter.AfterCloseReader(int affectedRows)
        {
            var duration = Stopwatch.GetTimestamp() - _beforeExecuteReaderTimestamp;
            var now      = LocalTime.Default.Now;
            var affected = affectedRows >= 0
                ? $"{affectedRows} row(s) affected."
                : null;
            var message = $"Command[{_commandCount - 1}] completed in {StopwatchTimeSpan.ToString(duration, 3)} seconds. {affected}";

            _addInfoMessage(new InfoMessage(now, InfoMessageSeverity.Verbose, null, message));

            if (_query != null)
            {
                var directory = _fileName != null?Path.GetDirectoryName(_fileName) : Path.GetTempPath();

                var results = _query.Results.EmptyIfNull().Zip(_results, ToResult).ToReadOnlyCollection();
                var query   = new DbRequest(directory, _query.Name, _query.Using, _query.Namespace, _commandText, 0, _parameters, results);

                var queryBuilder     = new DbRequestBuilder(query);
                var csharpSourceCode = queryBuilder.Build();

                var path = Path.Combine(query.Directory, $"{query.Name}.generated.cs");
                File.WriteAllText(path, csharpSourceCode, Encoding.UTF8);

                _query       = null;
                _parameters  = null;
                _commandText = null;
                _results     = null;
            }
        }
예제 #2
0
        void IResultWriter.AfterCloseReader(int affectedRows)
        {
            var duration      = Stopwatch.GetTimestamp() - _beforeExecuteReaderTimestamp;
            var header        = StopwatchTimeSpan.ToString(duration, 3);
            var stringBuilder = new StringBuilder();

            stringBuilder.Append($"Command[{_commandCount - 1}] completed.");
            if (affectedRows >= 0)
            {
                stringBuilder.Append($" {affectedRows} row(s) affected.");
            }
            var message = stringBuilder.ToString();

            _addInfoMessage(InfoMessageFactory.Create(InfoMessageSeverity.Verbose, header, message));

            if (_query != null)
            {
                var directory = _fileName != null?Path.GetDirectoryName(_fileName) : Path.GetTempPath();

                var results = _query.Results.EmptyIfNull().Zip(_results, ToResult).ToReadOnlyList();
                var query   = new DbRequest(directory, _query.Name, _query.Using, _query.Namespace, _commandText, 0, _parameters, results);

                var queryBuilder     = new DbRequestBuilder(query);
                var csharpSourceCode = queryBuilder.Build();

                var path = Path.Combine(query.Directory, $"{query.Name}.generated.cs");
                File.WriteAllText(path, csharpSourceCode, Encoding.UTF8);

                _query       = null;
                _parameters  = null;
                _commandText = null;
                _results     = null;
            }
        }
예제 #3
0
 public void DeleteWithoutWhereTest()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         var req =
             DbRequestBuilder.Of <DbFiels>()
             .WithTable("thing")
             .BuildDelete();
     });
 }
예제 #4
0
 public void SelectWithoutFieldsTest()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         var req =
             DbRequestBuilder.Of <DbFiels>()
             .WithTable("thing")
             .WhereCond(DbFiels.id, 54)
             .BuildSelect();
     });
 }
예제 #5
0
        public void DeleteWithWhereTest()
        {
            var req =
                DbRequestBuilder.Of <DbFiels>()
                .WithTable("thing")
                .WhereCond(DbFiels.id, 20)
                .BuildDelete();

            Assert.Equal(@"delete from thing  where id = @id ", req.Sql);
            Assert.Equal(20, req.Parameters[0].Value);
            Assert.Equal(DbRequest.CmdType.Command, req.Type);
        }
예제 #6
0
        public void SelectSimpleTest()
        {
            var req =
                DbRequestBuilder.Of <DbFiels>()
                .WithTable("thing")
                .WithSelectFields(DbFiels.id, DbFiels.name, DbFiels.value)
                .BuildSelect();


            Assert.Equal("select id,name,value from thing", req.Sql);
            Assert.Equal(DbRequest.CmdType.Query, req.Type);
        }
예제 #7
0
        public void SelectWithWhereTest()
        {
            var req =
                DbRequestBuilder.Of <DbFiels>()
                .WithTable("thing")
                .WithSelectFields(DbFiels.name, DbFiels.value)
                .WhereCond(DbFiels.id, 54)
                .BuildSelect();


            Assert.Equal("select name,value from thing where id = @id ", req.Sql);
            Assert.Equal(54, req.Parameters[0].Value);
            Assert.Equal(DbRequest.CmdType.Query, req.Type);
        }
예제 #8
0
        public void SelectWithWhereAndOrderTest()
        {
            var req =
                DbRequestBuilder.Of <DbFiels>()
                .WithTable("thing")
                .WithSelectFields(DbFiels.name, DbFiels.value)
                .WhereCond(DbFiels.date, new DateTime(2000, 01, 01), ">")
                .SetOrder($"{DbFiels.name} desc")
                .BuildSelect();


            Assert.Equal("select name,value from thing where date > @date  order by name desc", req.Sql);
            Assert.Equal(new DateTime(2000, 01, 01), req.Parameters[0].Value);
            Assert.Equal(DbRequest.CmdType.Query, req.Type);
        }
예제 #9
0
        public void SelectWithWhereAndOrderAndFieldCharTest()
        {
            var req =
                DbRequestBuilder.Of <DbFiels>()
                .WithTable("thing")
                .WithFieldQuotes()
                .WithSelectFields(DbFiels.name, DbFiels.value)
                .WhereCond(DbFiels.date, new DateTime(2000, 01, 01), ">")
                .SetOrder($"\"{DbFiels.name}\" desc")
                .BuildSelect();


            Assert.Equal(@"select ""name"",""value"" from ""thing"" where ""date"" > @date  order by ""name"" desc", req.Sql);
            Assert.Equal(new DateTime(2000, 01, 01), req.Parameters[0].Value);
            Assert.Equal(DbRequest.CmdType.Query, req.Type);
        }
예제 #10
0
        public void InsertWithScalarTest()
        {
            var req =
                DbRequestBuilder.Of <DbFiels>()
                .WithTable("thing")
                .WithFieldValues(DbFiels.name, "thing name'")
                .WithFieldValues(DbFiels.value, 15.78)
                .WithFieldValues(DbFiels.date, new DateTime(2000, 01, 01))
                .SetScalar(true)
                .BuildInsert();


            Assert.Equal(@"insert into thing (name,value,date) values (@name,@value,@date) ", req.Sql);
            Assert.Equal("thing name'", req.Parameters[0].Value);
            Assert.Equal(15.78, req.Parameters[1].Value);
            Assert.Equal(new DateTime(2000, 01, 01), req.Parameters[2].Value);
            Assert.Equal(DbRequest.CmdType.CommandWithIdentity, req.Type);
        }
예제 #11
0
        public void UpdateWithWhereTest()
        {
            var req =
                DbRequestBuilder.Of <DbFiels>()
                .WithTable("thing")
                .WithFieldValues(DbFiels.name, "thing name'")
                .WithFieldValues(DbFiels.value, 15.78)
                .WithFieldValues(DbFiels.date, new DateTime(2000, 01, 01))
                .WhereCond(DbFiels.id, 20)
                .BuildUpdate();


            Assert.Equal(@"update thing set name=@name,value=@value,date=@date where id = @id ", req.Sql);
            Assert.Equal("thing name'", req.Parameters[0].Value);
            Assert.Equal(15.78, req.Parameters[1].Value);
            Assert.Equal(new DateTime(2000, 01, 01), req.Parameters[2].Value);
            Assert.Equal(20, req.Parameters[3].Value);
            Assert.Equal(DbRequest.CmdType.Command, req.Type);
        }
예제 #12
0
        public void InsertWithFieldCharAndAppendTest()
        {
            var req =
                DbRequestBuilder.Of <DbFiels>()
                .WithTable("thing")
                .WithFieldQuotes()
                .WithFieldValues(DbFiels.name, "thing name'")
                .WithFieldValues(DbFiels.value, 15.78)
                .WithFieldValues(DbFiels.date, new DateTime(2000, 01, 01))
                .AppendToQuery(" anything else here ")
                .BuildInsert();


            Assert.Equal(@"insert into ""thing"" (""name"",""value"",""date"") values (@name,@value,@date)  anything else here ", req.Sql);
            Assert.Equal("thing name'", req.Parameters[0].Value);
            Assert.Equal(15.78, req.Parameters[1].Value);
            Assert.Equal(new DateTime(2000, 01, 01), req.Parameters[2].Value);
            Assert.Equal(DbRequest.CmdType.CommandWithIdentity, req.Type);
        }
예제 #13
0
        public void UpdateWithFieldArrayWhereTest()
        {
            var req =
                DbRequestBuilder.Of <DbFiels>()
                .WithTable("thing")
                .WithFieldValues(new List <KeyValuePair <string, object> > {
                new KeyValuePair <string, object>("name", "thing name'"),
                new KeyValuePair <string, object>("value", 15.78),
                new KeyValuePair <string, object>("date", new DateTime(2000, 01, 01)),
            })
                .WhereCond(DbFiels.id, 20)
                .BuildUpdate();

            Assert.Equal(@"update thing set name=@name,value=@value,date=@date where id = @id ", req.Sql);
            Assert.Equal("thing name'", req.Parameters[0].Value);
            Assert.Equal(15.78, req.Parameters[1].Value);
            Assert.Equal(new DateTime(2000, 01, 01), req.Parameters[2].Value);
            Assert.Equal(20, req.Parameters[3].Value);

            Assert.Equal(DbRequest.CmdType.Command, req.Type);
        }