/**
  * Parses COLUMN and other DDL within '(' and ')' in CREATE TYPE
  * definition.
  *
  * @param type type being parsed
  * @param line line being processed
  *
  * @throws ParserException Thrown if problem occurred while parsing DDL.
  */
 private static function parse_column_defs($type, $line)
 {
     if (strlen($line) > 0) {
         $matched = false;
         if (!$matched) {
             if (preg_match(self::PATTERN_COLUMN, $line, $matches) > 0) {
                 $column = new pgsql8_column(trim($matches[1]));
                 $column->parse_definition(trim($matches[2]));
                 $type->add_column($column);
                 $matched = true;
             }
         }
         if (!$matched) {
             throw new exception("Cannot parse command: " . $line);
         }
     }
 }
 /**
  * Parses COLUMN and other DDL within '(' and ')' in CREATE TABLE
  * definition.
  *
  * @param table table being parsed
  * @param line line being processed
  */
 private static function parse_column_defs(&$node_schema, &$node_table, $line)
 {
     if (strlen($line) > 0) {
         $matched = false;
         if (preg_match(self::PATTERN_CONSTRAINT, trim($line), $matches) > 0) {
             $node_constraint =& dbx::create_table_constraint($node_table, trim($matches[1]));
             dbx::set_attribute($node_constraint, 'definition', trim($matches[2]));
             //@TODO: type?
             $matched = true;
         }
         if (!$matched) {
             if (preg_match(self::PATTERN_COLUMN, $line, $matches) > 0) {
                 $node_column =& dbx::get_table_column($node_table, trim($matches[1]), true);
                 pgsql8_column::parse_definition($node_schema, $node_table, $node_column, trim($matches[2]));
                 $matched = true;
             }
         }
         if (!$matched) {
             throw new exception("Cannot parse command: " . $line);
         }
     }
 }
Example #3
0
 /**
  * escape a column's value, or return the default value if none specified
  *
  * @return string
  */
 public static function column_value_default($node_schema, $node_table, $data_column_name, $node_col)
 {
     // if marked, make it null or default, depending on column options
     if (isset($node_col['null']) && strcasecmp('true', $node_col['null']) == 0) {
         $value = 'NULL';
     } else {
         if (isset($node_col['empty']) && strcasecmp('true', $node_col['empty']) == 0) {
             if (pgsql8::E_ESCAPE) {
                 $value = "E''";
             } else {
                 $value = "''";
             }
         } else {
             if (isset($node_col['sql']) && strcasecmp($node_col['sql'], 'true') == 0) {
                 if (strcasecmp($node_col, 'default') === 0) {
                     $value = 'DEFAULT';
                 } else {
                     $value = '(' . $node_col . ')';
                 }
             } else {
                 if (strlen($node_col) == 0) {
                     // is there a default defined for the column?
                     $dummy_data_column = new stdClass();
                     $column_default_value = xml_parser::column_default_value($node_table, $data_column_name, $dummy_data_column);
                     if ($column_default_value != NULL) {
                         $value = $column_default_value;
                     } else {
                         $value = 'NULL';
                     }
                 } else {
                     //$node_column = dbx::get_table_column($node_table, $data_column_name);
                     $node_column = xml_parser::inheritance_get_column($node_table, $data_column_name);
                     $node_column = $node_column[0];
                     if ($node_column === NULL) {
                         throw new exception("Failed to find table " . $node_table['name'] . " column " . $data_column_name . " for default value check");
                     }
                     $value_type = pgsql8_column::column_type(dbsteward::$new_database, $node_schema, $node_table, $node_column, $foreign);
                     $value = pgsql8::value_escape($value_type, dbsteward::string_cast($node_col));
                 }
             }
         }
     }
     return $value;
 }
Example #4
0
 /**
  * Generate column defaults from column definitions, returns FALSE if
  * no defaults were defined, otherwise return the
  * ALTER TABLE ALTER COLUMN SET statements needed.
  * 
  * Don't know if this would work with functions referenced as function(argument1 ... argumentN)
  * 
  * @param type $node_schema
  * @param type $node_table
  * @param type $node_column
  * @param type $add_defaults
  * @param type $include_null_definition
  * @param type $include_default_nextval
  * @return boolean|string
  */
 public static function set_column_defaults($node_schema, $node_table, $node_column, $add_defaults, $include_null_definition = true, $include_default_nextval = TRUE)
 {
     $fq_table_name = pgsql8::get_fully_qualified_table_name($node_schema['name'], $node_table['name']);
     $base_sql = "ALTER TABLE " . $fq_table_name . " ALTER COLUMN " . pgsql8::get_quoted_column_name($node_column['name']) . " SET";
     $sql = $base_sql;
     $changes = FALSE;
     if (strlen($node_column['default']) > 0) {
         if (!$include_default_nextval && static::has_default_nextval($node_table, $node_column)) {
             // if the default is a nextval expression, don't specify it in the regular full definition
             // because if the sequence has not been defined yet,
             // the nextval expression will be evaluated inline and fail
             dbsteward::info("Skipping " . $node_column['name'] . " default expression \"" . $node_column['default'] . "\" - this default expression will be applied after all sequences have been created");
             return $changes;
         } else {
             $sql .= " DEFAULT " . $node_column['default'];
             $changes = TRUE;
         }
     } else {
         if (!pgsql8_column::null_allowed($node_table, $node_column) && $add_defaults) {
             $default_col_value = pgsql8_column::get_default_value($node_column['type']);
             if ($default_col_value != null) {
                 $sql .= " DEFAULT " . $default_col_value;
                 $changes = TRUE;
             }
         }
     }
     if ($include_null_definition && !pgsql8_column::null_allowed($node_table, $node_column)) {
         if ($changes) {
             $sql .= ";\n";
             $sql .= $base_sql . " NOT NULL";
         } else {
             $sql .= " NOT NULL";
             $changes = TRUE;
         }
     }
     // no changes? we don't have a default for this column... keep going pls
     if (!$changes) {
         return $changes;
     }
     $sql .= ";\n";
     return $sql;
 }
 public static function get_data_sql($old_schema, $old_table, $new_schema, $new_table, $delete_mode = false)
 {
     $sql = '';
     if ($old_table == null) {
         if (!$delete_mode) {
             // old table doesnt exist, pump inserts
             $new_table_rows = dbx::get_table_rows($new_table);
             if ($new_table_rows) {
                 $new_table_row_columns = preg_split("/[\\,\\s]+/", $new_table_rows['columns'], -1, PREG_SPLIT_NO_EMPTY);
                 foreach ($new_table_rows->row as $data_row) {
                     // is the row marked for delete?
                     if (isset($data_row['delete']) && strcasecmp($data_row['delete'], 'true') == 0) {
                         // don't insert it, we are inserting data that should be there
                     } else {
                         $sql .= self::get_data_row_insert($new_schema, $new_table, $new_table_row_columns, $data_row);
                     }
                 }
             }
             // set serial columns with serialStart defined to that value
             // this is done in get_data_sql to ensure the serial start is set post row insertion
             $sql .= pgsql8_column::get_serial_start_dml($new_schema, $new_table);
         }
     } else {
         // data row match scenarios are based on primary key matching
         $old_table_rows = dbx::get_table_rows($old_table);
         if ($old_table_rows) {
             $old_table_row_columns = preg_split("/[\\,\\s]+/", $old_table_rows['columns'], -1, PREG_SPLIT_NO_EMPTY);
         }
         // is caller asking for deletes or data updates?
         if ($delete_mode) {
             // what old rows have no matches in the new rows? delete them
             if ($old_table_rows) {
                 self::table_data_rows_compare($old_table, $new_table, false, $old_rows, $new_rows, $changes);
                 $count_old_rows = count($old_rows);
                 for ($i = 0; $i < $count_old_rows; $i++) {
                     self::get_data_row_delete($old_schema, $old_table, $old_table_row_columns, $old_rows[$i], $sql_append);
                     //@REVISIT
                     $sql .= $sql_append;
                 }
             }
         } else {
             $new_table_rows = dbx::get_table_rows($new_table);
             if ($new_table_rows) {
                 $new_table_row_columns = preg_split("/[\\,\\s]+/", $new_table_rows['columns'], -1, PREG_SPLIT_NO_EMPTY);
             }
             // what columns in matching rows between old and new are different?
             if ($old_table_rows && $new_table_rows) {
                 $new_table_primary_keys = preg_split("/[\\,\\s]+/", $new_table['primaryKey'], -1, PREG_SPLIT_NO_EMPTY);
                 self::table_data_rows_compare($old_table, $new_table, true, $old_rows, $new_rows, $changes);
                 $count_old_rows = count($old_rows);
                 for ($i = 0; $i < $count_old_rows; $i++) {
                     $new_data_row = null;
                     $changed_columns = null;
                     if (count($changes[$i]) > 0) {
                         // changes were found between primary key matched old_table_row and new_table_row
                         // get the sql to make that happen
                         $sql .= self::get_data_row_update($new_schema, $new_table, $old_table_row_columns, $old_rows[$i], $new_table_row_columns, $new_rows[$i], $changes[$i]);
                     }
                 }
             }
             // what new rows are missing from the old? insert them
             if ($new_table_rows) {
                 self::table_data_rows_compare($new_table, $old_table, false, $new_rows, $old_rows, $changes);
                 $count_new_rows = count($new_rows);
                 for ($i = 0; $i < $count_new_rows; $i++) {
                     $sql .= self::get_data_row_insert($new_schema, $new_table, $new_table_row_columns, $new_rows[$i]);
                 }
             }
         }
     }
     return $sql;
 }
 public static function null_allowed($node_table, $node_column)
 {
     $null_allowed = parent::null_allowed($node_table, $node_column);
     if ($null_allowed) {
         // if the column is in the primary_key list, make it NOT NULL anyway
         // mssql will not implicitly make the column NOT NULL like postgresql
         $primary_keys = pgsql8_table::primary_key_columns($node_table);
         if (in_array((string) $node_column['name'], $primary_keys)) {
             $null_allowed = FALSE;
         }
     }
     return $null_allowed;
 }