Lambda 轉換 SQL ORM 工具 SqlRepoEx

weixin_34127717發表於2018-10-09

主要解決:

1、解決拼接語句,使用where以外方法時,缺少Where子句時的錯誤;
2、解決多條件拼接Where;
3、增加操作時,不再受限於例項必需有Id的自增自段

Example

IRepository<ToDo> repository = repositoryFactory.Create<ToDo>();
            var results = repository.Query()
                         .Select(e => e.Id, e => e.Task, e => e.CreatedDate);
 results = results.Where(e => e.IsCompleted == false);
 results = results.Where(e => e.Id == 3);

未改之前

SELECT [dbo].[ToDo].[Id]
, [dbo].[ToDo].[Task]
, [dbo].[ToDo].[CreatedDate]
FROM [dbo].[ToDo]
WHERE ([dbo].[ToDo].[IsCompleted] = 0)
WHERE ([dbo].[ToDo].[Id] = 3);

更改後

SELECT [dbo].[ToDo].[Id]
, [dbo].[ToDo].[Task]
, [dbo].[ToDo].[CreatedDate]
FROM [dbo].[ToDo]
WHERE ([dbo].[ToDo].[IsCompleted] = 0)
And ([dbo].[ToDo].[Id] = 3);

指定非自增欄位

var repository = this.repositoryFactory.Create<DoitTest>();
DoitTest doitTest = new DoitTest();

doitTest.TestRmk = "測試";
doitTest.TestBool = true;
doitTest.TestId = 123;
Console.WriteLine(repository.Insert().UsingIdField(d => d.TestId, false).For(doitTest).Sql());

生成的SQL

INSERT [dbo].[DoitTest]([TestId], [TestRmk], [TestBool])
VALUES(123, '測試', 1);

指定非自增欄位

var repository = this.repositoryFactory.Create<DoitTest>();
DoitTest doitTest = new DoitTest();

doitTest.TestRmk = "測試";
doitTest.TestBool = true;
doitTest.TestId = 123;
Console.WriteLine(repository.Insert().UsingIdField(d => d.TestId).For(doitTest).Sql());

生成的SQL

INSERT [dbo].[DoitTest]([TestRmk], [TestBool])
VALUES('測試', 1);
SELECT *
FROM [dbo].[DoitTest]
WHERE [TestId] = SCOPE_IDENTITY();

原專案中的例子:

public class GettingStarted
{
    private IRepositoryFactory repositoryFactory;

    public GettingStarted(IRepositoryFactory repositoryFactory)
    {
        this.repositoryFactory = repositoryFactory;
    }

    public void DoIt()
    {
         var repository = this.repositoryFactory.Create<ToDo>();
         var results = repository.Query()
         .Select(e => e.Id, e => e.Task, e => e.CreatedDate)
         .Where(e => e.IsCompleted == false)
         .Go();
    }
}

Generates the following SQL statement and maps the results back to the list of ToDo objects.

SELECT [dbo].[ToDo].[Id], [dbo].[ToDo].[Task], [dbo].[ToDo].[CreatedDate]
FROM [dbo].[ToDo]
WHERE [dbo].[ToDo].[IsCompleted] = 0;

2018-9-25增加分頁操作

var repository = RepoFactory.Create<ToDo>();
            var results = repository.Query()
                                    .Select(e => e.Id, e => e.Task, e => e.CreatedDate)
                                    .OrderBy(e => e.Id)
                                    .Page(10, 3)
                                    .Go();

2018-9-25增加儲存OUTPUT引數返回

本文來自雲棲社群合作伙伴“開源中國”

本文作者:h4cd

原文連結

相關文章