# SQLBuilder.Core
**Repository Path**: MuNet/SQLBuilder.Core
## Basic Information
- **Project Name**: SQLBuilder.Core
- **Description**: .NET Standard 2.1、.NET 5 版本SQLBuilder,Expression表达式转换为SQL语句,支持SqlServer、MySql、Oracle、Sqlite、PostgreSql;基于Dapper实现了不同数据库对应的数据仓储Repository;
- **Primary Language**: C#
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: https://gitee.com/zqlovejyc/SQLBuilder.Core
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 33
- **Created**: 2021-06-07
- **Last Updated**: 2023-11-15
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
[](https://gitee.com/zqlovejyc/SQLBuilder.Core/stargazers) [](https://gitee.com/zqlovejyc/SQLBuilder.Core/members) [](https://github.com/zqlovejyc/SQLBuilder.Core/stargazers) [](https://github.com/zqlovejyc/SQLBuilder.Core/network) [](https://github.com/zqlovejyc/SQLBuilder.Core/blob/master/LICENSE) [](https://www.nuget.org/packages/Zq.SQLBuilder.Core)
.NET Standard 2.1、.NET 5、.NET 6、.NET 7 版本SQLBuilder,Expression表达式转换为SQL语句,支持SqlServer、MySql、Oracle、Sqlite、PostgreSql;基于Dapper实现了不同数据库对应的数据仓储Repository;
## 🌭 开源地址
- Gitee:[https://gitee.com/zqlovejyc/SQLBuilder.Core](https://gitee.com/zqlovejyc/SQLBuilder.Core)
- GitHub:[https://github.com/zqlovejyc/SQLBuilder.Core](https://github.com/zqlovejyc/SQLBuilder.Core)
- GitLab:[https://gitlab.com/zqlovejyc/SQLBuilder-Core](https://gitlab.com/zqlovejyc/SQLBuilder-Core)
- NuGet:[https://www.nuget.org/packages/Zq.SQLBuilder.Core](https://www.nuget.org/packages/Zq.SQLBuilder.Core)
- MyGet:[https://www.myget.org/feed/zq-myget/package/nuget/Zq.SQLBuilder.Core](https://www.myget.org/feed/zq-myget/package/nuget/Zq.SQLBuilder.Core)
## 🥥 框架扩展包
| 包类型 | 名称 | 版本 | 描述 |
| :---: | --- | :---: | --- |
| [](https://www.nuget.org/packages/Zq.SQLBuilder.Core) | Zq.SQLBuilder.Core | [](https://www.nuget.org/packages/Zq.SQLBuilder.Core) | SQLBuilder.Core 核心包 |
| [](https://www.nuget.org/packages/Zq.SQLBuilder.Core.SkyWalking) | Zq.SQLBuilder.Core.SkyWalking | [](https://www.nuget.org/packages/Zq.SQLBuilder.Core.SkyWalking) | SQLBuilder.Core SkyWalking 扩展包 |
| [](https://www.nuget.org/packages/Zq.SQLBuilder.Core.ElasticApm) | Zq.SQLBuilder.Core.ElasticApm | [](https://www.nuget.org/packages/Zq.SQLBuilder.Core.ElasticApm) | SQLBuilder.Core ElasticApm扩展包 |
| [](https://www.nuget.org/packages/Zq.SQLBuilder.Core.Diagnostics) | Zq.SQLBuilder.Core.Diagnostics | [](https://www.nuget.org/packages/Zq.SQLBuilder.Core.Diagnostics) | SQLBuilder.Core Diagnostics扩展包 |
## 🚀 快速入门
- #### ➕ 新增
```csharp
//新增
await _repository.InsertAsync(entity);
//批量新增
await _repository.InsertAsync(entities);
//新增
await SqlBuilder
.Insert(() =>
entity)
.ExecuteAsync(
_repository);
//批量新增
await SqlBuilder
.Insert(() =>
new[]
{
new UserInfo { Name = "张三", Sex = 2 },
new UserInfo { Name = "张三", Sex = 2 }
})
.ExecuteAsync(
_repository);
```
- #### 🗑 删除
```csharp
//删除
await _repository.DeleteAsync(entity);
//批量删除
await _repository.DeleteAsync(entitties);
//条件删除
await _repository.DeleteAsync(x => x.Id == "1");
//删除
await SqlBuilder
.Delete()
.Where(x =>
x.Id == "1")
.ExecuteAsync(
_repository);
//主键删除
await SqlBuilder
.Delete()
.WithKey("1")
.ExecuteAsync(
_repository);
```
- #### ✏ 更新
```csharp
//更新
await _repository.UpdateAsync(entity);
//批量更新
await _repository.UpdateAsync(entities);
//条件更新
await _repository.UpdateAsync(x => x.Id == "1", () => entity);
//更新
await SqlBuilder
.Update(() =>
entity,
DatabaseType.MySql,
isEnableFormat:true)
.Where(x =>
x.Id == "1")
.ExecuteAsync(
_repository);
```
- #### 🔍 查询
```csharp
//简单查询
await _repository.FindListAsync(x => x.Id == "1");
//连接查询
await SqlBuilder
.Select((u, t, a, s, d, e, f) =>
new { u.Id, UId = t.Id, a.Name, StudentName = s.Name, ClassName = d.Name, e.CityName, CountryName = f.Name })
.Join((x, t) =>
x.Id == t.Id) //注意此处单表多次Join所以要指明具体表别名,否则都会读取第一个表别名
.Join((x, y) =>
x.Id == y.UserId)
.LeftJoin((x, y) =>
x.Id == y.AccountId)
.RightJoin((x, y) =>
x.Id == y.UserId)
.InnerJoin((x, y) =>
x.CityId == y.Id)
.FullJoin((x, y) =>
x.CountryId == y.Id)
.Where(x =>
x.Id != null)
.ToListAsync(
_repository);
//分页查询
var condition = LinqExtensions
.True()
.And((x, y) =>
x.Id == y.UserId)
.WhereIf(
!name.IsNullOrEmpty(),
(x, y) => name.EndsWith("∞")
? x.Name.Contains(name.Trim('∞'))
: x.Name == name);
var hasWhere = false;
await SqlBuilder
.Select(
(u, a) => new { u.Id, UserName = "u.Name" })
.InnerJoin(
condition)
.WhereIf(
!name.IsNullOrEmpty(),
x => x.Email != null &&
(!name.EndsWith("∞") ? x.Name.Contains(name.TrimEnd('∞', '*')) : x.Name == name),
ref hasWhere)
.WhereIf(
!email.IsNullOrEmpty(),
x => x.Email == email,
ref hasWhere)
.ToPageAsync(
_repository.UseMasterOrSlave(false),
input.OrderField,
input.Ascending,
input.PageSize,
input.PageIndex);
//仓储分页查询
await _repository.FindListAsync(condition, input.OrderField, input.Ascending, input.PageSize, input.PageIndex);
//高级查询
Func @delegate = x => $"ks.{x[0]}{x[1]}{x[2]} WITH(NOLOCK)";
await SqlBuilder
.Select((u, a, s, d, e, f) =>
new { u, a.Name, StudentName = s.Name, ClassName = d.Name, e.CityName, CountryName = f.Name },
tableNameFunc: @delegate)
.Join((x, y) =>
x.Id == y.UserId,
@delegate)
.LeftJoin((x, y) =>
x.Id == y.AccountId,
@delegate)
.RightJoin((x, y) =>
y.Id == x.UserId,
@delegate)
.InnerJoin((x, y) =>
x.CityId == y.Id,
@delegate)
.FullJoin((x, y) =>
x.CountryId == y.Id,
@delegate)
.Where(u =>
u.Id != null)
.ToListAsync(
_repository);
```
- #### 🎫 队列
```csharp
//预提交队列
_repository.AddQueue(async repo =>
await repo.UpdateAsync(
x => x.Id == "1",
() => new
{
Name = "test"
}) > 0);
_repository.AddQueue(async repo =>
await repo.DeleteAsync(x =>
x.Enabled == 1) > 0);
//统一提交队列,默认开启事务
var res = await _repository.SaveQueueAsync();
```
### 🌌 IOC注入
根据appsettions.json配置自动注入不同类型数据仓储,支持一主多从配置
```csharp
//注入SQLBuilder仓储
services.AddSqlBuilder(Configuration, "Base", (sql, parameter) =>
{
//写入文本日志
if (WebHostEnvironment.IsDevelopment())
{
if (parameter is DynamicParameters dynamicParameters)
_logger.LogInformation($@"SQL语句:{sql} 参数:{dynamicParameters
.ParameterNames?
.ToDictionary(k => k, v => dynamicParameters.Get