diff --git a/Src/Asp.NetCore2/Json2Sql/DeleteableTest.cs b/Src/Asp.NetCore2/Json2Sql/DeleteableTest.cs new file mode 100644 index 000000000..b9c8cadee --- /dev/null +++ b/Src/Asp.NetCore2/Json2Sql/DeleteableTest.cs @@ -0,0 +1,51 @@ +using SqlSugar; +using System; +using System.Collections.Generic; + +namespace Test +{ + partial class Program + { + + private static void Deleteable01(JsonClient jsonToSqlClient) + { + jsonToSqlClient.Context.Queryable().AS("order").Max("id"); + Demo111(jsonToSqlClient); + Demo222(jsonToSqlClient); + Demo333(jsonToSqlClient); + } + + private static void Demo111(JsonClient jsonToSqlClient) + { + var json = @" + { + ""Table"":""order"", + Where:[ ""id"","" = "",""{int}:1"" ] + } + "; + var x1 = jsonToSqlClient.Deleteable(json).ToSqlList(); + var list1 = jsonToSqlClient.Context.Ado.SqlQuery(x1[0].Sql, x1[0].Parameters); + } + private static void Demo222(JsonClient jsonToSqlClient) + { + var json = @" + { + ""Table"":""order"", + Where:[ ""id"","" = "",""{int}:1"" ] + } + "; + var x1 = jsonToSqlClient.Deleteable(json).ToSqlList(); + var list1 = jsonToSqlClient.Context.Ado.SqlQuery(x1[0].Sql, x1[0].Parameters); + } + private static void Demo333(JsonClient jsonToSqlClient) + { + var json = @" + { + ""Table"":""order"" + } + "; + var x1 = jsonToSqlClient.Deleteable(json).ToSqlList(); + var list1 = jsonToSqlClient.Context.Ado.SqlQuery(x1[0].Sql, x1[0].Parameters); + } + } +} diff --git a/Src/Asp.NetCore2/Json2Sql/InsertableTest.cs b/Src/Asp.NetCore2/Json2Sql/InsertableTest.cs new file mode 100644 index 000000000..ad6601d93 --- /dev/null +++ b/Src/Asp.NetCore2/Json2Sql/InsertableTest.cs @@ -0,0 +1,53 @@ +using SqlSugar; +using System; +using System.Collections.Generic; + +namespace Test +{ + partial class Program + { + + private static void Insetable01(JsonClient jsonToSqlClient) + { + jsonToSqlClient.Context.Queryable().AS("order").Max("id"); + Demo1(jsonToSqlClient); + Demo2(jsonToSqlClient); + Demo3(jsonToSqlClient); + } + + private static void Demo1(JsonClient jsonToSqlClient) + { + var json = @" + { + ""Table"":""order"", + Columns:{name:""{string}:1"",price:""{decimal}:1""} + } + "; + var x1 = jsonToSqlClient.Insertable(json).ToSqlList(); + var list1 = jsonToSqlClient.Context.Ado.SqlQuery(x1[0].Sql, x1[0].Parameters); + } + private static void Demo2(JsonClient jsonToSqlClient) + { + var json = @" + { + ""Table"":""order"", + Columns:[ {name:""{string}:2"",price:""{decimal}:2""} , {name:""{string}:1"",price:""{decimal}:1""} ] + } + "; + var x1 = jsonToSqlClient.Insertable(json).ToSqlList(); + var list1 = jsonToSqlClient.Context.Ado.SqlQuery(x1[0].Sql, x1[0].Parameters); + } + private static void Demo3(JsonClient jsonToSqlClient) + { + var json = @" + { + ""Table"":""order"", + Identity:""id"", + Columns: {name:""{string}:2"",price:""{decimal}:2""} + } + "; + var x1 = jsonToSqlClient.Insertable(json).ToResult(); + + } + } +} diff --git a/Src/Asp.NetCore2/Json2Sql/Json2Sql.csproj b/Src/Asp.NetCore2/Json2Sql/Json2Sql.csproj new file mode 100644 index 000000000..84394ea2e --- /dev/null +++ b/Src/Asp.NetCore2/Json2Sql/Json2Sql.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.1 + + + + + + + diff --git a/Src/Asp.NetCore2/Json2Sql/Program.cs b/Src/Asp.NetCore2/Json2Sql/Program.cs new file mode 100644 index 000000000..36dadeaaa --- /dev/null +++ b/Src/Asp.NetCore2/Json2Sql/Program.cs @@ -0,0 +1,51 @@ +using SqlSugar; +using System; +using System.Collections.Generic; + +namespace Test +{ + partial class Program + { + static void Main(string[] args) + { + JsonClient jsonToSqlClient = new JsonClient(); + //jsonToSqlClient.Context = new SqlSugarClient(new ConnectionConfig() + //{ + // DbType = DbType.SqlServer, + // IsAutoCloseConnection = true, + // ConnectionString = "server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST" + //}); + jsonToSqlClient.Context = new SqlSugarClient(new ConnectionConfig() + { + DbType = DbType.MySql, + IsAutoCloseConnection = true, + ConnectionString = "server=localhost;Database=SqlSugar4xTest;Uid=root;Pwd=haosql" + }); ; + jsonToSqlClient.Context.Aop.OnLogExecuted = (sql, p) => + { + Console.WriteLine(sql); + }; + + Insetable01(jsonToSqlClient); + + Description(jsonToSqlClient); + FuncText(jsonToSqlClient); + OrderByTest(jsonToSqlClient); + GroupByTest(jsonToSqlClient); + SelectTest(jsonToSqlClient); + JoinTest(jsonToSqlClient); + WhereTest(jsonToSqlClient); + PageTest(jsonToSqlClient); + PageTest2(jsonToSqlClient); + PageTest3(jsonToSqlClient); + + Updateable01(jsonToSqlClient); + Deleteable01(jsonToSqlClient); + + + Console.WriteLine(); + } + + + } +} diff --git a/Src/Asp.NetCore2/Json2Sql/QueryableTest.cs b/Src/Asp.NetCore2/Json2Sql/QueryableTest.cs new file mode 100644 index 000000000..ddce760ad --- /dev/null +++ b/Src/Asp.NetCore2/Json2Sql/QueryableTest.cs @@ -0,0 +1,215 @@ +using SqlSugar; +using System; +using System.Collections.Generic; + +namespace Test +{ + partial class Program + { + #region Queryable + private static void FuncText(JsonClient jsonToSqlClient) + { + jsonToSqlClient.Context.Queryable().AS("order").Max("id"); + var json = @" + { + ""Table"":""order"", + Select:[ [{SqlFunc_AggregateMin:[""id""]},""id""], [{SqlFunc_GetDate:[]},""Date""] ] + } + "; + var x1 = jsonToSqlClient.Queryable(json).ToSqlList(); + var list1 = jsonToSqlClient.Context.Ado.SqlQuery(x1[0].Sql, x1[0].Parameters); + } + private static void WhereTest(JsonClient jsonToSqlClient) + { + jsonToSqlClient.Context.Queryable().AS("order").Max("id"); + var json = @" + { + ""Table"":""order"", + Where:[ ""name"",""="", { SqlFunc_ToString:[""{string}:xxx""] } ], + Select:[ [{SqlFunc_AggregateMin:[""id""]},""id""], [{SqlFunc_GetDate:[]},""Date""] ] + } + "; + var x1 = jsonToSqlClient.Queryable(json).ToSqlList(); + var json2 = @" + { + ""Table"":""order"", + Where: [{ ""FieldName"":""id"",""ConditionalType"":""0"",""FieldValue"":""1""}], + Select:[ + [{SqlFunc_GetDate:[]},""Date""] , + [""Name"",""Name""], + [""Id""], + ""price"" + ] + } + "; + var x2 = jsonToSqlClient.Queryable(json2).ToSqlList(); + + var list1 = jsonToSqlClient.Context.Ado.SqlQuery(x1[0].Sql, x1[0].Parameters); + var list2 = jsonToSqlClient.Context.Ado.SqlQuery(x2[0].Sql, x2[0].Parameters); + } + private static void JoinTest(JsonClient jsonToSqlClient) + { + var onList = + new ObjectFuncModel() + { + FuncName = "Equals", + Parameters = new List{ + "d.orderid","o.id" + } + + }; + + var selectItems = new List() { + new SelectModel() + { + AsName = "id", + FiledName = "o.id" + } + }; + var x = jsonToSqlClient.Context.Queryable() + .AS("order", "o") + .AddJoinInfo("orderdetail", "d", onList, JoinType.Left) + .Select(selectItems) + .ToList(); + var json = @" +{ + ""Table"":[ ""order"",""o""], + ""LeftJoin01"": [""orderdetail"", ""d"", [ ""d.orderid"","">"",""o.id"" ]], + ""Select"":[""o.id"" ,[""d.itemid"",""newitemid""]] +} +"; + var x1 = jsonToSqlClient.Queryable(json).ToSqlList(); + var list1 = jsonToSqlClient.Context.Ado.SqlQuery(x1[0].Sql, x1[0].Parameters); + } + private static void GroupByTest(JsonClient jsonToSqlClient) + { + jsonToSqlClient.Context.Queryable() + .AS("order").GroupBy(new List { + new GroupByModel(){ + FieldName="id" + } }).Select("iD").ToList(); + + var json = @" +{ + ""Table"": ""order"" , + GroupBy:[""name""], + Having: [{SqlFunc_AggregateAvg:[""id""]},"">"",""{int}:1"" ], + Select:[ [{SqlFunc_AggregateAvg:[""id""]},""id""],""name"" ] +} +"; + var x1 = jsonToSqlClient.Queryable(json).ToSqlList(); + var list1 = jsonToSqlClient.Context.Ado.SqlQuery(x1[0].Sql, x1[0].Parameters); + } + private static void OrderByTest(JsonClient jsonToSqlClient) + { + jsonToSqlClient.Context.Queryable() + .AS("order").OrderBy(new List { + new OrderByModel(){ + FieldName="id", + OrderByType=OrderByType.Desc + }, + new OrderByModel(){ + FieldName="name", + OrderByType=OrderByType.Asc + } + }).ToList(); + + + var x1 = jsonToSqlClient.Queryable("{Table:\"order\",OrderBy:[{FieldName:\"id\"},{FieldName:\"name\",OrderByType:\"desc\"}]}").ToSqlList(); + var list = jsonToSqlClient.Context.Ado.SqlQuery(x1[0].Sql, x1[0].Parameters); + + + var x2 = jsonToSqlClient.Queryable("{Table:\"order\",OrderBy:[[\"id\",\"desc\"],\"name\"]}").ToSqlList(); + var list1 = jsonToSqlClient.Context.Ado.SqlQuery(x2[0].Sql, x2[0].Parameters); + } + private static void PageTest(JsonClient jsonToSqlClient) + { + var x1 = jsonToSqlClient.Queryable("{Table:\"order\",OrderBy:[ [\"id\",\"desc\"] ],PageNumber:1,PageSize:8}").ToSqlList(); + var list = jsonToSqlClient.Context.Ado.SqlQuery(x1[0].Sql, x1[0].Parameters); + } + private static void SelectTest(JsonClient jsonToSqlClient) + { + var list = new List() { + new SelectModel() + { + AsName = "id1", + FiledName = "id" + }, + new SelectModel() + { + + FiledName = "id" + } + }; + jsonToSqlClient.Context + .Queryable() + .AS("order").Select(list).ToList(); + } + private static void Description(JsonClient jsonToSqlClient) + { + jsonToSqlClient.Context.Queryable().AS("order").Max("id"); + var json = @" + { + ""Table"":""order"", + PageNumber:""1"", + PageSize:""100"" + } + "; + var x1 = jsonToSqlClient.Queryable(json).ToResult(); + var result = jsonToSqlClient.Context.Utilities.SerializeObject(x1); + } + private static void PageTest2(JsonClient jsonToSqlClient) + { + var json = "{Table:\"order\",OrderBy:[ [\"id\",\"desc\"] ],Where:[\"name\",\"=\",\"{string}:a\" ],PageNumber:1,PageSize:8}"; + + var tableNames = jsonToSqlClient.GetTableNameList(json);//通过JSON获取JSON所有表 + var configs = GetConfigByUser(tableNames);//通过表获取行列过滤备注等信息 + + var sqlList = jsonToSqlClient + .Queryable(json) + .UseAuthentication(configs)//查询启用行列过滤 + .ShowDesciption()//查询返回备注 + .ToResult(); + + } + private static void PageTest3(JsonClient jsonToSqlClient) + { + var json = "{Table:\"order\",OrderBy:[ [\"id\",\"desc\"] ]," + + "Where:[\"name\",\"=\",\"{string}:a\" ]," + + "PageNumber:1,PageSize:8," + + "Select:[\"id\",\"name\",\"price\"]}"; + + var tableNames = jsonToSqlClient.GetTableNameList(json);//通过JSON获取JSON所有表 + var configs = GetConfigByUser(tableNames);//通过表获取行列过滤备注等信息 + + var sqlList = jsonToSqlClient + .Queryable(json) + .UseAuthentication(configs)//查询启用行列过滤 + .ShowDesciption()//查询返回备注 + .ToResult(); + + } + private static List GetConfigByUser(List tableNames) + { + + JsonTableConfig config = new JsonTableConfig() + { + TableName = "order", + Columns = new List() + { + new JsonColumnConfig(){ Name="id",Description="编号" }, + new JsonColumnConfig(){ Name="Name",Description="名称" , Validate="required", ValidateMessage="{Description}不能为空" }, + new JsonColumnConfig(){ Name="Name",Description="名称" , Validate="unique", ValidateMessage="{Description}已存在" } + }, + Conditionals = new List() + { + new ConditionalModel(){ FieldName="id", ConditionalType= ConditionalType.Equal, FieldValue="1" } + } + }; + return new List() { config }; + } + + + #endregion + } +} diff --git a/Src/Asp.NetCore2/Json2Sql/UpdateableTest.cs b/Src/Asp.NetCore2/Json2Sql/UpdateableTest.cs new file mode 100644 index 000000000..26582a5e3 --- /dev/null +++ b/Src/Asp.NetCore2/Json2Sql/UpdateableTest.cs @@ -0,0 +1,69 @@ +using SqlSugar; +using System; +using System.Collections.Generic; + +namespace Test +{ + partial class Program + { + + private static void Updateable01(JsonClient jsonToSqlClient) + { + jsonToSqlClient.Context.Queryable().AS("order").Max("id"); + Demo11(jsonToSqlClient); + Demo22(jsonToSqlClient); + Demo33(jsonToSqlClient); + Demo44(jsonToSqlClient); + } + + private static void Demo11(JsonClient jsonToSqlClient) + { + var json = @" + { + ""Table"":""order"", + Columns: { id:""{int}:1"" ,name:""{string}:1"" }, + WhereColumns:[""id""] + } + "; + var x1 = jsonToSqlClient.Updateable(json).ToSqlList(); + var list1 = jsonToSqlClient.Context.Ado.SqlQuery(x1[0].Sql, x1[0].Parameters); + } + private static void Demo22(JsonClient jsonToSqlClient) + { + var json = @" + { + ""Table"":""order"", + Columns:[ {id:2,name:""{string}:2"",price:""{decimal}:2""} , {id:1,name:""{string}:1"",price:""{decimal}:1""} ], + WhereColumns:[""id""] + } + "; + var x1 = jsonToSqlClient.Updateable(json).ToSqlList(); + var list1 = jsonToSqlClient.Context.Ado.SqlQuery(x1[0].Sql, x1[0].Parameters); + } + + private static void Demo33(JsonClient jsonToSqlClient) + { + var json = @" + { + ""Table"":""order"", + Columns: {name:""{string}:2"",price:""{decimal}:2""} , + Where:[""id"",""="",""{int}:11""] + } + "; + var x1 = jsonToSqlClient.Updateable(json).ToSqlList(); + var list1 = jsonToSqlClient.Context.Ado.SqlQuery(x1[0].Sql, x1[0].Parameters); + } + private static void Demo44(JsonClient jsonToSqlClient) + { + var json = @" + { + ""Table"":""order"", + Columns: {name:""{string}:2"",price:""{decimal}:2""} , + Where:[""id"",""="",""{int}:11""] + } + "; + var x1 = jsonToSqlClient.Updateable(json).ToSqlList(); + // var list1 = jsonToSqlClient.Context.Ado.SqlQuery(x1[0].Sql, x1[0].Parameters); + } + } +} diff --git a/Src/Asp.NetCore2/SqlSugarCore.sln b/Src/Asp.NetCore2/SqlSugarCore.sln index 62021c004..e9bdaf931 100644 --- a/Src/Asp.NetCore2/SqlSugarCore.sln +++ b/Src/Asp.NetCore2/SqlSugarCore.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30523.141 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32414.318 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SqlSeverTest", "SqlSeverTest\SqlSeverTest.csproj", "{B44F2513-E55B-480F-A41C-685DDD083355}" EndProject @@ -39,6 +39,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AccessTest", "AccessTest\Ac EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomDbTest", "CustomDbTest\CustomDbTest.csproj", "{7CAAE872-4A27-4373-B44C-504C5E6DDB85}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Json2Sql", "Json2Sql\Json2Sql.csproj", "{0B4998EE-28FB-4B7E-8976-B3261299B425}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -253,6 +255,18 @@ Global {7CAAE872-4A27-4373-B44C-504C5E6DDB85}.Release|ARM32.Build.0 = Release|ARM32 {7CAAE872-4A27-4373-B44C-504C5E6DDB85}.Release|x86.ActiveCfg = Release|Any CPU {7CAAE872-4A27-4373-B44C-504C5E6DDB85}.Release|x86.Build.0 = Release|Any CPU + {0B4998EE-28FB-4B7E-8976-B3261299B425}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0B4998EE-28FB-4B7E-8976-B3261299B425}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0B4998EE-28FB-4B7E-8976-B3261299B425}.Debug|ARM32.ActiveCfg = Debug|Any CPU + {0B4998EE-28FB-4B7E-8976-B3261299B425}.Debug|ARM32.Build.0 = Debug|Any CPU + {0B4998EE-28FB-4B7E-8976-B3261299B425}.Debug|x86.ActiveCfg = Debug|Any CPU + {0B4998EE-28FB-4B7E-8976-B3261299B425}.Debug|x86.Build.0 = Debug|Any CPU + {0B4998EE-28FB-4B7E-8976-B3261299B425}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0B4998EE-28FB-4B7E-8976-B3261299B425}.Release|Any CPU.Build.0 = Release|Any CPU + {0B4998EE-28FB-4B7E-8976-B3261299B425}.Release|ARM32.ActiveCfg = Release|Any CPU + {0B4998EE-28FB-4B7E-8976-B3261299B425}.Release|ARM32.Build.0 = Release|Any CPU + {0B4998EE-28FB-4B7E-8976-B3261299B425}.Release|x86.ActiveCfg = Release|Any CPU + {0B4998EE-28FB-4B7E-8976-B3261299B425}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE