/**
  * 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();
 }
Exemplo n.º 2
0
 /**
  * Create temp table for getting column type information for audit columns.
  *
  * @param string  $schemaName   The name of the table schema.
  * @param string  $tableName    The table name.
  * @param array[] $auditColumns Audit columns from config file.
  */
 public static function createTemporaryTable($schemaName, $tableName, $auditColumns)
 {
     $sql = new MySqlCompoundSyntaxCodeStore();
     $sql->append(sprintf('create table `%s`.`%s` (', $schemaName, $tableName));
     foreach ($auditColumns as $column) {
         $sql->append(sprintf('%s %s', $column['column_name'], $column['column_type']));
         if (end($auditColumns) !== $column) {
             $sql->appendToLastLine(',');
         }
     }
     $sql->append(')');
     self::executeNone($sql->getCode());
 }