/// <summary> /// 根据模版获取生成的代码 /// </summary> public string GetByTable( string tableName, TemplateEntity templ ) { TableEntity table = service.GetTableByName(Project.ID.ToString(), tableName); CodeBuilderHost host = new CodeBuilderHost(); host.User = User; host.Columns = service.GetColumnList(table.ID.ToString()).ToList(); host.Project = Project; host.Table = table; host.FileExtension = templ.FileExtensions; host.TemplateFile = ""; //开始生成代码 Engine engine = new Engine(); //string input = File.ReadAllText(host.TemplateFile,Encoding.Default); string input = templ.Content; string outputText = engine.ProcessTemplate(input, host); //写入生成错误信息 foreach (CompilerError createError in host.Errors) { Error.AppendLine(createError.ToString()); } return(outputText); }
protected void ReaderColumns(TableEntity tableEntity, List <ColumnInfo> columns) { if (columns != null && columns.Count > 0) { ColumnEntity[] listColumns = service.GetColumnList(tableEntity.ID.ToString()); int i = 0; foreach (ColumnInfo column in columns) { i++; try { //获取服务器字段信息 ColumnEntity columnEntity = listColumns.Where(o => o.Field == column.Field).FirstOrDefault(); if (columnEntity == null) { columnEntity = new ColumnEntity(); } DataType dataType = DataTypeManager.ParseDbDataType(DbType, column.DataType.ToLower()); columnEntity.Field = column.Field; columnEntity.DataType = dataType.ToString(); columnEntity.Required = !column.IsNullable; columnEntity.DefaultValue = column.DefaultValue; ModelReaderHelper.SetLength(columnEntity, dataType, column.Length, column.Precision); ModelReaderHelper.SetColumnEditor(columnEntity, dataType); if (columnEntity.ID <= 0) { //新增字段 columnEntity.SortID = i; columnEntity.Display = string.IsNullOrEmpty(column.Display) ? column.Field : column.Display; ModelReaderHelper.SetDefault(columnEntity); columnEntity.TableID = tableEntity.ID; ModelReaderHelper.SetFormatString(columnEntity, dataType); ModelReaderHelper.SetAlign(columnEntity, dataType); //设置主键信息 if (column.IsDataKey) { ModelReaderHelper.SetColumnDataKey(columnEntity); //更新表的主键信息 tableEntity.DataKey = column.Field; tableEntity.DataKeyType = column.IsIdentity ? WSH.CodeBuilder.DispatchServers.DataKeyType.IdEntity : WSH.CodeBuilder.DispatchServers.DataKeyType.Guid; tableEntity.DefaultSortName = column.Field; tableEntity.DefaultSortMode = DispatchServers.SortMode.Asc; service.UpdateTable(tableEntity); } columnEntity.CreateTime = DateTime.Now; service.AddColumn(columnEntity); } else { columnEntity.EditTime = DateTime.Now; service.UpdateColumn(columnEntity); } } catch (Exception ex) { Error.AppendLine(string.Format("读取数据表:{0}的{1}字段出错:", tableEntity.TableName, column.Field) + ex.Message); } } } }
private void ExportHtml() { #region 导出Html string outputText = string.Empty; StringBuilder nav = new StringBuilder(); StringBuilder txt = new StringBuilder(); int j = 0; foreach (WSH.CodeBuilder.DispatchServers.TableEntity table in Tables) { //if (isAllTable != true && table.Enable == false) { continue; } j++; string name = table.TableName + j; string enable = !table.Enabled ? "enable " : ""; //拼接nav nav.AppendLine("<a href=\"#" + name + "\" title=\"" + table.Attr + "\" class=\"" + enable + "\">" + table.TableName + "</a>"); //拼接内容 txt.AppendLine("<a hidefocus=\"true\" class=\"tabTitle " + enable + "\" href=\"javascript:void(0)\" name=\"" + name + "\"><span>" + table.TableName + "</span>(" + table.Attr + ")</a>"); txt.AppendLine("<table class=\"tab " + enable + "\" cellpadding=\"0\" cellspacing=\"0\">"); //生成列集合 WSH.CodeBuilder.DispatchServers.ColumnEntity[] columns = service.GetColumnList(table.ID.ToString()); txt.AppendLine(" <tr>"); txt.AppendLine(" <th>" + colName1 + "</th>"); txt.AppendLine(" <th>" + colName2 + "</th>"); txt.AppendLine(" <th>" + colName3 + "</th>"); if (AllowRequired == true) { txt.AppendLine(" <th>" + colName4 + "</th>"); } txt.AppendLine(" </tr>"); int i = 0; foreach (WSH.CodeBuilder.DispatchServers.ColumnEntity column in columns) { // if (isAllField != true && column.Enable == false) { continue; } string oddClass = i % 2 == 0 ? "" : "odd "; string enableClass = !column.Enabled ? "enable " : ""; txt.AppendLine(" <tr class=\"" + oddClass + enableClass + "\">"); txt.AppendLine(" <td>" + column.Field + "</td>"); txt.AppendLine(" <td>" + column.Display + "</td>"); txt.AppendLine(" <td>" + column.DataType + "</td>"); if (AllowRequired == true) { string required = column.Required ? " checked=\"true\"" : ""; txt.AppendLine(" <td><input disabled=\"true\" type=\"checkbox\"" + required + "/></td>"); } txt.AppendLine(" </tr>"); i++; } txt.AppendLine("</table>"); if (OnProgress != null) { OnProgress(this, new ProgressEventArgs() { Max = Tables.Count, Value = j }); } } #region 拼接Html内容 StringBuilder sb = new StringBuilder(); sb.AppendLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"); sb.AppendLine("<html xmlns=\"http://www.w3.org/1999/xhtml\">"); sb.AppendLine("<head>"); sb.AppendLine(" <title>" + DocumentName + "</title>"); sb.AppendLine(" <style type=\"text/css\">"); sb.AppendLine(" html,body{ height:100%; overflow-y:hidden; overflow-x:auto;}"); sb.AppendLine(" body{ background:#DFE8F6; text-align:center; margin:0px; font-family:Arial,宋体; font-size:12px;}"); sb.AppendLine(" .border{ border:1px solid #99BBE8;}"); sb.AppendLine(" .bg{ background:#DFE8F6;}"); sb.AppendLine(" .wrap{ height:80%; margin:0px auto; text-align:left; width:1000px; margin-top:10px;}"); sb.AppendLine(" .nav{ height:100%; width:200px; float:left; background:#fff; overflow:auto; font-size:13px;}"); sb.AppendLine(" .content{float:left; height:100%; background:#fff; margin-left:10px; width:786px; overflow:auto; text-align:center;}"); sb.AppendLine(" a{ text-decoration:none; color:#000; font-weight:bold;}"); sb.AppendLine(" a:hover{ color:Red; }"); sb.AppendLine(" .nav a{ display:block; height:20px; line-height:20px; padding:0px 2px;}"); sb.AppendLine(" .nav a:hover{ background:#eee; }"); sb.AppendLine(" .tabTitle{padding:10px 0px; display:block; outline:none;}"); sb.AppendLine(" .tabTitle span{ font-size:16px; font-weight:bold; margin-right:8px;}"); sb.AppendLine(" .tab{ width:92%; margin:0px auto;border-collapse:collapse; }"); sb.AppendLine(" .tab th,.tab td{ border:1px solid #A3BAE9; text-align:center;}"); sb.AppendLine(" .tab th{ background:#DEECFD; color:#333; font-weight:normal; font-size:13px; height:22px;}"); sb.AppendLine(" .tab td{ height:20px;}"); sb.AppendLine(" .tab tr.odd{ background:#F7F7F7;}"); sb.AppendLine(" .tab tr.enable,.enable{text-decoration:line-through;}"); sb.AppendLine(" </style>"); sb.AppendLine(" <script type=\"text/javascript\">"); sb.AppendLine(" ; (function () {"); sb.AppendLine(" window.j = function (el) {"); sb.AppendLine(" return typeof el == \"string\" ? document.getElementById(el) : el;"); sb.AppendLine(" };"); sb.AppendLine(" j.getClient = function () {"); sb.AppendLine(" return { width: document.documentElement.clientWidth, height: document.documentElement.clientHeight };"); sb.AppendLine(" }"); sb.AppendLine(" j.setHeight=function(){"); sb.AppendLine(" var client = j.getClient();"); sb.AppendLine(" var wrap = j(\"wrap\").style.height = client.height -2- 20+\"px\";"); sb.AppendLine(" }"); sb.AppendLine(" })();"); sb.AppendLine(" window.onresize=j.setHeight;"); sb.AppendLine(" window.onload = function () {"); sb.AppendLine(" j.setHeight();"); sb.AppendLine(""); sb.AppendLine(" };"); sb.AppendLine(" </script>"); sb.AppendLine("</head>"); sb.AppendLine("<body>"); sb.AppendLine(" <div class=\"wrap\" id=\"wrap\">"); sb.AppendLine(" <div class=\"nav border\">"); //生成表名导航 sb.Append(nav.ToString()); sb.AppendLine(" </div>"); sb.AppendLine(" <div class=\"content border\">"); //生成表内容 sb.Append(txt.ToString()); sb.AppendLine(" </div>"); sb.AppendLine(" </div>"); sb.AppendLine("</body>"); sb.AppendLine("</html>"); #endregion outputText = sb.ToString(); #endregion FileHelper.WriteFile(FileName, outputText); }
//建表脚本-Table public static string CreateTableScript(ProjectEntity project, TableEntity table, bool isUse) { CodeBuilderService service = ServiceHelper.GetCodeBuilderService(); StringBuilder sb = new StringBuilder(); try { if (project == null) { project = Global.GetCurrentProject(); } ColumnEntity[] columns = service.GetColumnList(table.ID.ToString()); //DataBaseType dataBaseType = DataBaseHelper.GetDbType(project.DbType); if (isUse) { sb.Append(GetUseScript(project.ProjectName)); } sb.AppendLine("--创建表:" + table.Attr); sb.AppendLine("if exists (select name from sysobjects where name='" + table.TableName + "')"); sb.AppendLine("\tbegin"); sb.AppendLine("\t\tprint '--温馨提示:" + table.TableName + "表已经存在,如需重新建表,请手动删除'"); sb.AppendLine("\t\tprint 'drop table " + table.TableName + "'"); sb.AppendLine("\tend"); sb.AppendLine("else"); sb.AppendLine("create table " + table.TableName); sb.AppendLine("("); for (int i = 0; i < columns.Length; i++) { string last = i == columns.Length - 1 ? "" : ","; ColumnEntity col = columns[i]; bool isPK = !string.IsNullOrEmpty(table.DataKey) && table.DataKey.ToLower() == col.Field.ToLower(); if (isPK) { string pkInfo = table.DataKeyType == WSH.CodeBuilder.DispatchServers.DataKeyType.Guid ? " varchar(40) primary key" : col.DataType + " primary key identity"; sb.AppendLine(string.Format("\t{0} {1}{2}--{3}", col.Field, pkInfo, last, col.Display)); } else { string notNull = col.Required == false ? " not null" : ""; string isDefault = string.IsNullOrEmpty(col.DefaultValue) ? "" : " default " + col.DefaultValue; string[] numbers = new string[] { "decimal", "numeric" }; string[] strings = new string[] { "nvarchar", "varchar", "char" }; string len = string.Empty; if (Array.IndexOf(numbers, col.DataType) > -1) { len = "(18," + col.Length + ")"; } else if (Array.IndexOf(strings, col.DataType) > -1) { len = "(" + col.Length + ")"; } string dataType = DataTypeManager.GetLangDataType("sqlserver", col.DataType); sb.AppendLine(string.Format("\t{0} {1}{2}{3}{4}--{5}", col.Field, (dataType + len), notNull, isDefault, last, col.Display)); } } sb.AppendLine(")"); } catch (Exception ex) { Information.AddFmt("创建表脚本出错,错误信息:{0}", ex.Message); } return(sb.ToString()); }