public override void OnFileCreateComplated(Configuration configuration, Item item, DbTable table, string fileName) { var projectFile = this.GetPorjectFile(configuration, item); var itemGroupItemType = this.GetItemGroupItemType(item.Extension); ProjectFileConfigurationManager.AddItemGroupItem(projectFile, fileName, itemGroupItemType); }
/// <summary> /// 获取表或视图的详细信息。 /// </summary> /// <param name="databaseName">数据库名称</param> /// <param name="tableName">表或视图名称</param> /// <param name="isView">是否为视图</param> /// <returns>表或视图信息</returns> public override DbTable GetTableOrViewDetails(string databaseName, string tableName, bool isView = false) { var result = new DbTable(); var dbHelper = GetDbHelper(databaseName); var columns = dbHelper.DbMetadata.GetColumns(tableName); result.Name = tableName; result.Schema = tableName.Contains('_') ? tableName.Split('_')[0] : "Core"; result.ModuleName = result.Schema.AsClassName(); result.ClassName = tableName.Contains('_') ? tableName.Substring(tableName.IndexOf('_')).AsClassName() : tableName.AsClassName(); foreach (var item in columns) { var dbColumn = new DbColumn { Name = item.Name }; dbColumn.PropertyName = dbColumn.Name.PascalNaming(); dbColumn.IsIdentity = item.IsIdentity; dbColumn.IsPrimaryKey = item.IsPrimaryKey; dbColumn.Nullable = item.IsNullable; dbColumn.Description = item.Description; dbColumn.SqlType = item.DataType; dbColumn.Length = item.DataLength; dbColumn.BasicType = dbColumn.SqlType; result.Columns.Add(dbColumn); } return result; }
/// <summary> /// 文件生成完毕后的处理。 /// </summary> /// <param name="configuration">代码生成配置信息</param> /// <param name="sectionItem">生成配置项</param> /// <param name="table">数据库表信息</param> /// <param name="fileName">文件名</param> public override void OnFileCreateComplated(Configuration configuration, Item sectionItem, DbTable table, string fileName) { var configFile = this.GetConfigFile(configuration, sectionItem, table); var xdocument = XDocument.Load(configFile); var id = string.Format("{0}Service", table.ClassName.CamelNaming()); var xmlns = this.GetDefaultNamespace(); var exists = (from o in xdocument.Descendants(xmlns + "object") where o.Attribute("id").Value == id select o).Any(); if (!exists) { var element = this.CreateElement("object"); element.SetAttributeValue("id", id); element.SetAttributeValue("type", string.Format(table.FullClassNameFormat, "Service")); element.SetAttributeValue("parent", "serviceSupport"); xdocument.Root.Add(new XComment(string.Format("配置{0}服务对象", table.Description))); xdocument.Root.Add(element); File.SetAttributes(configFile, FileAttributes.Normal); xdocument.Save(configFile, SaveOptions.None); } this.ConfigImports(configuration, sectionItem, table); }
/// <summary> /// 获取表或视图的详细信息。 /// </summary> /// <param name="databaseName">数据库名称</param> /// <param name="tableName">表或视图名称</param> /// <param name="isView">是否为视图</param> /// <returns>表或视图信息</returns> public override DbTable GetTableOrViewDetails(string databaseName, string tableName, bool isView = false) { var arrays = tableName.Split('.'); var result = new DbTable(); var dbHelper = GetDbHelper(databaseName); var columns = dbHelper.DbMetadata.GetColumns(tableName); result.Schema = arrays[0]; result.Name = arrays[0] == "dbo" ? arrays[1] : tableName; result.ModuleName = arrays[0] == "dbo" ? string.Empty : arrays[0]; result.ClassName = arrays[1].AsClassName(); foreach (var item in columns) { var dbColumn = new DbColumn { Name = item.Name }; dbColumn.PropertyName = dbColumn.Name.PascalNaming(); dbColumn.IsIdentity = item.IsIdentity; dbColumn.IsPrimaryKey = item.IsPrimaryKey; dbColumn.Nullable = item.IsNullable; dbColumn.Description = item.Description; dbColumn.SqlType = item.DataType; dbColumn.Length = item.DataLength; dbColumn.BasicType = dbColumn.SqlType; result.Columns.Add(dbColumn); } return result; }
/// <summary> /// 获取配置文件。 /// </summary> /// <param name="configuration">代码生成配置信息</param> /// <param name="item">文件生成配置项信息</param> /// <param name="table">数据库表领域信息</param> /// <returns>配置文件</returns> protected string GetConfigFile(Configuration configuration, Item item, DbTable table) { var projectName = item.Project.Split('\\').LastOrDefault(); var projectPath = item.GetProjectFolder(configuration.OutputFolder, configuration.BaseNamespace); var configFile = string.IsNullOrWhiteSpace(table.ModuleName) ? string.Format(@"{0}\ApplicationContext-{1}.xml", projectPath, projectName) : string.Format(@"{0}\ApplicationContext-{1}-{2}.xml", projectPath, projectName, table.ModuleName); if (!File.Exists(configFile)) { var file = new FileInfo(configFile); if (!file.Directory.Exists) { file.Directory.Create(); } var xdocument = new XDocument(this.CreateElement("objects")); xdocument.Save(configFile, SaveOptions.None); var projectFile = this.GetPorjectFile(configuration, item); var itemGroupItemType = this.GetItemGroupItemType("xml"); ProjectFileConfigurationManager.AddItemGroupItem(projectFile, configFile, itemGroupItemType); } return configFile; }
private void ConfigSqlConfig(Configuration configuration, DbTable table, string configFile, string projectName) { XNamespace xmlns = "http://ibatis.apache.org/dataMapper"; var embedded = string.Format("{0}.{1}.xml, {2}", configuration.CurrentDatabase.Type, table.ClassName, projectName); if (!string.IsNullOrWhiteSpace(table.ModuleName)) { embedded = string.Format("{0}.{1}", table.ModuleName, embedded); } var xdocument = XDocument.Load(configFile); var exists = (from s in xdocument.Descendants(xmlns + "sqlMap") where s.Attribute("embedded") != null && s.Attribute("embedded").Value == embedded select s).Any(); if (!exists) { var sqlMapElement = new XElement(xmlns + "sqlMap"); sqlMapElement.SetAttributeValue("embedded", embedded); if (xdocument.Descendants(xmlns + "sqlMaps").Any()) { xdocument.Descendants(xmlns + "sqlMaps").FirstOrDefault().Add(sqlMapElement); } else { var sqlMapsElement = new XElement(xmlns + "sqlMaps"); sqlMapsElement.Add(sqlMapElement); xdocument.Root.Add(sqlMapsElement); } File.SetAttributes(configFile, FileAttributes.Normal); xdocument.Save(configFile); } }
/// <summary> /// 文件生成完毕后的处理。 /// </summary> /// <param name="configuration">代码生成配置信息</param> /// <param name="item">生成配置项</param> /// <param name="table">数据库表信息</param> /// <param name="fileName">文件名</param> public override void OnFileCreateComplated(Configuration configuration, Item item, DbTable table, string fileName) { if (item.Parameters.ContainsKey("project")) { var projects = item.Parameters["project"].Split('&'); foreach (var project in projects) { var dbType = configuration.CurrentDatabase.Type; var writerSqlMapFilePath = configuration.OutputFolder + "\\" + string.Format(project, configuration.BaseNamespace, dbType, "Writer"); var readerSqlMapFilePath = configuration.OutputFolder + "\\" + string.Format(project, configuration.BaseNamespace, dbType, "Reader"); if (File.Exists(writerSqlMapFilePath)) { this.ConfigSqlConfig(configuration, table, writerSqlMapFilePath, item.GetProjectName(configuration.BaseNamespace)); } if (File.Exists(readerSqlMapFilePath)) { this.ConfigSqlConfig(configuration, table, readerSqlMapFilePath, item.GetProjectName(configuration.BaseNamespace)); } } } }
/// <summary> /// 将已序列化的数据恢复到新的表配置信息中。 /// </summary> /// <param name="serializedTables">已序列化的数据表配置信息集合</param> /// <param name="table">新的表配置信息</param> private void Recover(IList<DbTable> serializedTables, DbTable table) { if (serializedTables == null) { return; } var serializedTable = (from t in serializedTables where t.Name == table.Name select t).FirstOrDefault(); if (serializedTable != null) { table.ClassName = serializedTable.ClassName; table.Description = serializedTable.Description; table.ModuleName = serializedTable.ModuleName; table.ModuleDescription = serializedTable.ModuleDescription; table.IsEntityOnly = serializedTable.IsEntityOnly; table.HasCreate = serializedTable.HasCreate; table.HasUpdate = serializedTable.HasUpdate; table.HasCreateOrUpdate = serializedTable.HasCreateOrUpdate; table.HasRemove = serializedTable.HasRemove; table.HasSingleData = serializedTable.HasSingleData; table.HasSearchData = serializedTable.HasSearchData; foreach (var item in table.Columns) { var configItem = serializedTable.Columns.FirstOrDefault(c => c.Name == item.Name); if (configItem != null) { item.PropertyName = configItem.PropertyName; item.Description = configItem.Description; item.IsSearchCriteria = configItem.IsSearchCriteria; } } } }
private static void OnFileCreateComplated(Configuration configuration, Item sectionItem, DbTable table, string outputFile) { if (!string.IsNullOrWhiteSpace(sectionItem.Handler)) { var handlers = sectionItem.Handler.Split(';'); foreach (var handler in handlers) { IFileCreatedHandler fileCreateComplateHandler = null; if (_dictHandler.ContainsKey(handler)) { fileCreateComplateHandler = _dictHandler[handler]; } else { fileCreateComplateHandler = Activator.CreateInstance(Type.GetType(handler)) as IFileCreatedHandler; if (fileCreateComplateHandler != null) { _dictHandler.Add(handler, fileCreateComplateHandler); } } fileCreateComplateHandler?.OnFileCreateComplated(configuration, sectionItem, table, outputFile); } } }
/// <summary> /// 采用XSLT方式创建代码。 /// </summary> /// <param name="configuration"></param> /// <param name="item"></param> /// <param name="table"></param> private void CreateByXslt(Configuration configuration, Item item, DbTable table) { table.Namespace = item.GetNamespace(configuration.BaseNamespace, table.ModuleName); table.FullClassNameFormat = $"{table.Namespace}.{table.ClassName}{"{0}"}, {item.GetProjectName(configuration.BaseNamespace)}"; var xml = table.ToXml( configuration.CurrentDatabase, configuration.BaseNamespace, configuration.Author, configuration.BuildDate, configuration.CopyrightOwner); if (item.Dependencys != null) { var dependencys = new XElement("dependencys"); foreach (var dependency in item.Dependencys) { var dependencyItem = ConfigManager.Items[dependency]; if (dependencyItem == null || (dependencyItem.Name == "SearchResponse" && !table.HasSearchData)) { continue; } var dependencyNs = dependencyItem.GetNamespace(configuration.BaseNamespace, table.ModuleName); var element = new XElement("dependency", dependencyNs); element.SetAttributeValue("name", dependencyItem.Name); element.SetAttributeValue("className", string.IsNullOrWhiteSpace(dependencyItem.FileFormat) ? table.ClassName : string.Format(dependencyItem.FileFormat, table.ClassName)); element.SetAttributeValue("assembly", dependencyItem.GetProjectName(configuration.BaseNamespace)); var exists = (from d in dependencys.Descendants("dependency") where d.Value == dependencyNs select d).Any(); if (!exists) { dependencys.Add(element); } } xml.Root.Add(dependencys); } var templateFile = item.TemplateFile; var outputFile = item.GetOutputFile(configuration, table); XslHelper.Transform(xml, templateFile, outputFile); // 代码创建完成后的处理。 OnFileCreateComplated(configuration, item, table, outputFile); }
/// <summary> /// 获取生成项的输出文件。 /// </summary> /// <param name="config">代码生成配置信息</param> /// <param name="table">数据库表配置信息</param> /// <returns>生成项的输出文件</returns> public string GetOutputFile(Configuration config, DbTable table) { var folder = this.GetProjectFolder(config.OutputFolder, config.BaseNamespace); if (!string.IsNullOrWhiteSpace(table.ModuleName)) { folder = string.Format(@"{0}\{1}", folder, table.ModuleName); } if (!string.IsNullOrWhiteSpace(this.SubFolder)) { folder = string.Format(@"{0}\{1}", folder, this.SubFolder.Replace("{dbType}", config.CurrentDatabase.Type.ToString())); } if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } return string.Format(@"{0}\{1}.{2}", folder, string.Format(this.FileFormat, table.ClassName), this.Extension); }
protected void ConfigImports(Configuration configuration, Item item, DbTable table) { if (string.IsNullOrWhiteSpace(table.ModuleName)) { return; } XDocument xdocument = null; var projectName = item.Project.Split('\\').LastOrDefault(); var projectNamespace = item.GetProjectName(configuration.BaseNamespace); var projectPath = item.GetProjectFolder(configuration.OutputFolder, configuration.BaseNamespace); var configFile = string.Format(@"{0}\ApplicationContext-{1}.xml", projectPath, projectName); var assemblyResource = string.Format("assembly://{0}/{0}/ApplicationContext-{1}-{2}.xml", projectNamespace, projectName, table.ModuleName); if (!File.Exists(configFile)) { var file = new FileInfo(configFile); if (!file.Directory.Exists) { file.Directory.Create(); } xdocument = new XDocument(this.CreateElement("objects")); xdocument.Save(configFile, SaveOptions.None); } else { xdocument = XDocument.Load(configFile); } var xmlns = this.GetDefaultNamespace(); var exists = (from o in xdocument.Descendants(xmlns + "import") where o.Attribute("resource").Value == assemblyResource select o).Any(); if (!exists) { var element = this.CreateElement("import"); element.SetAttributeValue("resource", assemblyResource); var last = xdocument.Root.Descendants(xmlns + "import").LastOrDefault(); if (last != null) { last.AddAfterSelf(element); } else { var importComment = new XComment(string.Format("导入{0}模块的配置文件", table.ModuleDescription)); xdocument.Root.AddFirst(importComment); importComment.AddAfterSelf(element); } File.SetAttributes(configFile, FileAttributes.Normal); xdocument.Save(configFile, SaveOptions.None); } }
/// <summary> /// 文件生成完毕后的处理。 /// </summary> /// <param name="configuration">代码生成配置信息</param> /// <param name="item">生成配置项</param> /// <param name="table">数据库表信息</param> /// <param name="fileName">文件名</param> public abstract void OnFileCreateComplated(Configuration configuration, Item item, DbTable table, string fileName);
/// <summary> /// 获取表或视图的详细信息。 /// </summary> /// <param name="databaseName">数据库名称</param> /// <param name="tableName">表或视图名称</param> /// <param name="isView">是否为视图</param> /// <returns>表或视图信息</returns> public override DbTable GetTableOrViewDetails(string databaseName, string tableName, bool isView = false) { var result = new DbTable(); var dbUtil = new DbHelper(MYSQL_PROVIDER, string.Format(MYSQL_CONNECTION_FORMAT, this.ServerUri, this.Account, this.Password, databaseName)); result.Name = tableName; result.ClassName = tableName.AsClassName(); result.Description = result.ClassName; try { using (var reader = dbUtil.ExecuteReader(string.Format("DESC {0}", tableName))) { while (reader.Read()) { var field = new DbColumn { Name = reader.GetString(0), PropertyName = reader.GetString(0).PascalNaming(), IsIdentity = !Convert.IsDBNull(reader[5]) && reader.GetString(5).Contains("auto_increment"), IsPrimaryKey = !Convert.IsDBNull(reader[3]) && reader.GetString(3).ToUpper().Contains("PRI"), Nullable = reader.GetString(2) == "YES", Description = reader.GetString(0), }; var sqlType = reader.GetString(1).Split(' ')[0]; if (sqlType.Contains('(')) { sqlType = sqlType.Substring(0, sqlType.IndexOf('(')); } field.SqlType = sqlType; field.BasicType = sqlType; result.Columns.Add(field); } } } catch { } return result; }