mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-05 17:37:58 +08:00
Update demo
This commit is contained in:
parent
73dc46458e
commit
12425a4e5a
274
Src/Asp.Net/OracleTest/Demo/Demo0_SqlSugarClient.cs
Normal file
274
Src/Asp.Net/OracleTest/Demo/Demo0_SqlSugarClient.cs
Normal file
@ -0,0 +1,274 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SqlSugar;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class Demo0_SqlSugarClient
|
||||
{
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
SqlSugarClient();//Create db
|
||||
DbContext();//Optimizing SqlSugarClient usage
|
||||
SingletonPattern();//Singleten Pattern
|
||||
DistributedTransactionExample();
|
||||
CustomAttribute();
|
||||
}
|
||||
|
||||
private static void SqlSugarClient()
|
||||
{
|
||||
//Create db
|
||||
Console.WriteLine("#### SqlSugarClient Start ####");
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.Oracle,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//if no exist create datebase SQLSUGAR4XTEST (bin/database/)
|
||||
//db.DbMaintenance.CreateDatabase();
|
||||
|
||||
//Use db
|
||||
var dt = db.Ado.GetDataTable("select 1 from dual");
|
||||
|
||||
//create table OrderDetail
|
||||
db.CodeFirst.InitTables(typeof(OrderItem));
|
||||
|
||||
db.Insertable(new OrderItem() { OrderId = 1, Price = 0 }).ExecuteCommand();
|
||||
Console.WriteLine("#### SqlSugarClient End ####");
|
||||
|
||||
}
|
||||
|
||||
private static void DbContext()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### DbContext Start ####");
|
||||
var insertObj = new Order { Name = "jack", CreateTime = DateTime.Now };
|
||||
var InsertObjs = new Order[] { insertObj };
|
||||
|
||||
DbContext context = new DbContext();
|
||||
|
||||
context.Db.CodeFirst.InitTables<Order, OrderItem,Custom>();//Create Tables
|
||||
;
|
||||
var orderDb = context.OrderDb;
|
||||
|
||||
//Select
|
||||
var data1 = orderDb.GetById(1);
|
||||
var data2 = orderDb.GetList();
|
||||
var data3 = orderDb.GetList(it => it.Id == 1);
|
||||
var data4 = orderDb.GetSingle(it => it.Id == 1);
|
||||
var p = new PageModel() { PageIndex = 1, PageSize = 2 };
|
||||
var data5 = orderDb.GetPageList(it => it.Name == "xx", p);
|
||||
Console.Write(p.PageCount);
|
||||
var data6 = orderDb.GetPageList(it => it.Name == "xx", p, it => it.Name, OrderByType.Asc);
|
||||
Console.Write(p.PageCount);
|
||||
List<IConditionalModel> conModels = new List<IConditionalModel>();
|
||||
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" });//id=1
|
||||
var data7 = orderDb.GetPageList(conModels, p, it => it.Name, OrderByType.Asc);
|
||||
orderDb.AsQueryable().Where(x => x.Id == 1).ToList();
|
||||
|
||||
//Insert
|
||||
orderDb.Insert(insertObj);
|
||||
orderDb.InsertRange(InsertObjs);
|
||||
var id = orderDb.InsertReturnIdentity(insertObj);
|
||||
orderDb.AsInsertable(insertObj).ExecuteCommand();
|
||||
|
||||
|
||||
//Delete
|
||||
orderDb.Delete(insertObj);
|
||||
orderDb.DeleteById(1);
|
||||
orderDb.DeleteById(new int[] { 1, 2 });
|
||||
orderDb.Delete(it => it.Id == 1);
|
||||
orderDb.AsDeleteable().Where(it => it.Id == 1).ExecuteCommand();
|
||||
|
||||
//Update
|
||||
orderDb.Update(insertObj);
|
||||
orderDb.UpdateRange(InsertObjs);
|
||||
orderDb.Update(it => new Order() { Name = "a", }, it => it.Id == 1);
|
||||
orderDb.AsUpdateable(insertObj).UpdateColumns(it => new { it.Name }).ExecuteCommand();
|
||||
|
||||
//Use Inherit DbContext
|
||||
OrderDal dal = new OrderDal();
|
||||
var data = dal.GetById(1);
|
||||
var list = dal.GetList();
|
||||
|
||||
Console.WriteLine("#### DbContext End ####");
|
||||
}
|
||||
|
||||
private static void CustomAttribute()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Custom Attribute Start ####");
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = Config.ConnectionString,
|
||||
DbType = DbType.Oracle,
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
ConfigureExternalServices = new ConfigureExternalServices()
|
||||
{
|
||||
EntityService = (property, column) =>
|
||||
{
|
||||
|
||||
var attributes = property.GetCustomAttributes(true);//get all attributes
|
||||
|
||||
if (attributes.Any(it => it is KeyAttribute))// by attribute set primarykey
|
||||
{
|
||||
column.IsPrimarykey = true;
|
||||
}
|
||||
},
|
||||
EntityNameService = (type, entity) =>
|
||||
{
|
||||
var attributes = type.GetCustomAttributes(true);
|
||||
if (attributes.Any(it => it is TableAttribute))
|
||||
{
|
||||
entity.DbTableName = (attributes.First(it => it is TableAttribute) as TableAttribute).Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
db.CodeFirst.InitTables<MyCustomAttributeTable>();//Create Table
|
||||
|
||||
db.Insertable(new MyCustomAttributeTable() { Id = Guid.NewGuid().ToString(), Name = "Name" }).ExecuteCommand();
|
||||
var list = db.Queryable<MyCustomAttributeTable>().ToList();
|
||||
|
||||
Console.WriteLine("#### Custom Attribute End ####");
|
||||
}
|
||||
|
||||
|
||||
private static void SingletonPattern()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Singleton Pattern Start ####");
|
||||
Console.WriteLine("Db_Id:" + singleDb.ContextID);
|
||||
Console.WriteLine("Db_Id:" + singleDb.ContextID);
|
||||
var task = new Task(() =>
|
||||
{
|
||||
Console.WriteLine("Task DbId:" + singleDb.ContextID);
|
||||
new Task(() =>
|
||||
{
|
||||
Console.WriteLine("_Task_Task DbId:" + singleDb.ContextID);
|
||||
Console.WriteLine("_Task_Task DbId:" + singleDb.ContextID);
|
||||
|
||||
}).Start();
|
||||
Console.WriteLine("Task DbId:" + singleDb.ContextID);
|
||||
});
|
||||
task.Start();
|
||||
task.Wait();
|
||||
System.Threading.Thread.Sleep(500);
|
||||
Console.WriteLine(string.Join(",", singleDb.TempItems.Keys));
|
||||
|
||||
Console.WriteLine("#### Singleton Pattern end ####");
|
||||
}
|
||||
|
||||
static SqlSugarClient singleDb = new SqlSugarClient(
|
||||
new ConnectionConfig()
|
||||
{
|
||||
ConfigId = 1,
|
||||
DbType = DbType.Oracle,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents()
|
||||
{
|
||||
OnLogExecuting = (sql, p) => { Console.WriteLine(sql); }
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
private static void DistributedTransactionExample()
|
||||
{
|
||||
//see SqlServerTest
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DbContext Example 1
|
||||
/// </summary>
|
||||
public class DbContext
|
||||
{
|
||||
|
||||
public SqlSugarClient Db;
|
||||
public DbContext()
|
||||
{
|
||||
Db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = Config.ConnectionString,
|
||||
DbType = DbType.Oracle,
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
AopEvents = new AopEvents()
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
public SimpleClient<Order> OrderDb => new SimpleClient<Order>(Db);
|
||||
public SimpleClient<OrderItem> OrderItemDb => new SimpleClient<OrderItem>(Db);
|
||||
}
|
||||
|
||||
|
||||
public class OrderDal : DbContext<Order>
|
||||
{
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// DbContext Example 2
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class DbContext<T> where T : class, new()
|
||||
{
|
||||
|
||||
public SqlSugarClient Db;
|
||||
public DbContext()
|
||||
{
|
||||
Db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
ConnectionString = Config.ConnectionString,
|
||||
DbType = DbType.Oracle,
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
AopEvents = new AopEvents()
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
public SimpleClient<T> CurrentDb => new SimpleClient<T>(Db);
|
||||
public virtual T GetById(int id)
|
||||
{
|
||||
return CurrentDb.GetById(id);
|
||||
}
|
||||
public virtual List<T> GetList()
|
||||
{
|
||||
return CurrentDb.GetList();
|
||||
}
|
||||
public virtual bool Delete(int id)
|
||||
{
|
||||
return CurrentDb.DeleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -6,9 +6,10 @@ using System.Text;
|
||||
namespace OrmTest
|
||||
{
|
||||
|
||||
[SqlSugar.SugarTable("ORDERINFO")]
|
||||
public class Order
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true, OracleSequenceName = "seq_newsId")]
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
|
@ -8,7 +8,7 @@ namespace OrmTest
|
||||
[SqlSugar.SugarTable("OrderDetail")]
|
||||
public class OrderItem
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey =true, IsIdentity =true)]
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey =true, OracleSequenceName = "seq_newsId")]
|
||||
public int ItemId { get; set; }
|
||||
public int OrderId { get; set; }
|
||||
public decimal? Price { get; set; }
|
||||
|
@ -47,6 +47,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Config.cs" />
|
||||
<Compile Include="Demo\Demo0_SqlSugarClient.cs" />
|
||||
<Compile Include="Models\CarType.cs" />
|
||||
<Compile Include="Models\Custom.cs" />
|
||||
<Compile Include="Models\Mappers.cs" />
|
||||
|
@ -9,7 +9,9 @@ namespace OrmTest
|
||||
static void Main(string[] args)
|
||||
{
|
||||
OldTestMain.Init();
|
||||
|
||||
|
||||
Demo0_SqlSugarClient.Init();
|
||||
|
||||
//Unit test
|
||||
NewUnitTest.Init();
|
||||
|
||||
|
@ -36,22 +36,18 @@ namespace OrmTest
|
||||
|
||||
|
||||
var task6 = Db.Ado.SqlQuery<dynamic>("select @id as id from dual", new { id = 5 });
|
||||
UValidate.Check(5, task6[0].id, "ado");
|
||||
UValidate.Check(5, task6[0].ID, "ado");
|
||||
|
||||
|
||||
var task7 = Db.Ado.SqlQueryAsync<dynamic>("select @id as id from dual", new { id = 7 });
|
||||
task7.Wait();
|
||||
UValidate.Check(7, task7.Result[0].id, "ado");
|
||||
UValidate.Check(7, task7.Result[0].ID, "ado");
|
||||
|
||||
|
||||
var task8 = Db.Ado.SqlQueryAsync<dynamic>("select 8 as id from dual");
|
||||
task8.Wait();
|
||||
UValidate.Check(8, task8.Result[0].id, "ado");
|
||||
|
||||
var task9=Db.Ado.SqlQuery<Order, OrderItem>("select * from [order];select * from OrderDetail");
|
||||
|
||||
var task10 = Db.Ado.SqlQueryAsync<Order, OrderItem>("select * from [order];select * from OrderDetail");
|
||||
task10.Wait();
|
||||
UValidate.Check(8, task8.Result[0].ID, "ado");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ namespace OrmTest
|
||||
{
|
||||
[SqlSugar.SugarColumn(IndexGroupNameList = new string[] { "group1" })]
|
||||
public int Id { get; set; }
|
||||
[SqlSugar.SugarColumn(DefaultValue="getdate()", IndexGroupNameList =new string[] {"group1" } )]
|
||||
[SqlSugar.SugarColumn(DefaultValue="sysdate", IndexGroupNameList =new string[] {"group1" } )]
|
||||
public DateTime? CreateDate { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -13,12 +13,12 @@ namespace OrmTest
|
||||
{
|
||||
Db.CodeFirst.InitTables<JsonTest>();
|
||||
Db.DbMaintenance.TruncateTable<JsonTest>();
|
||||
Db.Insertable(new JsonTest() { Order = new Order { Id = 1, Name = "order1" } }).ExecuteCommand();
|
||||
Db.Insertable(new JsonTest() { Id=1, OrderX = new Order { Id = 1, Name = "order1" } }).ExecuteCommand();
|
||||
var list = Db.Queryable<JsonTest>().ToList();
|
||||
UValidate.Check("order1", list.First().Order.Name, "Json");
|
||||
Db.Updateable(new JsonTest() { Id = 1, Order = new Order { Id = 2, Name = "order2" } }).ExecuteCommand();
|
||||
UValidate.Check("order1", list.First().OrderX.Name, "Json");
|
||||
Db.Updateable(new JsonTest() { Id = 1, OrderX = new Order { Id = 2, Name = "order2" } }).ExecuteCommand();
|
||||
list= Db.Queryable<JsonTest>().ToList();
|
||||
UValidate.Check("order2", list.First().Order.Name, "Json");
|
||||
UValidate.Check("order2", list.First().OrderX.Name, "Json");
|
||||
var list2 = Db.Queryable<JsonTest>().ToList();
|
||||
}
|
||||
}
|
||||
@ -26,9 +26,9 @@ namespace OrmTest
|
||||
|
||||
public class JsonTest
|
||||
{
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||
[SqlSugar.SugarColumn(IsPrimaryKey = true)]
|
||||
public int Id { get; set; }
|
||||
[SqlSugar.SugarColumn(ColumnDataType = "varchar(max)", IsJson = true)]
|
||||
public Order Order { get; set; }
|
||||
[SqlSugar.SugarColumn(ColumnDataType = "varchar2(4000)", IsJson = true)]
|
||||
public Order OrderX { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -26,12 +26,12 @@ namespace OrmTest
|
||||
//Db.Ado.ExecuteCommand("insert testtree values(hierarchyid::GetRoot(),geography :: STGeomFromText ('POINT(55.9271035250276 -3.29431266523898)',4326),'name')");
|
||||
//var list2 = Db.Queryable<TestTree>().ToList();
|
||||
|
||||
Db.CodeFirst.InitTables<GuidTable>();
|
||||
Db.Queryable<GuidTable>().Where(it => it.Id.HasValue).ToList();
|
||||
Db.CodeFirst.InitTables<GuidTable2>();
|
||||
Db.Queryable<GuidTable2>().Where(it => it.Id.HasValue).ToList();
|
||||
}
|
||||
|
||||
|
||||
public class GuidTable
|
||||
public class GuidTable2
|
||||
{
|
||||
public Guid? Id { get; set; }
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ namespace OrmTest
|
||||
|
||||
public static SqlSugarClient simpleDb => new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.SqlServer,
|
||||
DbType = DbType.Oracle,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
@ -27,7 +27,7 @@ namespace OrmTest
|
||||
});
|
||||
public static SqlSugarClient ssDb => new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.SqlServer,
|
||||
DbType = DbType.Oracle,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
@ -43,7 +43,7 @@ namespace OrmTest
|
||||
});
|
||||
public static SqlSugarClient singleDb = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.SqlServer,
|
||||
DbType = DbType.Oracle,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
@ -58,7 +58,7 @@ namespace OrmTest
|
||||
});
|
||||
public static SqlSugarClient singleAndSsDb = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.SqlServer,
|
||||
DbType = DbType.Oracle,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
|
@ -55,21 +55,21 @@ namespace SqlSugar
|
||||
{
|
||||
get
|
||||
{
|
||||
return "";
|
||||
return "select count(1) from user_ind_columns where index_name=('{0}')";
|
||||
}
|
||||
}
|
||||
protected override string CreateIndexSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "";
|
||||
return "CREATE INDEX Index_{0}_{2} ON {0}({1})";
|
||||
}
|
||||
}
|
||||
protected override string AddDefaultValueSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "";
|
||||
return "ALTER TABLE {0} MODIFY({1} DEFAULT '{2}')";
|
||||
}
|
||||
}
|
||||
protected override string CreateDataBaseSql
|
||||
@ -262,6 +262,35 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
public override bool AddColumn(string tableName, DbColumnInfo columnInfo)
|
||||
{
|
||||
if (columnInfo.DataType == "varchar"&& columnInfo.Length ==0)
|
||||
{
|
||||
columnInfo.DataType = "varchar2";
|
||||
columnInfo.Length = 50;
|
||||
}
|
||||
return base.AddColumn(tableName,columnInfo);
|
||||
}
|
||||
public override bool CreateIndex(string tableName, string[] columnNames)
|
||||
{
|
||||
string sql = string.Format(CreateIndexSql, tableName, string.Join(",", columnNames), string.Join("_", columnNames.Select(it=>(it+"abc").Substring(0,3))));
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
return true;
|
||||
}
|
||||
public override bool AddDefaultValue(string tableName, string columnName, string defaultValue)
|
||||
{
|
||||
if (defaultValue.ToLower().IsIn("sysdate"))
|
||||
{
|
||||
var template = AddDefaultValueSql.Replace("'", "");
|
||||
string sql = string.Format(template,tableName,columnName,defaultValue);
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return base.AddDefaultValue(tableName, columnName, defaultValue);
|
||||
}
|
||||
}
|
||||
public override bool CreateDatabase(string databaseDirectory = null)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
|
Loading…
Reference in New Issue
Block a user