/// <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="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(); } }
/// <summary> /// 将字段信息对象添加到表信息对象中,并根据字段是否为主键或外键,分配到主键和外键集合中 /// </summary> /// <param name="columnInfo">字段信息对象</param> public void AddColumn(ColumnInfo columnInfo) { columnInfo.CurrTable = this; ColumnList.Add(columnInfo); if (columnInfo.IsPK) { this.PKList.Add(columnInfo); } if (columnInfo.IsFK) { this.FKList.Add(columnInfo); } }
/// <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="content">需要替换的内容</param> /// <param name="rkColumnInfo">关联键信息对象</param> /// <returns>返回替换后的内容</returns> private static string ReplaceRKContentPlaceholder(string content, ColumnInfo rkColumnInfo) { string result = content.Replace("{{rk:", "{{"); result = ReplaceTableContentPlaceholder(result, rkColumnInfo.CurrTable); result = ReplaceColumnContentPlaceholder(result, rkColumnInfo); return result; }
/// <summary> /// 主键判断占位符替换 /// </summary> /// <param name="content">需要替换的内容</param> /// <param name="columnInfo">字段信息对象</param> /// <param name="resultString">输出替换后的内容</param> /// <returns>返回有内容的判断占位符个数,如果为0则说明该内容中的所有判断占位符都没能返回内容</returns> private static int ReplacePKColumnIFSectionContent(string content, ColumnInfo columnInfo, out string resultString) { content = content.Replace(":pk:", ":"); return ReplaceColumnIFSectionContent(content, columnInfo, out resultString); }
/// <summary> /// 替换主键占位符内容 /// </summary> /// <param name="content">需要替换的内容</param> /// <param name="pkColumnInfo">主键信息对象</param> /// <returns>返回替换后的内容</returns> private static string ReplacePKContentPlaceholder(string content, ColumnInfo pkColumnInfo) { string result = content.Replace("{{pk:", "{{"); result = ReplaceColumnContentPlaceholder(result, pkColumnInfo); return result; }
/// <summary> /// 替换字段占位符内容 /// </summary> /// <param name="content">需要替换的内容</param> /// <param name="columnInfo">字段信息对象</param> /// <returns>返回替换后的内容</returns> private static string ReplaceColumnContentPlaceholder(string content, ColumnInfo columnInfo) { string result = content.Replace(ContentTemplatePlaceholder.ColumnName, columnInfo.Name) .Replace(ContentTemplatePlaceholder.PropertyName, columnInfo.PropertyName) .Replace(ContentTemplatePlaceholder.PropertyNameLow, columnInfo.PropertyNameLow) .Replace(ContentTemplatePlaceholder.PropertyNameUp, columnInfo.PropertyNameUp) .Replace(ContentTemplatePlaceholder.PropertyNameLowFirst, columnInfo.PropertyNameLowFirst) .Replace(ContentTemplatePlaceholder.ColumnComment, columnInfo.Comment) .Replace(ContentTemplatePlaceholder.ColumnPrecision, columnInfo.Precision.ToString()) .Replace(ContentTemplatePlaceholder.ColumnScale, columnInfo.Scale.ToString()) .Replace(ContentTemplatePlaceholder.ColumnCodeType, columnInfo.CodeType) .Replace(ContentTemplatePlaceholder.ColumnSqlType, columnInfo.SqlType) .Replace(ContentTemplatePlaceholder.ColumnDalType, columnInfo.DalType) .Replace(ContentTemplatePlaceholder.ColumnMaxLength, columnInfo.MaxLength.ToString()) .Replace(ContentTemplatePlaceholder.ColumnMax, columnInfo.MaxValue) .Replace(ContentTemplatePlaceholder.ColumnMin, columnInfo.MinValue) .Replace(ContentTemplatePlaceholder.ColumnFKName, columnInfo.FKName) .Replace(ContentTemplatePlaceholder.ColumnId, columnInfo.ColId.ToString()) .Replace(ContentTemplatePlaceholder.ColumnLoopIndex, columnInfo.LoopIndex.ToString()) .Replace(ContentTemplatePlaceholder.ColumnDefaultValue, columnInfo.CurrTable.CurrProjectInfo.CodeInfoGetter.GetDefaultValueString(columnInfo.DefaultValue, columnInfo.DbType)); Regex regex = null; MatchCollection matchCollection = null; string matchString = string.Empty; #region Convert To Code Type regex = GetRegex(ContentTemplatePlaceholder.ColumnConvert); matchCollection = regex.Matches(result); foreach (Match match in matchCollection) { matchString = RemovePlaceholder(ContentTemplatePlaceholder.ColumnConvert, match.Value); result = result.Replace(match.Value, columnInfo.CurrTable.CurrProjectInfo.CodeInfoGetter.GetConvertString(matchString, columnInfo.CodeType)); } #endregion #region To String regex = GetRegex(ContentTemplatePlaceholder.ColumnToString); matchCollection = regex.Matches(result); foreach (Match match in matchCollection) { matchString = RemovePlaceholder(ContentTemplatePlaceholder.ColumnToString, match.Value); result = result.Replace(match.Value, columnInfo.CurrTable.CurrProjectInfo.CodeInfoGetter.GetToString(matchString, columnInfo.CodeType)); } #endregion return result; }
/// <summary> /// 检测当前字段信息是否为排除项 /// </summary> /// <param name="matchString">正则匹配出来的字符串</param> /// <param name="columnInfo">字段信息对象</param> /// <returns>如果是排除内容则返回true,如果不是则返回false</returns> private static bool CheckIgnoreParamForPlaceholder(string matchString, ColumnInfo columnInfo) { if (string.IsNullOrWhiteSpace(matchString) || columnInfo == 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("}}") + 2); tagHead = tagHead.Replace("}}", "|}}").Replace("|", "||"); regex = new Regex(@"\|ignparam\:(.)*?\|"); match = regex.Match(tagHead); if (match.Success) { if (match.Value.IndexOf(":") > -1) { tagParamStr = match.Value.Replace("|", string.Empty).Split(':')[1]; if (string.IsNullOrWhiteSpace(tagParamStr)) { return false; } } paramArr = tagParamStr.Split(','); foreach (string param in paramArr) { tmpParam = param.ToLower().Trim(); if ("pk".Equals(tmpParam) && columnInfo.IsPK) { isIgnore = true; continue; } if ("fk".Equals(tmpParam) && columnInfo.IsFK) { isIgnore = true; continue; } if (columnInfo.PropertyNameLow.Equals(tmpParam)) { isIgnore = true; continue; } } } return isIgnore; }
/// <summary> /// 字段判断占位符替换,判断内容中是否有必填,最大,最小值,字符串,数字等 /// </summary> /// <param name="content">需要替换的内容</param> /// <param name="columnInfo">字段信息对象</param> /// <param name="resultString">输出替换后的内容</param> /// <returns>返回有内容的判断占位符个数,如果为0则说明该内容中的所有判断占位符都没能返回内容</returns> private static int ReplaceColumnIFSectionContent(string content, ColumnInfo columnInfo, out string resultString) { string contentString = content; int hasContentCount = 0; // Required hasContentCount += ReplaceConditionIFSectionContent(ContentTemplatePlaceholder.ColumnRequiredIFSection, contentString, !columnInfo.IsNullable, out contentString); // Max Length hasContentCount += ReplaceConditionIFSectionContent(ContentTemplatePlaceholder.ColumnMaxLenIFSection, contentString, columnInfo.IsMaxLen, out contentString); // Max hasContentCount += ReplaceConditionIFSectionContent(ContentTemplatePlaceholder.ColumnMaxIFSection, contentString, columnInfo.IsMax, out contentString); // Min hasContentCount += ReplaceConditionIFSectionContent(ContentTemplatePlaceholder.ColumnMinIFSection, contentString, columnInfo.IsMin, out contentString); // Unique hasContentCount += ReplaceConditionIFSectionContent(ContentTemplatePlaceholder.ColumnUniqueIFSection, contentString, columnInfo.IsUnique, out contentString); // String hasContentCount += ReplaceConditionIFSectionContent(ContentTemplatePlaceholder.ColumnStringIFSection, contentString, columnInfo.IsString, out contentString); // Number hasContentCount += ReplaceConditionIFSectionContent(ContentTemplatePlaceholder.ColumnNumberIFSection, contentString, columnInfo.IsNumber, out contentString); // Integer hasContentCount += ReplaceConditionIFSectionContent(ContentTemplatePlaceholder.ColumnIntegerIFSection, contentString, columnInfo.IsInteger, out contentString); // DateTime hasContentCount += ReplaceConditionIFSectionContent(ContentTemplatePlaceholder.ColumnDateTimeIFSection, contentString, columnInfo.IsDateTime, out contentString); resultString = contentString; return hasContentCount; }
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); }