Пример #1
0
 /**
  * determine SQL clause expression to match for data_row primary keys
  *
  * @return string
  */
 public static function primary_key_expression($db_doc, $node_schema, $node_table, $data_row_columns, $data_row)
 {
     $primary_keys = format_table::primary_key_columns($node_table);
     $primary_key_index = xml_parser::data_row_overlay_primary_key_index($primary_keys, $data_row_columns, $data_row_columns);
     $primary_key_index = $primary_key_index['base'];
     // figure out the primary key expression
     $primary_key_expression = array();
     foreach ($primary_keys as $primary_column_name) {
         if (!isset($primary_key_index[$primary_column_name])) {
             throw new exception("primary key column named {$primary_column_name} not found in primary_key_index");
         }
         $column_index = $primary_key_index[$primary_column_name];
         // get the type of the column, chasing foreign keys if necessary
         $node_column = dbx::get_table_column($node_table, $primary_column_name);
         $value_type = format_column::column_type($db_doc, $node_schema, $node_table, $node_column, $foreign);
         $primary_key_expression[] = format::get_quoted_column_name($primary_column_name) . ' = ' . format::value_escape($value_type, $data_row->col[$column_index]);
     }
     if (count($primary_key_expression) == 0) {
         throw new exception($node_table['name'] . " primary_key_expression is empty, determinate loop failed");
     }
     return implode(' AND ', $primary_key_expression);
 }
Пример #2
0
 protected static function get_data_row_update($node_schema, $node_table, $old_data_row_columns, $old_data_row, $new_data_row_columns, $new_data_row, $changed_columns)
 {
     if (count($changed_columns) == 0) {
         throw new exception("empty changed_columns passed");
     }
     // what columns from new_data_row are different in old_data_row?
     // those are the ones to push through the update statement to make the database current
     $old_columns = array();
     $update_columns = array();
     foreach ($changed_columns as $changed_column) {
         if (!isset($changed_column['old_col'])) {
             $old_columns[] = 'NOTDEFINED';
         } else {
             $old_col_value = format::column_value_default($node_schema, $node_table, $changed_column['name'], $changed_column['old_col']);
             $old_columns[] = $changed_column['name'] . ' = ' . $old_col_value;
         }
         $update_col_name = format::get_quoted_column_name($changed_column['name']);
         $update_col_value = format::column_value_default($node_schema, $node_table, $changed_column['name'], $changed_column['new_col']);
         $update_columns[] = $update_col_name . ' = ' . $update_col_value;
     }
     // if the computed update_columns expression is < 5 chars, complain
     // if ( strlen($update_columns) < 5 ) {
     //   var_dump($update_columns);
     //   throw new exception(sprintf("%s.%s update_columns is < 5 chars, unexpected", $node_schema['name'], $node_table['name']));
     // }
     $old_columns = implode(', ', $old_columns);
     $update_columns = implode(', ', $update_columns);
     // use multiline comments here, so when data has newlines they can be preserved, but upgrade scripts don't catch on fire
     $sql = sprintf("UPDATE %s SET %s WHERE (%s); /* old values: %s */\n", format::get_fully_qualified_table_name($node_schema['name'], $node_table['name']), $update_columns, dbx::primary_key_expression(dbsteward::$new_database, $node_schema, $node_table, $new_data_row_columns, $new_data_row), $old_columns);
     return $sql;
 }
Пример #3
0
 public static function get_foreign_key_reference_sql($foreign)
 {
     return format::get_fully_qualified_table_name($foreign['schema']['name'], $foreign['table']['name']) . ' (' . format::get_quoted_column_name($foreign['column']['name']) . ')';
 }