Exemplo n.º 1
0
 /// <summary>
 /// 初始化服务,任一环节失败即启动失败
 /// + 创建日志对象
 /// + 读取配置
 /// + 缓存默认库表结构、同步时间
 /// + 缓存Sql语句
 /// + 启动http服务器
 /// 此方法不可异步,否则启动有问题!!!
 /// </summary>
 /// <param name="p_stub">服务存根</param>
 /// <param name="p_args">启动参数</param>
 public static void Run(ISvcStub p_stub, string[] p_args)
 {
     Kit.Stub = p_stub ?? throw new ArgumentNullException(nameof(p_stub));
     CreateLogger();
     LoadConfig();
     DbSchema.Init();
     Silo.CacheSql();
     RunWebHost(p_args);
     Log.CloseAndFlush();
 }
Exemplo n.º 2
0
Arquivo: Table.cs Projeto: Daoting/dt
        /// <summary>
        /// 根据表名创建空Table
        /// </summary>
        /// <param name="p_tblName">表名</param>
        /// <returns></returns>
        public static Table Create(string p_tblName)
        {
            Throw.IfNullOrEmpty(p_tblName);
            Table tbl = new Table();

#if SERVER
            var schema = DbSchema.GetTableSchema(p_tblName);
            foreach (var col in schema.PrimaryKey.Concat(schema.Columns))
            {
                tbl._columns.Add(new Column(col.Name, col.Type));
            }
#else
            foreach (var col in AtModel.EachColumns(p_tblName))
            {
                tbl._columns.Add(new Column(col.ColName, GetColType(col.DbType)));
            }
#endif
            return(tbl);
        }
Exemplo n.º 3
0
 internal static TableSchema GetTableSchema(string p_tblName)
 {
     return(DbSchema.GetTableSchema(p_tblName));
 }
Exemplo n.º 4
0
        /// <summary>
        /// 生成实体类
        /// </summary>
        /// <param name="p_tblName">表名</param>
        /// <returns></returns>
        public string 生成实体类(string p_tblName)
        {
            if (string.IsNullOrEmpty(p_tblName))
            {
                return(null);
            }

            string tblName = p_tblName.ToLower();
            string clsName = GetClsName(tblName);
            var    schema  = DbSchema.GetTableSchema(tblName);

            StringBuilder sb = new StringBuilder();

            AppendTabSpace(sb, 1);
            sb.AppendLine("#region 自动生成");

            // Tbl标签
            AppendTabSpace(sb, 1);
            sb.Append($"[Tbl(\"{tblName}\")]");

            sb.AppendLine();
            AppendTabSpace(sb, 1);
            sb.Append($"public partial class {clsName} : Entity");
            sb.AppendLine();
            AppendTabSpace(sb, 1);
            sb.AppendLine("{");

            AppendTabSpace(sb, 2);
            sb.AppendLine("#region 构造方法");

            // 默认构造方法,私有为避免外部使用,内部在反序列化时使用
            AppendTabSpace(sb, 2);
            sb.AppendLine($"{clsName}() {{ }}");
            sb.AppendLine();

            // 构造方法
            AppendTabSpace(sb, 2);
            sb.Append("public ");
            sb.Append(clsName);
            sb.AppendLine("(");
            foreach (var col in schema.PrimaryKey)
            {
                AppendTabSpace(sb, 3);
                var colType = col.Type == typeof(byte) ? GetEnumName(col) : GetTypeName(col.Type);
                sb.Append(colType);
                sb.Append(" ");
                sb.Append(col.Name);
                sb.AppendLine(",");
            }
            foreach (var col in schema.Columns)
            {
                AppendTabSpace(sb, 3);
                var colType = col.Type == typeof(byte) ? GetEnumName(col) : GetTypeName(col.Type);
                sb.Append(colType);
                sb.Append(" ");
                sb.Append(col.Name);
                if (string.IsNullOrEmpty(col.Default))
                {
                    sb.AppendLine(" = default,");
                }
                else if (col.Type == typeof(string))
                {
                    sb.AppendLine($" = \"{col.Default}\",");
                }
                else if (col.Type == typeof(bool))
                {
                    sb.Append(" = ");
                    if (col.Default == "1")
                    {
                        sb.AppendLine("true,");
                    }
                    else
                    {
                        sb.AppendLine("false,");
                    }
                }
                else if (col.Type == typeof(byte) && colType != "byte")
                {
                    sb.AppendLine($" = ({colType}){col.Default},");
                }
                else
                {
                    sb.AppendLine($" = {col.Default},");
                }
            }
            sb.Remove(sb.Length - 3, 3);
            sb.Append(")");
            sb.AppendLine();
            AppendTabSpace(sb, 2);
            sb.AppendLine("{");
            foreach (var col in schema.PrimaryKey.Concat(schema.Columns))
            {
                AppendTabSpace(sb, 3);
                sb.Append("AddCell(\"");
                // 简化写法不需要类型
                //sb.Append(GetTypeName(col.Type));
                //sb.Append(">(\"");
                sb.Append(col.Name);
                sb.Append("\", ");
                // 内部为enum类型
                //if (IsEnumCol(col))
                //    sb.Append("(byte)");
                sb.Append(col.Name);
                sb.AppendLine(");");
            }
            AppendTabSpace(sb, 3);
            sb.AppendLine("IsAdded = true;");
            AppendTabSpace(sb, 3);
            sb.AppendLine("AttachHook();");
            AppendTabSpace(sb, 2);
            sb.AppendLine("}");
            AppendTabSpace(sb, 2);
            sb.AppendLine("#endregion");

            sb.AppendLine();
            AppendTabSpace(sb, 2);
            sb.Append("#region 属性");

            // 主键属性
            bool existID = false;

            foreach (var col in schema.PrimaryKey)
            {
                if (col.Name.Equals("ID", StringComparison.OrdinalIgnoreCase))
                {
                    existID = true;
                    if (col.Type != typeof(long))
                    {
                        // 类型不同时,覆盖原ID
                        AppendColumn(col, sb, true);
                    }
                }
                else
                {
                    AppendColumn(col, sb, false);
                }
            }
            if (!existID)
            {
                // 无ID时,屏蔽ID属性
                sb.AppendLine();
                AppendTabSpace(sb, 2);
                sb.AppendLine("new public long ID { get { return -1; } }");
            }

            // 普通列
            foreach (var col in schema.Columns)
            {
                AppendColumn(col, sb, false);
            }
            AppendTabSpace(sb, 2);
            sb.AppendLine("#endregion");

            AppendTabSpace(sb, 1);
            sb.AppendLine("}");
            AppendTabSpace(sb, 1);
            sb.AppendLine("#endregion");

            return(sb.ToString());
        }
Exemplo n.º 5
0
 public string 更新表结构缓存()
 {
     return(DbSchema.LoadSchema());
 }
Exemplo n.º 6
0
        /// <summary>
        /// 生成实体类框架
        /// </summary>
        /// <param name="p_tblName">表名</param>
        /// <returns></returns>
        public string 实体类框架(string p_tblName)
        {
            if (string.IsNullOrEmpty(p_tblName))
            {
                return(null);
            }

            string tblName = p_tblName.ToLower();
            string clsName = GetClsName(tblName);
            var    schema  = DbSchema.GetTableSchema(tblName);

            StringBuilder sb = new StringBuilder();

            AppendTabSpace(sb, 1);
            sb.Append($"public partial class {clsName}");
            sb.AppendLine();
            AppendTabSpace(sb, 1);
            sb.AppendLine("{");

            AppendTabSpace(sb, 2);
            sb.AppendLine("async Task OnSaving()");
            AppendTabSpace(sb, 2);
            sb.AppendLine("{");
            AppendTabSpace(sb, 2);
            sb.AppendLine("}");

            sb.AppendLine();
            AppendTabSpace(sb, 2);
            sb.AppendLine("async Task OnDeleting()");
            AppendTabSpace(sb, 2);
            sb.AppendLine("{");
            AppendTabSpace(sb, 2);
            sb.AppendLine("}");

            sb.AppendLine();
            AppendTabSpace(sb, 2);
            sb.AppendLine($"public static async Task<{clsName}> New()");
            AppendTabSpace(sb, 2);
            sb.AppendLine("{");
            AppendTabSpace(sb, 2);
            sb.AppendLine("}");

            sb.AppendLine();
            AppendTabSpace(sb, 2);
            sb.AppendLine($"public static async Task<{clsName}> Get(long p_id)");
            AppendTabSpace(sb, 2);
            sb.AppendLine("{");
            AppendTabSpace(sb, 2);
            sb.AppendLine("}");

            foreach (var col in schema.PrimaryKey.Concat(schema.Columns))
            {
                // 注释SetXXX方法,提供复制
                sb.AppendLine();
                AppendTabSpace(sb, 2);
                string tpName = GetTypeName(col.Type);
                sb.AppendLine($"void Set{col.Name}({tpName} p_value)");
                AppendTabSpace(sb, 2);
                sb.AppendLine("{");
                AppendTabSpace(sb, 2);
                sb.AppendLine("}");
            }

            AppendTabSpace(sb, 1);
            sb.AppendLine("}");

            return(sb.ToString());
        }