SqlSugar源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
8.7 KiB

3 years ago
<p align="center">
<span>English</span> |
3 years ago
<a href="https://www.donet5.com/Home/Doc"><font color="red">中文</font></a>
3 years ago
</p>
3 years ago
3 years ago
## SqlSugar ORM
3 years ago
3 years ago
SqlSugar ORM is a library providing Object/Relational Mapping (ORM)
An ORM framework from the future
3 years ago
Using SqlSugar is very simple , And it's powerful.
3 years ago
## Description
3 years ago
- Support SqlServer、MySql、PgSql and Oracle insert blukcopy
3 years ago
- Split table big data self-processing
3 years ago
- Support Multi-tenant, multi-library transactions
3 years ago
- Support Support CodeFirst data migration.
- Support Join query 、 Union all 、 Subquery
- Support Configure the query
- Support DbFirst import entity class from database, or use Generation Tool.
- Support one-to-many and many-to-many navigation properties
3 years ago
- Support MySql、SqlServer、Sqlite、Oracle 、 postgresql 、达梦、人大金仓 、神通数据库、MsAccess、Custom
3 years ago
- Support AOP 、 Diff Log 、 Query Filter
3 years ago
3 years ago
## Documentation
3 years ago
|Other |Select | Insert | Update | Delete|
3 years ago
| ----- | --------- | ----------- | ------- |------- |
2 years ago
<a target="_blank" href="https://github.com/donet5/SqlSugar/wiki/NUGET">Nuget</a>| <a href="https://www.donet5.com/Home/Doc?typeId=1187">Query</a> | <a target="_blank" href="https://www.donet5.com/Home/Doc?typeId=1193"> Insert </a> |<a target="_blank" href="https://www.donet5.com/Home/Doc?typeId=1191">Update</a>| <a target="_blank" href="https://www.donet5.com/Home/Doc?typeId=1195">Delete</a> |
2 years ago
<a target="_blank" href="https://github.com/donet5/SqlSugar/wiki/Create--database-operation-object"> Start guide</a> | <a target="_bank" href="https://www.donet5.com/Home/Doc?typeId=1185">Join query </a> |<a href="https://www.donet5.com/Home/Doc?typeId=2422">Insert without entity </a> | <a href="https://www.donet5.com/Home/Doc?typeId=2423">Update without entity</a> | <a href="https://www.donet5.com/Home/Doc?typeId=2424"> Delete without entity </a> | |
2 years ago
|<a href="https://www.donet5.com/Home/Doc?typeId=2246">Multiple databases</a> | <a target="_bank" href="https://www.donet5.com/Home/Doc?typeId=1188">Include query</a>|<a target="_bank" href="https://www.donet5.com/Home/Doc?typeId=2430">Include Insert</a>| <a target="_bank" href="https://www.donet5.com/Home/Doc?typeId=2432">Include Update</a>| <a target="_bank" href="https://www.donet5.com/Home/Doc?typeId=2431">Include Delete</a>
3 years ago
## Feature characteristic
3 years ago
### Feature1 : Join query
3 years ago
Super simple query syntax
```cs
3 years ago
var query = db.Queryable<Order>()
3 years ago
.LeftJoin<Custom> ((o, cus) => o.CustomId == cus.Id)
.LeftJoin<OrderItem> ((o, cus, oritem ) => o.Id == oritem.OrderId)
.LeftJoin<OrderItem> ((o, cus, oritem , oritem2) => o.Id == oritem2.OrderId)
.Where(o => o.Id == 1)
.Select((o, cus) => new ViewOrder { Id = o.Id, CustomName = cus.Name })
.ToList();
```
```sql
SELECT
[o].[Id] AS [Id],
[cus].[Name] AS [CustomName]
FROM
[Order] o
Left JOIN [Custom] cus ON ([o].[CustomId] = [cus].[Id])
Left JOIN [OrderDetail] oritem ON ([o].[Id] = [oritem].[OrderId])
Left JOIN [OrderDetail] oritem2 ON ([o].[Id] = [oritem2].[OrderId])
WHERE
([o].[Id] = @Id0)
6 years ago
```
2 years ago
### Feature2 :Include Query、Insert、Delete and Update
```cs
2 years ago
//query by nav
3 years ago
var list=db.Queryable<Test>()
2 years ago
.Includes(x => x.Provinces,x=>x.Citys ,x=>x.Street) //multi-level
3 years ago
.Includes(x => x.ClassInfo)
.ToList();
2 years ago
2 years ago
//insert by nav
2 years ago
db.InsertNav(list) //Finer operation than EFCore's SaveChange
2 years ago
.Include(z1 => z1.SchoolA)
.ThenInclude(z1 => z1.RoomList)
.Include(z1 => z1.Books)
.ExecuteCommand();
2 years ago
2 years ago
//delete by nav
2 years ago
db.DeleteNav<Student>(it=>it.Id==1)
.Include(z1 => z1.SchoolA)
.ThenInclude(z1 => z1.RoomList) st
.Include(z1 => z1.Books)
.ExecuteCommand();
2 years ago
2 years ago
//update by nav
2 years ago
db.UpdateNav(list)
.Include(z1 => z1.SchoolA)
.ThenInclude(z1 => z1.RoomList)
.Include(z1 => z1.Books)
.ExecuteCommand();
3 years ago
```
### Feature3 : Page query
3 years ago
```cs
int pageIndex = 1;
int pageSize = 20;
int totalCount=0;
var page = db.Queryable<Student>().ToPageList(pageIndex, pageSize, ref totalCount);
```
6 years ago
3 years ago
### Feature4 : Dynamic expression
3 years ago
```cs
var names= new string [] { "a","b"};
Expressionable<Order> exp = new Expressionable<Order>();
foreach (var item in names)
{
exp.Or(it => it.Name.Contains(item.ToString()));
}
var list= db.Queryable<Order>().Where(exp.ToExpression()).ToList();
```
```sql
SELECT [Id],[Name],[Price],[CreateTime],[CustomId]
FROM [Order] WHERE (
([Name] like '%'+ CAST(@MethodConst0 AS NVARCHAR(MAX))+'%') OR
([Name] like '%'+ CAST(@MethodConst1 AS NVARCHAR(MAX))+'%')
)
```
3 years ago
### Feature5 : Multi-tenant transaction
3 years ago
```cs
//Creaate database object
SqlSugarClient db = new SqlSugarClient(new List<ConnectionConfig>()
{
new ConnectionConfig(){ ConfigId="0", DbType=DbType.SqlServer, ConnectionString=Config.ConnectionString, IsAutoCloseConnection=true },
new ConnectionConfig(){ ConfigId="1", DbType=DbType.MySql, ConnectionString=Config.ConnectionString4 ,IsAutoCloseConnection=true}
});
var mysqldb = db.GetConnection("1");//mysql db
var sqlServerdb = db.GetConnection("0");// sqlserver db
db.BeginTran();
mysqldb.Insertable(new Order()
{
CreateTime = DateTime.Now,
CustomId = 1,
Name = "a",
Price = 1
}).ExecuteCommand();
mysqldb.Queryable<Order>().ToList();
sqlServerdb.Queryable<Order>().ToList();
db.CommitTran();
```
3 years ago
### Feature6 : Singleton Pattern
3 years ago
Implement transactions across methods
```CS
public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
{
DbType = SqlSugar.DbType.SqlServer,
ConnectionString = Config.ConnectionString,
IsAutoCloseConnection = true
},
db=> {
db.Aop.OnLogExecuting = (s, p) =>
{
Console.WriteLine(s);
};
});
using (var tran = Db.UseTran())
{
3 years ago
3 years ago
new Test2().Insert(XX);
new Test1().Insert(XX);
3 years ago
.....
3 years ago
....
3 years ago
tran.CommitTran();
3 years ago
}
```
3 years ago
### Feature7 : Query filter
3 years ago
```cs
3 years ago
//set filter
3 years ago
db.QueryFilter.Add(new TableFilterItem<Order>(it => it.Name.Contains("a")));
3 years ago
db.Queryable<Order>().ToList();
3 years ago
//SELECT [Id],[Name],[Price],[CreateTime],[CustomId] FROM [Order] WHERE ([Name] like '%'+@MethodConst0+'%')
3 years ago
db.Queryable<OrderItem, Order>((i, o) => i.OrderId == o.Id)
.Where(i => i.OrderId != 0)
.Select("i.*").ToList();
//SELECT i.* FROM [OrderDetail] i ,[Order] o WHERE ( [i].[OrderId] = [o].[Id] ) AND
//( [i].[OrderId] <> @OrderId0 ) AND ([o].[Name] like '%'+@MethodConst1+'%')
```
3 years ago
3 years ago
### Feature8 : Insert or update
3 years ago
insert or update
3 years ago
```cs
var x = Db.Storageable(list2).ToStorage();
x.AsInsertable.ExecuteCommand();
x.AsUpdateable.ExecuteCommand();
```
3 years ago
insert into not exists
```cs
var x = Db.Storageable(list).SplitInsert(it => !it.Any()).ToStorage()
x.AsInsertable.ExecuteCommand();
```
3 years ago
3 years ago
### Feature9 :Auto split table
3 years ago
Split entity
3 years ago
```cs
3 years ago
[SplitTable(SplitType.Year)]//Table by year (the table supports year, quarter, month, week and day)
[SugarTable("SplitTestTable_{year}{month}{day}")]
3 years ago
public class SplitTestTable
{
[SugarColumn(IsPrimaryKey =true)]
public long Id { get; set; }
public string Name { get; set; }
3 years ago
3 years ago
//When the sub-table field is inserted, which table will be inserted according to this field.
//When it is updated and deleted, it can also be convenient to use this field to
//find out the related table
3 years ago
[SplitField]
3 years ago
public DateTime CreateTime { get; set; }
}
3 years ago
```
Split query
```cs
3 years ago
var lis2t = db.Queryable<OrderSpliteTest>()
.SplitTable(DateTime.Now.Date.AddYears(-1), DateTime.Now)
.ToPageList(1,2); 
3 years ago
```
3 years ago
3 years ago
### Feature10: Big data insert or update
3 years ago
```cs
//Insert A million only takes a few seconds
3 years ago
db.Fastest<RealmAuctionDatum>().BulkCopy(GetList());
3 years ago
3 years ago
//update A million only takes a few seconds
3 years ago
db.Fastest<RealmAuctionDatum>().BulkUpdate(GetList());//A million only takes a few seconds完
3 years ago
db.Fastest<RealmAuctionDatum>().BulkUpdate(GetList(),new string[]{"id"},new string[]{"name","time"})//no primary key
3 years ago
//if exists update, else insert
var x= db.Storageable<Order>(data).ToStorage();
x.BulkCopy();
x.BulkUpdate();
//set table name
db.Fastest<RealmAuctionDatum>().AS("tableName").BulkCopy(GetList())
//set page
db.Fastest<Order>().PageSize(300000).BulkCopy(insertObjs);
```