Exemplo n.º 1
0
 /**
  * Returns a SQL statement for creating the audit table.
  *
  * @return string
  */
 public function buildStatement()
 {
     $code = new MySqlCompoundSyntaxCodeStore();
     $code->append(sprintf('create table `%s`.`%s`', $this->auditSchemaName, $this->tableName));
     // Create SQL for columns.
     $code->append('(');
     $code->append($this->getColumnDefinitions());
     // Create SQL for table options.
     $tableOptions = AuditDataLayer::getTableOptions($this->dataSchemaName, $this->tableName);
     $code->append(sprintf(') engine=%s character set=%s collate=%s', $tableOptions['engine'], $tableOptions['character_set_name'], $tableOptions['table_collation']));
     return $code->getCode();
 }
 /**
  * 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.º 3
0
 /**
  * Generate code for stored procedure.
  *
  * @param string $tableName The table name.
  * @param string $spType    Stored procedure type {insert|update|delete|select}.
  * @param string $spName    Stored procedure name.
  *
  * @return string
  */
 private function generateSP($tableName, $spType, $spName)
 {
     $this->storedProcedureCode = new MySqlCompoundSyntaxCodeStore();
     switch ($spType) {
         case 'UPDATE':
             $routine = new UpdateRoutine($this->input, $this->output, $this->helper, $spType, $spName, $tableName, $this->dataSchema);
             $this->storedProcedureCode->append($routine->getCode(), false);
             break;
         case 'DELETE':
             $routine = new DeleteRoutine($this->input, $this->output, $this->helper, $spType, $spName, $tableName, $this->dataSchema);
             $this->storedProcedureCode->append($routine->getCode(), false);
             break;
         case 'SELECT':
             $routine = new SelectRoutine($this->input, $this->output, $this->helper, $spType, $spName, $tableName, $this->dataSchema);
             $this->storedProcedureCode->append($routine->getCode(), false);
             break;
         case 'INSERT':
             $routine = new InsertRoutine($this->input, $this->output, $this->helper, $spType, $spName, $tableName, $this->dataSchema);
             $this->storedProcedureCode->append($routine->getCode(), false);
             break;
     }
     return $this->storedProcedureCode->getCode();
 }
Exemplo n.º 4
0
 /**
  * Returns the SQL code for creating an audit trigger.
  *
  * @throws FallenException
  */
 public function buildStatement()
 {
     $this->code = new MySqlCompoundSyntaxCodeStore();
     $rowState = [];
     switch ($this->triggerAction) {
         case 'INSERT':
             $rowState[] = 'NEW';
             break;
         case 'DELETE':
             $rowState[] = 'OLD';
             break;
         case 'UPDATE':
             $rowState[] = 'OLD';
             $rowState[] = 'NEW';
             break;
         default:
             throw new FallenException('action', $this->triggerAction);
     }
     $this->code->append(sprintf('create trigger `%s`.`%s`', $this->dataSchemaName, $this->triggerName));
     $this->code->append(sprintf('after %s on `%s`.`%s`', strtolower($this->triggerAction), $this->dataSchemaName, $this->tableName));
     $this->code->append('for each row');
     $this->code->append('begin');
     if ($this->skipVariable !== null) {
         $this->code->append(sprintf('if (%s is null) then', $this->skipVariable));
     }
     $this->code->append($this->additionalSql);
     $this->createInsertStatement($rowState[0]);
     if (sizeof($rowState) == 2) {
         $this->createInsertStatement($rowState[1]);
     }
     if ($this->skipVariable !== null) {
         $this->code->append('end if;');
     }
     $this->code->append('end');
     return $this->code->getCode();
 }
Exemplo n.º 5
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());
 }
Exemplo n.º 6
0
 /**
  * Get code.
  *
  * @return string
  */
 public function getCode()
 {
     return $this->storedProcedureCode->getCode();
 }