コード例 #1
0
        /// <summary>
        /// This method adds all the databases in the current project
        /// to the DataManager (DM).
        /// </summary>
        /// <param name="currentProject"></param>
        public void AddDatabases(ref Project currentProject)
        {
            // Make sure the CurrentProject is set; I think this changed and now the CurrentProject
            // is created earlier.
            if ((CurrentProject == null) || (!CurrentProject.Equals(currentProject)))
            {
                // this has to be set before the DataManager can be created
                this.CurrentProject = currentProject;
            }

            // locals
            Database           database     = null;
            List <Enumeration> enumerations = null;
            List <DTNDatabase> databases    = null;

            // If the currentProject object exists
            if (NullHelper.Exists(currentProject))
            {
                // set the databases
                databases = currentProject.Databases;

                // if the current project has enumerations
                if (ListHelper.HasOneOrMoreItems(currentProject.Enumerations))
                {
                    // get a list of enumerations
                    enumerations = new List <Enumeration>();

                    // if the enumrations exist
                    foreach (Enumeration enumeration in currentProject.Enumerations)
                    {
                        // add thsi enumerations
                        enumerations.Add(enumeration);
                    }
                }
            }

            // if databases exists
            if ((ListHelper.HasOneOrMoreItems(databases)) && (DataManager != null))
            {
                // loop through each database
                foreach (DTNDatabase db in databases)
                {
                    // convert to DataJuggler.Net.Sql database
                    database = ConvertSQLDatabase(db, enumerations);

                    // if the database exists
                    if (database != null)
                    {
                        // If the database has Serializable selected
                        if (db.Serializable)
                        {
                            // Serialize each table
                            foreach (DataTable table in database.Tables)
                            {
                                // Set serializable on the table
                                table.Serializable = true;
                            }
                        }

                        // if there are one or more tables in the current Project
                        if (ListHelper.HasOneOrMoreItems(CurrentProject.Tables))
                        {
                            // iterate the tables
                            foreach (DTNTable table in CurrentProject.Tables)
                            {
                                // attempt to find this table in this database
                                DataTable dataTable = database.Tables.FirstOrDefault(x => x.Name == table.TableName);

                                // If the dataTable object exists
                                if (NullHelper.Exists(dataTable))
                                {
                                    // Store the tableId in dataTable so a save performs an update and not a duplicate insert
                                    dataTable.TableId = table.TableId;

                                    // Set the value for Exclude in the dataTable
                                    dataTable.Exclude = table.Exclude;
                                    dataTable.CreateBindingCallback = table.CreateBindingCallback;

                                    // if this table has one or more fields
                                    if (ListHelper.HasOneOrMoreItems(table.Fields))
                                    {
                                        // iterate the fields
                                        foreach (DTNField field in table.Fields)
                                        {
                                            // We now must find the field in this DataTable
                                            DataField dataField = dataTable.Fields.FirstOrDefault(x => x.FieldName == field.FieldName);

                                            // If the dataField object exists
                                            if (NullHelper.Exists(dataField))
                                            {
                                                // Store the FieldId, which is not really mapped, but when the field is converted
                                                // back to a DTNField, this FieldId is converted if present.
                                                dataField.FieldId = field.FieldId;

                                                // set Exclude
                                                dataField.Exclude = field.Exclude;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        // Add this database
                        this.DataManager.Databases.Add(database);
                    }
                }

                // now we must recreate the currentProject.Tables to match the database
                if ((DataManager != null) && (DataManager.Databases != null) && (DataManager.Databases.Count > 0) && (DataManager.Databases[0].Tables.Count > 0))
                {
                    // create the currentTables
                    List <DTNTable> currentTables = new List <DTNTable>();

                    // iterate the databases
                    foreach (Database db in DataManager.Databases)
                    {
                        // attempt to find the database
                        DTNDatabase dtnDatabase = currentProject.Databases.FirstOrDefault(x => x.DatabaseName == db.Name);

                        // If the dtnDatabase object exists
                        if (NullHelper.Exists(dtnDatabase))
                        {
                            // iterate the tables
                            foreach (DataTable table in db.Tables)
                            {
                                // create the table
                                DTNTable dtnTable = DataConverter.ConvertDataTable(table, currentProject, dtnDatabase);

                                // We must check if we have an existing table already with this name that is excluded
                                DTNTable existingTable = CurrentProject.Tables.FirstOrDefault(x => x.TableName == table.Name);

                                // if the table exists
                                if (NullHelper.Exists(dtnTable))
                                {
                                    // if the existingTable was found
                                    if (NullHelper.Exists(existingTable))
                                    {
                                        // Update the value for exclude
                                        dtnTable.Exclude = existingTable.Exclude;
                                        dtnTable.CreateBindingCallback = existingTable.CreateBindingCallback;
                                    }

                                    // Add this table
                                    currentTables.Add(dtnTable);
                                }
                            }
                        }
                    }

                    // Now set the CurrentProject.Tables back
                    currentProject.Tables = currentTables;
                }
            }
        }