/// <summary> /// 往表中填充字段信息 /// </summary> /// <param name="connectionString">数据库连接字符串</param> /// <param name="tableInfo">需要填充的表信息对象</param> public void FillColumnInfo(string connectionString, TableInfo tableInfo) { MySqlConnection dbConn = new MySqlConnection(connectionString); dbConn.Open(); string currentSchema = dbConn.Database; dbConn.Close(); dbConn.Dispose(); dbConn = null; StringBuilder colQuery = new StringBuilder(); colQuery.AppendLine(@"SELECT "); colQuery.AppendLine(@" C.*, "); colQuery.AppendLine(@" PKTBL.PK, "); colQuery.AppendLine(@" UKTBL.UK, "); colQuery.AppendLine(@" FKTBL.FK, "); colQuery.AppendLine(@" FKTBL.FK_NAME, "); colQuery.AppendLine(@" FKTBL.REF_SCHEMA, "); colQuery.AppendLine(@" FKTBL.REF_TABLE_NAME, "); colQuery.AppendLine(@" FKTBL.REF_COLUMN_NAME "); colQuery.AppendLine(@"FROM "); colQuery.AppendLine(@" `information_schema`.`columns` AS C "); colQuery.AppendLine(@"LEFT JOIN "); colQuery.AppendLine(@" (SELECT "); colQuery.AppendLine(@" k.`table_schema`, "); colQuery.AppendLine(@" k.`table_name`, "); colQuery.AppendLine(@" k.`column_name`, "); colQuery.AppendLine(@" tc.constraint_name, "); colQuery.AppendLine(@" tc.constraint_type AS PK "); colQuery.AppendLine(@" FROM "); colQuery.AppendLine(@" `information_schema`.KEY_COLUMN_USAGE AS k "); colQuery.AppendLine(@" INNER JOIN `information_schema`.TABLE_CONSTRAINTS AS tc ON k.CONSTRAINT_NAME = tc.CONSTRAINT_NAME "); colQuery.AppendLine(@" AND k.`table_name` = tc.`table_name` "); colQuery.AppendLine(@" AND k.table_schema = tc.table_schema "); colQuery.AppendLine(@" AND tc.CONSTRAINT_TYPE = 'PRIMARY KEY' "); colQuery.AppendLine(@" ) PKTBL ON PKTBL.`table_schema` = C.`table_schema` "); colQuery.AppendLine(@" AND PKTBL.`table_name` = C.`table_name` "); colQuery.AppendLine(@" AND PKTBL.`column_name` = C.`column_name` "); colQuery.AppendLine(@"LEFT JOIN "); colQuery.AppendLine(@" (SELECT DISTINCT "); colQuery.AppendLine(@" k.`table_schema`, "); colQuery.AppendLine(@" k.`table_name`, "); colQuery.AppendLine(@" k.`column_name`, "); colQuery.AppendLine(@" tc.constraint_type AS UK "); colQuery.AppendLine(@" FROM"); colQuery.AppendLine(@" `information_schema`.KEY_COLUMN_USAGE AS k "); colQuery.AppendLine(@" INNER JOIN `information_schema`.TABLE_CONSTRAINTS AS tc ON k.CONSTRAINT_NAME = tc.CONSTRAINT_NAME "); colQuery.AppendLine(@" AND k.`table_name` = tc.`table_name` "); colQuery.AppendLine(@" AND k.table_schema = tc.table_schema "); colQuery.AppendLine(@" AND tc.CONSTRAINT_TYPE = 'UNIQUE' "); colQuery.AppendLine(@" ) UKTBL ON UKTBL.`table_schema` = C.`table_schema` "); colQuery.AppendLine(@" AND UKTBL.`table_name` = C.`table_name` "); colQuery.AppendLine(@" AND UKTBL.`column_name` = C.`column_name` "); colQuery.AppendLine(@"LEFT JOIN "); colQuery.AppendLine(@" (SELECT "); colQuery.AppendLine(@" k.`table_schema`, "); colQuery.AppendLine(@" k.`table_name`,"); colQuery.AppendLine(@" k.`column_name`, "); colQuery.AppendLine(@" tc.constraint_type AS FK, "); colQuery.AppendLine(@" tc.constraint_name AS FK_NAME, "); colQuery.AppendLine(@" k.referenced_table_schema AS REF_SCHEMA, "); colQuery.AppendLine(@" k.referenced_table_name AS REF_TABLE_NAME, "); colQuery.AppendLine(@" k.referenced_column_name AS REF_COLUMN_NAME "); colQuery.AppendLine(@" FROM"); colQuery.AppendLine(@" `information_schema`.KEY_COLUMN_USAGE AS k "); colQuery.AppendLine(@" INNER JOIN `information_schema`.TABLE_CONSTRAINTS AS tc ON k.CONSTRAINT_NAME = tc.CONSTRAINT_NAME "); colQuery.AppendLine(@" AND k.`table_name` = tc.`table_name` "); colQuery.AppendLine(@" AND k.table_schema = tc.table_schema "); colQuery.AppendLine(@" AND tc.CONSTRAINT_TYPE = 'FOREIGN KEY' "); colQuery.AppendLine(@" ) FKTBL ON FKTBL.`table_schema` = C.`table_schema` "); colQuery.AppendLine(@" AND FKTBL.`table_name` = C.`table_name` "); colQuery.AppendLine(@" AND FKTBL.`column_name` = C.`column_name` "); colQuery.AppendLine(@"WHERE"); colQuery.AppendLine(@" C.TABLE_SCHEMA = @TableSchema "); colQuery.AppendLine(@"AND "); colQuery.AppendLine(@" C.TABLE_NAME = @TableName "); colQuery.AppendLine(@"ORDER BY "); colQuery.AppendLine(@" C.ORDINAL_POSITION "); MySqlParameter[] paramCollection = new MySqlParameter[2]; paramCollection[0] = new MySqlParameter("TableSchema", MySqlDbType.String); paramCollection[1] = new MySqlParameter("TableName", MySqlDbType.String); paramCollection[0].Value = currentSchema; paramCollection[1].Value = tableInfo.Name; ColumnInfo colInfo = null; int tmpInt = 0; try { DataSet colDs = MySqlHelper.ExecuteDataset(connectionString, colQuery.ToString(), paramCollection); if (colDs != null && colDs.Tables.Count > 0 && colDs.Tables[0].Rows.Count > 0) { DataTable colTbl = colDs.Tables[0]; foreach(DataRow dRow in colTbl.Rows) { colInfo = new ColumnInfo(); colInfo.Name = dRow["COLUMN_NAME"].ToString(); colInfo.SqlType = dRow["DATA_TYPE"].ToString().ToLower(); colInfo.MaxLength = !int.TryParse(dRow["CHARACTER_MAXIMUM_LENGTH"].ToString(), out tmpInt) ? 0 : tmpInt; colInfo.IsNullable = "YES".Equals(dRow["IS_NULLABLE"].ToString()) ? true : false; colInfo.ColId = Convert.ToInt32(dRow["ORDINAL_POSITION"]); colInfo.Comment = dRow["COLUMN_COMMENT"].ToString(); colInfo.Precision = !int.TryParse(dRow["NUMERIC_PRECISION"].ToString(), out tmpInt) ? 0 : tmpInt; colInfo.Scale = !int.TryParse(dRow["NUMERIC_SCALE"].ToString(), out tmpInt) ? 0 : tmpInt; colInfo.DefaultValue = dRow["COLUMN_DEFAULT"].ToString(); colInfo.IsUnique = dRow["UK"] is DBNull ? false : true; colInfo.IsPK = dRow["PK"] is DBNull ? false : true; colInfo.IsFK = dRow["FK"] is DBNull ? false : true; if (colInfo.IsFK) { colInfo.FKName = dRow["FK_NAME"].ToString(); colInfo.FKColumnName = dRow["REF_COLUMN_NAME"].ToString(); colInfo.FKTableName = dRow["REF_TABLE_NAME"].ToString(); } colInfo.DbType = ToDbType(colInfo.SqlType, colInfo.Precision, colInfo.Scale); colInfo.DalType = ToDalType(colInfo.SqlType, colInfo.Precision, colInfo.Scale); tableInfo.AddColumn(colInfo); } } } catch (Exception ex) { throw new ResponseException(GenCodeToolResource.Error_GetColumn, ex); } }
/// <summary> /// 获得指定表的表信息对象 /// </summary> /// <param name="connectionString">数据库连接字符串</param> /// <param name="tableName">数据库表名</param> /// <returns>返回表信息对象,如果没有该表,则返回null</returns> public TableInfo GetTableInfo(string connectionString, string tableName) { TableInfo result = new TableInfo(); MySqlConnection dbConn = new MySqlConnection(connectionString); dbConn.Open(); string currentSchema = dbConn.Database; dbConn.Close(); dbConn.Dispose(); dbConn = null; StringBuilder tblQuery = new StringBuilder(); tblQuery.AppendLine(@"SELECT "); tblQuery.AppendLine(@" * "); tblQuery.AppendLine(@"FROM "); tblQuery.AppendLine(@" `information_schema`.`tables` "); tblQuery.AppendLine(@"WHERE "); tblQuery.AppendLine(@" table_schema=@TableSchema "); tblQuery.AppendLine(@"AND "); tblQuery.AppendLine(@" table_name=@TableName "); MySqlParameter[] paramCollection = new MySqlParameter[2]; paramCollection[0] = new MySqlParameter("TableSchema", MySqlDbType.String, 100); paramCollection[1] = new MySqlParameter("TableName", MySqlDbType.String, 100); paramCollection[0].Value = currentSchema; paramCollection[1].Value = tableName; try { DataSet tblDs = MySqlHelper.ExecuteDataset(connectionString, tblQuery.ToString(), paramCollection); if (tblDs != null && tblDs.Tables.Count > 0 && tblDs.Tables[0].Rows.Count > 0) { DataTable tblInfoTbl = tblDs.Tables[0]; DataRow dRow = tblInfoTbl.Rows[0]; result = new TableInfo(); result.Name = dRow["TABLE_NAME"].ToString(); result.Comment = dRow["TABLE_COMMENT"].ToString(); } } catch (Exception ex) { throw new ResponseException(GenCodeToolResource.Error_GetTable, ex); } return result; }
/// <summary> /// 获取指定数据库中所有表的信息对象集合 /// </summary> /// <param name="connectionString">数据库连接字符串</param> /// <returns>返回表信息对象集合</returns> public IList<TableInfo> GetTableInfoList(string connectionString) { IList<TableInfo> resultList = new List<TableInfo>(); MySqlConnection dbConn = new MySqlConnection(connectionString); dbConn.Open(); string currentSchema = dbConn.Database; dbConn.Close(); dbConn.Dispose(); dbConn = null; StringBuilder tblQuery = new StringBuilder(); tblQuery.AppendLine(@"SELECT "); tblQuery.AppendLine(@" * "); tblQuery.AppendLine(@"FROM "); tblQuery.AppendLine(@" `information_schema`.`tables` "); tblQuery.AppendLine(@"WHERE "); tblQuery.AppendLine(@" table_schema=@TableSchema "); tblQuery.AppendLine(@"ORDER BY "); tblQuery.AppendLine(@" table_name ASC"); MySqlParameter[] paramCollection = new MySqlParameter[1]; paramCollection[0] = new MySqlParameter("TableSchema", MySqlDbType.String, 100); paramCollection[0].Value = currentSchema; TableInfo tableInfo = null; try { DataSet tblDs = MySqlHelper.ExecuteDataset(connectionString, tblQuery.ToString(), paramCollection); if (tblDs != null && tblDs.Tables.Count > 0 && tblDs.Tables[0].Rows.Count > 0) { DataTable tblInfoTbl = tblDs.Tables[0]; foreach (DataRow aRow in tblInfoTbl.Rows) { tableInfo = new TableInfo(); tableInfo.Name = aRow["TABLE_NAME"].ToString(); tableInfo.Comment = aRow["TABLE_COMMENT"].ToString(); resultList.Add(tableInfo); } } } catch (Exception ex) { throw new ResponseException(GenCodeToolResource.Error_GetTable, ex); } return resultList; }
/// <summary> /// 判断占位符替换,如果内部没有判断占位符或内部的判断占位符为返回的内容都为空字符串,则整段内容返回空字符串 /// </summary> /// <param name="content">需要替换的内容</param> /// <param name="tableInfo">表信息对象</param> /// <returns>返回替换后的内容</returns> private static string ReplaceIFSectionContent(string content, TableInfo tableInfo) { string contentString = content; Regex regex = GetRegex(ContentTemplatePlaceholder.IFSection); MatchCollection matchCollection = regex.Matches(contentString); string matchString = string.Empty; string tmpString = string.Empty; int count = 0; foreach (Match match in matchCollection) { matchString = match.Value; matchString = RemovePlaceholder(ContentTemplatePlaceholder.IFSection, matchString); count = ReplaceColumnLoopIFSectionContent(matchString, tableInfo, out tmpString); if (count == 0) { contentString = contentString.Replace(match.Value, string.Empty); } else { contentString = contentString.Replace(match.Value, tmpString); } } return contentString; }
/// <summary> /// 循环所有字段,进行判断占位符替换 /// </summary> /// <param name="content">需要替换的内容</param> /// <param name="tableInfo">表信息对象</param> /// <param name="resultString">输出替换后的内容</param> /// <returns>返回有内容的判断占位符个数,如果为0则说明该内容中的所有判断占位符都没能返回内容</returns> private static int ReplaceColumnLoopIFSectionContent(string content, TableInfo tableInfo, out string resultString) { resultString = content; Regex regex = GetRegex(ContentTemplatePlaceholder.ColumnLoopIFSection); MatchCollection matchCollection = regex.Matches(resultString); string matchString = string.Empty; string contentString = string.Empty; string tmpString = string.Empty; string tmpResultString = string.Empty; int count = 0; int hasContentCount = 0; int colIndex = tableInfo.CurrProjectInfo.CodeInfoGetter.ArrayStartIndex; int loopIndex = 1; foreach (Match match in matchCollection) { contentString = string.Empty; matchString = match.Value; matchString = RemovePlaceholder(ContentTemplatePlaceholder.ColumnLoopIFSection, matchString); colIndex = tableInfo.CurrProjectInfo.CodeInfoGetter.ArrayStartIndex; foreach (ColumnInfo columnInfo in tableInfo.ColumnList) { if(CheckIgnoreParamForPlaceholder(match.Value, columnInfo)) { continue; } columnInfo.LoopIndex = colIndex; tmpString = ReplaceColumnContentPlaceholder(matchString, columnInfo); count += ReplaceColumnIFSectionContent(tmpString, columnInfo, out tmpResultString); tmpResultString = ReplaceFunctionSectionContent(tmpResultString); contentString += tmpResultString; colIndex++; } if (count == 0) { resultString = resultString.Replace(match.Value, string.Empty); } else { contentString = ReplaceParamForPlaceholder(match.Value, contentString); resultString = resultString.Replace(match.Value, contentString); hasContentCount++; } //resultString = resultString.Replace(string.Format(ContentTemplatePlaceholder.ColumnLoopCount, loopIndex.ToString()), colIndex.ToString()); //loopIndex++; } return hasContentCount; }
/// <summary> /// 获取指定数据库中所有表的信息对象集合 /// </summary> /// <param name="connectionString">数据库连接字符串</param> /// <returns>返回表信息对象集合</returns> public IList<TableInfo> GetTableInfoList(string connectionString) { IList<TableInfo> resultList = new List<TableInfo>(); OleDbConnection oleDbConn = new OleDbConnection(connectionString); try { oleDbConn.Open(); } catch (Exception ex) { throw new ResponseException(GenCodeToolResource.Error_OpenDB, ex); } StringBuilder tblQuery = new StringBuilder(); tblQuery.AppendLine(@"SELECT "); tblQuery.AppendLine(@" a.TABLE_NAME, "); tblQuery.AppendLine(@" b.COMMENTS "); tblQuery.AppendLine(@"FROM "); tblQuery.AppendLine(@" user_tables a, "); tblQuery.AppendLine(@" user_tab_comments b "); tblQuery.AppendLine(@"WHERE "); tblQuery.AppendLine(@" a.TABLE_NAME = b.TABLE_NAME "); tblQuery.AppendLine(@"ORDER BY "); tblQuery.AppendLine(@" a.TABLE_NAME "); tblQuery.AppendLine(@"; "); OleDbCommand command = new OleDbCommand(tblQuery.ToString(), oleDbConn); OleDbDataReader dataReader = null; TableInfo tableInfo = null; try { dataReader = command.ExecuteReader(); while (dataReader.Read()) { tableInfo = new TableInfo(); tableInfo.Name = dataReader[0].ToString(); tableInfo.Comment = dataReader[1].ToString(); resultList.Add(tableInfo); } } catch (Exception ex) { throw new ResponseException(GenCodeToolResource.Error_GetTable, ex); } finally { dataReader.Close(); oleDbConn.Close(); } return resultList; }
/// <summary> /// 表判断占位符替换 /// </summary> /// <param name="content">需要替换的内容</param> /// <param name="tableInfo">表信息对象</param> /// <param name="resultString">输出替换后的内容</param> /// <returns>返回有内容的判断占位符个数,如果为0则说明该内容中的所有判断占位符都没能返回内容</returns> private static int ReplaceTableIFSectionContent(string content, TableInfo tableInfo, out string resultString) { string contentString = content; int hasContentCount = 0; // Many PK hasContentCount += ReplaceConditionIFSectionContent(ContentTemplatePlaceholder.PKMIFSection, contentString, (tableInfo.PKList.Count > 1), out contentString); // Single PK hasContentCount += ReplaceConditionIFSectionContent(ContentTemplatePlaceholder.PKSIFSection, contentString, (tableInfo.PKList.Count == 1), out contentString); // Many FK hasContentCount += ReplaceConditionIFSectionContent(ContentTemplatePlaceholder.FKMIFSection, contentString, (tableInfo.FKList.Count > 1), out contentString); // Single FK hasContentCount += ReplaceConditionIFSectionContent(ContentTemplatePlaceholder.FKSIFSection, contentString, (tableInfo.FKList.Count == 1), out contentString); // Many RK hasContentCount += ReplaceConditionIFSectionContent(ContentTemplatePlaceholder.RKMIFSection, contentString, (tableInfo.RKList.Count > 1), out contentString); // Single RK hasContentCount += ReplaceConditionIFSectionContent(ContentTemplatePlaceholder.RKSIFSection, contentString, (tableInfo.RKList.Count == 1), out contentString); resultString = contentString; return hasContentCount; }
/// <summary> /// 根据表信息进行递归生成 /// </summary> /// <param name="templateFolderPath">需要生成的模板目录路径</param> /// <param name="targetParentFolderPath">生成的目录所要保存的父目标目录</param> /// <param name="tableInfo">需要生成的表信息对象</param> private static void RecursionGen(string templateFolderPath, string targetParentFolderPath, TableInfo tableInfo) { string[] templateDirectories = Directory.GetDirectories(templateFolderPath); if (templateDirectories.Length > 0) { string targetPath = string.Empty; foreach (string templatePath in templateDirectories) { targetPath = GenTargetPath(templatePath, targetParentFolderPath); targetPath = ReplaceProjectFFPlaceholder(targetPath, tableInfo.CurrProjectInfo); targetPath = ReplaceTableFFPlaceholder(targetPath, tableInfo); if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } RecursionGen(templatePath, targetPath, tableInfo); } } string[] templateFiles = Directory.GetFiles(templateFolderPath); if (templateFiles.Length > 0) { foreach (string filePath in templateFiles) { GenProjectFile(filePath, targetParentFolderPath, tableInfo); } } }
/// <summary> /// 生成项目文件 /// </summary> /// <param name="templateFilePath">需要生成的模板文件路径</param> /// <param name="targetFolderPath">生成文件需要保存的目标目录</param> /// <param name="tableInfo">需要生成的表信息对象</param> private static void GenProjectFile(string templateFilePath, string targetFolderPath, TableInfo tableInfo) { if (!tableInfo.IsGen) { return; } string resultPath = GenTargetPath(templateFilePath, targetFolderPath); resultPath = ReplaceProjectFFPlaceholder(resultPath, tableInfo.CurrProjectInfo); resultPath = ReplaceTableFFPlaceholder(resultPath, tableInfo); File.Copy(templateFilePath, resultPath, true); ReplaceFileContent(resultPath, tableInfo); }
/// <summary> /// 替换表占位符内容 /// </summary> /// <param name="content">需要替换的内容</param> /// <param name="tableInfo">表信息对象</param> /// <returns>返回替换后的内容</returns> private static string ReplaceTableContentPlaceholder(string content, TableInfo tableInfo) { string result = content.Replace(ContentTemplatePlaceholder.TableName, tableInfo.Name) .Replace(ContentTemplatePlaceholder.ClassName, tableInfo.ClassName) .Replace(ContentTemplatePlaceholder.ClassNameLow, tableInfo.ClassNameLow) .Replace(ContentTemplatePlaceholder.ClassNameUp, tableInfo.ClassNameUp) .Replace(ContentTemplatePlaceholder.ClassNameLowFirst, tableInfo.ClassNameLowFirst) .Replace(ContentTemplatePlaceholder.TableComment, tableInfo.Comment) .Replace(ContentTemplatePlaceholder.TableLoopIndex, tableInfo.LoopIndex.ToString()) .Replace(ContentTemplatePlaceholder.ColumnCount, tableInfo.ColumnList.Count.ToString()); return result; }
/// <summary> /// 填充表信息对象中的字段信息 /// </summary> /// <param name="dbInfoGetter">数据库连接字符串</param> /// <param name="dbInfoGetter">指定的数据库信息获取器</param> /// <param name="tableInfo">需要填充的表信息对象</param> public static void FillColumnInfoList(string connectionString, IDBInfoGetter dbInfoGetter, ICodeInfoGetter codeInfoGetter, TableInfo tableInfo) { if (tableInfo.ColumnList != null && tableInfo.ColumnList.Count > 0) { return; } dbInfoGetter.FillColumnInfo(connectionString, tableInfo); foreach (ColumnInfo columnInfo in tableInfo.ColumnList) { columnInfo.DalType = dbInfoGetter.ToDalType(columnInfo.SqlType, columnInfo.Precision, columnInfo.Scale); columnInfo.CodeType = codeInfoGetter.ToCodeType(columnInfo.DbType); } }
/// <summary> /// 替换表在物理目录和文件名称上的占位符内容 /// </summary> /// <param name="ffPath">目录或文件目录路径</param> /// <param name="tableInfo">表信息对象</param> /// <returns>返回替换后的内容</returns> private static string ReplaceTableFFPlaceholder(string ffPath, TableInfo tableInfo) { string targetFolderPath = ffPath.Replace(FolderFileTemplatePlaceholder.TableName, tableInfo.Name) .Replace(FolderFileTemplatePlaceholder.ClassName, tableInfo.ClassName) .Replace(FolderFileTemplatePlaceholder.ClassNameLow, tableInfo.ClassNameLow) .Replace(FolderFileTemplatePlaceholder.ClassNameUp, tableInfo.ClassNameUp) .Replace(FolderFileTemplatePlaceholder.ClassNameLowFirst, tableInfo.ClassNameLowFirst) .Replace(FolderFileTemplatePlaceholder.TableLoop, string.Empty); return targetFolderPath; }
/// <summary> /// 检测当前表信息是否为排除项 /// </summary> /// <param name="matchString">正则匹配出来的字符串</param> /// <param name="tableInfo">表信息对象</param> /// <returns>返回替换后的内容</returns> private static bool CheckIgnoreParamForPlaceholder(string matchString, TableInfo tableInfo) { if (string.IsNullOrWhiteSpace(matchString) || tableInfo == null) { return false; } bool isIgnore = false; Regex regex = null; Match match = null; string[] paramArr = null; string tmpParam = string.Empty; string tagParamStr = string.Empty; string tagHead = matchString.Substring(0, matchString.IndexOf("}}") + 1); tagHead = tagHead.Replace("}}", "|}}").Replace("|", "||"); regex = new Regex(@"\|ignparam\:(.)*?\|"); match = regex.Match(tagHead); if (match.Success) { if (match.Value.IndexOf(":") > -1) { tagParamStr = match.Value.Split(':')[1]; if (string.IsNullOrWhiteSpace(tagParamStr)) { return false; } } paramArr = tagParamStr.Split(','); foreach (string param in paramArr) { tmpParam = param.ToLower().Trim(); if ("pk".Equals(tmpParam)) { isIgnore = true; continue; } if ("fk".Equals(tmpParam)) { isIgnore = true; continue; } if ("rk".Equals(tmpParam)) { isIgnore = true; continue; } if (tableInfo.ClassNameLow.Equals(tmpParam)) { isIgnore = true; continue; } } } return isIgnore; }
/// <summary> /// 当特定字段存在时才生成内容 /// </summary> /// <param name="content">需要替换的内容</param> /// <param name="tableInfo">表信息对象</param> /// <returns>返回替换后的内容</returns> private static string ReplaceColumnExistIFSectionContent(string content, TableInfo tableInfo) { string contentString = content; Regex regex = GetRegex(ContentTemplatePlaceholder.ColumnExistIFSection); MatchCollection matchCollection = regex.Matches(contentString); string matchString = string.Empty; string tagHead = string.Empty; string[] paramArr = null; bool isExist = false; foreach (Match match in matchCollection) { matchString = match.Value; tagHead = matchString.Substring(0, matchString.IndexOf("}}") + 2); tagHead = tagHead.Replace("}}", "").Replace("{{", ""); paramArr = tagHead.Split('|'); if(paramArr.Length <= 1) { isExist = false; } else { isExist = (tableInfo.ColumnList.Count(c => c.PropertyNameLow.Equals(paramArr[1].ToLower())) > 0); } matchString = RemovePlaceholder(ContentTemplatePlaceholder.ColumnExistIFSection, matchString); if (!isExist) { contentString = contentString.Replace(match.Value, string.Empty); } else { contentString = contentString.Replace(match.Value, matchString); } } return contentString; }
/// <summary> /// 获取指定数据库中所有表的信息对象集合 /// </summary> /// <param name="connectionString">数据库连接字符串</param> /// <returns>返回表信息对象集合</returns> public IList<TableInfo> GetTableInfoList(string connectionString) { IList<TableInfo> resultList = new List<TableInfo>(); SqlConnection sqlConn = new SqlConnection(connectionString); try { sqlConn.Open(); } catch (Exception ex) { throw new ResponseException(GenCodeToolResource.Error_OpenDB, ex); } StringBuilder tblQuery = new StringBuilder(); tblQuery.AppendLine(@"SELECT "); tblQuery.AppendLine(@" A.name, B.value AS comment "); tblQuery.AppendLine(@"FROM "); tblQuery.AppendLine(@" sysobjects A "); tblQuery.AppendLine(@"LEFT JOIN "); tblQuery.AppendLine(@" sys.extended_properties B ON(A.id = B.major_id AND B.minor_id = 0 AND B.name = 'MS_Description')"); tblQuery.AppendLine(@"WHERE "); tblQuery.AppendLine(@" type='U' "); tblQuery.AppendLine(@"ORDER BY "); tblQuery.AppendLine(@" name ASC"); SqlCommand command = new SqlCommand(tblQuery.ToString(), sqlConn); SqlDataReader dataReader = null; TableInfo tableInfo = null; try { dataReader = command.ExecuteReader(); while (dataReader.Read()) { tableInfo = new TableInfo(); tableInfo.Name = dataReader[0].ToString(); tableInfo.Comment = dataReader[1].ToString(); resultList.Add(tableInfo); } } catch (Exception ex) { throw new ResponseException(GenCodeToolResource.Error_GetTable, ex); } finally { dataReader.Close(); sqlConn.Close(); } return resultList; }
/// <summary> /// 替换文件中的占位符 /// </summary> /// <param name="filePath">生成后的文件路径</param> /// <param name="tableInfo">需要生成的表信息对象</param> private static void ReplaceFileContent(string filePath, TableInfo tableInfo) { string fileContent = File.ReadAllText(filePath); fileContent = ReplaceProjectContentPlaceholder(fileContent, tableInfo.CurrProjectInfo); fileContent = ReplaceTableContentPlaceholder(fileContent, tableInfo); fileContent = ReplaceColumnExistIFSectionContent(fileContent, tableInfo); fileContent = ReplaceIFSectionContent(fileContent, tableInfo); fileContent = ReplaceColumnLoopContent(fileContent, tableInfo); fileContent = ReplacePKLoopContent(fileContent, tableInfo); ReplaceTableIFSectionContent(fileContent, tableInfo, out fileContent); if (tableInfo.PKList.Count > 0) { fileContent = ReplacePKContentPlaceholder(fileContent, tableInfo.PKList[0]); ReplacePKColumnIFSectionContent(fileContent, tableInfo.PKList[0], out fileContent); } fileContent = ReplaceFKLoopContent(fileContent, tableInfo); fileContent = ReplaceRKLoopContent(fileContent, tableInfo); fileContent = ReplaceFunctionSectionContent(fileContent); File.WriteAllText(filePath, fileContent, Encoding.UTF8); tableInfo.CurrProjectInfo.CurrGenFileList.Add(filePath); }
/// <summary> /// 往表中填充字段信息 /// </summary> /// <param name="connectionString">数据库连接字符串</param> /// <param name="tableInfo">需要填充的表信息对象</param> public void FillColumnInfo(string connectionString, TableInfo tableInfo) { SqlConnection sqlConn = new SqlConnection(connectionString); try { sqlConn.Open(); } catch (Exception ex) { throw new ResponseException(GenCodeToolResource.Error_OpenDB, ex); } StringBuilder colQuery = new StringBuilder(); colQuery.AppendLine(@"SELECT "); colQuery.AppendLine(@" A.colorder, "); colQuery.AppendLine(@" A.name AS columnname, "); colQuery.AppendLine(@" B.name AS typename, "); colQuery.AppendLine(@" A.length, "); colQuery.AppendLine(@" A.isnullable,"); colQuery.AppendLine(@" A.colid, "); colQuery.AppendLine(@" D.value AS comment,"); colQuery.AppendLine(@" A.prec,"); colQuery.AppendLine(@" A.scale,"); colQuery.AppendLine(@" G.text AS initialvalue,"); colQuery.AppendLine(@" isunique = CASE WHEN EXISTS(SELECT "); colQuery.AppendLine(@" 1 "); colQuery.AppendLine(@" FROM "); colQuery.AppendLine(@" sys.indexes U1 "); colQuery.AppendLine(@" LEFT JOIN "); colQuery.AppendLine(@" sys.index_columns U2 ON(U1.object_id = U2.object_id AND U1.index_id = u2.index_id) "); colQuery.AppendLine(@" WHERE "); colQuery.AppendLine(@" U1.object_id = A.id "); colQuery.AppendLine(@" AND "); colQuery.AppendLine(@" U2.column_id = A.colid "); colQuery.AppendLine(@" AND "); colQuery.AppendLine(@" U1.is_unique = 1) "); colQuery.AppendLine(@" THEN "); colQuery.AppendLine(@" 1 "); colQuery.AppendLine(@" ELSE "); colQuery.AppendLine(@" 0 "); colQuery.AppendLine(@" END,"); colQuery.AppendLine(@" ispkey = CASE WHEN EXISTS(SELECT "); colQuery.AppendLine(@" 1 "); colQuery.AppendLine(@" FROM "); colQuery.AppendLine(@" sysobjects "); colQuery.AppendLine(@" WHERE "); colQuery.AppendLine(@" xtype='PK' "); colQuery.AppendLine(@" AND "); colQuery.AppendLine(@" parent_obj=A.id "); colQuery.AppendLine(@" AND "); colQuery.AppendLine(@" name IN (SELECT "); colQuery.AppendLine(@" name "); colQuery.AppendLine(@" FROM "); colQuery.AppendLine(@" sysindexes "); colQuery.AppendLine(@" WHERE "); colQuery.AppendLine(@" indid IN(SELECT "); colQuery.AppendLine(@" indid "); colQuery.AppendLine(@" FROM "); colQuery.AppendLine(@" sysindexkeys "); colQuery.AppendLine(@" WHERE "); colQuery.AppendLine(@" id=A.id "); colQuery.AppendLine(@" AND "); colQuery.AppendLine(@" colid=A.colid))) "); colQuery.AppendLine(@" THEN "); colQuery.AppendLine(@" 1 "); colQuery.AppendLine(@" ELSE "); colQuery.AppendLine(@" 0 "); colQuery.AppendLine(@" END,"); colQuery.AppendLine(@" isfkey = CASE WHEN E.fkey IS NOT NULL THEN 1 ELSE 0 END,"); colQuery.AppendLine(@" I.name AS fkname,"); colQuery.AppendLine(@" H.name AS fkcolumn,"); colQuery.AppendLine(@" F.name AS fktable"); colQuery.AppendLine(@"FROM "); colQuery.AppendLine(@" syscolumns A "); colQuery.AppendLine(@"LEFT JOIN "); colQuery.AppendLine(@" systypes B ON(A.xtype = B.xtype) "); colQuery.AppendLine(@"LEFT JOIN "); colQuery.AppendLine(@" sysobjects C ON(C.id = A.id AND C.xtype = 'U' AND C.name <> 'dtproperties') "); colQuery.AppendLine(@"LEFT JOIN "); colQuery.AppendLine(@" sys.extended_properties D ON(D.minor_id = A.colid AND D.major_id = A.id) "); colQuery.AppendLine(@"LEFT JOIN "); colQuery.AppendLine(@" sysforeignkeys E ON(E.fkeyid = A.id and E.fkey = A.colid) "); colQuery.AppendLine(@"LEFT JOIN "); colQuery.AppendLine(@" sysobjects F ON(E.rkeyid = F.id) "); colQuery.AppendLine(@"LEFT JOIN "); colQuery.AppendLine(@" syscomments G ON(A.cdefault = G.id) "); colQuery.AppendLine(@"LEFT JOIN "); colQuery.AppendLine(@" syscolumns H ON(E.rkeyid = H.id and E.rkey = H.colid) "); colQuery.AppendLine(@"LEFT JOIN "); colQuery.AppendLine(@" sysobjects I ON(I.id = E.constid) "); colQuery.AppendLine(@"WHERE "); colQuery.AppendLine(@" A.id = (SELECT "); colQuery.AppendLine(@" id "); colQuery.AppendLine(@" FROM "); colQuery.AppendLine(@" sysobjects "); colQuery.AppendLine(@" WHERE "); colQuery.AppendLine(@" id = OBJECT_ID(@TableName)) "); colQuery.AppendLine(@" AND "); colQuery.AppendLine(@" B.name <> 'sysname' "); colQuery.AppendLine(@"ORDER BY "); colQuery.AppendLine(@" A.colorder ASC"); SqlCommand command = new SqlCommand(colQuery.ToString(), sqlConn); SqlDataReader dataReader = null; ColumnInfo colInfo = null; int tmpInt = 0; try { command.Parameters.Add(new SqlParameter("@TableName", tableInfo.Name)); dataReader = command.ExecuteReader(); while (dataReader.Read()) { colInfo = new ColumnInfo(); colInfo.Name = dataReader[1].ToString(); colInfo.SqlType = dataReader[2].ToString().ToLower(); colInfo.MaxLength = !int.TryParse(dataReader[3].ToString(), out tmpInt) ? 0 : tmpInt; colInfo.IsNullable = "1".Equals(dataReader[4].ToString()) ? true : false; colInfo.ColId = int.Parse(dataReader[5].ToString()); colInfo.Comment = dataReader[6].ToString(); colInfo.Precision = !int.TryParse(dataReader[7].ToString(), out tmpInt) ? 0 : tmpInt; colInfo.Scale = !int.TryParse(dataReader[8].ToString(), out tmpInt) ? 0 : tmpInt; colInfo.DefaultValue = dataReader[9].ToString(); colInfo.IsUnique = "1".Equals(dataReader[10].ToString()) ? true : false; colInfo.IsPK = "1".Equals(dataReader[11].ToString()) ? true : false; colInfo.IsFK = "1".Equals(dataReader[12].ToString()) ? true : false; if (colInfo.IsFK) { colInfo.FKName = dataReader[13].ToString(); colInfo.FKColumnName = dataReader[14].ToString(); colInfo.FKTableName = dataReader[15].ToString(); } colInfo.DbType = ToDbType(colInfo.SqlType, colInfo.Precision, colInfo.Scale); colInfo.DalType = ToDalType(colInfo.SqlType, colInfo.Precision, colInfo.Scale); tableInfo.AddColumn(colInfo); } } catch (Exception ex) { throw new ResponseException(GenCodeToolResource.Error_GetColumn, ex); } finally { dataReader.Close(); sqlConn.Close(); } }
/// <summary> /// 循环所有字段替换内容 /// </summary> /// <param name="content">需要替换的内容</param> /// <param name="tableInfo">表信息对象</param> /// <returns>返回替换后的内容</returns> private static string ReplaceColumnLoopContent(string content, TableInfo tableInfo) { string sectionContent = content; Regex regex = GetRegex(ContentTemplatePlaceholder.ColumnLoopSection); MatchCollection matchCollection = regex.Matches(sectionContent); string matchString = string.Empty; string contentString = string.Empty; string tmpString = string.Empty; int colIndex = tableInfo.CurrProjectInfo.CodeInfoGetter.ArrayStartIndex; int loopIndex = 1; foreach (Match match in matchCollection) { contentString = string.Empty; matchString = match.Value; matchString = RemovePlaceholder(ContentTemplatePlaceholder.ColumnLoopSection, matchString); colIndex = tableInfo.CurrProjectInfo.CodeInfoGetter.ArrayStartIndex; foreach (ColumnInfo columnInfo in tableInfo.ColumnList) { if (CheckIgnoreParamForPlaceholder(match.Value, columnInfo)) { continue; } columnInfo.LoopIndex = colIndex; tmpString = ReplaceColumnContentPlaceholder(matchString, columnInfo); ReplaceColumnIFSectionContent(tmpString, columnInfo, out tmpString); contentString += tmpString; colIndex++; } contentString = ReplaceParamForPlaceholder(match.Value, contentString); sectionContent = sectionContent.Replace(match.Value, contentString); sectionContent = sectionContent.Replace(ContentTemplatePlaceholder.ColumnLoopCount.Replace("{0}", loopIndex.ToString()), colIndex.ToString()); loopIndex++; } return sectionContent; }
/// <summary> /// 往表中填充字段信息 /// </summary> /// <param name="connectionString">数据库连接字符串</param> /// <param name="tableInfo">需要填充的表信息对象</param> public void FillColumnInfo(string connectionString, TableInfo tableInfo) { OleDbConnection oleDbConn = new OleDbConnection(connectionString); try { oleDbConn.Open(); } catch (Exception ex) { throw new ResponseException(GenCodeToolResource.Error_OpenDB, ex); } StringBuilder colQuery = new StringBuilder(); colQuery.AppendLine(@"SELECT "); colQuery.AppendLine(@" t.table_name, "); colQuery.AppendLine(@" t.column_name, "); colQuery.AppendLine(@" t.data_type, "); colQuery.AppendLine(@" t.data_length, "); colQuery.AppendLine(@" t.nullable, "); colQuery.AppendLine(@" t.column_id, "); colQuery.AppendLine(@" c.comments, "); colQuery.AppendLine(@" t.data_precision, "); colQuery.AppendLine(@" t.data_scale, "); colQuery.AppendLine(@" t.data_default, "); colQuery.AppendLine(@" (SELECT CASE WHEN n.column_name is not null AND n.constraint_type = 'U' THEN 'true' ELSE 'false' END FROM DUAL) isunique, "); colQuery.AppendLine(@" (SELECT CASE WHEN n.column_name is not null AND n.constraint_type = 'P' THEN 'true' ELSE 'false' END FROM DUAL) ispk, "); colQuery.AppendLine(@" (SELECT CASE WHEN n.column_name is not null AND n.constraint_type = 'R' THEN 'true' ELSE 'false' END FROM DUAL) isfk, "); colQuery.AppendLine(@" n.fk_name, "); colQuery.AppendLine(@" n.fk_col_name, "); colQuery.AppendLine(@" n.fk_table_name "); colQuery.AppendLine(@"FROM "); colQuery.AppendLine(@" user_tab_cols t "); colQuery.AppendLine(@"LEFT JOIN "); colQuery.AppendLine(@" user_col_comments c ON (c.table_name=t.table_name AND c.column_name=t.column_name) "); colQuery.AppendLine(@"LEFT JOIN "); colQuery.AppendLine(@" (SELECT "); colQuery.AppendLine(@" col1.column_name, "); colQuery.AppendLine(@" col1.table_name, "); colQuery.AppendLine(@" con1.constraint_type, "); colQuery.AppendLine(@" con1.constraint_name, "); colQuery.AppendLine(@" con1.r_constraint_name AS fk_name, "); colQuery.AppendLine(@" col2.column_name AS fk_col_name, "); colQuery.AppendLine(@" col2.table_name AS fk_table_name "); colQuery.AppendLine(@" FROM "); colQuery.AppendLine(@" user_cons_columns col1 "); colQuery.AppendLine(@" LEFT JOIN "); colQuery.AppendLine(@" user_constraints con1 ON(col1.table_name = con1.table_name AND col1.constraint_name = con1.constraint_name) "); colQuery.AppendLine(@" LEFT JOIN "); colQuery.AppendLine(@" user_cons_columns col2 ON(col2.constraint_name = con1.r_constraint_name) "); colQuery.AppendLine(@" LEFT JOIN "); colQuery.AppendLine(@" user_constraints con2 ON(col2.table_name = con2.table_name AND con2.constraint_name = con1.r_constraint_name) "); colQuery.AppendLine(@" WHERE "); colQuery.AppendLine(@" "); colQuery.AppendLine(@" AND "); colQuery.AppendLine(@" con1.constraint_type != 'C') n ON (n.column_name = t.column_name and LOWER(col1.table_name)=LOWER(t.table_name)) "); colQuery.AppendLine(@"WHERE "); colQuery.AppendLine(@" LOWER(t.table_name)='" + tableInfo.Name.ToLower() + @"' "); colQuery.AppendLine(@" AND "); colQuery.AppendLine(@" t.hidden_column='NO' "); colQuery.AppendLine(@"ORDER BY "); colQuery.AppendLine(@" t.column_id "); OleDbCommand command = new OleDbCommand(colQuery.ToString(), oleDbConn); OleDbDataReader dataReader = null; ColumnInfo colInfo = null; int tmpInt = 0; try { dataReader = command.ExecuteReader(); while (dataReader.Read()) { colInfo = new ColumnInfo(); colInfo.Name = dataReader[1].ToString(); colInfo.SqlType = dataReader[2].ToString().ToLower(); colInfo.MaxLength = !int.TryParse(dataReader[3].ToString(), out tmpInt) ? 0 : tmpInt; colInfo.IsNullable = "Y".Equals(dataReader[4].ToString()) ? true : false; colInfo.ColId = int.Parse(dataReader[5].ToString()); colInfo.Comment = dataReader[6].ToString(); colInfo.Precision = !int.TryParse(dataReader[7].ToString(), out tmpInt) ? 0 : tmpInt; colInfo.Scale = !int.TryParse(dataReader[8].ToString(), out tmpInt) ? 0 : tmpInt; colInfo.DefaultValue = dataReader[9].ToString(); colInfo.IsUnique = bool.Parse(dataReader[10].ToString()); colInfo.IsPK = bool.Parse(dataReader[11].ToString()); colInfo.IsFK = bool.Parse(dataReader[12].ToString()); if (colInfo.IsFK) { colInfo.FKName = dataReader[13].ToString(); colInfo.FKColumnName = dataReader[14].ToString(); colInfo.FKTableName = dataReader[15].ToString(); } colInfo.DbType = ToDbType(colInfo.SqlType, colInfo.Precision, colInfo.Scale); colInfo.DalType = ToDalType(colInfo.SqlType, colInfo.Precision, colInfo.Scale); tableInfo.AddColumn(colInfo); } } catch (Exception ex) { throw new ResponseException(GenCodeToolResource.Error_GetColumn, ex); } finally { dataReader.Close(); oleDbConn.Close(); } }
public void TestMethod1() { ProjectInfo projectInfo = new ProjectInfo(); projectInfo.TemplatePath = @"E:\Projects\Framework\SourceCode\Master\GenCodeTool\CodeTemplate\CSWeb"; projectInfo.GenTargetPath = @"E:\TestGenCode"; projectInfo.Name = "TestProject"; projectInfo.Namespace = "NFramework.TestProject"; projectInfo.DisplayName = "测试生成"; projectInfo.CodeInfoGetter = new CSCodeInfoGetter(); projectInfo.DBInfoGetter = new MssqlDBInfoGetter(); projectInfo.IsClearTargetFolder = true; TableInfo tableInfo = null; ColumnInfo colInfo = null; #region Company tableInfo = new TableInfo(); tableInfo.Name = "Company"; tableInfo.Comment = "公司"; TableInfo comTableInfo = tableInfo; colInfo = new ColumnInfo(); colInfo.Name = "Name"; colInfo.DbType = DbType.String; colInfo.SqlType = "VarChar"; colInfo.DalType = projectInfo.DBInfoGetter.ToDalType(colInfo.SqlType, colInfo.Precision, colInfo.Scale); colInfo.CodeType = projectInfo.CodeInfoGetter.ToCodeType(colInfo.DbType); colInfo.MaxLength = 100; colInfo.IsNullable = false; colInfo.Comment = "名称"; colInfo.CurrTable = tableInfo; colInfo.IsGenSearchCondition = true; colInfo.IsGenInput = true; colInfo.IsGenSearchResult = true; colInfo.IsUnique = false; tableInfo.AddColumn(colInfo); colInfo = new ColumnInfo(); colInfo.Name = "Code"; colInfo.DbType = DbType.String; colInfo.SqlType = "VarChar"; colInfo.DalType = projectInfo.DBInfoGetter.ToDalType(colInfo.SqlType, colInfo.Precision, colInfo.Scale); colInfo.CodeType = projectInfo.CodeInfoGetter.ToCodeType(colInfo.DbType); colInfo.MaxLength = 10; colInfo.IsNullable = false; colInfo.Comment = "编号"; colInfo.CurrTable = tableInfo; colInfo.IsGenSearchCondition = true; colInfo.IsGenInput = true; colInfo.IsGenSearchResult = true; colInfo.IsUnique = true; tableInfo.AddColumn(colInfo); colInfo = new ColumnInfo(); colInfo.Name = "CompanyId"; colInfo.DbType = DbType.String; colInfo.SqlType = "VarChar"; colInfo.DalType = projectInfo.DBInfoGetter.ToDalType(colInfo.SqlType, colInfo.Precision, colInfo.Scale); colInfo.CodeType = projectInfo.CodeInfoGetter.ToCodeType(colInfo.DbType); colInfo.MaxLength = 40; colInfo.IsNullable = false; colInfo.IsPK = true; colInfo.Comment = "ID"; colInfo.CurrTable = tableInfo; colInfo.IsGenSearchCondition = false; colInfo.IsGenInput = false; colInfo.IsGenSearchResult = false; colInfo.IsUnique = true; tableInfo.AddColumn(colInfo); colInfo = new ColumnInfo(); colInfo.Name = "CreateTime"; colInfo.DbType = DbType.DateTime; colInfo.SqlType = "DateTime"; colInfo.DalType = projectInfo.DBInfoGetter.ToDalType(colInfo.SqlType, colInfo.Precision, colInfo.Scale); colInfo.CodeType = projectInfo.CodeInfoGetter.ToCodeType(colInfo.DbType); colInfo.MaxLength = 12; colInfo.IsNullable = false; colInfo.IsPK = false; colInfo.Comment = "创建时间"; colInfo.CurrTable = tableInfo; colInfo.IsGenSearchCondition = false; colInfo.IsGenInput = false; colInfo.IsGenSearchResult = false; colInfo.IsUnique = false; tableInfo.AddColumn(colInfo); colInfo = new ColumnInfo(); colInfo.Name = "Status"; colInfo.DbType = DbType.Int32; colInfo.SqlType = "Int"; colInfo.DalType = projectInfo.DBInfoGetter.ToDalType(colInfo.SqlType, colInfo.Precision, colInfo.Scale); colInfo.CodeType = projectInfo.CodeInfoGetter.ToCodeType(colInfo.DbType); colInfo.MaxLength = 8; colInfo.DefaultValue = "((1))"; colInfo.IsNullable = false; colInfo.IsPK = false; colInfo.Comment = "Status"; colInfo.CurrTable = tableInfo; colInfo.IsGenSearchCondition = true; colInfo.IsGenInput = false; colInfo.IsGenSearchResult = true; colInfo.IsUnique = false; tableInfo.AddColumn(colInfo); colInfo = new ColumnInfo(); colInfo.Name = "Col2"; colInfo.DbType = DbType.String; colInfo.SqlType = "VarChar"; colInfo.DalType = projectInfo.DBInfoGetter.ToDalType(colInfo.SqlType, colInfo.Precision, colInfo.Scale); colInfo.CodeType = projectInfo.CodeInfoGetter.ToCodeType(colInfo.DbType); colInfo.MaxLength = 50; colInfo.DefaultValue = "22"; colInfo.IsNullable = true; colInfo.IsPK = false; colInfo.Comment = "Col2的字段"; colInfo.CurrTable = tableInfo; colInfo.IsGenSearchCondition = true; colInfo.IsGenInput = false; colInfo.IsGenSearchResult = true; colInfo.IsUnique = false; tableInfo.AddColumn(colInfo); projectInfo.AddTableInfo(tableInfo); #endregion #region Department tableInfo = new TableInfo(); tableInfo.Name = "Department"; tableInfo.Comment = "部门"; colInfo = new ColumnInfo(); colInfo.Name = "Name"; colInfo.DbType = DbType.String; colInfo.SqlType = "VarChar"; colInfo.DalType = projectInfo.DBInfoGetter.ToDalType(colInfo.SqlType, colInfo.Precision, colInfo.Scale); colInfo.CodeType = projectInfo.CodeInfoGetter.ToCodeType(colInfo.DbType); colInfo.MaxLength = 100; colInfo.IsNullable = false; colInfo.Comment = "名称"; colInfo.CurrTable = tableInfo; colInfo.IsGenSearchCondition = true; colInfo.IsGenInput = true; colInfo.IsGenSearchResult = true; colInfo.IsUnique = false; tableInfo.AddColumn(colInfo); colInfo = new ColumnInfo(); colInfo.Name = "Code"; colInfo.DbType = DbType.String; colInfo.SqlType = "VarChar"; colInfo.DalType = projectInfo.DBInfoGetter.ToDalType(colInfo.SqlType, colInfo.Precision, colInfo.Scale); colInfo.CodeType = projectInfo.CodeInfoGetter.ToCodeType(colInfo.DbType); colInfo.MaxLength = 10; colInfo.IsNullable = false; colInfo.Comment = "编号"; colInfo.CurrTable = tableInfo; colInfo.IsGenSearchCondition = true; colInfo.IsGenInput = true; colInfo.IsGenSearchResult = true; colInfo.IsUnique = true; tableInfo.AddColumn(colInfo); colInfo = new ColumnInfo(); colInfo.Name = "DepartmentId"; colInfo.DbType = DbType.String; colInfo.SqlType = "VarChar"; colInfo.DalType = projectInfo.DBInfoGetter.ToDalType(colInfo.SqlType, colInfo.Precision, colInfo.Scale); colInfo.CodeType = projectInfo.CodeInfoGetter.ToCodeType(colInfo.DbType); colInfo.MaxLength = 40; colInfo.IsNullable = false; colInfo.IsPK = true; colInfo.Comment = "ID"; colInfo.CurrTable = tableInfo; colInfo.IsGenSearchCondition = false; colInfo.IsGenInput = false; colInfo.IsGenSearchResult = false; colInfo.IsUnique = true; tableInfo.AddColumn(colInfo); colInfo = new ColumnInfo(); colInfo.Name = "CompanyId"; colInfo.DbType = DbType.String; colInfo.SqlType = "VarChar"; colInfo.DalType = projectInfo.DBInfoGetter.ToDalType(colInfo.SqlType, colInfo.Precision, colInfo.Scale); colInfo.CodeType = projectInfo.CodeInfoGetter.ToCodeType(colInfo.DbType); colInfo.MaxLength = 40; colInfo.IsNullable = false; colInfo.IsPK = false; colInfo.IsFK = true; colInfo.FKName = "FK_Company_Department"; colInfo.Comment = "所属公司ID"; colInfo.CurrTable = tableInfo; colInfo.IsGenSearchCondition = false; colInfo.IsGenInput = false; colInfo.IsGenSearchResult = false; colInfo.IsUnique = false; colInfo.FKColumn = comTableInfo.PKList[0]; tableInfo.AddColumn(colInfo); projectInfo.AddTableInfo(tableInfo); #endregion #region Position tableInfo = new TableInfo(); tableInfo.Name = "Position"; projectInfo.AddTableInfo(tableInfo); #endregion #region Employee tableInfo = new TableInfo(); tableInfo.Name = "Employee"; projectInfo.AddTableInfo(tableInfo); #endregion GenCodeHandler.GenCode(projectInfo); }