public ActionResult <ApiResult <string> > Copy([FromForm] ProjectViewModel2 model) { var result = new ApiResult <string>(); var tables = model.Tables; var project = ProjectDb.GetSingle(it => it.Id == model.ProjectId); base.Check(project == null, "请选择方案,没有方案可以在手动生成里面创建"); model.Tables = tables; var template = TemplateDb.GetById(project.TemplateId1).Content; var tableids = Newtonsoft.Json.JsonConvert.DeserializeObject <List <CodeTypeGridViewModel> >(model.Tables).Select(it => it.Id).ToList(); var tableList = CodeTableDb.GetList(it => tableids.Contains(it.Id)); int dbId = tableList.First().DbId; var connection = base.GetTryDb(dbId); List <EntitiesGen> genList = GetGenList(tableList, CodeTypeDb.GetList(), connection.CurrentConnectionConfig.DbType); string key = TemplateHelper.EntityKey + template.GetHashCode(); foreach (var item in genList.Take(1)) { item.name_space = GetNameSpace(project.FileModel, item.name_space); result.Data = TemplateHelper.GetTemplateValue(key, template, item); } ProjectController_Common.CreateProject(project.Id, model.DbId.Value); result.IsSuccess = true; return(result); }
public ActionResult <ApiResult <bool> > SaveCodetableImport([FromForm] int dbid, [FromForm] string model) { ApiResult <bool> result = new ApiResult <bool>(); var list = Newtonsoft.Json.JsonConvert.DeserializeObject <List <DbTableGridViewModel> >(model); var tableDb = base.GetTryDb(dbid); var systemDb = Db; var type = CodeTypeDb.GetList(); var entityList = CodeTableDb.GetList(it => it.DbId == dbid); systemDb.BeginTran(); try { List <CodeTable> Inserts = new List <CodeTable>(); foreach (var item in list) { CodeTableViewModel code = new CodeTableViewModel() { ClassName = PubMehtod.GetCsharpName(item.Name), TableName = item.Name, DbId = dbid, Description = item.Description, ColumnInfoList = new List <CodeColumnsViewModel>() }; var entity = entityList.FirstOrDefault(it => it.TableName.Equals(item.Name, StringComparison.OrdinalIgnoreCase)); if (entity == null) { entity = new CodeTable(); } foreach (var columnInfo in tableDb.DbMaintenance.GetColumnInfosByTableName(item.Name, false)) { var typeInfo = GetEntityType(type, columnInfo, this, tableDb.CurrentConnectionConfig.DbType); CodeColumnsViewModel column = new CodeColumnsViewModel() { ClassProperName = PubMehtod.GetCsharpName(columnInfo.DbColumnName), DbColumnName = columnInfo.DbColumnName, Description = columnInfo.ColumnDescription, IsIdentity = columnInfo.IsIdentity, IsPrimaryKey = columnInfo.IsPrimarykey, Required = columnInfo.IsNullable == false, CodeTableId = entity.Id, CodeType = typeInfo.CodeType.Name, Length = typeInfo.DbTypeInfo.Length, DecimalDigits = typeInfo.DbTypeInfo.DecimalDigits, DefaultValue = columnInfo.DefaultValue }; code.ColumnInfoList.Add(column); } SaveCodeTableToDb(code); } ; systemDb.CommitTran(); result.IsSuccess = true; } catch (Exception ex) { systemDb.RollbackTran(); throw ex; } return(result); }
public ActionResult <ApiResult <bool> > CreateFileByProjectId([FromForm] ProjectViewModel2 model, bool disOpen = true) { var result = new ApiResult <bool>(); var tables = model.Tables; var project = ProjectDb.GetSingle(it => it.Id == model.ProjectId); base.Check(project == null, "请选择方案"); model.Tables = tables; var template = TemplateDb.GetById(project.TemplateId1).Content; var tableids = Newtonsoft.Json.JsonConvert.DeserializeObject <List <CodeTypeGridViewModel> >(model.Tables).Select(it => it.Id).ToList(); var tableList = CodeTableDb.GetList(it => tableids.Contains(it.Id)); int dbId = tableList.First().DbId; var connection = base.GetTryDb(dbId); List <EntitiesGen> genList = GetGenList(tableList, CodeTypeDb.GetList(), connection.CurrentConnectionConfig.DbType); string key = TemplateHelper.EntityKey + template.GetHashCode(); foreach (var item in genList) { item.name_space = GetNameSpace(project.FileModel, item.name_space); var html = TemplateHelper.GetTemplateValue(key, template, item); var fileName = GetFileName(project, item); FileSugar.CreateFileReplace(fileName, html, Encoding.UTF8); } OpenPath(disOpen, project); ProjectController_Common.CreateProject(project.Id, model.DbId.Value); result.IsSuccess = true; result.Message = "生成生功"; return(result); }
public ActionResult <ApiResult <string> > CreateTableByView([FromForm] string ViewSql, [FromForm] int dbid, [FromForm] string className) { ApiResult <string> result = new ApiResult <string>() { IsSuccess = true }; var tableDb = base.GetTryDb(dbid); var dt = tableDb.Ado.GetDataTable(ViewSql); base.Check(dt.Rows.Count == 0, "SQL查询必须要有一条记录才能创建类"); CodeTable table = new CodeTable() { TableName = className, CreateTime = DateTime.Now, ClassName = className, IsLock = true, DbId = dbid, UpdateTime = DateTime.Now, Description = "数据源导入" }; var id = Db.Insertable(table).ExecuteReturnIdentity(); List <CodeColumns> cols = new List <CodeColumns>(); var listtypes = CodeTypeDb.GetList(); foreach (System.Data.DataColumn item in dt.Columns) { CodeColumns columns = new CodeColumns() { CodeTableId = id, ClassProperName = item.ColumnName, DbColumnName = item.ColumnName, CodeType = listtypes.FirstOrDefault(it => it.CSharepType.Equals(item.DataType.Name, StringComparison.OrdinalIgnoreCase) || it.DbType.Any(y => y.Name.Equals(item.DataType.Name, StringComparison.OrdinalIgnoreCase)))?.Name }; if (item.DataType.Name.ToLower() == "int32") { columns.CodeType = listtypes.Where(it => it.CSharepType == "int").First().Name; } if (item.DataType.Name.ToLower() == "int16") { columns.CodeType = listtypes.Where(it => it.CSharepType == "short").First().Name; } if (item.DataType.Name.ToLower() == "int64") { columns.CodeType = listtypes.Where(it => it.CSharepType == "long").First().Name; } if (string.IsNullOrEmpty(columns.CodeType)) { columns.CodeType = listtypes.Where(it => it.CSharepType == "string").First().Name; } cols.Add(columns); } Db.Insertable(cols).ExecuteReturnIdentity(); result.IsSuccess = true; result.Data = result.Message = "创建成功"; return(result); }
private DataTable [] Export(string model, SqlSugarClient tableDb) { var list = Newtonsoft.Json.JsonConvert.DeserializeObject <List <CodeTableViewModel> >(model); var oldList = CodeTableDb.AsQueryable().In(list.Select(it => it.Id).ToList()).ToList(); List <EntitiesGen> genList = GetGenList(oldList, CodeTypeDb.GetList(), tableDb.CurrentConnectionConfig.DbType); List <DataTable> datatables = new List <DataTable>(); foreach (var item in genList) { DataTable dt = new DataTable(); dt.Columns.Add("列名"); dt.Columns.Add("列描述"); dt.Columns.Add("列类型"); dt.Columns.Add("实体类型"); dt.Columns.Add("主键"); dt.Columns.Add("自增"); dt.Columns.Add("可空"); dt.Columns.Add("长度"); dt.Columns.Add("精度"); dt.Columns.Add("默认值"); dt.Columns.Add("表名");; dt.Columns.Add("表描述"); foreach (var it in item.PropertyGens) { var dr = dt.NewRow(); dr["列名"] = it.DbColumnName; dr["列描述"] = it.Description; dr["列类型"] = it.DbType; dr["实体类型"] = it.Type; dr["表名"] = item.TableName;; dr["表描述"] = item.Description; dr["主键"] = it.IsPrimaryKey?"是":""; dr["自增"] = it.IsIdentity ? "是" : ""; dr["可空"] = it.IsNullable ? "是" : ""; dr["长度"] = it.Length; dr["精度"] = it.DecimalDigits; dr["默认值"] = it.DefaultValue; dt.Rows.Add(dr); } dt.TableName = item.TableName; datatables.Add(dt); } return(datatables.ToArray()); }
public ActionResult <ApiResult <bool> > CreateTables([FromForm] string model, [FromForm] int dbid) { var tableDb = base.GetTryDb(dbid); var result = new ApiResult <bool>(); if (!string.IsNullOrEmpty(model)) { var list = Newtonsoft.Json.JsonConvert.DeserializeObject <List <CodeTableViewModel> >(model); var oldList = CodeTableDb.AsQueryable().In(list.Select(it => it.Id).ToList()).ToList(); base.Check(oldList.Any(it => it.IsLock), string.Join(",", oldList.Where(it => it.IsLock).Select(it => it.ClassName)) + "是锁表状态禁止建表"); List <EntitiesGen> genList = GetGenList(oldList, CodeTypeDb.GetList(), tableDb.CurrentConnectionConfig.DbType); foreach (var item in genList) { item.PropertyGens = item.PropertyGens.Where(it => it.IsIgnore == false).ToList(); foreach (var property in item.PropertyGens) { if (property.IsSpecialType) { property.Type = "string"; } } } string key = TemplateHelper.EntityKey + SyntaxTreeHelper.TemplateString.GetHashCode(); foreach (var item in genList) { var classString = TemplateHelper.GetTemplateValue(key, SyntaxTreeHelper.TemplateString, item); var type = SyntaxTreeHelper.GetModelTypeByClass(classString, item.ClassName); tableDb.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices() { EntityNameService = (type, info) => { if (info.EntityName == item.ClassName || (info.EntityName == null && info.DbTableName == item.ClassName)) { info.EntityName = item.ClassName; info.DbTableName = item.TableName; info.TableDescription = item.Description; } }, EntityService = (type, info) => { if (info.EntityName == item.ClassName) { var column = item.PropertyGens.FirstOrDefault(it => it.PropertyName == info.PropertyName); info.DbColumnName = column.DbColumnName; info.ColumnDescription = column.Description; info.IsNullable = column.IsNullable; info.Length = Convert.ToInt32(column.Length); info.DecimalDigits = Convert.ToInt32(column.DecimalDigits); info.IsPrimarykey = column.IsPrimaryKey; info.IsIdentity = column.IsIdentity; info.IsIgnore = column.IsIgnore; info.DataType = column.DbType; if (tableDb.CurrentConnectionConfig.DbType == DbType.Sqlite && info.IsIdentity) { info.DataType = "integer"; } } } }; tableDb.CodeFirst.InitTables(type); } } result.IsSuccess = true; return(result); }