增加对sql只返回string的支持;

修复添加表单时,如果关联的数据库已存在的bug
This commit is contained in:
yubaolee 2022-01-19 15:49:29 +08:00
parent 636624a872
commit 99daa8752f
10 changed files with 352 additions and 139 deletions

View File

@ -0,0 +1,141 @@
using System;
using System.Linq;
using System.Text;
using Infrastructure;
using Newtonsoft.Json.Linq;
using OpenAuth.Repository;
using OpenAuth.Repository.Interface;
using OpenAuth.Repository.QueryObj;
namespace OpenAuth.App
{
/// <summary>
/// 拖动表单,根据表单字段创建数据库表
/// </summary>
public class DragForm: IForm
{
private IUnitWork<OpenAuthDBContext> _unitWork;
public DragForm(IUnitWork<OpenAuthDBContext> unitWork)
{
_unitWork = unitWork;
}
/**
* : sql server
*/
public string GetSql(Repository.Domain.Form form, string dbType)
{
if (dbType == Define.DBTYPE_SQLSERVER) //Sql Server
{
// 获取字段并处理
var jsonArray = JsonHelper.Instance.Deserialize<JObject>(form.ContentData)["list"];
// 数据库名称
string tableName = form.DbName;
var exist = _unitWork.FromSql<QueryStringObj>($"select '1' as value from sysobjects where name = '{tableName}' and type = 'U'").SingleOrDefault();
if (exist != null) return string.Empty;
// 创建数据表
StringBuilder sql = new StringBuilder($"CREATE TABLE {tableName} ( [Id] varchar(50) COLLATE Chinese_PRC_CI_AS NOT NULL,"); //主键
string sqlDefault = "";
foreach (var json in jsonArray)
{
string type = json["type"].ToString();
string name = json["model"].ToString();
sql.Append("[" + name + "] " + field_type_sql(type)); //字段拼接
if ("checkboxs" == type)
sqlDefault += field_type_sql_default(tableName, name, "0");
else
sqlDefault += field_type_sql_default(tableName, name, "''");
}
sql.Append(");");
//设置主键
sql.Append("ALTER TABLE " + tableName + " ADD CONSTRAINT [PK_" + form.DbName +
"] PRIMARY KEY NONCLUSTERED ([Id])");
sql.Append(
"WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ");
sql.Append("ON [PRIMARY];");
//主键默认值
sql.Append("ALTER TABLE " + tableName + " ADD DEFAULT (newid()) FOR [Id];");
return sql + sqlDefault;
}
else
{
// 获取字段并处理
var jsonArray = JsonHelper.Instance.Deserialize<JObject>(form.ContentData)["list"];
// 数据库名称
string tableName = form.DbName;
var exist = _unitWork.FromSql<QueryStringObj>($"select table_name as value from information_schema.tables where table_name ='{tableName}'").SingleOrDefault();
if (exist != null) return string.Empty;
// 创建数据表
StringBuilder sql = new StringBuilder("create table if not exists `"
+ tableName
+ "` ( Id varchar(50) not null primary key,"); //主键
foreach (var json in jsonArray)
{
string type = json["type"].ToString();
string name = json["model"].ToString();
sql.Append("`" + name + "` " + field_type_mysql(type)); //字段拼接
}
sql.Append(");");
return sql.ToString();
}
}
private string field_type_sql(string leipiplugins)
{
if ("textarea" == leipiplugins || "listctrl" == leipiplugins)
{
return " text NULL ,";
}
else if ("checkboxs" == leipiplugins)
{
return " int NOT NULL ,";
}
else
{
return " varchar(255) NULL ,";
}
}
private string field_type_mysql(string leipiplugins)
{
if ("textarea" == leipiplugins || "listctrl" == leipiplugins)
{
return " varchar(255) null ,";
}
else if ("checkboxs" == leipiplugins)
{
return " tinyint not null ,";
}
else
{
return " varchar(255) NULL ,";
}
}
private string field_type_sql_default(string tablename, string field, string defaultValue)
{
return "ALTER TABLE " + tablename + " ADD DEFAULT (" + defaultValue + ") FOR [" + field + "];";
}
}
}

View File

@ -49,7 +49,7 @@ namespace OpenAuth.App
{
var dbtype = _appConfiguration.Value.DbTypes[_httpContextAccessor.GetTenantId()];
UnitWork.ExecuteSql(FormUtil.GetSql(obj, dbtype));
UnitWork.ExecuteSql(FormFactory.CreateForm(obj, this.UnitWork).GetSql(obj, dbtype));
}
UnitWork.Save();
}
@ -73,7 +73,7 @@ namespace OpenAuth.App
if (!string.IsNullOrEmpty(obj.DbName))
{
var dbtype = _appConfiguration.Value.DbTypes[_httpContextAccessor.GetTenantId()];
UnitWork.ExecuteSql(FormUtil.GetSql(obj, dbtype));
UnitWork.ExecuteSql(FormFactory.CreateForm(obj,this.UnitWork).GetSql(obj, dbtype));
}
}

View File

@ -0,0 +1,25 @@
using System;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Interface;
namespace OpenAuth.App
{
public class FormFactory
{
public static IForm CreateForm(Form form, IUnitWork<OpenAuthDBContext> unitWork)
{
if (form.FrmType == 0)
{
return new LeipiForm(unitWork);
}else if (form.FrmType == 1)
{
throw new Exception("自定义表单不需要创建数据库表");
}
else
{
return new DragForm(unitWork);
}
}
}
}

View File

@ -494,142 +494,5 @@ namespace OpenAuth.App
// return temp_html;
// }
/**
* : sql server
*/
public static string GetSql(Form form, string dbType){
if (dbType == Define.DBTYPE_SQLSERVER) //Sql Server
{
// 获取字段并处理
var jsonArray = JArray.Parse(form.ContentData);
// 数据库名称
string tableName= form.DbName ;
// 创建数据表
StringBuilder sql =new StringBuilder("if exists ( select * from sysobjects where name = '"
+tableName+"' and type = 'U') drop table "
+ tableName +";") ;
sql.Append("CREATE TABLE "
+ tableName
+ " ( [Id] varchar(50) COLLATE Chinese_PRC_CI_AS NOT NULL,"); //主键
string sqlDefault = "";
foreach (var json in jsonArray)
{
string name;
string type = json["leipiplugins"].ToString();
if ("checkboxs" == type)
name = json["parse_name"].ToString();
else
name = json["name"].ToString();
sql.Append("[" + name + "] " + field_type_sql(type));//字段拼接
if ("checkboxs" == type)
sqlDefault += field_type_sql_default(tableName, name, "0");
else
sqlDefault += field_type_sql_default(tableName, name, "''");
}
sql.Append(");");
//设置主键
sql.Append("ALTER TABLE "+tableName+" ADD CONSTRAINT [PK_"+form.DbName+"] PRIMARY KEY NONCLUSTERED ([Id])");
sql.Append(
"WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ");
sql.Append("ON [PRIMARY];");
//主键默认值
sql.Append("ALTER TABLE "+tableName+" ADD DEFAULT (newid()) FOR [Id];");
return sql+sqlDefault;
}
else
{
// 获取字段并处理
var jsonArray = JArray.Parse(form.ContentData);
// 数据库名称
string tableName= form.DbName ;
// 创建数据表
StringBuilder sql =new StringBuilder("create table if not exists `"
+ tableName
+ "` ( Id varchar(50) not null primary key,") ; //主键
// string sqlDefault = "";
foreach (var json in jsonArray)
{
string name;
string type = json["leipiplugins"].ToString();
if ("checkboxs" == type)
name = json["parse_name"].ToString();
else
name = json["name"].ToString();
sql.Append("`" + name + "` " + field_type_mysql(type));//字段拼接
//
// if ("checkboxs" == type)
// sqlDefault += field_type_sql_default(tableName, name, "0");
// else
// sqlDefault += field_type_sql_default(tableName, name, "''");
}
sql.Append(");");
return sql.ToString();
}
}
//获取控件字段类型 的sql
private static string field_type_sql(string leipiplugins)
{
if ("textarea"==leipiplugins || "listctrl"==leipiplugins)
{
return " text NULL ,";
}
else if ("checkboxs"==leipiplugins)
{
return " int NOT NULL ,";
}
else
{
return " varchar(255) NULL ,";
}
}
private static string field_type_mysql(string leipiplugins)
{
if ("textarea"==leipiplugins || "listctrl"==leipiplugins)
{
return " varchar(255) null ,";
}
else if ("checkboxs"==leipiplugins)
{
return " tinyint not null ,";
}
else
{
return " varchar(255) NULL ,";
}
}
private static string field_type_sql_default(string tablename, string field, string defaultValue)
{
return "ALTER TABLE "+tablename+" ADD DEFAULT ("+defaultValue+") FOR ["+field+"];";
}
}
}

View File

@ -0,0 +1,7 @@
namespace OpenAuth.App
{
public interface IForm
{
string GetSql(Repository.Domain.Form form, string dbType);
}
}

View File

@ -0,0 +1,148 @@
using System.Linq;
using System.Text;
using Infrastructure;
using Newtonsoft.Json.Linq;
using OpenAuth.Repository;
using OpenAuth.Repository.Interface;
using OpenAuth.Repository.QueryObj;
namespace OpenAuth.App
{
public class LeipiForm : IForm
{
private IUnitWork<OpenAuthDBContext> _uniwWork;
public LeipiForm(IUnitWork<OpenAuthDBContext> uniwWork)
{
_uniwWork = uniwWork;
}
/**
* : sql server
*/
public string GetSql(Repository.Domain.Form form, string dbType)
{
if (dbType == Define.DBTYPE_SQLSERVER) //Sql Server
{
// 获取字段并处理
var jsonArray = JArray.Parse(form.ContentData);
// 数据库名称
string tableName = form.DbName;
var exist = _uniwWork.FromSql<QueryStringObj>($"select '1' as value from sysobjects where name = '{tableName}' and type = 'U'").SingleOrDefault();
if (exist != null) return string.Empty;
// 创建数据表
StringBuilder sql = new StringBuilder($"CREATE TABLE {tableName} ( [Id] varchar(50) COLLATE Chinese_PRC_CI_AS NOT NULL,"); //主键
string sqlDefault = "";
foreach (var json in jsonArray)
{
string name;
string type = json["leipiplugins"].ToString();
if ("checkboxs" == type)
name = json["parse_name"].ToString();
else
name = json["name"].ToString();
sql.Append("[" + name + "] " + field_type_sql(type)); //字段拼接
if ("checkboxs" == type)
sqlDefault += field_type_sql_default(tableName, name, "0");
else
sqlDefault += field_type_sql_default(tableName, name, "''");
}
sql.Append(");");
//设置主键
sql.Append("ALTER TABLE " + tableName + " ADD CONSTRAINT [PK_" + form.DbName +
"] PRIMARY KEY NONCLUSTERED ([Id])");
sql.Append(
"WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ");
sql.Append("ON [PRIMARY];");
//主键默认值
sql.Append("ALTER TABLE " + tableName + " ADD DEFAULT (newid()) FOR [Id];");
return sql + sqlDefault;
}
else
{
// 获取字段并处理
var jsonArray = JArray.Parse(form.ContentData);
// 数据库名称
string tableName = form.DbName;
var exist = _uniwWork.FromSql<QueryStringObj>($"select table_name as value from information_schema.tables where table_name ='{tableName}'").SingleOrDefault();
if (exist != null) return string.Empty;
// 创建数据表
StringBuilder sql = new StringBuilder("create table if not exists `"
+ tableName
+ "` ( Id varchar(50) not null primary key,"); //主键
foreach (var json in jsonArray)
{
string name;
string type = json["leipiplugins"].ToString();
if ("checkboxs" == type)
name = json["parse_name"].ToString();
else
name = json["name"].ToString();
sql.Append("`" + name + "` " + field_type_mysql(type)); //字段拼接
//
// if ("checkboxs" == type)
// sqlDefault += field_type_sql_default(tableName, name, "0");
// else
// sqlDefault += field_type_sql_default(tableName, name, "''");
}
sql.Append(");");
return sql.ToString();
}
}
private string field_type_sql(string leipiplugins)
{
if ("textarea" == leipiplugins || "listctrl" == leipiplugins)
{
return " text NULL ,";
}
else if ("checkboxs" == leipiplugins)
{
return " int NOT NULL ,";
}
else
{
return " varchar(255) NULL ,";
}
}
private string field_type_mysql(string leipiplugins)
{
if ("textarea" == leipiplugins || "listctrl" == leipiplugins)
{
return " varchar(255) null ,";
}
else if ("checkboxs" == leipiplugins)
{
return " tinyint not null ,";
}
else
{
return " varchar(255) NULL ,";
}
}
private string field_type_sql_default(string tablename, string field, string defaultValue)
{
return "ALTER TABLE " + tablename + " ADD DEFAULT (" + defaultValue + ") FOR [" + field + "];";
}
}
}

View File

@ -7,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection;
using Moq;
using OpenAuth.App.Request;
using OpenAuth.App.SSO;
using OpenAuth.Repository.Domain;
namespace OpenAuth.App.Test
{
@ -41,5 +42,19 @@ namespace OpenAuth.App.Test
});
Console.WriteLine(JsonHelper.Instance.Serialize(result));
}
[Test]
public void Update()
{
var app = _autofacServiceProvider.GetService<FormApp>();
app.Update(new Form()
{
Id = "5815ff07-b457-47a2-8a19-c9080c3575d8",
ContentData = "{\"list\":[{\"type\":\"input\",\"icon\":\"icon-input\",\"name\":\"单行文本\",\"options\":{\"width\":\"100%\",\"defaultValue\":\"\",\"required\":false,\"dataType\":\"string\",\"pattern\":\"\",\"placeholder\":\"\",\"disabled\":false,\"remoteFunc\":\"func_1642332330000_92043\"},\"key\":\"1642332330000_92043\",\"model\":\"Name\",\"rules\":[{\"type\":\"string\",\"message\":\"Name格式不正确\"}]},{\"type\":\"textarea\",\"icon\":\"icon-diy-com-textarea\",\"name\":\"多行文本\",\"options\":{\"width\":\"100%\",\"defaultValue\":\"\",\"required\":false,\"disabled\":false,\"pattern\":\"\",\"placeholder\":\"\",\"remoteFunc\":\"func_1642332331000_66846\"},\"key\":\"1642332331000_66846\",\"model\":\"Price\",\"rules\":[]},{\"type\":\"number\",\"icon\":\"icon-number\",\"name\":\"计数器\",\"options\":{\"width\":\"\",\"required\":false,\"defaultValue\":0,\"min\":0,\"max\":0,\"step\":1,\"disabled\":false,\"controlsPosition\":\"\",\"remoteFunc\":\"func_1642332332000_75553\"},\"key\":\"1642332332000_75553\",\"model\":\"number_1642332332000_75553\",\"rules\":[]},{\"type\":\"text\",\"icon\":\"icon-wenzishezhi-\",\"name\":\"文字\",\"options\":{\"defaultValue\":\"This is a text\",\"customClass\":\"\",\"remoteFunc\":\"func_1642332335000_15010\"},\"key\":\"1642332335000_15010\",\"model\":\"text_1642332335000_15010\",\"rules\":[]},{\"type\":\"textarea\",\"icon\":\"icon-diy-com-textarea\",\"name\":\"多行文本\",\"options\":{\"width\":\"100%\",\"defaultValue\":\"\",\"required\":false,\"disabled\":false,\"pattern\":\"\",\"placeholder\":\"\",\"remoteFunc\":\"func_1642332496000_1253\"},\"key\":\"1642332496000_1253\",\"model\":\"textarea_1642332496000_1253\",\"rules\":[]}],\"config\":{\"labelWidth\":100,\"labelPosition\":\"right\",\"size\":\"small\"}}",
DbName = "stock",
OrgId = "08f41bf6-4388-4b1e-bd3e-2ff538b44b1b",
FrmType = 2
});
}
}
}

View File

@ -77,6 +77,7 @@ namespace OpenAuth.Repository
modelBuilder.Entity<DataPrivilegeRule>()
.HasKey(c => new { c.Id });
modelBuilder.Entity<SysTableColumn>().HasNoKey();
modelBuilder.Entity<QueryStringObj>().HasNoKey();
}
public virtual DbSet<Application> Applications { get; set; }
@ -110,6 +111,7 @@ namespace OpenAuth.Repository
public virtual DbSet<BuilderTable> BuilderTables { get; set; }
public virtual DbSet<BuilderTableColumn> BuilderTableColumns { get; set; }
//非数据库表格
public virtual DbSet<QueryStringObj> QueryStringObjs { get; set; }
public virtual DbSet<SysTableColumn> SysTableColumns { get; set; }
}

View File

@ -0,0 +1,11 @@
namespace OpenAuth.Repository.QueryObj
{
/// <summary>
/// 针对只返回字符串类型的数值。查询SQL必需使用Value作为返回字段
/// 因为string没有构造函数不能作为DbSet/DbQuery泛型的参数
/// </summary>
public class QueryStringObj
{
public string Value { get; set; }
}
}

View File

@ -204,6 +204,7 @@ namespace OpenAuth.Repository
public int ExecuteSql(string sql)
{
if (string.IsNullOrEmpty(sql)) return 0;
return _context.Database.ExecuteSqlRaw(sql);
}