/// <summary> /// 检测关系 /// </summary> /// <param name="dbInfo">数据库</param> /// <param name="table">要检测的表</param> /// <returns></returns> private static void CheckRelation(List <string> lstSql, DBInfo dbInfo, KeyWordTableParamItem table) { List <TableRelationAttribute> lstRelation = dbInfo.DBStructure.GetRelation(dbInfo.DefaultOperate, dbInfo, new string[] { table.TableName }); if (lstRelation == null) { return; } foreach (TableRelationAttribute item in table.RelationItems) { bool exists = false; foreach (TableRelationAttribute existsItem in lstRelation) { if (item.SourceName.Equals(existsItem.SourceName, StringComparison.CurrentCultureIgnoreCase)) { exists = true; break; } } if (!exists) { dbInfo.DBStructure.OnCheckEvent(table, dbInfo, CheckEvent.RelationBeginCheck, lstSql); item.CreateName(); BQLQuery bql = BQL.AlterTable(table.TableName).AddForeignkey(item); AbsCondition con = BQLKeyWordManager.ToCondition(bql, dbInfo, null, true); lstSql.Add(con.GetSql(false)); dbInfo.DBStructure.OnCheckEvent(table, dbInfo, CheckEvent.RelationChecked, lstSql); } } }
/// <summary> /// 检测表结构 /// </summary> /// <param name="lstSql">需要更新的SQL</param> /// <param name="dbInfo">数据库信息</param> /// <param name="table">要检测的表</param> private static void CheckTableStruct(List <string> lstSql, DBInfo dbInfo, KeyWordTableParamItem table) { string tableName = table.TableName; BQLQuery bql = BQL.Select(BQL.ToTable(tableName)._).From(BQL.ToTable(tableName)); SelectCondition con = BQLKeyWordManager.ToCondition(bql, dbInfo, null, true) as SelectCondition; string sql = dbInfo.CurrentDbAdapter.GetTopSelectSql(con, 1); Dictionary <string, bool> dic = new Dictionary <string, bool>(); using (IDataReader reader = dbInfo.DefaultOperate.Query(sql, new Buffalo.DB.DbCommon.ParamList(), null)) { for (int i = 0; i < reader.FieldCount; i++) { dic[reader.GetName(i).ToLower()] = true; } } StringBuilder sbSql = new StringBuilder(); string desSQL = null; IDBAdapter idb = dbInfo.CurrentDbAdapter; foreach (EntityParam pInfo in table.Params) { if (!dic.ContainsKey(pInfo.ParamName.ToLower())) { dbInfo.DBStructure.OnCheckEvent(pInfo, dbInfo, CheckEvent.TablenBeginCheck, lstSql); bql = BQL.AlterTable(tableName).AddParam(pInfo); AbsCondition acon = BQLKeyWordManager.ToCondition(bql, dbInfo, null, true); lstSql.Add(acon.GetSql(false)); if (!string.IsNullOrEmpty(pInfo.Description))//添加注释 { desSQL = idb.GetAddDescriptionSQL(table, pInfo, dbInfo); if (!string.IsNullOrEmpty(desSQL)) { lstSql.Add(desSQL); } } dbInfo.DBStructure.OnCheckEvent(pInfo, dbInfo, CheckEvent.TableChecked, lstSql); } } }