/// <summary>根据数据行取得数据表</summary> /// <param name="rows">数据行</param> /// <param name="names">指定表名</param> /// <param name="data">扩展</param> /// <returns></returns> protected List <IDataTable> GetTables(DataRow[] rows, String[] names, IDictionary <String, DataTable> data = null) { if (rows == null || rows.Length == 0) { return(new List <IDataTable>()); } // 表名过滤 if (names != null && names.Length > 0) { var hs = new HashSet <String>(names, StringComparer.OrdinalIgnoreCase); rows = rows.Where(dr => TryGetDataRowValue(dr, _.TalbeName, out String name) && hs.Contains(name)).ToArray(); } var columns = data?["Columns"]; var indexes = data?["Indexes"]; var indexColumns = data?["IndexColumns"]; if (columns == null) { columns = GetSchema(_.Columns, null); } if (indexes == null) { indexes = GetSchema(_.Indexes, null); } if (indexColumns == null) { indexColumns = GetSchema(_.IndexColumns, null); } var list = new List <IDataTable>(); foreach (var dr in rows) { #region 基本属性 var table = DAL.CreateTable(); table.TableName = GetDataRowValue <String>(dr, _.TalbeName); // 描述 table.Description = GetDataRowValue <String>(dr, "DESCRIPTION"); // 拥有者 table.Owner = GetDataRowValue <String>(dr, "OWNER"); // 是否视图 table.IsView = "View".EqualIgnoreCase(GetDataRowValue <String>(dr, "TABLE_TYPE")); table.DbType = Database.Type; #endregion #region 字段及修正 var cs = GetFields(table, columns, data); if (cs != null && cs.Count > 0) { table.Columns.AddRange(cs); } var dis = GetIndexes(table, indexes, indexColumns); if (dis != null && dis.Count > 0) { table.Indexes.AddRange(dis); } FixTable(table, dr, data); // 修正关系数据 table.Fix(); list.Add(table); #endregion } return(list); }
/// <summary>根据数据行取得数据表</summary> /// <param name="rows">数据行</param> /// <returns></returns> protected List <IDataTable> GetTables(DataRow[] rows) { if (_columns == null) { try { _columns = GetSchema(_.Columns, null); } catch (Exception ex) { DAL.WriteDebugLog(ex.ToString()); } } if (_indexes == null) { try { _indexes = GetSchema(_.Indexes, null); } catch (Exception ex) { DAL.WriteDebugLog(ex.ToString()); } } if (_indexColumns == null) { try { _indexColumns = GetSchema(_.IndexColumns, null); } catch (Exception ex) { DAL.WriteDebugLog(ex.ToString()); } } try { var list = new List <IDataTable>(); foreach (DataRow dr in rows) { #region 基本属性 IDataTable table = DAL.CreateTable(); table.TableName = GetDataRowValue <String>(dr, _.TalbeName); // 顺序、编号 Int32 id = 0; if (TryGetDataRowValue <Int32>(dr, "TABLE_ID", out id)) { table.ID = id; } else { table.ID = list.Count + 1; } // 描述 table.Description = GetDataRowValue <String>(dr, "DESCRIPTION"); // 拥有者 table.Owner = GetDataRowValue <String>(dr, "OWNER"); // 是否视图 table.IsView = "View".EqualIgnoreCase(GetDataRowValue <String>(dr, "TABLE_TYPE")); table.DbType = Database.DbType; #endregion #region 字段及修正 // 字段的获取可能有异常,但不应该影响整体架构的获取 try { var columns = GetFields(table); if (columns != null && columns.Count > 0) { table.Columns.AddRange(columns); } var indexes = GetIndexes(table); if (indexes != null && indexes.Count > 0) { table.Indexes.AddRange(indexes); } // 先修正一次关系数据 table.Fix(); } catch (Exception ex) { if (DAL.Debug) { DAL.WriteLog(ex.ToString()); } } FixTable(table, dr); list.Add(table); #endregion } #region 表间关系处理 //// 某字段名,为另一个表的(表名+单主键名)形式时,作为关联字段处理 //foreach (var table in list) //{ // foreach (var rtable in list) // { // if (table != rtable) table.Connect(rtable); // } //} ModelHelper.Connect(list); //// 因为可能修改了表间关系,再修正一次 //foreach (var table in list) //{ // table.Fix(); //} #endregion return(list); // 不要把这些清空。因为,多线程同时操作的时候,前面的线程有可能把后面线程的数据给清空了 } finally { _columns = null; _indexes = null; _indexColumns = null; } }