예제 #1
0
 public static function build_data($db_doc, $ofs, $tables)
 {
     // use the dependency order to then write out the actual data inserts into the data sql file
     $tables_count = count($tables);
     $limit_to_tables_count = count(dbsteward::$limit_to_tables);
     for ($i = 0; $i < $tables_count; $i++) {
         $schema = $tables[$i]['schema'];
         $table = $tables[$i]['table'];
         if ($table['name'] === dbsteward::TABLE_DEPENDENCY_IGNORABLE_NAME) {
             // don't do anything with this table, it is a magic internal DBSteward value
             continue;
         }
         if ($limit_to_tables_count > 0) {
             if (in_array($schema['name'], array_keys(dbsteward::$limit_to_tables))) {
                 if (in_array($table['name'], dbsteward::$limit_to_tables[(string) $schema['name']])) {
                     // table is to be included
                 } else {
                     continue;
                 }
             } else {
                 continue;
             }
         }
         $ofs->write(pgsql8_diff_tables::get_data_sql(NULL, NULL, $schema, $table, FALSE));
         // set serial primary keys to the max value after inserts have been performed
         // only if the PRIMARY KEY is not a multi column
         $node_rows =& dbx::get_table_rows($table);
         $columns = preg_split("/,|\\s/", $node_rows['columns'], -1, PREG_SPLIT_NO_EMPTY);
         if (isset($table['primaryKey']) && strlen($table['primaryKey']) > 0 && in_array(dbsteward::string_cast($table['primaryKey']), $columns)) {
             $pk_column = dbsteward::string_cast($table['primaryKey']);
             // only do it if the primary key column is also a serial/bigserial
             $nodes = xml_parser::inheritance_get_column($table, $pk_column);
             if (count($nodes) != 1) {
                 var_dump($nodes);
                 throw new exception("Failed to find primary key column '" . $pk_column . "' for " . $schema['name'] . "." . $table['name']);
             }
             $pk = $nodes[0];
             $pk_column_type = strtolower(dbsteward::string_cast($pk['type']));
             if (preg_match(pgsql8::PATTERN_TABLE_LINKED_TYPES, $pk_column_type) > 0) {
                 // only set the pkey to MAX() if serialStart is not defined
                 if (!isset($pk['serialStart'])) {
                     $sql = "SELECT setval(pg_get_serial_sequence('" . $schema['name'] . "." . $table['name'] . "', '" . $pk_column . "'), MAX({$pk_column}), TRUE) FROM " . $schema['name'] . "." . $table['name'] . ";\n";
                     $ofs->write($sql);
                 }
             }
         }
         // check if primary key is a column of this table - FS#17481
         $primary_keys_exist = self::primary_key_split($table['primaryKey']);
         foreach ($table->column as $column) {
             // while looping through columns, check to see if primary key is one of them
             // if it is remove it from the primary keys array, at the end of loop array should be empty
             $key = array_search($column['name'], $primary_keys_exist);
             if (is_numeric($key)) {
                 unset($primary_keys_exist[$key]);
             }
         }
         // throw an error if the table is using a primaryKey column that does not actually exist
         if (!empty($primary_keys_exist)) {
             if (empty($table['inheritsTable'])) {
                 throw new exception('Primary key ' . $table['primaryKey'] . ' does not exist as a column in table ' . $table['name']);
             } else {
                 dbsteward::info('Primary key ' . $table['primaryKey'] . ' does not exist as a column in child table ' . $table['name'] . ', but may exist in parent table');
             }
         }
     }
     // include all of the unstaged sql elements
     dbx::build_staged_sql($db_doc, $ofs, NULL);
     $ofs->write("\n");
 }