private SpecificObjectInfo AddSpecificObject(SpecificObjectInfo obj, bool reuseGroupId) { var res = obj.CloneSpecificObject(this); if (!reuseGroupId) { res.GroupId = Guid.NewGuid().ToString(); } switch (obj.ObjectType) { case DatabaseObjectType.View: Views.Add((ViewInfo)res); break; case DatabaseObjectType.StoredProcedure: StoredProcedures.Add((StoredProcedureInfo)res); break; case DatabaseObjectType.Function: Functions.Add((FunctionInfo)res); break; case DatabaseObjectType.Trigger: Triggers.Add((TriggerInfo)res); break; } return(res); }
public void AssignPhaseData(DatabaseObjectInfo source, DatabaseAnalysePhase phase) { var src = (DatabaseInfo)source; if ((phase & DatabaseAnalysePhase.Tables) != 0) { Tables.Clear(); foreach (var obj in src.Tables) { Tables.Add(obj.CloneTable(this)); } } if ((phase & DatabaseAnalysePhase.Views) != 0) { Views.Clear(); foreach (var obj in src.Views) { Views.Add(obj.CloneView(this)); } } if ((phase & DatabaseAnalysePhase.Functions) != 0) { StoredProcedures.Clear(); Functions.Clear(); Triggers.Clear(); foreach (var obj in src.StoredProcedures) { StoredProcedures.Add(obj.CloneStoredProcedure(this)); } foreach (var obj in src.Functions) { Functions.Add(obj.CloneFunction(this)); } foreach (var obj in src.Triggers) { Triggers.Add(obj.CloneTrigger(this)); } } if ((phase & DatabaseAnalysePhase.Settings) != 0) { Schemas.Clear(); foreach (var obj in src.Schemas) { Schemas.Add(obj.CloneSchema(this)); } DefaultSchema = src.DefaultSchema; } }
/// <summary> /// Carrega todos os objetos do assembly do banco de dados /// </summary> private void LoadObjects() { if (Assembly == null) { throw new Exception("Favor preencher a propriedade Assembly da classe de Banco de dados"); } foreach (Type myT in Assembly.GetTypes()) { if (myT.BaseType == null | myT.IsAbstract) { continue; } if (myT.BaseType == typeof(TableAdapter) || myT.BaseType.Name == typeof(TableAdapterDomain <Object>).Name) { var table = (TableAdapter)GetInstanceObject(null, myT); Tables.Add(table); } else if (myT.BaseType.IsAbstract && myT.BaseType.BaseType == typeof(TableAdapter)) { var table = (TableAdapter)GetInstanceObject(null, myT); Tables.Add(table); } else if (myT.BaseType == typeof(StoredProcedure)) { var mySp = GetInstanceObject(null, myT) as StoredProcedure; Debug.Assert(mySp != null, "mySp != null"); mySp.LoadFiles(Assembly); StoredProcedures.Add(mySp); } } Tables.Add(new AppVersion { DBName = DataBaseName }); }
public bool LoadDatabaseMetadata(string database_name, string connection_string) { if (string.IsNullOrEmpty(database_name)) { throw new ArgumentException("Database name is null or empty"); } Reset(); Name = database_name; ConnectionString = connection_string; // load and parse out table data try { string sql_query = GetTableData(); DataTable dt = Database.ExecuteQuery(sql_query, null, ConnectionString); if (dt != null && dt.Rows.Count != 0 && dt.Columns.Count != 0) { foreach (DataRow dr in dt.Rows) { string table_name = (string)dr["TableName"]; string column_name = (string)dr["ColumnName"]; if (!Tables.ContainsKey(table_name)) { SqlTable sql_table = new SqlTable(this, table_name); Tables.Add(table_name, sql_table); } SqlColumn sql_column = new SqlColumn(); sql_column.Table = Tables[table_name]; sql_column.Name = (string)dr["ColumnName"]; sql_column.DataType = (string)dr["DataType"]; sql_column.Length = Convert.ToInt32(dr["Length"]); sql_column.Precision = Convert.ToInt32(dr["Precision"]); sql_column.IsNullable = Convert.ToBoolean(dr["IsNullable"]); sql_column.IsPk = Convert.ToBoolean(dr["IsPK"]); sql_column.IsIdentity = Convert.ToBoolean(dr["IsIdentity"]); sql_column.ColumnOrdinal = Convert.ToInt32(dr["ColumnOrdinal"]); if (Tables[table_name].Columns.ContainsKey(column_name)) { throw new Exception($"Column {column_name} already exists in table {Tables[table_name]}"); } else { Tables[table_name].Columns.Add(column_name, sql_column); } } } } catch (Exception ex) { ErrorList.Add(ex); } // get SP try { string sql_query = GetStoredProcedures(); DataTable dt = Database.ExecuteQuery(sql_query, null, ConnectionString); if (dt != null && dt.Rows.Count != 0 && dt.Columns.Count != 0) { foreach (DataRow dr in dt.Rows) { SqlScript sql_script = new SqlScript(); sql_script.Name = (string)dr["Name"]; sql_script.Body = (string)dr["Body"]; if (StoredProcedures.ContainsKey(sql_script.Name)) { StoredProcedures[sql_script.Name].Body += sql_script.Body; } else { StoredProcedures.Add(sql_script.Name, sql_script); } } } } catch (Exception ex) { ErrorList.Add(ex); } // get functions try { string sql_query = GetFunctions(); DataTable dt = Database.ExecuteQuery(sql_query, null, ConnectionString); if (dt != null && dt.Rows.Count != 0 && dt.Columns.Count != 0) { foreach (DataRow dr in dt.Rows) { SqlScript sql_script = new SqlScript(); sql_script.Name = (string)dr["Name"]; sql_script.Body = (string)dr["Body"]; if (Functions.ContainsKey(sql_script.Name)) { Functions[sql_script.Name].Body += sql_script.Body; } else { Functions.Add(sql_script.Name, sql_script); } } } } catch (Exception ex) { ErrorList.Add(ex); } // get constraints try { string sql_query = GetConstraints(); DataTable dt = Database.ExecuteQuery(sql_query, null, ConnectionString); if (dt != null && dt.Rows.Count != 0 && dt.Columns.Count != 0) { foreach (DataRow dr in dt.Rows) { SqlConstraint sql_constraint = new SqlConstraint(); sql_constraint.ConstraintName = (string)dr["ConstraintName"]; sql_constraint.FKTable = (string)dr["FKTable"]; sql_constraint.FKColumn = (string)dr["FKColumn"]; sql_constraint.PKTable = (string)dr["PKTable"]; sql_constraint.PKColumn = (string)dr["PKColumn"]; if (Constraints.ContainsKey(sql_constraint.ConstraintName)) { throw new Exception(string.Format("Constraint {0} already exists.", sql_constraint.ConstraintName)); } else { Constraints.Add(sql_constraint.ConstraintName, sql_constraint); } } } } catch (Exception ex) { ErrorList.Add(ex); } // load default values try { string sql_query = GetDefaultValues(); DataTable dt = Database.ExecuteQuery(sql_query, null, ConnectionString); if (dt != null && dt.Rows.Count != 0 && dt.Columns.Count != 0) { foreach (DataRow dr in dt.Rows) { if (Tables.ContainsKey((string)dr["TableName"])) { if (Tables[(string)dr["TableName"]].Columns.ContainsKey((string)dr["ColumnName"])) { Tables[(string)dr["TableName"]].Columns[(string)dr["ColumnName"]].DefaultValue = RemoveWrappingCharacters((string)dr["DefaultValue"]); } } } } } catch (Exception ex) { ErrorList.Add(ex); } return(ErrorList.Count == 0); }