关系数据库元数据处理类(三) 创建查询元数据公共类
1 public abstract class BaseMetadata : IMetadata 2 { 3 protected IDbUtility DbUtility; 4 5 protected BaseMetadata(IDbUtility dbUtility) 6 { 7 DbUtility = dbUtility; 8 } 9 10 #region 字符串 11 12 /// <summary> 13 /// 获取所有数据库信息的字符串 14 /// </summary> 15 /// <returns></returns> 16 protected abstract string GetDataBaseSting(); 17 18 /// <summary> 19 /// 获取所有表信息字符串 20 /// </summary> 21 /// <returns></returns> 22 protected abstract string GetDataTableString(string database = ""); 23 24 /// <summary> 25 /// 获取所有字段字符串 26 /// </summary> 27 /// <returns></returns> 28 protected abstract string GetDataColumnString(); 29 30 /// <summary> 31 /// 获取所有外键字符串 32 /// </summary> 33 /// <returns></returns> 34 protected abstract string GetDataRelationString(); 35 36 #endregion 37 38 #region 转换 39 40 /// <summary> 41 /// 将DataRow转换为Database对象 42 /// </summary> 43 /// <param name="table"></param> 44 /// <returns></returns> 45 protected abstract List<Database> ConversionDatabase(DataTable table); 46 47 /// <summary> 48 /// 将DataRow转换为Table对象 49 /// </summary> 50 /// <param name="table"></param> 51 /// <returns></returns> 52 protected abstract List<Table> ConversionDataTable(DataTable table); 53 54 /// <summary> 55 /// 将DataRow转换为Table对象 56 /// </summary> 57 /// <param name="table"></param> 58 /// <returns></returns> 59 protected abstract List<Column> ConversionColumn(DataTable table); 60 61 /// <summary> 62 /// 将DataRow转换为Table对象 63 /// </summary> 64 /// <param name="table"></param> 65 /// <returns></returns> 66 protected abstract List<Relation> ConversionDataRelation(DataTable table); 67 68 #endregion 69 70 #region 操作 71 72 public Database GetDatabase(ISpecification<Database> specification) 73 { 74 return GetAllDatabase().First(specification.SatisfiedBy().Compile()); 75 } 76 77 public List<Database> GetAllDatabase() 78 { 79 return FinDatabases(null); 80 } 81 82 public List<Database> FinDatabases(ISpecification<Database> specification) 83 { 84 DataTable data = DbUtility.ExecuteDataTable(GetDataBaseSting()); 85 List<Database> databases = ConversionDatabase(data); 86 if (specification != null) 87 databases = databases.Where(specification.SatisfiedBy().Compile()).ToList(); 88 return databases; 89 } 90 91 public Table GeTable(ISpecification<Table> specification) 92 { 93 return GetAllTable().First(specification.SatisfiedBy().Compile()); 94 } 95 96 public List<Table> GetAllTable() 97 { 98 return FindTables(null); 99 } 100 101 public List<Table> FindTables(ISpecification<Table> specification) 102 { 103 DataTable data = DbUtility.ExecuteDataTable(GetDataTableString()); 104 List<Table> tables = ConversionDataTable(data); 105 if (specification != null) 106 tables = tables.Where(specification.SatisfiedBy().Compile()).ToList(); 107 return tables; 108 } 109 110 public Column GetColumn(ISpecification<Column> specification) 111 { 112 return GetAllColumns().First(specification.SatisfiedBy().Compile()); 113 } 114 115 public List<Column> GetAllColumns() 116 { 117 return FindColumns(null); 118 } 119 120 public List<Column> FindColumns(ISpecification<Column> specification) 121 { 122 DataTable data = DbUtility.ExecuteDataTable(GetDataColumnString()); 123 List<Column> columns = ConversionColumn(data); 124 if (specification != null) 125 columns = columns.Where(specification.SatisfiedBy().Compile()).ToList(); 126 return columns; 127 } 128 129 public List<Relation> GetAllRelation() 130 { 131 return FindRelations(null); 132 } 133 134 public Relation GeRelation(ISpecification<Relation> specification) 135 { 136 return GetAllRelation().First(specification.SatisfiedBy().Compile()); 137 } 138 139 140 public List<Relation> FindRelations(ISpecification<Relation> specification) 141 { 142 DataTable data = DbUtility.ExecuteDataTable(GetDataRelationString()); 143 List<Relation> relations = ConversionDataRelation(data); 144 if (specification != null) 145 relations = relations.Where(specification.SatisfiedBy().Compile()).ToList(); 146 return relations; 147 } 148 149 #endregion 150 }