예제 #1
0
        /// <summary>
        /// 获取表或视图的详细信息。
        /// </summary>
        /// <param name="databaseName">数据库名称</param>
        /// <param name="tableName">表或视图名称</param>
        /// <param name="isView">是否为视图</param>
        /// <returns>表或视图信息</returns>
        public override DbTable GetTableOrViewDetails(string databaseName, string tableName, bool isView = false)
        {
            var arrays = tableName.Split('.');

            var result = new DbTable();
            var dbHelper = GetDbHelper(databaseName);
            var columns = dbHelper.DbMetadata.GetColumns(tableName);

            result.Schema = arrays[0];
            result.Name = arrays[0] == "dbo" ? arrays[1] : tableName;
            result.ModuleName = arrays[0] == "dbo" ? string.Empty : arrays[0];
            result.ClassName = arrays[1].AsClassName();

            foreach (var item in columns)
            {
                var dbColumn = new DbColumn { Name = item.Name };

                dbColumn.PropertyName = dbColumn.Name.PascalNaming();
                dbColumn.IsIdentity = item.IsIdentity;
                dbColumn.IsPrimaryKey = item.IsPrimaryKey;
                dbColumn.Nullable = item.IsNullable;
                dbColumn.Description = item.Description;
                dbColumn.SqlType = item.DataType;
                dbColumn.Length = item.DataLength;
                dbColumn.BasicType = dbColumn.SqlType;

                result.Columns.Add(dbColumn);
            }

            return result;
        }
예제 #2
0
        /// <summary>
        /// 获取表或视图的详细信息。
        /// </summary>
        /// <param name="databaseName">数据库名称</param>
        /// <param name="tableName">表或视图名称</param>
        /// <param name="isView">是否为视图</param>
        /// <returns>表或视图信息</returns>
        public override DbTable GetTableOrViewDetails(string databaseName, string tableName, bool isView = false)
        {
            var result = new DbTable();
            var dbHelper = GetDbHelper(databaseName);
            var columns = dbHelper.DbMetadata.GetColumns(tableName);

            result.Name = tableName;
            result.Schema = tableName.Contains('_') ? tableName.Split('_')[0] : "Core";
            result.ModuleName = result.Schema.AsClassName();
            result.ClassName = tableName.Contains('_') ? tableName.Substring(tableName.IndexOf('_')).AsClassName() : tableName.AsClassName();

            foreach (var item in columns)
            {
                var dbColumn = new DbColumn { Name = item.Name };

                dbColumn.PropertyName = dbColumn.Name.PascalNaming();
                dbColumn.IsIdentity = item.IsIdentity;
                dbColumn.IsPrimaryKey = item.IsPrimaryKey;
                dbColumn.Nullable = item.IsNullable;
                dbColumn.Description = item.Description;
                dbColumn.SqlType = item.DataType;
                dbColumn.Length = item.DataLength;
                dbColumn.BasicType = dbColumn.SqlType;

                result.Columns.Add(dbColumn);
            }

            return result;
        }
예제 #3
0
        /// <summary>
        /// 获取表或视图的详细信息。
        /// </summary>
        /// <param name="databaseName">数据库名称</param>
        /// <param name="tableName">表或视图名称</param>
        /// <param name="isView">是否为视图</param>
        /// <returns>表或视图信息</returns>
        public override DbTable GetTableOrViewDetails(string databaseName, string tableName, bool isView = false)
        {
            var result = new DbTable();
            var dbUtil = new DbHelper(MYSQL_PROVIDER, string.Format(MYSQL_CONNECTION_FORMAT, this.ServerUri, this.Account, this.Password, databaseName));

            result.Name = tableName;
            result.ClassName = tableName.AsClassName();
            result.Description = result.ClassName;

            try
            {
                using (var reader = dbUtil.ExecuteReader(string.Format("DESC {0}", tableName)))
                {
                    while (reader.Read())
                    {
                        var field = new DbColumn
                        {
                            Name = reader.GetString(0),
                            PropertyName = reader.GetString(0).PascalNaming(),
                            IsIdentity = !Convert.IsDBNull(reader[5]) && reader.GetString(5).Contains("auto_increment"),
                            IsPrimaryKey = !Convert.IsDBNull(reader[3]) && reader.GetString(3).ToUpper().Contains("PRI"),
                            Nullable = reader.GetString(2) == "YES",
                            Description = reader.GetString(0),
                        };

                        var sqlType = reader.GetString(1).Split(' ')[0];

                        if (sqlType.Contains('('))
                        {
                            sqlType = sqlType.Substring(0, sqlType.IndexOf('('));
                        }

                        field.SqlType = sqlType;
                        field.BasicType = sqlType;

                        result.Columns.Add(field);
                    }
                }
            }
            catch { }

            return result;
        }