예제 #1
0
 /**
  * Generate body of stored procedure.
  *
  * @param array[] $columns Columns from table.
  * @param array[] $params  Params for where block.
  */
 private function generateBodyPart($params, $columns)
 {
     $lines = [];
     $this->modifiesPart($this->checkAutoIncrement($columns), $lines);
     $lines[] = 'begin';
     $this->bodyPart($params, $columns, $lines);
     $lines[] = 'end';
     $this->storedProcedureCode->append($lines, false);
 }
예제 #2
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();
 }
예제 #4
0
 /**
  * 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));
 }
예제 #5
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();
 }
예제 #6
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());
 }