public string GenerateCode(IDbEngine engine, SettingInfo setting, string sql) { List<ColumnInfo> columns = new List<ColumnInfo>(); engine.Execute(sql).ToReader(reader => { reader.Read(); for(int i = 0; i < reader.FieldCount; i++) { var column = new ColumnInfo() { Name = reader.GetName(i), }; column.SetType(reader.GetFieldType(i)); columns.Add(column); } }); var code = this.GenerateCode(columns, setting, "自定义语句"); code = "/*********** SQL **********\r\n\r\n" + sql + "\r\n\r\n********** SQL ***********/\r\n\r\n" + code; return code; }
private string GenerateCode(List<ColumnInfo> columns, SettingInfo setting, string owner) { StringBuilder builder = new StringBuilder(); builder.AppendLine("using System;"); builder.AppendLine("using System.Collections.Generic;"); builder.AppendLine("using System.Linq;"); builder.AppendLine("using System.Text;"); builder.AppendLine("using Aoite.Data;"); builder.AppendLine(); builder.Append("namespace "); builder.AppendLine(setting.Namespace); builder.AppendLine("{"); builder.AppendLine(" /// <summary>"); builder.Append(" /// "); if(!string.IsNullOrEmpty(this._Comments)) { if(!this._Comments.StartsWith("表示一个")) builder.Append("表示一个"); builder.Append(this._Comments); if(this._Comments.Last() != '。') builder.Append("。"); } builder.AppendLine(); //builder.Append(owner); //builder.Append("“"); //builder.Append(this.Name); //builder.AppendLine("”对应的实体类型。"); builder.AppendLine(" /// </summary>"); if(!string.Equals(this.Name, this.ClassName, StringComparison.CurrentCultureIgnoreCase)) { builder.Append(" [Table(\""); builder.Append(this.Name); builder.AppendLine("\")]"); } builder.Append(" public partial class "); builder.AppendLine(this.ClassName); builder.AppendLine(" {"); builder.AppendLine(); foreach(var item in columns) { builder.AppendLine(item.GenerateCode(this, setting, owner)); builder.AppendLine(); } builder.AppendLine(" }"); builder.AppendLine("}"); return builder.ToString(); }
private string InnerGenerateCode(ObjectInfo objectInfo, SettingInfo setting) { var attrs = this.GenerateAttributes(); var f = (attrs.Length > 0 ? (" " + attrs + "\r\n") : string.Empty) + Format1; return string.Format(f, this.PropertyType, this.PropertyName); }
public string GenerateCode(ObjectInfo objectInfo, SettingInfo setting, string owner) { if(string.IsNullOrEmpty(PropertyType)) this.PropertyType = this.ParseDbType(); var comments = this.Comments; if(!string.IsNullOrEmpty(comments)) { if(!comments.StartsWith("设置或获取一个值,表示")) comments = ("设置或获取一个值,表示") + comments; if(comments.Last() != '。') comments += "。"; } return @" /// <summary> /// " + comments + @" /// </summary> " + this.InnerGenerateCode(objectInfo, setting); }
public string GenerateCode(IDbEngine engine, SettingInfo setting) { var columnInfos = engine.Execute(@" SELECT a.Name, t.name as DbType, a.max_length as MaxLength, IsPrimaryKey=case when exists(SELECT 1 FROM sys.objects where type='PK' and name in ( SELECT name FROM sys.indexes WHERE index_id in( SELECT indid FROM sys.sysindexkeys WHERE id = a.object_id AND colid=a.column_id ))) then 1 else 0 end, IsNullable=a.is_nullable, Comments=isnull(g.[value],'') FROM sys.columns a inner join sys.objects d on a.object_id=d.object_id and d.name<>'dtproperties' inner join sys.types t on a.user_type_id=t.user_type_id left join sys.extended_properties g on a.object_id=g.major_id and a.column_id=g.minor_id WHERE d.Name=@tableName ORDER BY a.column_id", "@tableName", this.Name) .ToEntities<ColumnInfo>(); //var table = engine.Execute("SELECT * FROM " + this.Name + " WHERE 1=2").ToTable(); //var columns = table.Columns; //foreach(var item in columnInfos) //{ // item.SetType(columns[item.Name].DataType); //} return GenerateCode(columnInfos, setting, this.Type == ObjectType.Table ? "数据库表对象" : "数据库视图对象"); }