Ejemplo n.º 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
     $limit_to_tables_count = count(dbsteward::$limit_to_tables);
     foreach ($tables as $dep_table) {
         $schema = $dep_table['schema'];
         $table = $dep_table['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(mysql5_diff_tables::get_data_sql(NULL, NULL, $schema, $table, FALSE));
         $table_primary_keys = mysql5_table::primary_key_columns($table);
         $table_column_names = dbx::to_array($table->column, 'name');
         $node_rows =& dbx::get_table_rows($table);
         // the <rows> element
         $data_column_names = preg_split("/,|\\s/", $node_rows['columns'], -1, PREG_SPLIT_NO_EMPTY);
         // set serial primary keys to the max value after inserts have been performed
         // only if the PRIMARY KEY is not a multi column
         if (count($table_primary_keys) == 1 && in_array($table_primary_keys[0], $data_column_names)) {
             $pk_column_name = $table_primary_keys[0];
             $node_pk_column = dbx::get_table_column($table, $pk_column_name);
             if ($node_pk_column == NULL) {
                 throw new exception("Failed to find primary key column '" . $pk_column_name . "' for " . $schema['name'] . "." . $table['name']);
             }
             // only set the pkey to MAX() if the primary key column is also a serial/bigserial and if serialStart is not defined
             if (mysql5_column::is_serial($node_pk_column['type']) && !isset($node_pk_column['serialStart'])) {
                 $fqtn = mysql5::get_fully_qualified_table_name($schema['name'], $table['name']);
                 $qcol = mysql5::get_quoted_column_name($pk_column_name);
                 $setval = mysql5_sequence::get_setval_call(mysql5_column::get_serial_sequence_name($schema, $table, $node_pk_column), "MAX({$qcol})", "TRUE");
                 $sql = "SELECT {$setval} FROM {$fqtn};\n";
                 $ofs->write($sql);
             }
         }
         // unlike the pg class, we cannot just set identity column start values here with setval without inserting a row
         // check if primary key is a column of this table - FS#17481
         if (count(array_diff($table_primary_keys, $table_column_names)) != 0) {
             throw new exception('Primary key ' . $table['primaryKey'] . ' does not exist as a column in table ' . $table['name']);
         }
     }
     // include all of the unstaged sql elements
     dbx::build_staged_sql($db_doc, $ofs, NULL);
     $ofs->write("\n");
 }
Ejemplo n.º 2
0
 /**
  * Creates and returns SQL command for dropping the sequence.
  *
  * @return string
  */
 public static function get_drop_sql($node_schema, $node_sequences)
 {
     $table_name = mysql5::get_quoted_table_name(self::TABLE_NAME);
     $seq_col = mysql5::get_quoted_column_name(self::SEQ_COL);
     $sequences = dbx::to_array($node_sequences);
     if (count($sequences) === 0) {
         return '';
     }
     $sequence_names = "('" . implode("', '", array_map(function ($n) {
         if ($n instanceof SimpleXMLElement) {
             return $n['name'];
         }
         return $n;
     }, $sequences)) . "')";
     return "DELETE FROM {$table_name} WHERE {$seq_col} IN {$sequence_names};";
 }
Ejemplo n.º 3
0
 public static function get_enum_type_declaration($node_type)
 {
     return "ENUM('" . implode("','", dbx::to_array($node_type->enum, 'name')) . "')";
 }