/**
  * Returns the format specifier for printing column names and column types
  *
  * @return string
  */
 private function getFormat()
 {
     $width = 0;
     foreach ($this->columns->getColumns() as $column) {
         $width = max($width, mb_strlen($column->getProperty('column_name')));
     }
     return sprintf('  %%-%ds %%s', $width + 2);
 }
 /**
  * Returns a SQL statement for adding new columns to the audit table.
  *
  * @return string
  */
 public function buildStatement()
 {
     $code = new MySqlCompoundSyntaxCodeStore();
     $code->append(sprintf('alter table `%s`.`%s`', $this->auditSchemaName, $this->tableName));
     /** @var ColumnMetadata $column */
     foreach ($this->columns->getColumns() as $column) {
         $code->append(sprintf('  add `%s` %s', $column->getProperty('column_name'), $column->getProperty('column_type')), false);
         $after = $column->getProperty('after');
         if (isset($after)) {
             $code->appendToLastLine(sprintf(' after `%s`', $after));
         } else {
             $code->appendToLastLine(' first');
         }
         $columns = $this->columns->getColumns();
         if (end($columns) !== $column) {
             $code->appendToLastLine(',');
         }
     }
     return $code->getCode();
 }
 /**
  * Adds the "values" part of an insert SQL statement to SQL code for a trigger.
  *
  * @param string $rowState The row state (i.e. OLD or NEW).
  */
 private function createInsertStatementValues($rowState)
 {
     $values = '';
     // First the values for the audit columns.
     foreach ($this->auditColumns->getColumns() as $column) {
         $column = $column->getProperties();
         if ($values) {
             $values .= ',';
         }
         switch (true) {
             case isset($column['value_type']):
                 switch ($column['value_type']) {
                     case 'ACTION':
                         $values .= StaticDataLayer::quoteString($this->triggerAction);
                         break;
                     case 'STATE':
                         $values .= StaticDataLayer::quoteString($rowState);
                         break;
                     default:
                         throw new FallenException('value_type', $column['value_type']);
                 }
                 break;
             case isset($column['expression']):
                 $values .= $column['expression'];
                 break;
             default:
                 throw new RuntimeException('None of value_type and expression are set.');
         }
     }
     // Second the values for the audit columns.
     foreach ($this->tableColumns->getColumns() as $column) {
         if ($values) {
             $values .= ',';
         }
         $values .= sprintf('%s.`%s`', $rowState, $column->getProperty('column_name'));
     }
     $this->code->append(sprintf('values(%s);', $values));
 }
Beispiel #4
0
 /**
  * Sets the columns metadata of a table in the configuration file.
  *
  * @param string               $tableName The name of table.
  * @param TableColumnsMetadata $columns   The metadata of the table columns.
  */
 public function setConfigTableColumns($tableName, $columns)
 {
     $newColumns = [];
     foreach ($columns->getColumns() as $column) {
         $newColumns[] = $column->getProperties();
     }
     $this->configMetadata['table_columns'][$tableName] = $newColumns;
 }
 /**
  * Compares two sets of metadata of table columns and returns a set of metadata of table columns that are in the first
  * sets of metadata of table columns but not in the second sets of metadata of table columns.
  *
  * @param TableColumnsMetadata $columns1 The first sets of metadata of table columns.
  * @param TableColumnsMetadata $columns2 The second sets of metadata of table columns.
  *
  * @return TableColumnsMetadata
  */
 public static function notInOtherSet($columns1, $columns2)
 {
     $diff = new TableColumnsMetadata();
     foreach ($columns1->columns as $column_name => $column1) {
         if (!isset($columns2->columns[$column_name])) {
             $diff->appendTableColumn($column1);
         }
     }
     return $diff;
 }
Beispiel #6
0
 /**
  * Logs info about new, obsolete, and altered columns.
  *
  * @param TableColumnsMetadata $newColumns      The metadata of the new columns.
  * @param TableColumnsMetadata $obsoleteColumns The metadata of the obsolete columns.
  * @param TableColumnsMetadata $alteredColumns  The metadata of the altered columns.
  */
 private function logColumnInfo($newColumns, $obsoleteColumns, $alteredColumns)
 {
     foreach ($newColumns->getColumns() as $column) {
         $this->io->logInfo('New column <dbo>%s.%s</dbo>', $this->configTable->getTableName(), $column->getProperty('column_name'));
     }
     foreach ($obsoleteColumns->getColumns() as $column) {
         $this->io->logInfo('Obsolete column <dbo>%s.%s</dbo>', $this->configTable->getTableName(), $column->getProperty('column_name'));
     }
     foreach ($alteredColumns->getColumns() as $column) {
         $this->io->logInfo('Type of <dbo>%s.%s</dbo> has been altered to <dbo>%s</dbo>', $this->configTable->getTableName(), $column->getProperty('column_name'), $column->getProperty('column_type'));
     }
 }