This commit is contained in:
sunkaixuan 2022-06-20 13:38:02 +08:00
parent 51df1a3bce
commit 06a669a1da
9 changed files with 552 additions and 2 deletions

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
</configuration>

View File

@ -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<object>().AS("order").Max<int>("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<dynamic>(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<dynamic>(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<dynamic>(x1[0].Sql, x1[0].Parameters);
}
}
}

View File

@ -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<object>().AS("order").Max<int>("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<dynamic>(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<dynamic>(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();
}
}
}

View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{B30F8D3F-7A48-4C7B-B808-A3F900B9F3D4}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Json2SqlTest</RootNamespace>
<AssemblyName>Json2SqlTest</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DeleteableTest.cs" />
<Compile Include="InsertableTest.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="QueryableTest.cs" />
<Compile Include="UpdateableTest.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SqlSugar\SqlSugar.csproj">
<Project>{489bb790-226c-4fad-8d1e-51d72a7ff8e5}</Project>
<Name>SqlSugar</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -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();
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("Json2SqlTest")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("微软中国")]
[assembly: AssemblyProduct("Json2SqlTest")]
[assembly: AssemblyCopyright("Copyright © 微软中国 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("b30f8d3f-7a48-4c7b-b808-a3f900b9f3d4")]
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -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<object>().AS("order").Max<int>("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<dynamic>(x1[0].Sql, x1[0].Parameters);
}
private static void WhereTest(JsonClient jsonToSqlClient)
{
jsonToSqlClient.Context.Queryable<object>().AS("order").Max<int>("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<dynamic>(x1[0].Sql, x1[0].Parameters);
var list2 = jsonToSqlClient.Context.Ado.SqlQuery<dynamic>(x2[0].Sql, x2[0].Parameters);
}
private static void JoinTest(JsonClient jsonToSqlClient)
{
var onList =
new ObjectFuncModel()
{
FuncName = "Equals",
Parameters = new List<object>{
"d.orderid","o.id"
}
};
var selectItems = new List<SelectModel>() {
new SelectModel()
{
AsName = "id",
FiledName = "o.id"
}
};
var x = jsonToSqlClient.Context.Queryable<object>()
.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<dynamic>(x1[0].Sql, x1[0].Parameters);
}
private static void GroupByTest(JsonClient jsonToSqlClient)
{
jsonToSqlClient.Context.Queryable<object>()
.AS("order").GroupBy(new List<GroupByModel> {
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<dynamic>(x1[0].Sql, x1[0].Parameters);
}
private static void OrderByTest(JsonClient jsonToSqlClient)
{
jsonToSqlClient.Context.Queryable<object>()
.AS("order").OrderBy(new List<OrderByModel> {
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<dynamic>(x1[0].Sql, x1[0].Parameters);
var x2 = jsonToSqlClient.Queryable("{Table:\"order\",OrderBy:[[\"id\",\"desc\"],\"name\"]}").ToSqlList();
var list1 = jsonToSqlClient.Context.Ado.SqlQuery<dynamic>(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<dynamic>(x1[0].Sql, x1[0].Parameters);
}
private static void SelectTest(JsonClient jsonToSqlClient)
{
var list = new List<SelectModel>() {
new SelectModel()
{
AsName = "id1",
FiledName = "id"
},
new SelectModel()
{
FiledName = "id"
}
};
jsonToSqlClient.Context
.Queryable<object>()
.AS("order").Select(list).ToList();
}
private static void Description(JsonClient jsonToSqlClient)
{
jsonToSqlClient.Context.Queryable<object>().AS("order").Max<int>("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<JsonTableConfig> GetConfigByUser(List<string> tableNames)
{
JsonTableConfig config = new JsonTableConfig()
{
TableName = "order",
Columns = new List<JsonColumnConfig>()
{
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<IConditionalModel>()
{
new ConditionalModel(){ FieldName="id", ConditionalType= ConditionalType.Equal, FieldValue="1" }
}
};
return new List<JsonTableConfig>() { config };
}
#endregion
}
}

View File

@ -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<object>().AS("order").Max<int>("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<dynamic>(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<dynamic>(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<dynamic>(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<dynamic>(x1[0].Sql, x1[0].Parameters);
}
}
}

View File

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31624.102
# Visual Studio Version 17
VisualStudioVersion = 17.1.32414.318
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlSugar", "SqlSugar\SqlSugar.csproj", "{489BB790-226C-4FAD-8D1E-51D72A7FF8E5}"
EndProject
@ -41,6 +41,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccessTest", "AccessTest\Ac
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomDbTest", "CustomDbTest\CustomDbTest.csproj", "{7BAAEBFC-D86E-4127-8640-E7EFAB8F40EA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Json2SqlTest", "Json2SqlTest\Json2SqlTest.csproj", "{B30F8D3F-7A48-4C7B-B808-A3F900B9F3D4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -115,6 +117,10 @@ Global
{7BAAEBFC-D86E-4127-8640-E7EFAB8F40EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7BAAEBFC-D86E-4127-8640-E7EFAB8F40EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7BAAEBFC-D86E-4127-8640-E7EFAB8F40EA}.Release|Any CPU.Build.0 = Release|Any CPU
{B30F8D3F-7A48-4C7B-B808-A3F900B9F3D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B30F8D3F-7A48-4C7B-B808-A3F900B9F3D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B30F8D3F-7A48-4C7B-B808-A3F900B9F3D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B30F8D3F-7A48-4C7B-B808-A3F900B9F3D4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE