@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | * |
| 54 | 54 | * @param array[] $columns The metadata of the columns of a table. |
| 55 | 55 | * |
| 56 | - * @return array[] |
|
| 56 | + * @return \array[] |
|
| 57 | 57 | */ |
| 58 | 58 | private static function removeMatchingColumns($columns) |
| 59 | 59 | { |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | * @param Columns $dataColumns The table columns from data schema. |
| 146 | 146 | * @param Columns $auditColumns The table columns from audit schema. |
| 147 | 147 | * |
| 148 | - * @return \array[] |
|
| 148 | + * @return ColumnTypesExtended |
|
| 149 | 149 | */ |
| 150 | 150 | private function createDiffArray($dataColumns, $auditColumns) |
| 151 | 151 | { |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | /** |
| 183 | 183 | * Add not null to audit columns if it not nullable. |
| 184 | 184 | * |
| 185 | - * @param array $theColumns Audit columns. |
|
| 185 | + * @param \array[] $theColumns Audit columns. |
|
| 186 | 186 | * |
| 187 | 187 | * @return array |
| 188 | 188 | */ |
@@ -23,89 +23,89 @@ discard block |
||
| 23 | 23 | */ |
| 24 | 24 | class DiffCommand extends AuditCommand |
| 25 | 25 | { |
| 26 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 27 | - /** |
|
| 28 | - * Array with columns for each table. |
|
| 29 | - * array [ |
|
| 30 | - * table_name [ |
|
| 31 | - * column [ |
|
| 32 | - * data table type, |
|
| 33 | - * audit table type |
|
| 34 | - * ], |
|
| 35 | - * ... |
|
| 36 | - * ] |
|
| 37 | - * ] |
|
| 38 | - * |
|
| 39 | - * @var array[] |
|
| 40 | - */ |
|
| 41 | - private $diffColumns; |
|
| 26 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 27 | + /** |
|
| 28 | + * Array with columns for each table. |
|
| 29 | + * array [ |
|
| 30 | + * table_name [ |
|
| 31 | + * column [ |
|
| 32 | + * data table type, |
|
| 33 | + * audit table type |
|
| 34 | + * ], |
|
| 35 | + * ... |
|
| 36 | + * ] |
|
| 37 | + * ] |
|
| 38 | + * |
|
| 39 | + * @var array[] |
|
| 40 | + */ |
|
| 41 | + private $diffColumns; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * If set all tables and columns are shown. |
|
| 45 | - * |
|
| 46 | - * @var boolean |
|
| 47 | - */ |
|
| 48 | - private $full; |
|
| 43 | + /** |
|
| 44 | + * If set all tables and columns are shown. |
|
| 45 | + * |
|
| 46 | + * @var boolean |
|
| 47 | + */ |
|
| 48 | + private $full; |
|
| 49 | 49 | |
| 50 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 51 | - /** |
|
| 52 | - * Check full full and return array without new or obsolete columns if full not set. |
|
| 53 | - * |
|
| 54 | - * @param array[] $columns The metadata of the columns of a table. |
|
| 55 | - * |
|
| 56 | - * @return array[] |
|
| 57 | - */ |
|
| 58 | - private static function removeMatchingColumns($columns) |
|
| 59 | - { |
|
| 50 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 51 | + /** |
|
| 52 | + * Check full full and return array without new or obsolete columns if full not set. |
|
| 53 | + * |
|
| 54 | + * @param array[] $columns The metadata of the columns of a table. |
|
| 55 | + * |
|
| 56 | + * @return array[] |
|
| 57 | + */ |
|
| 58 | + private static function removeMatchingColumns($columns) |
|
| 59 | + { |
|
| 60 | 60 | $cleaned = []; |
| 61 | 61 | /** @var ColumnTypesHelper $column */ |
| 62 | 62 | foreach ($columns as $column) |
| 63 | 63 | { |
| 64 | - $columnsArray = $column->getTypes(); |
|
| 65 | - if (!isset($columnsArray['data_column_type'])) |
|
| 66 | - { |
|
| 64 | + $columnsArray = $column->getTypes(); |
|
| 65 | + if (!isset($columnsArray['data_column_type'])) |
|
| 66 | + { |
|
| 67 | 67 | if ($columnsArray['audit_column_type']!=$columnsArray['config_column_type']) |
| 68 | 68 | { |
| 69 | - $cleaned[] = $column; |
|
| 69 | + $cleaned[] = $column; |
|
| 70 | 70 | } |
| 71 | - } |
|
| 72 | - elseif (!isset($columnsArray['config_column_type'])) |
|
| 73 | - { |
|
| 71 | + } |
|
| 72 | + elseif (!isset($columnsArray['config_column_type'])) |
|
| 73 | + { |
|
| 74 | 74 | if (($columnsArray['audit_column_type']!=$columnsArray['data_column_type']) || ($columnsArray['audit_character_set_name']!=$columnsArray['data_character_set_name'] || $columnsArray['audit_collation_name']!=$columnsArray['data_collation_name'])) |
| 75 | 75 | { |
| 76 | - $cleaned[] = $column; |
|
| 76 | + $cleaned[] = $column; |
|
| 77 | 77 | } |
| 78 | - } |
|
| 79 | - else |
|
| 80 | - { |
|
| 78 | + } |
|
| 79 | + else |
|
| 80 | + { |
|
| 81 | 81 | if (($columnsArray['data_column_type']!=$columnsArray['audit_column_type'] && $columnsArray['audit_column_type']!=$columnsArray['config_column_type']) || ($columnsArray['audit_column_type']!=$columnsArray['config_column_type'] && !empty($columnsArray['config_column_type']))) |
| 82 | 82 | { |
| 83 | - $cleaned[] = $column; |
|
| 83 | + $cleaned[] = $column; |
|
| 84 | + } |
|
| 84 | 85 | } |
| 85 | - } |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | return $cleaned; |
| 89 | - } |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 92 | - /** |
|
| 93 | - * {@inheritdoc} |
|
| 94 | - */ |
|
| 95 | - protected function configure() |
|
| 96 | - { |
|
| 91 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 92 | + /** |
|
| 93 | + * {@inheritdoc} |
|
| 94 | + */ |
|
| 95 | + protected function configure() |
|
| 96 | + { |
|
| 97 | 97 | $this->setName('diff') |
| 98 | - ->setDescription('Compares data tables and audit tables') |
|
| 99 | - ->addArgument('config file', InputArgument::OPTIONAL, 'The audit configuration file', 'etc/audit.json') |
|
| 100 | - ->addOption('full', 'f', InputOption::VALUE_NONE, 'Show all columns'); |
|
| 101 | - } |
|
| 98 | + ->setDescription('Compares data tables and audit tables') |
|
| 99 | + ->addArgument('config file', InputArgument::OPTIONAL, 'The audit configuration file', 'etc/audit.json') |
|
| 100 | + ->addOption('full', 'f', InputOption::VALUE_NONE, 'Show all columns'); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 104 | - /** |
|
| 105 | - * {@inheritdoc} |
|
| 106 | - */ |
|
| 107 | - protected function execute(InputInterface $input, OutputInterface $output) |
|
| 108 | - { |
|
| 103 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 104 | + /** |
|
| 105 | + * {@inheritdoc} |
|
| 106 | + */ |
|
| 107 | + protected function execute(InputInterface $input, OutputInterface $output) |
|
| 108 | + { |
|
| 109 | 109 | $this->io = new StratumStyle($input, $output); |
| 110 | 110 | // Style for column names with miss matched column types. |
| 111 | 111 | $style = new OutputFormatterStyle(null, 'red'); |
@@ -136,152 +136,152 @@ discard block |
||
| 136 | 136 | |
| 137 | 137 | $this->getDiff(); |
| 138 | 138 | $this->printDiff($output); |
| 139 | - } |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 142 | - /** |
|
| 143 | - * Get the difference between data and audit tables. |
|
| 144 | - * |
|
| 145 | - * @param Columns $dataColumns The table columns from data schema. |
|
| 146 | - * @param Columns $auditColumns The table columns from audit schema. |
|
| 147 | - * |
|
| 148 | - * @return \array[] |
|
| 149 | - */ |
|
| 150 | - private function createDiffArray($dataColumns, $auditColumns) |
|
| 151 | - { |
|
| 141 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 142 | + /** |
|
| 143 | + * Get the difference between data and audit tables. |
|
| 144 | + * |
|
| 145 | + * @param Columns $dataColumns The table columns from data schema. |
|
| 146 | + * @param Columns $auditColumns The table columns from audit schema. |
|
| 147 | + * |
|
| 148 | + * @return \array[] |
|
| 149 | + */ |
|
| 150 | + private function createDiffArray($dataColumns, $auditColumns) |
|
| 151 | + { |
|
| 152 | 152 | $diff = new ColumnTypesExtended($this->config['audit_columns'], $auditColumns, $dataColumns); |
| 153 | 153 | |
| 154 | 154 | return $diff; |
| 155 | - } |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 158 | - /** |
|
| 159 | - * Computes the difference between data and audit tables. |
|
| 160 | - */ |
|
| 161 | - private function getDiff() |
|
| 162 | - { |
|
| 157 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 158 | + /** |
|
| 159 | + * Computes the difference between data and audit tables. |
|
| 160 | + */ |
|
| 161 | + private function getDiff() |
|
| 162 | + { |
|
| 163 | 163 | foreach ($this->dataSchemaTables as $table) |
| 164 | 164 | { |
| 165 | - if ($this->config['tables'][$table['table_name']]['audit']) |
|
| 166 | - { |
|
| 165 | + if ($this->config['tables'][$table['table_name']]['audit']) |
|
| 166 | + { |
|
| 167 | 167 | $res = StaticDataLayer::searchInRowSet('table_name', $table['table_name'], $this->auditSchemaTables); |
| 168 | 168 | if (isset($res)) |
| 169 | 169 | { |
| 170 | - $dataColumns = new Columns(DataLayer::getTableColumns($this->config['database']['data_schema'], $table['table_name'])); |
|
| 171 | - $auditColumns = DataLayer::getTableColumns($this->config['database']['audit_schema'], $table['table_name']); |
|
| 172 | - $auditColumns = $this->addNotNull($auditColumns); |
|
| 173 | - $auditColumns = new Columns($auditColumns); |
|
| 170 | + $dataColumns = new Columns(DataLayer::getTableColumns($this->config['database']['data_schema'], $table['table_name'])); |
|
| 171 | + $auditColumns = DataLayer::getTableColumns($this->config['database']['audit_schema'], $table['table_name']); |
|
| 172 | + $auditColumns = $this->addNotNull($auditColumns); |
|
| 173 | + $auditColumns = new Columns($auditColumns); |
|
| 174 | 174 | |
| 175 | - $this->diffColumns[$table['table_name']] = $this->createDiffArray($dataColumns, $auditColumns); |
|
| 175 | + $this->diffColumns[$table['table_name']] = $this->createDiffArray($dataColumns, $auditColumns); |
|
| 176 | 176 | } |
| 177 | - } |
|
| 177 | + } |
|
| 178 | + } |
|
| 178 | 179 | } |
| 179 | - } |
|
| 180 | 180 | |
| 181 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 182 | - /** |
|
| 183 | - * Add not null to audit columns if it not nullable. |
|
| 184 | - * |
|
| 185 | - * @param array $theColumns Audit columns. |
|
| 186 | - * |
|
| 187 | - * @return array |
|
| 188 | - */ |
|
| 189 | - private function addNotNull($theColumns) |
|
| 190 | - { |
|
| 181 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 182 | + /** |
|
| 183 | + * Add not null to audit columns if it not nullable. |
|
| 184 | + * |
|
| 185 | + * @param array $theColumns Audit columns. |
|
| 186 | + * |
|
| 187 | + * @return array |
|
| 188 | + */ |
|
| 189 | + private function addNotNull($theColumns) |
|
| 190 | + { |
|
| 191 | 191 | $modifiedColumns = []; |
| 192 | 192 | foreach ($theColumns as $column) |
| 193 | 193 | { |
| 194 | - $modifiedColumn = $column; |
|
| 195 | - $auditColumn = StaticDataLayer::searchInRowSet('column_name', $modifiedColumn['column_name'], $this->config['audit_columns']); |
|
| 196 | - if (isset($auditColumn)) |
|
| 197 | - { |
|
| 194 | + $modifiedColumn = $column; |
|
| 195 | + $auditColumn = StaticDataLayer::searchInRowSet('column_name', $modifiedColumn['column_name'], $this->config['audit_columns']); |
|
| 196 | + if (isset($auditColumn)) |
|
| 197 | + { |
|
| 198 | 198 | if ($modifiedColumn['is_nullable']==='NO') |
| 199 | 199 | { |
| 200 | - $modifiedColumn['column_type'] = sprintf('%s not null', $modifiedColumn['column_type']); |
|
| 200 | + $modifiedColumn['column_type'] = sprintf('%s not null', $modifiedColumn['column_type']); |
|
| 201 | 201 | } |
| 202 | - } |
|
| 203 | - $modifiedColumns[] = $modifiedColumn; |
|
| 202 | + } |
|
| 203 | + $modifiedColumns[] = $modifiedColumn; |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | return $modifiedColumns; |
| 207 | - } |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 210 | - /** |
|
| 211 | - * Writes the difference between the audit tables and metadata tables to the output. |
|
| 212 | - * |
|
| 213 | - * @param OutputInterface $output The output. |
|
| 214 | - */ |
|
| 215 | - private function diffTables($output) |
|
| 216 | - { |
|
| 209 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 210 | + /** |
|
| 211 | + * Writes the difference between the audit tables and metadata tables to the output. |
|
| 212 | + * |
|
| 213 | + * @param OutputInterface $output The output. |
|
| 214 | + */ |
|
| 215 | + private function diffTables($output) |
|
| 216 | + { |
|
| 217 | 217 | foreach ($this->config['tables'] as $tableName => $table) |
| 218 | 218 | { |
| 219 | - $res = StaticDataLayer::searchInRowSet('table_name', $tableName, $this->auditSchemaTables); |
|
| 220 | - if ($table['audit'] && !isset($res)) |
|
| 221 | - { |
|
| 219 | + $res = StaticDataLayer::searchInRowSet('table_name', $tableName, $this->auditSchemaTables); |
|
| 220 | + if ($table['audit'] && !isset($res)) |
|
| 221 | + { |
|
| 222 | 222 | $output->writeln(sprintf('<miss_table>%s</>', $tableName)); |
| 223 | - } |
|
| 224 | - else if (!$table['audit'] && isset($res)) |
|
| 225 | - { |
|
| 223 | + } |
|
| 224 | + else if (!$table['audit'] && isset($res)) |
|
| 225 | + { |
|
| 226 | 226 | $output->writeln(sprintf('<obsolete_table>%s</>', $tableName)); |
| 227 | - } |
|
| 227 | + } |
|
| 228 | + } |
|
| 228 | 229 | } |
| 229 | - } |
|
| 230 | 230 | |
| 231 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 232 | - /** |
|
| 233 | - * Writes the difference between the audit and data tables to the output. |
|
| 234 | - * |
|
| 235 | - * @param OutputInterface $output The output. |
|
| 236 | - */ |
|
| 237 | - private function printDiff($output) |
|
| 238 | - { |
|
| 231 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 232 | + /** |
|
| 233 | + * Writes the difference between the audit and data tables to the output. |
|
| 234 | + * |
|
| 235 | + * @param OutputInterface $output The output. |
|
| 236 | + */ |
|
| 237 | + private function printDiff($output) |
|
| 238 | + { |
|
| 239 | 239 | $first = true; |
| 240 | 240 | if (isset($this->diffColumns)) |
| 241 | 241 | { |
| 242 | - /** |
|
| 243 | - * @var ColumnTypesExtended $columns |
|
| 244 | - */ |
|
| 245 | - foreach ($this->diffColumns as $tableName => $columns) |
|
| 246 | - { |
|
| 242 | + /** |
|
| 243 | + * @var ColumnTypesExtended $columns |
|
| 244 | + */ |
|
| 245 | + foreach ($this->diffColumns as $tableName => $columns) |
|
| 246 | + { |
|
| 247 | 247 | $columns = $columns->getTypes(); |
| 248 | 248 | // Remove matching columns unless the full option is used. |
| 249 | 249 | if (!$this->full) |
| 250 | 250 | { |
| 251 | - /** @var array[] $columns */ |
|
| 252 | - $columns = self::removeMatchingColumns($columns); |
|
| 251 | + /** @var array[] $columns */ |
|
| 252 | + $columns = self::removeMatchingColumns($columns); |
|
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | if (!empty($columns)) |
| 256 | 256 | { |
| 257 | - // Add an empty line between tables. |
|
| 258 | - if ($first) |
|
| 259 | - { |
|
| 257 | + // Add an empty line between tables. |
|
| 258 | + if ($first) |
|
| 259 | + { |
|
| 260 | 260 | $first = false; |
| 261 | - } |
|
| 262 | - else |
|
| 263 | - { |
|
| 261 | + } |
|
| 262 | + else |
|
| 263 | + { |
|
| 264 | 264 | $output->writeln(''); |
| 265 | - } |
|
| 265 | + } |
|
| 266 | 266 | |
| 267 | - // Write table name. |
|
| 268 | - $output->writeln($tableName); |
|
| 267 | + // Write table name. |
|
| 268 | + $output->writeln($tableName); |
|
| 269 | 269 | |
| 270 | - // Write table with columns. |
|
| 271 | - $rows = new TableHelper($this->config['database']['data_schema'], $this->config['database']['audit_schema'], $tableName, $this->config['audit_columns'], $this->full); |
|
| 272 | - $rows->appendRows($columns); |
|
| 273 | - $rows->addHighlighting(); |
|
| 274 | - $table = new Table($output); |
|
| 275 | - $table->setHeaders(['column', 'data table', 'audit table', 'config']) |
|
| 270 | + // Write table with columns. |
|
| 271 | + $rows = new TableHelper($this->config['database']['data_schema'], $this->config['database']['audit_schema'], $tableName, $this->config['audit_columns'], $this->full); |
|
| 272 | + $rows->appendRows($columns); |
|
| 273 | + $rows->addHighlighting(); |
|
| 274 | + $table = new Table($output); |
|
| 275 | + $table->setHeaders(['column', 'data table', 'audit table', 'config']) |
|
| 276 | 276 | ->setRows($rows->getRows()); |
| 277 | - $table->render(); |
|
| 277 | + $table->render(); |
|
| 278 | + } |
|
| 278 | 279 | } |
| 279 | - } |
|
| 280 | 280 | } |
| 281 | 281 | $this->diffTables($output); |
| 282 | - } |
|
| 282 | + } |
|
| 283 | 283 | |
| 284 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 284 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 285 | 285 | } |
| 286 | 286 | |
| 287 | 287 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -68,15 +68,13 @@ discard block |
||
| 68 | 68 | { |
| 69 | 69 | $cleaned[] = $column; |
| 70 | 70 | } |
| 71 | - } |
|
| 72 | - elseif (!isset($columnsArray['config_column_type'])) |
|
| 71 | + } elseif (!isset($columnsArray['config_column_type'])) |
|
| 73 | 72 | { |
| 74 | 73 | if (($columnsArray['audit_column_type']!=$columnsArray['data_column_type']) || ($columnsArray['audit_character_set_name']!=$columnsArray['data_character_set_name'] || $columnsArray['audit_collation_name']!=$columnsArray['data_collation_name'])) |
| 75 | 74 | { |
| 76 | 75 | $cleaned[] = $column; |
| 77 | 76 | } |
| 78 | - } |
|
| 79 | - else |
|
| 77 | + } else |
|
| 80 | 78 | { |
| 81 | 79 | if (($columnsArray['data_column_type']!=$columnsArray['audit_column_type'] && $columnsArray['audit_column_type']!=$columnsArray['config_column_type']) || ($columnsArray['audit_column_type']!=$columnsArray['config_column_type'] && !empty($columnsArray['config_column_type']))) |
| 82 | 80 | { |
@@ -220,8 +218,7 @@ discard block |
||
| 220 | 218 | if ($table['audit'] && !isset($res)) |
| 221 | 219 | { |
| 222 | 220 | $output->writeln(sprintf('<miss_table>%s</>', $tableName)); |
| 223 | - } |
|
| 224 | - else if (!$table['audit'] && isset($res)) |
|
| 221 | + } else if (!$table['audit'] && isset($res)) |
|
| 225 | 222 | { |
| 226 | 223 | $output->writeln(sprintf('<obsolete_table>%s</>', $tableName)); |
| 227 | 224 | } |
@@ -258,8 +255,7 @@ discard block |
||
| 258 | 255 | if ($first) |
| 259 | 256 | { |
| 260 | 257 | $first = false; |
| 261 | - } |
|
| 262 | - else |
|
| 258 | + } else |
|
| 263 | 259 | { |
| 264 | 260 | $output->writeln(''); |
| 265 | 261 | } |
@@ -3,7 +3,6 @@ |
||
| 3 | 3 | namespace SetBased\Audit\MySql\Helper; |
| 4 | 4 | |
| 5 | 5 | //---------------------------------------------------------------------------------------------------------------------- |
| 6 | -use SetBased\Audit\MySql\Table\Columns; |
|
| 7 | 6 | use SetBased\Audit\MySQl\Table\ColumnType; |
| 8 | 7 | |
| 9 | 8 | /** |
@@ -11,72 +11,72 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class ColumnTypesHelper |
| 13 | 13 | { |
| 14 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 15 | - /** |
|
| 16 | - * The metadata of the column. |
|
| 17 | - * |
|
| 18 | - * @var array[] |
|
| 19 | - */ |
|
| 20 | - protected $columnTypes = []; |
|
| 14 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 15 | + /** |
|
| 16 | + * The metadata of the column. |
|
| 17 | + * |
|
| 18 | + * @var array[] |
|
| 19 | + */ |
|
| 20 | + protected $columnTypes = []; |
|
| 21 | 21 | |
| 22 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 23 | - /** |
|
| 24 | - * Object constructor. |
|
| 25 | - * |
|
| 26 | - * @param array[]|ColumnType $columnTypes The metadata of the column. |
|
| 27 | - * @param null|string $typePrefix Prefix for column type name. |
|
| 28 | - */ |
|
| 29 | - public function __construct($columnTypes, $typePrefix) |
|
| 30 | - { |
|
| 22 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 23 | + /** |
|
| 24 | + * Object constructor. |
|
| 25 | + * |
|
| 26 | + * @param array[]|ColumnType $columnTypes The metadata of the column. |
|
| 27 | + * @param null|string $typePrefix Prefix for column type name. |
|
| 28 | + */ |
|
| 29 | + public function __construct($columnTypes, $typePrefix) |
|
| 30 | + { |
|
| 31 | 31 | $this->extendColumnTypes($columnTypes, $typePrefix); |
| 32 | - } |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 35 | - /** |
|
| 36 | - * Create array with all columns types. |
|
| 37 | - * |
|
| 38 | - * @param array[]|ColumnType $columnTypes The metadata of the column. |
|
| 39 | - * @param null|string $typePrefix Prefix for column type name. |
|
| 40 | - */ |
|
| 41 | - public function extendColumnTypes($columnTypes, $typePrefix) |
|
| 42 | - { |
|
| 34 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 35 | + /** |
|
| 36 | + * Create array with all columns types. |
|
| 37 | + * |
|
| 38 | + * @param array[]|ColumnType $columnTypes The metadata of the column. |
|
| 39 | + * @param null|string $typePrefix Prefix for column type name. |
|
| 40 | + */ |
|
| 41 | + public function extendColumnTypes($columnTypes, $typePrefix) |
|
| 42 | + { |
|
| 43 | 43 | $columnTypes = $columnTypes->getType(); |
| 44 | 44 | foreach ($columnTypes as $typeName => $typeValue) |
| 45 | 45 | { |
| 46 | - if ($typeName=='column_name') |
|
| 47 | - { |
|
| 46 | + if ($typeName=='column_name') |
|
| 47 | + { |
|
| 48 | 48 | if (!isset($this->columnTypes['column_name'])) |
| 49 | 49 | { |
| 50 | - $this->columnTypes[$typeName] = $typeValue; |
|
| 50 | + $this->columnTypes[$typeName] = $typeValue; |
|
| 51 | + } |
|
| 51 | 52 | } |
| 52 | - } |
|
| 53 | - else |
|
| 54 | - { |
|
| 53 | + else |
|
| 54 | + { |
|
| 55 | 55 | $format = '%s_%s'; |
| 56 | 56 | if (isset($typePrefix)) |
| 57 | 57 | { |
| 58 | - $this->columnTypes[sprintf($format, $typePrefix, $typeName)] = $typeValue; |
|
| 58 | + $this->columnTypes[sprintf($format, $typePrefix, $typeName)] = $typeValue; |
|
| 59 | 59 | } |
| 60 | 60 | else |
| 61 | 61 | { |
| 62 | - $this->columnTypes[$typeName] = $typeValue; |
|
| 62 | + $this->columnTypes[$typeName] = $typeValue; |
|
| 63 | + } |
|
| 63 | 64 | } |
| 64 | - } |
|
| 65 | 65 | } |
| 66 | - } |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 69 | - /** |
|
| 70 | - * Get columns type. |
|
| 71 | - * |
|
| 72 | - * @return \array[] |
|
| 73 | - */ |
|
| 74 | - public function getTypes() |
|
| 75 | - { |
|
| 68 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 69 | + /** |
|
| 70 | + * Get columns type. |
|
| 71 | + * |
|
| 72 | + * @return \array[] |
|
| 73 | + */ |
|
| 74 | + public function getTypes() |
|
| 75 | + { |
|
| 76 | 76 | return $this->columnTypes; |
| 77 | - } |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 79 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | if (isset($columns1->columns[$column2['column_name']])) |
| 75 | 75 | { |
| 76 | 76 | $column1 = $columns1->columns[$column2['column_name']]; |
| 77 | - if ($column2['column_type']!=$column1['column_type']) |
|
| 77 | + if ($column2['column_type'] != $column1['column_type']) |
|
| 78 | 78 | { |
| 79 | 79 | $diff[] = $column1; |
| 80 | 80 | } |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | $columns = array_keys($this->columns); |
| 136 | 136 | $key = array_search($columnName, $columns); |
| 137 | 137 | |
| 138 | - if ($key!==false) |
|
| 138 | + if ($key !== false) |
|
| 139 | 139 | { |
| 140 | 140 | $column = $this->columns[$columns[$key]]; |
| 141 | 141 | $column['character_set_name'] = isset($column['character_set_name']) ? ' '.$column['character_set_name'] : ''; |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | $columns = array_keys($this->columns); |
| 161 | 161 | $key = array_search($columnName, $columns); |
| 162 | 162 | |
| 163 | - if ($key>=1) |
|
| 163 | + if ($key >= 1) |
|
| 164 | 164 | { |
| 165 | 165 | return $columns[$key - 1]; |
| 166 | 166 | } |
@@ -8,188 +8,188 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class Columns |
| 10 | 10 | { |
| 11 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 12 | - /** |
|
| 13 | - * The metadata of the columns. |
|
| 14 | - * |
|
| 15 | - * @var array<string,ColumnType> |
|
| 16 | - */ |
|
| 17 | - private $columns = []; |
|
| 18 | - |
|
| 19 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 20 | - /** |
|
| 21 | - * Object constructor. |
|
| 22 | - * |
|
| 23 | - * @param array[] $columns The metadata of the columns as returned by DataLayer::getTableColumns(). |
|
| 24 | - */ |
|
| 25 | - public function __construct($columns) |
|
| 26 | - { |
|
| 11 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 12 | + /** |
|
| 13 | + * The metadata of the columns. |
|
| 14 | + * |
|
| 15 | + * @var array<string,ColumnType> |
|
| 16 | + */ |
|
| 17 | + private $columns = []; |
|
| 18 | + |
|
| 19 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 20 | + /** |
|
| 21 | + * Object constructor. |
|
| 22 | + * |
|
| 23 | + * @param array[] $columns The metadata of the columns as returned by DataLayer::getTableColumns(). |
|
| 24 | + */ |
|
| 25 | + public function __construct($columns) |
|
| 26 | + { |
|
| 27 | 27 | foreach ($columns as $column) |
| 28 | 28 | { |
| 29 | - if (!is_array($column)) |
|
| 30 | - { |
|
| 29 | + if (!is_array($column)) |
|
| 30 | + { |
|
| 31 | 31 | /** @var ColumnType $column */ |
| 32 | 32 | $column = $column->getType(); |
| 33 | - } |
|
| 34 | - /** @var array $column */ |
|
| 35 | - $this->columns[$column['column_name']] = new ColumnType($column); |
|
| 33 | + } |
|
| 34 | + /** @var array $column */ |
|
| 35 | + $this->columns[$column['column_name']] = new ColumnType($column); |
|
| 36 | + } |
|
| 36 | 37 | } |
| 37 | - } |
|
| 38 | - |
|
| 39 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 40 | - /** |
|
| 41 | - * Generate array with audit columns and columns from data table. |
|
| 42 | - * |
|
| 43 | - * @param Columns $auditColumnsMetadata AuditApplication columns for adding to exist columns |
|
| 44 | - * @param Columns $currentColumnsMetadata Exist table columns |
|
| 45 | - * |
|
| 46 | - * @return Columns |
|
| 47 | - */ |
|
| 48 | - public static function combine($auditColumnsMetadata, $currentColumnsMetadata) |
|
| 49 | - { |
|
| 38 | + |
|
| 39 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 40 | + /** |
|
| 41 | + * Generate array with audit columns and columns from data table. |
|
| 42 | + * |
|
| 43 | + * @param Columns $auditColumnsMetadata AuditApplication columns for adding to exist columns |
|
| 44 | + * @param Columns $currentColumnsMetadata Exist table columns |
|
| 45 | + * |
|
| 46 | + * @return Columns |
|
| 47 | + */ |
|
| 48 | + public static function combine($auditColumnsMetadata, $currentColumnsMetadata) |
|
| 49 | + { |
|
| 50 | 50 | $columns = []; |
| 51 | 51 | |
| 52 | 52 | foreach ($auditColumnsMetadata->columns as $column) |
| 53 | 53 | { |
| 54 | - $columns[] = $column; |
|
| 54 | + $columns[] = $column; |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | foreach ($currentColumnsMetadata->columns as $column) |
| 58 | 58 | { |
| 59 | - $columns[] = $column; |
|
| 59 | + $columns[] = $column; |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | return new Columns($columns); |
| 63 | - } |
|
| 64 | - |
|
| 65 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 66 | - /** |
|
| 67 | - * Compares two Columns objects and returns an array with columns that are in the first columns object and in the |
|
| 68 | - * second Columns object but have different types. |
|
| 69 | - * |
|
| 70 | - * @param Columns $columns1 The first Columns object. |
|
| 71 | - * @param Columns $columns2 The second Columns object. |
|
| 72 | - * |
|
| 73 | - * @return Columns |
|
| 74 | - */ |
|
| 75 | - public static function differentColumnTypes($columns1, $columns2) |
|
| 76 | - { |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 66 | + /** |
|
| 67 | + * Compares two Columns objects and returns an array with columns that are in the first columns object and in the |
|
| 68 | + * second Columns object but have different types. |
|
| 69 | + * |
|
| 70 | + * @param Columns $columns1 The first Columns object. |
|
| 71 | + * @param Columns $columns2 The second Columns object. |
|
| 72 | + * |
|
| 73 | + * @return Columns |
|
| 74 | + */ |
|
| 75 | + public static function differentColumnTypes($columns1, $columns2) |
|
| 76 | + { |
|
| 77 | 77 | $diff = []; |
| 78 | 78 | foreach ($columns2->columns as $column2) |
| 79 | 79 | { |
| 80 | - if (!is_array($column2)) |
|
| 81 | - { |
|
| 80 | + if (!is_array($column2)) |
|
| 81 | + { |
|
| 82 | 82 | /** @var ColumnType $column2 */ |
| 83 | 83 | $column2 = $column2->getType(); |
| 84 | - } |
|
| 85 | - if (isset($columns1->columns[$column2['column_name']])) |
|
| 86 | - { |
|
| 84 | + } |
|
| 85 | + if (isset($columns1->columns[$column2['column_name']])) |
|
| 86 | + { |
|
| 87 | 87 | $column1 = $columns1->columns[$column2['column_name']]; |
| 88 | 88 | if (!is_array($column1)) |
| 89 | 89 | { |
| 90 | - /** @var ColumnType $column1 */ |
|
| 91 | - $column1 = $column1->getType(); |
|
| 90 | + /** @var ColumnType $column1 */ |
|
| 91 | + $column1 = $column1->getType(); |
|
| 92 | 92 | } |
| 93 | 93 | if ($column2['column_type']!=$column1['column_type']) |
| 94 | 94 | { |
| 95 | - $diff[] = $column1; |
|
| 95 | + $diff[] = $column1; |
|
| 96 | + } |
|
| 96 | 97 | } |
| 97 | - } |
|
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | return new Columns($diff); |
| 101 | - } |
|
| 102 | - |
|
| 103 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 104 | - /** |
|
| 105 | - * Compares two Columns objects and returns an array with columns that are in the first columns object but not in the |
|
| 106 | - * second Columns object. |
|
| 107 | - * |
|
| 108 | - * @param Columns $columns1 The first Columns object. |
|
| 109 | - * @param Columns $columns2 The second Columns object. |
|
| 110 | - * |
|
| 111 | - * @return Columns |
|
| 112 | - */ |
|
| 113 | - public static function notInOtherSet($columns1, $columns2) |
|
| 114 | - { |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 104 | + /** |
|
| 105 | + * Compares two Columns objects and returns an array with columns that are in the first columns object but not in the |
|
| 106 | + * second Columns object. |
|
| 107 | + * |
|
| 108 | + * @param Columns $columns1 The first Columns object. |
|
| 109 | + * @param Columns $columns2 The second Columns object. |
|
| 110 | + * |
|
| 111 | + * @return Columns |
|
| 112 | + */ |
|
| 113 | + public static function notInOtherSet($columns1, $columns2) |
|
| 114 | + { |
|
| 115 | 115 | $diff = []; |
| 116 | 116 | if (isset($columns1)) |
| 117 | 117 | { |
| 118 | - foreach ($columns1->columns as $column1) |
|
| 119 | - { |
|
| 118 | + foreach ($columns1->columns as $column1) |
|
| 119 | + { |
|
| 120 | 120 | if (!is_array($column1)) |
| 121 | 121 | { |
| 122 | - /** @var ColumnType $column1 */ |
|
| 123 | - $column1 = $column1->getType(); |
|
| 122 | + /** @var ColumnType $column1 */ |
|
| 123 | + $column1 = $column1->getType(); |
|
| 124 | 124 | } |
| 125 | 125 | if (!isset($columns2->columns[$column1['column_name']])) |
| 126 | 126 | { |
| 127 | - $diff[] = $column1; |
|
| 127 | + $diff[] = $column1; |
|
| 128 | + } |
|
| 128 | 129 | } |
| 129 | - } |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | return new Columns($diff); |
| 133 | - } |
|
| 134 | - |
|
| 135 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 136 | - /** |
|
| 137 | - * Returns the underlying array with metadata of the columns. |
|
| 138 | - * |
|
| 139 | - * @return array[] |
|
| 140 | - */ |
|
| 141 | - public function getColumns() |
|
| 142 | - { |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 136 | + /** |
|
| 137 | + * Returns the underlying array with metadata of the columns. |
|
| 138 | + * |
|
| 139 | + * @return array[] |
|
| 140 | + */ |
|
| 141 | + public function getColumns() |
|
| 142 | + { |
|
| 143 | 143 | return $this->columns; |
| 144 | - } |
|
| 145 | - |
|
| 146 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 147 | - /** |
|
| 148 | - * Return column type with character set and collation. |
|
| 149 | - * |
|
| 150 | - * @param string $columnName The column name. |
|
| 151 | - * |
|
| 152 | - * @return null|string |
|
| 153 | - */ |
|
| 154 | - public function getColumnTypeWithCharSetCollation($columnName) |
|
| 155 | - { |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 147 | + /** |
|
| 148 | + * Return column type with character set and collation. |
|
| 149 | + * |
|
| 150 | + * @param string $columnName The column name. |
|
| 151 | + * |
|
| 152 | + * @return null|string |
|
| 153 | + */ |
|
| 154 | + public function getColumnTypeWithCharSetCollation($columnName) |
|
| 155 | + { |
|
| 156 | 156 | $columns = array_keys($this->columns); |
| 157 | 157 | $key = array_search($columnName, $columns); |
| 158 | 158 | |
| 159 | 159 | if ($key!==false) |
| 160 | 160 | { |
| 161 | - $column = $this->columns[$columns[$key]]; |
|
| 162 | - $column['character_set_name'] = isset($column['character_set_name']) ? ' '.$column['character_set_name'] : ''; |
|
| 163 | - $column['collation_name'] = isset($column['collation_name']) ? ' '.$column['collation_name'] : ''; |
|
| 161 | + $column = $this->columns[$columns[$key]]; |
|
| 162 | + $column['character_set_name'] = isset($column['character_set_name']) ? ' '.$column['character_set_name'] : ''; |
|
| 163 | + $column['collation_name'] = isset($column['collation_name']) ? ' '.$column['collation_name'] : ''; |
|
| 164 | 164 | |
| 165 | - return sprintf('%s%s%s', $column['column_type'], $column['character_set_name'], $column['collation_name']); |
|
| 165 | + return sprintf('%s%s%s', $column['column_type'], $column['character_set_name'], $column['collation_name']); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | return null; |
| 169 | - } |
|
| 170 | - |
|
| 171 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 172 | - /** |
|
| 173 | - * Returns previous column of a columns. Returns null if the column name is not found in this Columns. |
|
| 174 | - * |
|
| 175 | - * @param string $columnName The column name. |
|
| 176 | - * |
|
| 177 | - * @return null|string |
|
| 178 | - */ |
|
| 179 | - public function getPreviousColumn($columnName) |
|
| 180 | - { |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 172 | + /** |
|
| 173 | + * Returns previous column of a columns. Returns null if the column name is not found in this Columns. |
|
| 174 | + * |
|
| 175 | + * @param string $columnName The column name. |
|
| 176 | + * |
|
| 177 | + * @return null|string |
|
| 178 | + */ |
|
| 179 | + public function getPreviousColumn($columnName) |
|
| 180 | + { |
|
| 181 | 181 | $columns = array_keys($this->columns); |
| 182 | 182 | $key = array_search($columnName, $columns); |
| 183 | 183 | |
| 184 | 184 | if ($key>=1) |
| 185 | 185 | { |
| 186 | - return $columns[$key - 1]; |
|
| 186 | + return $columns[$key - 1]; |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | return null; |
| 190 | - } |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 192 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -28,7 +28,7 @@ |
||
| 28 | 28 | |
| 29 | 29 | if (isset($this->columnType['column_type'])) |
| 30 | 30 | { |
| 31 | - if ($this->columnType['column_type']==='timestamp') |
|
| 31 | + if ($this->columnType['column_type'] === 'timestamp') |
|
| 32 | 32 | { |
| 33 | 33 | $this->columnType['column_type'] = $this->columnType['column_type'].' NULL'; |
| 34 | 34 | } |
@@ -8,63 +8,63 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class ColumnType |
| 10 | 10 | { |
| 11 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 12 | - /** |
|
| 13 | - * The metadata of the column. |
|
| 14 | - * |
|
| 15 | - * @var array[] |
|
| 16 | - */ |
|
| 17 | - protected $columnType = []; |
|
| 11 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 12 | + /** |
|
| 13 | + * The metadata of the column. |
|
| 14 | + * |
|
| 15 | + * @var array[] |
|
| 16 | + */ |
|
| 17 | + protected $columnType = []; |
|
| 18 | 18 | |
| 19 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 20 | - /** |
|
| 21 | - * Object constructor. |
|
| 22 | - * |
|
| 23 | - * @param array[]|Columns $columnProperties The metadata of the column. |
|
| 24 | - */ |
|
| 25 | - public function __construct($columnProperties) |
|
| 26 | - { |
|
| 19 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 20 | + /** |
|
| 21 | + * Object constructor. |
|
| 22 | + * |
|
| 23 | + * @param array[]|Columns $columnProperties The metadata of the column. |
|
| 24 | + */ |
|
| 25 | + public function __construct($columnProperties) |
|
| 26 | + { |
|
| 27 | 27 | $this->columnType = $columnProperties; |
| 28 | 28 | |
| 29 | 29 | if (isset($this->columnType['column_type'])) |
| 30 | 30 | { |
| 31 | - if ($this->columnType['column_type']==='timestamp') |
|
| 32 | - { |
|
| 31 | + if ($this->columnType['column_type']==='timestamp') |
|
| 32 | + { |
|
| 33 | 33 | $this->columnType['column_type'] = $this->columnType['column_type'].' NULL'; |
| 34 | - } |
|
| 34 | + } |
|
| 35 | + } |
|
| 35 | 36 | } |
| 36 | - } |
|
| 37 | 37 | |
| 38 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 39 | - /** |
|
| 40 | - * Get columns type. |
|
| 41 | - * |
|
| 42 | - * @return array[] |
|
| 43 | - */ |
|
| 44 | - public function getType() |
|
| 45 | - { |
|
| 38 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 39 | + /** |
|
| 40 | + * Get columns type. |
|
| 41 | + * |
|
| 42 | + * @return array[] |
|
| 43 | + */ |
|
| 44 | + public function getType() |
|
| 45 | + { |
|
| 46 | 46 | return $this->columnType; |
| 47 | - } |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 50 | - /** |
|
| 51 | - * Get column property. |
|
| 52 | - * |
|
| 53 | - * @param string $propertyName The property name. |
|
| 54 | - * |
|
| 55 | - * @return string|null |
|
| 56 | - */ |
|
| 57 | - public function getProperty($propertyName) |
|
| 58 | - { |
|
| 49 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 50 | + /** |
|
| 51 | + * Get column property. |
|
| 52 | + * |
|
| 53 | + * @param string $propertyName The property name. |
|
| 54 | + * |
|
| 55 | + * @return string|null |
|
| 56 | + */ |
|
| 57 | + public function getProperty($propertyName) |
|
| 58 | + { |
|
| 59 | 59 | if (isset($this->columnType[$propertyName])) |
| 60 | 60 | { |
| 61 | - return $this->columnType[$propertyName]; |
|
| 61 | + return $this->columnType[$propertyName]; |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | return null; |
| 65 | - } |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 67 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -16,90 +16,90 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | class DataLayer |
| 18 | 18 | { |
| 19 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 20 | - /** |
|
| 21 | - * The connection to the MySQL instance. |
|
| 22 | - * |
|
| 23 | - * @var StaticDataLayer |
|
| 24 | - */ |
|
| 25 | - private static $dl; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The Output decorator. |
|
| 29 | - * |
|
| 30 | - * @var StratumStyle |
|
| 31 | - */ |
|
| 32 | - private static $io; |
|
| 33 | - |
|
| 34 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 35 | - /** |
|
| 36 | - * Adds new columns to an audit table. |
|
| 37 | - * |
|
| 38 | - * @param string $auditSchemaName The name of audit schema. |
|
| 39 | - * @param string $tableName The name of the table. |
|
| 40 | - * @param \SetBased\Audit\MySql\Table\Columns $columns The metadata of the new columns. |
|
| 41 | - */ |
|
| 42 | - public static function addNewColumns($auditSchemaName, $tableName, $columns) |
|
| 43 | - { |
|
| 19 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 20 | + /** |
|
| 21 | + * The connection to the MySQL instance. |
|
| 22 | + * |
|
| 23 | + * @var StaticDataLayer |
|
| 24 | + */ |
|
| 25 | + private static $dl; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * The Output decorator. |
|
| 29 | + * |
|
| 30 | + * @var StratumStyle |
|
| 31 | + */ |
|
| 32 | + private static $io; |
|
| 33 | + |
|
| 34 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 35 | + /** |
|
| 36 | + * Adds new columns to an audit table. |
|
| 37 | + * |
|
| 38 | + * @param string $auditSchemaName The name of audit schema. |
|
| 39 | + * @param string $tableName The name of the table. |
|
| 40 | + * @param \SetBased\Audit\MySql\Table\Columns $columns The metadata of the new columns. |
|
| 41 | + */ |
|
| 42 | + public static function addNewColumns($auditSchemaName, $tableName, $columns) |
|
| 43 | + { |
|
| 44 | 44 | $helper = new AlterAuditTableAddColumns($auditSchemaName, $tableName, $columns); |
| 45 | 45 | $sql = $helper->buildStatement(); |
| 46 | 46 | |
| 47 | 47 | self::executeNone($sql); |
| 48 | - } |
|
| 49 | - |
|
| 50 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 51 | - /** |
|
| 52 | - * Connects to a MySQL instance. |
|
| 53 | - * |
|
| 54 | - * Wrapper around [mysqli::__construct](http://php.net/manual/mysqli.construct.php), however on failure an exception |
|
| 55 | - * is thrown. |
|
| 56 | - * |
|
| 57 | - * @param string $host The hostname. |
|
| 58 | - * @param string $user The MySQL user name. |
|
| 59 | - * @param string $passWord The password. |
|
| 60 | - * @param string $database The default database. |
|
| 61 | - * @param int $port The port number. |
|
| 62 | - */ |
|
| 63 | - public static function connect($host, $user, $passWord, $database, $port = 3306) |
|
| 64 | - { |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 51 | + /** |
|
| 52 | + * Connects to a MySQL instance. |
|
| 53 | + * |
|
| 54 | + * Wrapper around [mysqli::__construct](http://php.net/manual/mysqli.construct.php), however on failure an exception |
|
| 55 | + * is thrown. |
|
| 56 | + * |
|
| 57 | + * @param string $host The hostname. |
|
| 58 | + * @param string $user The MySQL user name. |
|
| 59 | + * @param string $passWord The password. |
|
| 60 | + * @param string $database The default database. |
|
| 61 | + * @param int $port The port number. |
|
| 62 | + */ |
|
| 63 | + public static function connect($host, $user, $passWord, $database, $port = 3306) |
|
| 64 | + { |
|
| 65 | 65 | self::$dl = new StaticDataLayer(); |
| 66 | 66 | |
| 67 | 67 | self::$dl->connect($host, $user, $passWord, $database, $port); |
| 68 | - } |
|
| 69 | - |
|
| 70 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 71 | - /** |
|
| 72 | - * Creates an audit table. |
|
| 73 | - * |
|
| 74 | - * @param string $dataSchemaName The name of the data schema. |
|
| 75 | - * @param string $auditSchemaName The name of the audit schema. |
|
| 76 | - * @param string $tableName The name of the table. |
|
| 77 | - * @param Columns $columns The metadata of the columns of the audit table (i.e. the audit columns and columns |
|
| 78 | - * of the data table). |
|
| 79 | - */ |
|
| 80 | - public static function createAuditTable($dataSchemaName, $auditSchemaName, $tableName, $columns) |
|
| 81 | - { |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 71 | + /** |
|
| 72 | + * Creates an audit table. |
|
| 73 | + * |
|
| 74 | + * @param string $dataSchemaName The name of the data schema. |
|
| 75 | + * @param string $auditSchemaName The name of the audit schema. |
|
| 76 | + * @param string $tableName The name of the table. |
|
| 77 | + * @param Columns $columns The metadata of the columns of the audit table (i.e. the audit columns and columns |
|
| 78 | + * of the data table). |
|
| 79 | + */ |
|
| 80 | + public static function createAuditTable($dataSchemaName, $auditSchemaName, $tableName, $columns) |
|
| 81 | + { |
|
| 82 | 82 | $helper = new CreateAuditTable($dataSchemaName, $auditSchemaName, $tableName, $columns); |
| 83 | 83 | $sql = $helper->buildStatement(); |
| 84 | 84 | |
| 85 | 85 | self::executeNone($sql); |
| 86 | - } |
|
| 87 | - |
|
| 88 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 89 | - /** |
|
| 90 | - * Creates a trigger on a table. |
|
| 91 | - * |
|
| 92 | - * @param string $dataSchemaName The name of the data schema. |
|
| 93 | - * @param string $auditSchemaName The name of the audit schema. |
|
| 94 | - * @param string $tableName The name of the table. |
|
| 95 | - * @param string $triggerAction The trigger action (i.e. INSERT, UPDATE, or DELETE). |
|
| 96 | - * @param string $triggerName The name of the trigger. |
|
| 97 | - * @param \SetBased\Audit\MySql\Table\Columns $tableColumns The data table columns. |
|
| 98 | - * @param \SetBased\Audit\MySql\Table\Columns $auditColumns The audit table columns. |
|
| 99 | - * @param string $skipVariable The skip variable. |
|
| 100 | - * @param string[] $additionSql Additional SQL statements. |
|
| 101 | - */ |
|
| 102 | - public static function createAuditTrigger($dataSchemaName, |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 89 | + /** |
|
| 90 | + * Creates a trigger on a table. |
|
| 91 | + * |
|
| 92 | + * @param string $dataSchemaName The name of the data schema. |
|
| 93 | + * @param string $auditSchemaName The name of the audit schema. |
|
| 94 | + * @param string $tableName The name of the table. |
|
| 95 | + * @param string $triggerAction The trigger action (i.e. INSERT, UPDATE, or DELETE). |
|
| 96 | + * @param string $triggerName The name of the trigger. |
|
| 97 | + * @param \SetBased\Audit\MySql\Table\Columns $tableColumns The data table columns. |
|
| 98 | + * @param \SetBased\Audit\MySql\Table\Columns $auditColumns The audit table columns. |
|
| 99 | + * @param string $skipVariable The skip variable. |
|
| 100 | + * @param string[] $additionSql Additional SQL statements. |
|
| 101 | + */ |
|
| 102 | + public static function createAuditTrigger($dataSchemaName, |
|
| 103 | 103 | $auditSchemaName, |
| 104 | 104 | $tableName, |
| 105 | 105 | $triggerAction, |
@@ -108,151 +108,151 @@ discard block |
||
| 108 | 108 | $auditColumns, |
| 109 | 109 | $skipVariable, |
| 110 | 110 | $additionSql) |
| 111 | - { |
|
| 111 | + { |
|
| 112 | 112 | $helper = new CreateAuditTrigger($dataSchemaName, |
| 113 | - $auditSchemaName, |
|
| 114 | - $tableName, |
|
| 115 | - $triggerAction, |
|
| 116 | - $triggerName, |
|
| 117 | - $tableColumns, |
|
| 118 | - $auditColumns, |
|
| 119 | - $skipVariable, |
|
| 120 | - $additionSql); |
|
| 113 | + $auditSchemaName, |
|
| 114 | + $tableName, |
|
| 115 | + $triggerAction, |
|
| 116 | + $triggerName, |
|
| 117 | + $tableColumns, |
|
| 118 | + $auditColumns, |
|
| 119 | + $skipVariable, |
|
| 120 | + $additionSql); |
|
| 121 | 121 | $sql = $helper->buildStatement(); |
| 122 | 122 | |
| 123 | 123 | self::executeNone($sql); |
| 124 | - } |
|
| 125 | - |
|
| 126 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 127 | - /** |
|
| 128 | - * Closes the connection to the MySQL instance, if connected. |
|
| 129 | - */ |
|
| 130 | - public static function disconnect() |
|
| 131 | - { |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 127 | + /** |
|
| 128 | + * Closes the connection to the MySQL instance, if connected. |
|
| 129 | + */ |
|
| 130 | + public static function disconnect() |
|
| 131 | + { |
|
| 132 | 132 | if (self::$dl!==null) |
| 133 | 133 | { |
| 134 | - self::$dl->disconnect(); |
|
| 135 | - self::$dl = null; |
|
| 134 | + self::$dl->disconnect(); |
|
| 135 | + self::$dl = null; |
|
| 136 | 136 | } |
| 137 | - } |
|
| 138 | - |
|
| 139 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 140 | - /** |
|
| 141 | - * Drops a trigger. |
|
| 142 | - * |
|
| 143 | - * @param string $triggerSchema The name of the trigger schema. |
|
| 144 | - * @param string $triggerName The mame of trigger. |
|
| 145 | - */ |
|
| 146 | - public static function dropTrigger($triggerSchema, $triggerName) |
|
| 147 | - { |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 140 | + /** |
|
| 141 | + * Drops a trigger. |
|
| 142 | + * |
|
| 143 | + * @param string $triggerSchema The name of the trigger schema. |
|
| 144 | + * @param string $triggerName The mame of trigger. |
|
| 145 | + */ |
|
| 146 | + public static function dropTrigger($triggerSchema, $triggerName) |
|
| 147 | + { |
|
| 148 | 148 | $sql = sprintf('drop trigger `%s`.`%s`', $triggerSchema, $triggerName); |
| 149 | 149 | |
| 150 | 150 | self::executeNone($sql); |
| 151 | - } |
|
| 152 | - |
|
| 153 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 154 | - /** |
|
| 155 | - * @param string $query The SQL statement. |
|
| 156 | - * |
|
| 157 | - * @return int The number of affected rows (if any). |
|
| 158 | - */ |
|
| 159 | - public static function executeNone($query) |
|
| 160 | - { |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 154 | + /** |
|
| 155 | + * @param string $query The SQL statement. |
|
| 156 | + * |
|
| 157 | + * @return int The number of affected rows (if any). |
|
| 158 | + */ |
|
| 159 | + public static function executeNone($query) |
|
| 160 | + { |
|
| 161 | 161 | self::logQuery($query); |
| 162 | 162 | |
| 163 | 163 | return self::$dl->executeNone($query); |
| 164 | - } |
|
| 165 | - |
|
| 166 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 167 | - /** |
|
| 168 | - * Executes a query that returns 0 or 1 row. |
|
| 169 | - * Throws an exception if the query selects 2 or more rows. |
|
| 170 | - * |
|
| 171 | - * @param string $query The SQL statement. |
|
| 172 | - * |
|
| 173 | - * @return array|null The selected row. |
|
| 174 | - */ |
|
| 175 | - public static function executeRow0($query) |
|
| 176 | - { |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 167 | + /** |
|
| 168 | + * Executes a query that returns 0 or 1 row. |
|
| 169 | + * Throws an exception if the query selects 2 or more rows. |
|
| 170 | + * |
|
| 171 | + * @param string $query The SQL statement. |
|
| 172 | + * |
|
| 173 | + * @return array|null The selected row. |
|
| 174 | + */ |
|
| 175 | + public static function executeRow0($query) |
|
| 176 | + { |
|
| 177 | 177 | self::logQuery($query); |
| 178 | 178 | |
| 179 | 179 | return self::$dl->executeRow0($query); |
| 180 | - } |
|
| 181 | - |
|
| 182 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 183 | - /** |
|
| 184 | - * Executes a query that returns 1 and only 1 row. |
|
| 185 | - * Throws an exception if the query selects none, 2 or more rows. |
|
| 186 | - * |
|
| 187 | - * @param string $query The SQL statement. |
|
| 188 | - * |
|
| 189 | - * @return array The selected row. |
|
| 190 | - */ |
|
| 191 | - public static function executeRow1($query) |
|
| 192 | - { |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 183 | + /** |
|
| 184 | + * Executes a query that returns 1 and only 1 row. |
|
| 185 | + * Throws an exception if the query selects none, 2 or more rows. |
|
| 186 | + * |
|
| 187 | + * @param string $query The SQL statement. |
|
| 188 | + * |
|
| 189 | + * @return array The selected row. |
|
| 190 | + */ |
|
| 191 | + public static function executeRow1($query) |
|
| 192 | + { |
|
| 193 | 193 | self::logQuery($query); |
| 194 | 194 | |
| 195 | 195 | return self::$dl->executeRow1($query); |
| 196 | - } |
|
| 197 | - |
|
| 198 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 199 | - /** |
|
| 200 | - * Executes a query that returns 0 or more rows. |
|
| 201 | - * |
|
| 202 | - * @param string $query The SQL statement. |
|
| 203 | - * |
|
| 204 | - * @return \array[] |
|
| 205 | - */ |
|
| 206 | - public static function executeRows($query) |
|
| 207 | - { |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 199 | + /** |
|
| 200 | + * Executes a query that returns 0 or more rows. |
|
| 201 | + * |
|
| 202 | + * @param string $query The SQL statement. |
|
| 203 | + * |
|
| 204 | + * @return \array[] |
|
| 205 | + */ |
|
| 206 | + public static function executeRows($query) |
|
| 207 | + { |
|
| 208 | 208 | self::logQuery($query); |
| 209 | 209 | |
| 210 | 210 | return self::$dl->executeRows($query); |
| 211 | - } |
|
| 212 | - |
|
| 213 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 214 | - /** |
|
| 215 | - * Executes a query that returns 0 or 1 row. |
|
| 216 | - * Throws an exception if the query selects 2 or more rows. |
|
| 217 | - * |
|
| 218 | - * @param string $query The SQL statement. |
|
| 219 | - * |
|
| 220 | - * @return int|string|null The selected row. |
|
| 221 | - */ |
|
| 222 | - public static function executeSingleton0($query) |
|
| 223 | - { |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 214 | + /** |
|
| 215 | + * Executes a query that returns 0 or 1 row. |
|
| 216 | + * Throws an exception if the query selects 2 or more rows. |
|
| 217 | + * |
|
| 218 | + * @param string $query The SQL statement. |
|
| 219 | + * |
|
| 220 | + * @return int|string|null The selected row. |
|
| 221 | + */ |
|
| 222 | + public static function executeSingleton0($query) |
|
| 223 | + { |
|
| 224 | 224 | self::logQuery($query); |
| 225 | 225 | |
| 226 | 226 | return self::$dl->executeSingleton0($query); |
| 227 | - } |
|
| 228 | - |
|
| 229 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 230 | - /** |
|
| 231 | - * Executes a query that returns 1 and only 1 row with 1 column. |
|
| 232 | - * Throws an exception if the query selects none, 2 or more rows. |
|
| 233 | - * |
|
| 234 | - * @param string $query The SQL statement. |
|
| 235 | - * |
|
| 236 | - * @return int|string The selected row. |
|
| 237 | - */ |
|
| 238 | - public static function executeSingleton1($query) |
|
| 239 | - { |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 230 | + /** |
|
| 231 | + * Executes a query that returns 1 and only 1 row with 1 column. |
|
| 232 | + * Throws an exception if the query selects none, 2 or more rows. |
|
| 233 | + * |
|
| 234 | + * @param string $query The SQL statement. |
|
| 235 | + * |
|
| 236 | + * @return int|string The selected row. |
|
| 237 | + */ |
|
| 238 | + public static function executeSingleton1($query) |
|
| 239 | + { |
|
| 240 | 240 | self::logQuery($query); |
| 241 | 241 | |
| 242 | 242 | return self::$dl->executeSingleton1($query); |
| 243 | - } |
|
| 244 | - |
|
| 245 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 246 | - /** |
|
| 247 | - * Selects metadata of all columns of table. |
|
| 248 | - * |
|
| 249 | - * @param string $schemaName The name of the table schema. |
|
| 250 | - * @param string $tableName The name of the table. |
|
| 251 | - * |
|
| 252 | - * @return \array[] |
|
| 253 | - */ |
|
| 254 | - public static function getTableColumns($schemaName, $tableName) |
|
| 255 | - { |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 246 | + /** |
|
| 247 | + * Selects metadata of all columns of table. |
|
| 248 | + * |
|
| 249 | + * @param string $schemaName The name of the table schema. |
|
| 250 | + * @param string $tableName The name of the table. |
|
| 251 | + * |
|
| 252 | + * @return \array[] |
|
| 253 | + */ |
|
| 254 | + public static function getTableColumns($schemaName, $tableName) |
|
| 255 | + { |
|
| 256 | 256 | $sql = sprintf(' |
| 257 | 257 | select COLUMN_NAME as column_name |
| 258 | 258 | , COLUMN_TYPE as column_type |
@@ -263,23 +263,23 @@ discard block |
||
| 263 | 263 | where TABLE_SCHEMA = %s |
| 264 | 264 | and TABLE_NAME = %s |
| 265 | 265 | order by ORDINAL_POSITION', |
| 266 | - self::$dl->quoteString($schemaName), |
|
| 267 | - self::$dl->quoteString($tableName)); |
|
| 266 | + self::$dl->quoteString($schemaName), |
|
| 267 | + self::$dl->quoteString($tableName)); |
|
| 268 | 268 | |
| 269 | 269 | return self::$dl->executeRows($sql); |
| 270 | - } |
|
| 271 | - |
|
| 272 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 273 | - /** |
|
| 274 | - * Selects table engine, character_set_name and table_collation. |
|
| 275 | - * |
|
| 276 | - * @param string $schemaName The name of the table schema. |
|
| 277 | - * @param string $tableName The name of the table. |
|
| 278 | - * |
|
| 279 | - * @return array |
|
| 280 | - */ |
|
| 281 | - public static function getTableOptions($schemaName, $tableName) |
|
| 282 | - { |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 273 | + /** |
|
| 274 | + * Selects table engine, character_set_name and table_collation. |
|
| 275 | + * |
|
| 276 | + * @param string $schemaName The name of the table schema. |
|
| 277 | + * @param string $tableName The name of the table. |
|
| 278 | + * |
|
| 279 | + * @return array |
|
| 280 | + */ |
|
| 281 | + public static function getTableOptions($schemaName, $tableName) |
|
| 282 | + { |
|
| 283 | 283 | $sql = sprintf(' |
| 284 | 284 | SELECT t1.TABLE_COLLATION as table_collation |
| 285 | 285 | , t1.ENGINE as engine |
@@ -288,100 +288,100 @@ discard block |
||
| 288 | 288 | inner join information_schema.COLLATION_CHARACTER_SET_APPLICABILITY t2 on t2.COLLATION_NAME = t1.TABLE_COLLATION |
| 289 | 289 | WHERE t1.TABLE_SCHEMA = %s |
| 290 | 290 | AND t1.TABLE_NAME = %s', |
| 291 | - self::$dl->quoteString($schemaName), |
|
| 292 | - self::$dl->quoteString($tableName)); |
|
| 291 | + self::$dl->quoteString($schemaName), |
|
| 292 | + self::$dl->quoteString($tableName)); |
|
| 293 | 293 | |
| 294 | 294 | return self::$dl->executeRow1($sql); |
| 295 | - } |
|
| 296 | - |
|
| 297 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 298 | - /** |
|
| 299 | - * Create temp table for getting column type information for audit columns. |
|
| 300 | - * |
|
| 301 | - * @param string $schemaName The name of the table schema. |
|
| 302 | - * @param string $tableName The table name. |
|
| 303 | - * @param array[] $auditColumns Audit columns from config file. |
|
| 304 | - */ |
|
| 305 | - public static function createTemporaryTable($schemaName, $tableName, $auditColumns) |
|
| 306 | - { |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 298 | + /** |
|
| 299 | + * Create temp table for getting column type information for audit columns. |
|
| 300 | + * |
|
| 301 | + * @param string $schemaName The name of the table schema. |
|
| 302 | + * @param string $tableName The table name. |
|
| 303 | + * @param array[] $auditColumns Audit columns from config file. |
|
| 304 | + */ |
|
| 305 | + public static function createTemporaryTable($schemaName, $tableName, $auditColumns) |
|
| 306 | + { |
|
| 307 | 307 | $sql = new CompoundSyntaxStore(); |
| 308 | 308 | $sql->append(sprintf('create temporary table `%s`.`%s` (', $schemaName, $tableName)); |
| 309 | 309 | foreach ($auditColumns as $column) |
| 310 | 310 | { |
| 311 | - $sql->append(sprintf('%s %s', $column['column_name'], $column['column_type'])); |
|
| 312 | - if (end($auditColumns)!==$column) |
|
| 313 | - { |
|
| 311 | + $sql->append(sprintf('%s %s', $column['column_name'], $column['column_type'])); |
|
| 312 | + if (end($auditColumns)!==$column) |
|
| 313 | + { |
|
| 314 | 314 | $sql->appendToLastLine(','); |
| 315 | - } |
|
| 315 | + } |
|
| 316 | 316 | } |
| 317 | 317 | $sql->append(')'); |
| 318 | 318 | |
| 319 | 319 | self::$dl->executeNone($sql->getCode()); |
| 320 | - } |
|
| 321 | - |
|
| 322 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 323 | - /** |
|
| 324 | - * Drop table. |
|
| 325 | - * |
|
| 326 | - * @param string $schemaName The name of the table schema. |
|
| 327 | - * @param string $tableName The name of the table. |
|
| 328 | - * |
|
| 329 | - * @return \array[] |
|
| 330 | - */ |
|
| 331 | - public static function showColumns($schemaName, $tableName) |
|
| 332 | - { |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 323 | + /** |
|
| 324 | + * Drop table. |
|
| 325 | + * |
|
| 326 | + * @param string $schemaName The name of the table schema. |
|
| 327 | + * @param string $tableName The name of the table. |
|
| 328 | + * |
|
| 329 | + * @return \array[] |
|
| 330 | + */ |
|
| 331 | + public static function showColumns($schemaName, $tableName) |
|
| 332 | + { |
|
| 333 | 333 | $sql = sprintf('SHOW COLUMNS FROM `%s`.`%s`', $schemaName, $tableName); |
| 334 | 334 | |
| 335 | 335 | return self::$dl->executeRows($sql); |
| 336 | - } |
|
| 337 | - |
|
| 338 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 339 | - /** |
|
| 340 | - * Drop table. |
|
| 341 | - * |
|
| 342 | - * @param string $schemaName The name of the table schema. |
|
| 343 | - * @param string $tableName The name of the table. |
|
| 344 | - */ |
|
| 345 | - public static function dropTemporaryTable($schemaName, $tableName) |
|
| 346 | - { |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 339 | + /** |
|
| 340 | + * Drop table. |
|
| 341 | + * |
|
| 342 | + * @param string $schemaName The name of the table schema. |
|
| 343 | + * @param string $tableName The name of the table. |
|
| 344 | + */ |
|
| 345 | + public static function dropTemporaryTable($schemaName, $tableName) |
|
| 346 | + { |
|
| 347 | 347 | $sql = sprintf('drop temporary table `%s`.`%s`', $schemaName, $tableName); |
| 348 | 348 | |
| 349 | 349 | self::$dl->executeNone($sql); |
| 350 | - } |
|
| 351 | - |
|
| 352 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 353 | - /** |
|
| 354 | - * Selects all triggers on a table. |
|
| 355 | - * |
|
| 356 | - * @param string $schemaName The name of the table schema. |
|
| 357 | - * @param string $tableName The name of the table. |
|
| 358 | - * |
|
| 359 | - * @return \array[] |
|
| 360 | - */ |
|
| 361 | - public static function getTableTriggers($schemaName, $tableName) |
|
| 362 | - { |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 353 | + /** |
|
| 354 | + * Selects all triggers on a table. |
|
| 355 | + * |
|
| 356 | + * @param string $schemaName The name of the table schema. |
|
| 357 | + * @param string $tableName The name of the table. |
|
| 358 | + * |
|
| 359 | + * @return \array[] |
|
| 360 | + */ |
|
| 361 | + public static function getTableTriggers($schemaName, $tableName) |
|
| 362 | + { |
|
| 363 | 363 | $sql = sprintf(' |
| 364 | 364 | select Trigger_Name as trigger_name |
| 365 | 365 | from information_schema.TRIGGERS |
| 366 | 366 | where TRIGGER_SCHEMA = %s |
| 367 | 367 | and EVENT_OBJECT_TABLE = %s |
| 368 | 368 | order by Trigger_Name', |
| 369 | - self::$dl->quoteString($schemaName), |
|
| 370 | - self::$dl->quoteString($tableName)); |
|
| 369 | + self::$dl->quoteString($schemaName), |
|
| 370 | + self::$dl->quoteString($tableName)); |
|
| 371 | 371 | |
| 372 | 372 | return self::$dl->executeRows($sql); |
| 373 | - } |
|
| 374 | - |
|
| 375 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 376 | - /** |
|
| 377 | - * Selects all table names in a schema. |
|
| 378 | - * |
|
| 379 | - * @param string $schemaName The name of the schema. |
|
| 380 | - * |
|
| 381 | - * @return \array[] |
|
| 382 | - */ |
|
| 383 | - public static function getTablesNames($schemaName) |
|
| 384 | - { |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 376 | + /** |
|
| 377 | + * Selects all table names in a schema. |
|
| 378 | + * |
|
| 379 | + * @param string $schemaName The name of the schema. |
|
| 380 | + * |
|
| 381 | + * @return \array[] |
|
| 382 | + */ |
|
| 383 | + public static function getTablesNames($schemaName) |
|
| 384 | + { |
|
| 385 | 385 | $sql = sprintf(" |
| 386 | 386 | select TABLE_NAME as table_name |
| 387 | 387 | from information_schema.TABLES |
@@ -390,67 +390,67 @@ discard block |
||
| 390 | 390 | order by TABLE_NAME", self::$dl->quoteString($schemaName)); |
| 391 | 391 | |
| 392 | 392 | return self::$dl->executeRows($sql); |
| 393 | - } |
|
| 394 | - |
|
| 395 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 396 | - /** |
|
| 397 | - * Acquires a write lock on a table. |
|
| 398 | - * |
|
| 399 | - * @param string $tableName The table name. |
|
| 400 | - */ |
|
| 401 | - public static function lockTable($tableName) |
|
| 402 | - { |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 396 | + /** |
|
| 397 | + * Acquires a write lock on a table. |
|
| 398 | + * |
|
| 399 | + * @param string $tableName The table name. |
|
| 400 | + */ |
|
| 401 | + public static function lockTable($tableName) |
|
| 402 | + { |
|
| 403 | 403 | $sql = sprintf('lock tables `%s` write', $tableName); |
| 404 | 404 | |
| 405 | 405 | self::$dl->executeNone($sql); |
| 406 | - } |
|
| 407 | - |
|
| 408 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 409 | - /** |
|
| 410 | - * Sets the Output decorator. |
|
| 411 | - * |
|
| 412 | - * @param StratumStyle $io The Output decorator. |
|
| 413 | - */ |
|
| 414 | - public static function setIo($io) |
|
| 415 | - { |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 409 | + /** |
|
| 410 | + * Sets the Output decorator. |
|
| 411 | + * |
|
| 412 | + * @param StratumStyle $io The Output decorator. |
|
| 413 | + */ |
|
| 414 | + public static function setIo($io) |
|
| 415 | + { |
|
| 416 | 416 | self::$io = $io; |
| 417 | - } |
|
| 418 | - |
|
| 419 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 420 | - /** |
|
| 421 | - * Releases all table locks. |
|
| 422 | - */ |
|
| 423 | - public static function unlockTables() |
|
| 424 | - { |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 420 | + /** |
|
| 421 | + * Releases all table locks. |
|
| 422 | + */ |
|
| 423 | + public static function unlockTables() |
|
| 424 | + { |
|
| 425 | 425 | $sql = 'unlock tables'; |
| 426 | 426 | |
| 427 | 427 | self::$dl->executeNone($sql); |
| 428 | - } |
|
| 429 | - |
|
| 430 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 431 | - /** |
|
| 432 | - * Logs the query on the console. |
|
| 433 | - * |
|
| 434 | - * @param string $query The query. |
|
| 435 | - */ |
|
| 436 | - private static function logQuery($query) |
|
| 437 | - { |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 431 | + /** |
|
| 432 | + * Logs the query on the console. |
|
| 433 | + * |
|
| 434 | + * @param string $query The query. |
|
| 435 | + */ |
|
| 436 | + private static function logQuery($query) |
|
| 437 | + { |
|
| 438 | 438 | $query = trim($query); |
| 439 | 439 | |
| 440 | 440 | if (strpos($query, "\n")!==false) |
| 441 | 441 | { |
| 442 | - // Query is a multi line query. |
|
| 443 | - self::$io->logVeryVerbose('Executing query:'); |
|
| 444 | - self::$io->logVeryVerbose('<sql>%s</sql>', $query); |
|
| 442 | + // Query is a multi line query. |
|
| 443 | + self::$io->logVeryVerbose('Executing query:'); |
|
| 444 | + self::$io->logVeryVerbose('<sql>%s</sql>', $query); |
|
| 445 | 445 | } |
| 446 | 446 | else |
| 447 | 447 | { |
| 448 | - // Query is a single line query. |
|
| 449 | - self::$io->logVeryVerbose('Executing query: <sql>%s</sql>', $query); |
|
| 448 | + // Query is a single line query. |
|
| 449 | + self::$io->logVeryVerbose('Executing query: <sql>%s</sql>', $query); |
|
| 450 | + } |
|
| 450 | 451 | } |
| 451 | - } |
|
| 452 | 452 | |
| 453 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 453 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 454 | 454 | } |
| 455 | 455 | |
| 456 | 456 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -11,71 +11,71 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class ColumnTypesExtended |
| 13 | 13 | { |
| 14 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 15 | - /** |
|
| 16 | - * Contains all column types from audit and data schemas. |
|
| 17 | - * |
|
| 18 | - * @var array[] |
|
| 19 | - */ |
|
| 20 | - private $columnTypes = []; |
|
| 14 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 15 | + /** |
|
| 16 | + * Contains all column types from audit and data schemas. |
|
| 17 | + * |
|
| 18 | + * @var array[] |
|
| 19 | + */ |
|
| 20 | + private $columnTypes = []; |
|
| 21 | 21 | |
| 22 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 23 | - /** |
|
| 24 | - * Object constructor |
|
| 25 | - * |
|
| 26 | - * @param array[] $configColumns The table columns from config file. |
|
| 27 | - * @param Columns $auditColumns The table columns from audit schema. |
|
| 28 | - * @param Columns $dataColumns The table columns from data schema. |
|
| 29 | - */ |
|
| 30 | - public function __construct($configColumns, $auditColumns, $dataColumns) |
|
| 31 | - { |
|
| 22 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 23 | + /** |
|
| 24 | + * Object constructor |
|
| 25 | + * |
|
| 26 | + * @param array[] $configColumns The table columns from config file. |
|
| 27 | + * @param Columns $auditColumns The table columns from audit schema. |
|
| 28 | + * @param Columns $dataColumns The table columns from data schema. |
|
| 29 | + */ |
|
| 30 | + public function __construct($configColumns, $auditColumns, $dataColumns) |
|
| 31 | + { |
|
| 32 | 32 | $auditConfigTypes = new Columns($configColumns); |
| 33 | 33 | $auditTypes = $auditColumns; |
| 34 | 34 | $dataTypes = $dataColumns; |
| 35 | 35 | $allTypes = ['config' => $auditConfigTypes, 'audit' => $auditTypes, 'data' => $dataTypes]; |
| 36 | 36 | |
| 37 | 37 | $this->appendColumnTypes($allTypes); |
| 38 | - } |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 41 | - /** |
|
| 42 | - * Add to array all columns types. |
|
| 43 | - * |
|
| 44 | - * @param array[] $columnTypes The metadata of the column. |
|
| 45 | - */ |
|
| 46 | - private function appendColumnTypes($columnTypes) |
|
| 47 | - { |
|
| 40 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 41 | + /** |
|
| 42 | + * Add to array all columns types. |
|
| 43 | + * |
|
| 44 | + * @param array[] $columnTypes The metadata of the column. |
|
| 45 | + */ |
|
| 46 | + private function appendColumnTypes($columnTypes) |
|
| 47 | + { |
|
| 48 | 48 | /** @var Columns $typesArray */ |
| 49 | 49 | foreach ($columnTypes as $typePrefix => $typesArray) |
| 50 | 50 | { |
| 51 | - $typesArray = $typesArray->getColumns(); |
|
| 52 | - /** @var ColumnType $type */ |
|
| 53 | - foreach ($typesArray as $type) |
|
| 54 | - { |
|
| 51 | + $typesArray = $typesArray->getColumns(); |
|
| 52 | + /** @var ColumnType $type */ |
|
| 53 | + foreach ($typesArray as $type) |
|
| 54 | + { |
|
| 55 | 55 | if (isset($this->columnTypes[$type->getProperty('column_name')])) |
| 56 | 56 | { |
| 57 | - $this->columnTypes[$type->getProperty('column_name')]->extendColumnTypes($type, $typePrefix); |
|
| 57 | + $this->columnTypes[$type->getProperty('column_name')]->extendColumnTypes($type, $typePrefix); |
|
| 58 | 58 | } |
| 59 | 59 | else |
| 60 | 60 | { |
| 61 | - $this->columnTypes[$type->getProperty('column_name')] = new ColumnTypesHelper($type, $typePrefix); |
|
| 61 | + $this->columnTypes[$type->getProperty('column_name')] = new ColumnTypesHelper($type, $typePrefix); |
|
| 62 | 62 | } |
| 63 | - } |
|
| 63 | + } |
|
| 64 | + } |
|
| 64 | 65 | } |
| 65 | - } |
|
| 66 | 66 | |
| 67 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 68 | - /** |
|
| 69 | - * Get columns types. |
|
| 70 | - * |
|
| 71 | - * @return array[] |
|
| 72 | - */ |
|
| 73 | - public function getTypes() |
|
| 74 | - { |
|
| 67 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 68 | + /** |
|
| 69 | + * Get columns types. |
|
| 70 | + * |
|
| 71 | + * @return array[] |
|
| 72 | + */ |
|
| 73 | + public function getTypes() |
|
| 74 | + { |
|
| 75 | 75 | return $this->columnTypes; |
| 76 | - } |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 78 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -55,8 +55,7 @@ |
||
| 55 | 55 | if (isset($this->columnTypes[$type->getProperty('column_name')])) |
| 56 | 56 | { |
| 57 | 57 | $this->columnTypes[$type->getProperty('column_name')]->extendColumnTypes($type, $typePrefix); |
| 58 | - } |
|
| 59 | - else |
|
| 58 | + } else |
|
| 60 | 59 | { |
| 61 | 60 | $this->columnTypes[$type->getProperty('column_name')] = new ColumnTypesHelper($type, $typePrefix); |
| 62 | 61 | } |
@@ -8,16 +8,16 @@ discard block |
||
| 8 | 8 | */ |
| 9 | 9 | class RowHelper |
| 10 | 10 | { |
| 11 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 12 | - /** |
|
| 13 | - * Create table row. |
|
| 14 | - * |
|
| 15 | - * @param array[] $theRow Data for table row. |
|
| 16 | - * |
|
| 17 | - * @return array<string,null|string> |
|
| 18 | - */ |
|
| 19 | - public static function createColumnOptionsRow($theRow) |
|
| 20 | - { |
|
| 11 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 12 | + /** |
|
| 13 | + * Create table row. |
|
| 14 | + * |
|
| 15 | + * @param array[] $theRow Data for table row. |
|
| 16 | + * |
|
| 17 | + * @return array<string,null|string> |
|
| 18 | + */ |
|
| 19 | + public static function createColumnOptionsRow($theRow) |
|
| 20 | + { |
|
| 21 | 21 | $dataCharsetName = isset($theRow['data_character_set_name']) ? $theRow['data_character_set_name'] : null; |
| 22 | 22 | $dataCollationName = isset($theRow['data_collation_name']) ? $theRow['data_collation_name'] : null; |
| 23 | 23 | |
@@ -25,98 +25,98 @@ discard block |
||
| 25 | 25 | $auditCollationName = isset($theRow['audit_collation_name']) ? $theRow['audit_collation_name'] : null; |
| 26 | 26 | |
| 27 | 27 | $tableRow = ['column_name' => null, |
| 28 | - 'data_table_type' => self::styledOptionsRow($dataCharsetName, $dataCollationName), |
|
| 29 | - 'audit_table_type' => self::styledOptionsRow($auditCharsetName, $auditCollationName), |
|
| 30 | - 'config_type' => null]; |
|
| 28 | + 'data_table_type' => self::styledOptionsRow($dataCharsetName, $dataCollationName), |
|
| 29 | + 'audit_table_type' => self::styledOptionsRow($auditCharsetName, $auditCollationName), |
|
| 30 | + 'config_type' => null]; |
|
| 31 | 31 | |
| 32 | 32 | return $tableRow; |
| 33 | - } |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 36 | - /** |
|
| 37 | - * Create table row. |
|
| 38 | - * |
|
| 39 | - * @param $theCharacterSetName |
|
| 40 | - * @param $theCollationName |
|
| 41 | - * |
|
| 42 | - * @return string |
|
| 43 | - */ |
|
| 44 | - public static function styledOptionsRow($theCharacterSetName, $theCollationName) |
|
| 45 | - { |
|
| 35 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 36 | + /** |
|
| 37 | + * Create table row. |
|
| 38 | + * |
|
| 39 | + * @param $theCharacterSetName |
|
| 40 | + * @param $theCollationName |
|
| 41 | + * |
|
| 42 | + * @return string |
|
| 43 | + */ |
|
| 44 | + public static function styledOptionsRow($theCharacterSetName, $theCollationName) |
|
| 45 | + { |
|
| 46 | 46 | $charsetName = isset($theCharacterSetName) ? '['.$theCharacterSetName.']' : null; |
| 47 | 47 | $collationName = isset($theCollationName) ? '['.$theCollationName.']' : null; |
| 48 | 48 | |
| 49 | 49 | return trim(sprintf('%s %s', $charsetName, $collationName)); |
| 50 | - } |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 53 | - /** |
|
| 54 | - * Create table row. |
|
| 55 | - * |
|
| 56 | - * @param array[] $theRow Data for table row. |
|
| 57 | - * |
|
| 58 | - * @return array<string,null|string> |
|
| 59 | - */ |
|
| 60 | - public static function createTableRow($theRow) |
|
| 61 | - { |
|
| 52 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 53 | + /** |
|
| 54 | + * Create table row. |
|
| 55 | + * |
|
| 56 | + * @param array[] $theRow Data for table row. |
|
| 57 | + * |
|
| 58 | + * @return array<string,null|string> |
|
| 59 | + */ |
|
| 60 | + public static function createTableRow($theRow) |
|
| 61 | + { |
|
| 62 | 62 | $tableRow = ['column_name' => isset($theRow['column_name']) ? $theRow['column_name'] : null, |
| 63 | - 'data_table_type' => isset($theRow['data_column_type']) ? $theRow['data_column_type'] : null, |
|
| 64 | - 'audit_table_type' => isset($theRow['audit_column_type']) ? $theRow['audit_column_type'] : null, |
|
| 65 | - 'config_type' => isset($theRow['config_column_type']) ? $theRow['config_column_type'] : null]; |
|
| 63 | + 'data_table_type' => isset($theRow['data_column_type']) ? $theRow['data_column_type'] : null, |
|
| 64 | + 'audit_table_type' => isset($theRow['audit_column_type']) ? $theRow['audit_column_type'] : null, |
|
| 65 | + 'config_type' => isset($theRow['config_column_type']) ? $theRow['config_column_type'] : null]; |
|
| 66 | 66 | |
| 67 | 67 | return $tableRow; |
| 68 | - } |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 71 | - /** |
|
| 72 | - * Check isset options(collation, character set name) from row. |
|
| 73 | - * |
|
| 74 | - * @param array[] $theRow Row for append. |
|
| 75 | - * |
|
| 76 | - * @return bool |
|
| 77 | - */ |
|
| 78 | - private static function checkOptions($theRow) |
|
| 79 | - { |
|
| 70 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 71 | + /** |
|
| 72 | + * Check isset options(collation, character set name) from row. |
|
| 73 | + * |
|
| 74 | + * @param array[] $theRow Row for append. |
|
| 75 | + * |
|
| 76 | + * @return bool |
|
| 77 | + */ |
|
| 78 | + private static function checkOptions($theRow) |
|
| 79 | + { |
|
| 80 | 80 | if (isset($theRow['audit_character_set_name'])) |
| 81 | 81 | { |
| 82 | - return true; |
|
| 82 | + return true; |
|
| 83 | 83 | } |
| 84 | 84 | if (isset($theRow['data_character_set_name'])) |
| 85 | 85 | { |
| 86 | - return true; |
|
| 86 | + return true; |
|
| 87 | 87 | } |
| 88 | 88 | if (isset($theRow['audit_collation_name'])) |
| 89 | 89 | { |
| 90 | - return true; |
|
| 90 | + return true; |
|
| 91 | 91 | } |
| 92 | 92 | if (isset($theRow['data_collation_name'])) |
| 93 | 93 | { |
| 94 | - return true; |
|
| 94 | + return true; |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | return false; |
| 98 | - } |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 101 | - /** |
|
| 102 | - * Append a row. |
|
| 103 | - * |
|
| 104 | - * @param \array[] $theExistRows Exist rows array for appending. |
|
| 105 | - * @param ColumnTypesHelper $theRow Row for append. |
|
| 106 | - */ |
|
| 107 | - public static function appendRow(&$theExistRows, $theRow) |
|
| 108 | - { |
|
| 100 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 101 | + /** |
|
| 102 | + * Append a row. |
|
| 103 | + * |
|
| 104 | + * @param \array[] $theExistRows Exist rows array for appending. |
|
| 105 | + * @param ColumnTypesHelper $theRow Row for append. |
|
| 106 | + */ |
|
| 107 | + public static function appendRow(&$theExistRows, $theRow) |
|
| 108 | + { |
|
| 109 | 109 | $theRow = $theRow->getTypes(); |
| 110 | 110 | $theExistRows[] = self::createTableRow($theRow); |
| 111 | 111 | if (self::checkOptions($theRow)) |
| 112 | 112 | { |
| 113 | - $theRow = self::createColumnOptionsRow($theRow); |
|
| 113 | + $theRow = self::createColumnOptionsRow($theRow); |
|
| 114 | 114 | |
| 115 | - $theExistRows[] = $theRow; |
|
| 115 | + $theExistRows[] = $theRow; |
|
| 116 | + } |
|
| 116 | 117 | } |
| 117 | - } |
|
| 118 | 118 | |
| 119 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 119 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -12,194 +12,194 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class TableHelper |
| 14 | 14 | { |
| 15 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 16 | - /** |
|
| 17 | - * Array with rows for table. |
|
| 18 | - * |
|
| 19 | - * @var \array[] |
|
| 20 | - */ |
|
| 21 | - private $rows = []; |
|
| 15 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 16 | + /** |
|
| 17 | + * Array with rows for table. |
|
| 18 | + * |
|
| 19 | + * @var \array[] |
|
| 20 | + */ |
|
| 21 | + private $rows = []; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Audit columns from config file. |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - private $auditColumns; |
|
| 23 | + /** |
|
| 24 | + * Audit columns from config file. |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + private $auditColumns; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Table options from audit schema. |
|
| 32 | - * |
|
| 33 | - * @var array |
|
| 34 | - */ |
|
| 35 | - private $auditTableOptions; |
|
| 30 | + /** |
|
| 31 | + * Table options from audit schema. |
|
| 32 | + * |
|
| 33 | + * @var array |
|
| 34 | + */ |
|
| 35 | + private $auditTableOptions; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Table options from data schema. |
|
| 39 | - * |
|
| 40 | - * @var array |
|
| 41 | - */ |
|
| 42 | - private $dataTableOptions; |
|
| 37 | + /** |
|
| 38 | + * Table options from data schema. |
|
| 39 | + * |
|
| 40 | + * @var array |
|
| 41 | + */ |
|
| 42 | + private $dataTableOptions; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Full option. |
|
| 46 | - * |
|
| 47 | - * @var bool |
|
| 48 | - */ |
|
| 49 | - private $fullOption; |
|
| 44 | + /** |
|
| 45 | + * Full option. |
|
| 46 | + * |
|
| 47 | + * @var bool |
|
| 48 | + */ |
|
| 49 | + private $fullOption; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Check existing separator. |
|
| 53 | - * |
|
| 54 | - * @var bool |
|
| 55 | - */ |
|
| 56 | - private $existSeparator = false; |
|
| 51 | + /** |
|
| 52 | + * Check existing separator. |
|
| 53 | + * |
|
| 54 | + * @var bool |
|
| 55 | + */ |
|
| 56 | + private $existSeparator = false; |
|
| 57 | 57 | |
| 58 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 59 | - /** |
|
| 60 | - * Object constructor. |
|
| 61 | - * |
|
| 62 | - * @param string $dataSchema Data schema name. |
|
| 63 | - * @param string $auditSchema Audit schema name. |
|
| 64 | - * @param string $tableName The table name. |
|
| 65 | - * @param array[] $theAuditColumns Audit columns from config file. |
|
| 66 | - * @param bool $fullOption If set append table options to rows. |
|
| 67 | - */ |
|
| 68 | - public function __construct($dataSchema, $auditSchema, $tableName, $theAuditColumns, $fullOption) |
|
| 69 | - { |
|
| 58 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 59 | + /** |
|
| 60 | + * Object constructor. |
|
| 61 | + * |
|
| 62 | + * @param string $dataSchema Data schema name. |
|
| 63 | + * @param string $auditSchema Audit schema name. |
|
| 64 | + * @param string $tableName The table name. |
|
| 65 | + * @param array[] $theAuditColumns Audit columns from config file. |
|
| 66 | + * @param bool $fullOption If set append table options to rows. |
|
| 67 | + */ |
|
| 68 | + public function __construct($dataSchema, $auditSchema, $tableName, $theAuditColumns, $fullOption) |
|
| 69 | + { |
|
| 70 | 70 | $this->auditColumns = $theAuditColumns; |
| 71 | 71 | $this->fullOption = $fullOption; |
| 72 | 72 | $this->auditTableOptions = DataLayer::getTableOptions($auditSchema, $tableName); |
| 73 | 73 | $this->dataTableOptions = DataLayer::getTableOptions($dataSchema, $tableName); |
| 74 | - } |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 77 | - /** |
|
| 78 | - * Append row with table option. |
|
| 79 | - * |
|
| 80 | - * @param string $theOption The option. |
|
| 81 | - * @param null|string $theName Display name. |
|
| 82 | - */ |
|
| 83 | - public function appendTableOption($theOption, $theName = null) |
|
| 84 | - { |
|
| 76 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 77 | + /** |
|
| 78 | + * Append row with table option. |
|
| 79 | + * |
|
| 80 | + * @param string $theOption The option. |
|
| 81 | + * @param null|string $theName Display name. |
|
| 82 | + */ |
|
| 83 | + public function appendTableOption($theOption, $theName = null) |
|
| 84 | + { |
|
| 85 | 85 | if ($this->dataTableOptions[$theOption]!=$this->auditTableOptions[$theOption] || $this->fullOption) |
| 86 | 86 | { |
| 87 | - if (!$this->existSeparator) |
|
| 88 | - { |
|
| 87 | + if (!$this->existSeparator) |
|
| 88 | + { |
|
| 89 | 89 | $this->rows[] = new TableSeparator(); |
| 90 | 90 | $this->existSeparator = true; |
| 91 | - } |
|
| 92 | - if ($theName===null) |
|
| 93 | - { |
|
| 91 | + } |
|
| 92 | + if ($theName===null) |
|
| 93 | + { |
|
| 94 | 94 | $theName = $theOption; |
| 95 | - } |
|
| 96 | - $tableRow = ['column_name' => $theName, |
|
| 97 | - 'data_column_type' => $this->dataTableOptions[$theOption], |
|
| 98 | - 'audit_column_type' => $this->auditTableOptions[$theOption], |
|
| 99 | - 'config_column_type' => null]; |
|
| 100 | - $this->rows[$theOption] = RowHelper::createTableRow($tableRow); |
|
| 95 | + } |
|
| 96 | + $tableRow = ['column_name' => $theName, |
|
| 97 | + 'data_column_type' => $this->dataTableOptions[$theOption], |
|
| 98 | + 'audit_column_type' => $this->auditTableOptions[$theOption], |
|
| 99 | + 'config_column_type' => null]; |
|
| 100 | + $this->rows[$theOption] = RowHelper::createTableRow($tableRow); |
|
| 101 | + } |
|
| 101 | 102 | } |
| 102 | - } |
|
| 103 | 103 | |
| 104 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 105 | - /** |
|
| 106 | - * Appends rows. |
|
| 107 | - * |
|
| 108 | - * @param \array[] $theRows Rows array. |
|
| 109 | - */ |
|
| 110 | - public function appendRows($theRows) |
|
| 111 | - { |
|
| 104 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 105 | + /** |
|
| 106 | + * Appends rows. |
|
| 107 | + * |
|
| 108 | + * @param \array[] $theRows Rows array. |
|
| 109 | + */ |
|
| 110 | + public function appendRows($theRows) |
|
| 111 | + { |
|
| 112 | 112 | foreach ($theRows as $row) |
| 113 | 113 | { |
| 114 | - RowHelper::appendRow($this->rows, $row); |
|
| 114 | + RowHelper::appendRow($this->rows, $row); |
|
| 115 | 115 | } |
| 116 | 116 | $this->appendTableOption('engine'); |
| 117 | 117 | $this->appendTableOption('character_set_name', 'character set'); |
| 118 | 118 | $this->appendTableOption('table_collation', 'collation'); |
| 119 | - } |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 122 | - /** |
|
| 123 | - * Add highlighting to columns. |
|
| 124 | - */ |
|
| 125 | - public function addHighlighting() |
|
| 126 | - { |
|
| 121 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 122 | + /** |
|
| 123 | + * Add highlighting to columns. |
|
| 124 | + */ |
|
| 125 | + public function addHighlighting() |
|
| 126 | + { |
|
| 127 | 127 | $styledColumns = []; |
| 128 | 128 | foreach ($this->rows as $key => $column) |
| 129 | 129 | { |
| 130 | - $styledColumn = $column; |
|
| 131 | - if (is_array($column)) |
|
| 132 | - { |
|
| 130 | + $styledColumn = $column; |
|
| 131 | + if (is_array($column)) |
|
| 132 | + { |
|
| 133 | 133 | // Highlighting for data table column types and audit. |
| 134 | 134 | if (!empty($column['data_table_type'])) |
| 135 | 135 | { |
| 136 | - if (isset($column['data_table_type']) && !isset($column['audit_table_type'])) |
|
| 137 | - { |
|
| 136 | + if (isset($column['data_table_type']) && !isset($column['audit_table_type'])) |
|
| 137 | + { |
|
| 138 | 138 | if (!isset($column['column_name'])) |
| 139 | 139 | { |
| 140 | - $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']); |
|
| 140 | + $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']); |
|
| 141 | 141 | } |
| 142 | 142 | $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']); |
| 143 | 143 | $styledColumn['data_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['data_table_type']); |
| 144 | - } |
|
| 145 | - else if (!isset($column['data_table_type']) && isset($column['audit_table_type'])) |
|
| 146 | - { |
|
| 144 | + } |
|
| 145 | + else if (!isset($column['data_table_type']) && isset($column['audit_table_type'])) |
|
| 146 | + { |
|
| 147 | 147 | $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['audit_table_type']); |
| 148 | - } |
|
| 149 | - else if (strcmp($column['data_table_type'], $column['audit_table_type'])) |
|
| 150 | - { |
|
| 148 | + } |
|
| 149 | + else if (strcmp($column['data_table_type'], $column['audit_table_type'])) |
|
| 150 | + { |
|
| 151 | 151 | if (!isset($column['column_name'])) |
| 152 | 152 | { |
| 153 | - $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']); |
|
| 153 | + $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']); |
|
| 154 | 154 | } |
| 155 | 155 | $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']); |
| 156 | 156 | $styledColumn['data_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['data_table_type']); |
| 157 | 157 | $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['audit_table_type']); |
| 158 | - } |
|
| 158 | + } |
|
| 159 | 159 | } |
| 160 | 160 | else |
| 161 | 161 | { |
| 162 | - // Highlighting for audit table column types and audit_columns in config file. |
|
| 163 | - $searchColumn = StaticDataLayer::searchInRowSet('column_name', $styledColumn['column_name'], $this->auditColumns); |
|
| 164 | - if (isset($searchColumn)) |
|
| 165 | - { |
|
| 162 | + // Highlighting for audit table column types and audit_columns in config file. |
|
| 163 | + $searchColumn = StaticDataLayer::searchInRowSet('column_name', $styledColumn['column_name'], $this->auditColumns); |
|
| 164 | + if (isset($searchColumn)) |
|
| 165 | + { |
|
| 166 | 166 | $configType = $this->auditColumns[$searchColumn]['column_type']; |
| 167 | 167 | if (isset($configType) && !isset($column['audit_table_type'])) |
| 168 | 168 | { |
| 169 | - $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']); |
|
| 170 | - $styledColumn['config_type'] = sprintf('<mm_type>%s</>', $styledColumn['config_type']); |
|
| 169 | + $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']); |
|
| 170 | + $styledColumn['config_type'] = sprintf('<mm_type>%s</>', $styledColumn['config_type']); |
|
| 171 | 171 | } |
| 172 | 172 | else if (!isset($configType) && isset($column['audit_table_type'])) |
| 173 | 173 | { |
| 174 | - $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $column['audit_table_type']); |
|
| 174 | + $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $column['audit_table_type']); |
|
| 175 | 175 | } |
| 176 | 176 | else if (strcmp($configType, $column['audit_table_type'])) |
| 177 | 177 | { |
| 178 | - $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']); |
|
| 179 | - $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $column['audit_table_type']); |
|
| 180 | - $styledColumn['config_type'] = sprintf('<mm_type>%s</>', $styledColumn['config_type']); |
|
| 178 | + $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']); |
|
| 179 | + $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $column['audit_table_type']); |
|
| 180 | + $styledColumn['config_type'] = sprintf('<mm_type>%s</>', $styledColumn['config_type']); |
|
| 181 | + } |
|
| 181 | 182 | } |
| 182 | - } |
|
| 183 | 183 | } |
| 184 | - } |
|
| 185 | - $styledColumns[] = $styledColumn; |
|
| 184 | + } |
|
| 185 | + $styledColumns[] = $styledColumn; |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | $this->rows = $styledColumns; |
| 189 | - } |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 192 | - /** |
|
| 193 | - * Get rows. |
|
| 194 | - * |
|
| 195 | - * @return \array[] |
|
| 196 | - */ |
|
| 197 | - public function getRows() |
|
| 198 | - { |
|
| 191 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 192 | + /** |
|
| 193 | + * Get rows. |
|
| 194 | + * |
|
| 195 | + * @return \array[] |
|
| 196 | + */ |
|
| 197 | + public function getRows() |
|
| 198 | + { |
|
| 199 | 199 | return $this->rows; |
| 200 | - } |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 202 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -11,92 +11,92 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | class Table |
| 13 | 13 | { |
| 14 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 15 | - /** |
|
| 16 | - * The unique alias for this data table. |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 20 | - private $alias; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * The metadata (additional) audit columns (as stored in the config file). |
|
| 24 | - * |
|
| 25 | - * @var Columns |
|
| 26 | - */ |
|
| 27 | - private $auditColumns; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The name of the schema with the audit tables. |
|
| 31 | - * |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - private $auditSchemaName; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The name of the schema with the data tables. |
|
| 38 | - * |
|
| 39 | - * @var string |
|
| 40 | - */ |
|
| 41 | - private $dataSchemaName; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The metadata of the columns of the data table as stored in the config file. |
|
| 45 | - * |
|
| 46 | - * @var Columns |
|
| 47 | - */ |
|
| 48 | - private $dataTableColumnsConfig; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * The metadata of the columns of the data table retrieved from information_schema. |
|
| 52 | - * |
|
| 53 | - * @var Columns |
|
| 54 | - */ |
|
| 55 | - private $dataTableColumnsDatabase; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The output decorator |
|
| 59 | - * |
|
| 60 | - * @var StratumStyle |
|
| 61 | - */ |
|
| 62 | - private $io; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * The skip variable for triggers. |
|
| 66 | - * |
|
| 67 | - * @var string |
|
| 68 | - */ |
|
| 69 | - private $skipVariable; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * The name of this data table. |
|
| 73 | - * |
|
| 74 | - * @var string |
|
| 75 | - */ |
|
| 76 | - private $tableName; |
|
| 77 | - |
|
| 78 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 79 | - /** |
|
| 80 | - * Object constructor. |
|
| 81 | - * |
|
| 82 | - * @param StratumStyle $io The output for log messages. |
|
| 83 | - * @param string $tableName The table name. |
|
| 84 | - * @param string $dataSchema The name of the schema with data tables. |
|
| 85 | - * @param string $auditSchema The name of the schema with audit tables. |
|
| 86 | - * @param array[] $configColumnsMetadata The columns of the data table as stored in the config file. |
|
| 87 | - * @param array[] $auditColumnsMetadata The columns of the audit table as stored in the config file. |
|
| 88 | - * @param string $alias An unique alias for this table. |
|
| 89 | - * @param string $skipVariable The skip variable |
|
| 90 | - */ |
|
| 91 | - public function __construct($io, |
|
| 92 | - $tableName, |
|
| 93 | - $dataSchema, |
|
| 94 | - $auditSchema, |
|
| 95 | - $configColumnsMetadata, |
|
| 96 | - $auditColumnsMetadata, |
|
| 97 | - $alias, |
|
| 98 | - $skipVariable) |
|
| 99 | - { |
|
| 14 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 15 | + /** |
|
| 16 | + * The unique alias for this data table. |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | + private $alias; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * The metadata (additional) audit columns (as stored in the config file). |
|
| 24 | + * |
|
| 25 | + * @var Columns |
|
| 26 | + */ |
|
| 27 | + private $auditColumns; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The name of the schema with the audit tables. |
|
| 31 | + * |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + private $auditSchemaName; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The name of the schema with the data tables. |
|
| 38 | + * |
|
| 39 | + * @var string |
|
| 40 | + */ |
|
| 41 | + private $dataSchemaName; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The metadata of the columns of the data table as stored in the config file. |
|
| 45 | + * |
|
| 46 | + * @var Columns |
|
| 47 | + */ |
|
| 48 | + private $dataTableColumnsConfig; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * The metadata of the columns of the data table retrieved from information_schema. |
|
| 52 | + * |
|
| 53 | + * @var Columns |
|
| 54 | + */ |
|
| 55 | + private $dataTableColumnsDatabase; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The output decorator |
|
| 59 | + * |
|
| 60 | + * @var StratumStyle |
|
| 61 | + */ |
|
| 62 | + private $io; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * The skip variable for triggers. |
|
| 66 | + * |
|
| 67 | + * @var string |
|
| 68 | + */ |
|
| 69 | + private $skipVariable; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * The name of this data table. |
|
| 73 | + * |
|
| 74 | + * @var string |
|
| 75 | + */ |
|
| 76 | + private $tableName; |
|
| 77 | + |
|
| 78 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 79 | + /** |
|
| 80 | + * Object constructor. |
|
| 81 | + * |
|
| 82 | + * @param StratumStyle $io The output for log messages. |
|
| 83 | + * @param string $tableName The table name. |
|
| 84 | + * @param string $dataSchema The name of the schema with data tables. |
|
| 85 | + * @param string $auditSchema The name of the schema with audit tables. |
|
| 86 | + * @param array[] $configColumnsMetadata The columns of the data table as stored in the config file. |
|
| 87 | + * @param array[] $auditColumnsMetadata The columns of the audit table as stored in the config file. |
|
| 88 | + * @param string $alias An unique alias for this table. |
|
| 89 | + * @param string $skipVariable The skip variable |
|
| 90 | + */ |
|
| 91 | + public function __construct($io, |
|
| 92 | + $tableName, |
|
| 93 | + $dataSchema, |
|
| 94 | + $auditSchema, |
|
| 95 | + $configColumnsMetadata, |
|
| 96 | + $auditColumnsMetadata, |
|
| 97 | + $alias, |
|
| 98 | + $skipVariable) |
|
| 99 | + { |
|
| 100 | 100 | $this->io = $io; |
| 101 | 101 | $this->tableName = $tableName; |
| 102 | 102 | $this->dataTableColumnsConfig = new Columns($configColumnsMetadata); |
@@ -106,39 +106,39 @@ discard block |
||
| 106 | 106 | $this->auditColumns = new Columns($auditColumnsMetadata); |
| 107 | 107 | $this->alias = $alias; |
| 108 | 108 | $this->skipVariable = $skipVariable; |
| 109 | - } |
|
| 110 | - |
|
| 111 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 112 | - /** |
|
| 113 | - * Returns a random alias for this table. |
|
| 114 | - * |
|
| 115 | - * @return string |
|
| 116 | - */ |
|
| 117 | - public static function getRandomAlias() |
|
| 118 | - { |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 112 | + /** |
|
| 113 | + * Returns a random alias for this table. |
|
| 114 | + * |
|
| 115 | + * @return string |
|
| 116 | + */ |
|
| 117 | + public static function getRandomAlias() |
|
| 118 | + { |
|
| 119 | 119 | return uniqid(); |
| 120 | - } |
|
| 121 | - |
|
| 122 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 123 | - /** |
|
| 124 | - * Creates missing audit table for this table. |
|
| 125 | - */ |
|
| 126 | - public function createMissingAuditTable() |
|
| 127 | - { |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 123 | + /** |
|
| 124 | + * Creates missing audit table for this table. |
|
| 125 | + */ |
|
| 126 | + public function createMissingAuditTable() |
|
| 127 | + { |
|
| 128 | 128 | $this->io->logInfo('Creating audit table <dbo>%s.%s<dbo>', $this->auditSchemaName, $this->tableName); |
| 129 | 129 | |
| 130 | 130 | $columns = Columns::combine($this->auditColumns, $this->dataTableColumnsDatabase); |
| 131 | 131 | DataLayer::createAuditTable($this->dataSchemaName, $this->auditSchemaName, $this->tableName, $columns); |
| 132 | - } |
|
| 133 | - |
|
| 134 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 135 | - /** |
|
| 136 | - * Creates audit triggers on this table. |
|
| 137 | - * |
|
| 138 | - * @param string[] $additionalSql Additional SQL statements to be include in triggers. |
|
| 139 | - */ |
|
| 140 | - public function createTriggers($additionalSql) |
|
| 141 | - { |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 135 | + /** |
|
| 136 | + * Creates audit triggers on this table. |
|
| 137 | + * |
|
| 138 | + * @param string[] $additionalSql Additional SQL statements to be include in triggers. |
|
| 139 | + */ |
|
| 140 | + public function createTriggers($additionalSql) |
|
| 141 | + { |
|
| 142 | 142 | // Lock the table to prevent insert, updates, or deletes between dropping and creating triggers. |
| 143 | 143 | $this->lockTable($this->tableName); |
| 144 | 144 | |
@@ -152,142 +152,142 @@ discard block |
||
| 152 | 152 | |
| 153 | 153 | // Insert, updates, and deletes are no audited again. So, release lock on the table. |
| 154 | 154 | $this->unlockTables(); |
| 155 | - } |
|
| 156 | - |
|
| 157 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 158 | - /** |
|
| 159 | - * Returns the name of this table. |
|
| 160 | - * |
|
| 161 | - * @return string |
|
| 162 | - */ |
|
| 163 | - public function getTableName() |
|
| 164 | - { |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 158 | + /** |
|
| 159 | + * Returns the name of this table. |
|
| 160 | + * |
|
| 161 | + * @return string |
|
| 162 | + */ |
|
| 163 | + public function getTableName() |
|
| 164 | + { |
|
| 165 | 165 | return $this->tableName; |
| 166 | - } |
|
| 167 | - |
|
| 168 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 169 | - /** |
|
| 170 | - * Main function for work with table. |
|
| 171 | - * |
|
| 172 | - * @param string[] $additionalSql Additional SQL statements to be include in triggers. |
|
| 173 | - * |
|
| 174 | - * @return \array[] Columns for config file |
|
| 175 | - */ |
|
| 176 | - public function main($additionalSql) |
|
| 177 | - { |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 169 | + /** |
|
| 170 | + * Main function for work with table. |
|
| 171 | + * |
|
| 172 | + * @param string[] $additionalSql Additional SQL statements to be include in triggers. |
|
| 173 | + * |
|
| 174 | + * @return \array[] Columns for config file |
|
| 175 | + */ |
|
| 176 | + public function main($additionalSql) |
|
| 177 | + { |
|
| 178 | 178 | $comparedColumns = null; |
| 179 | 179 | if (isset($this->dataTableColumnsConfig)) |
| 180 | 180 | { |
| 181 | - $comparedColumns = $this->getTableColumnInfo(); |
|
| 181 | + $comparedColumns = $this->getTableColumnInfo(); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | $newColumns = $comparedColumns['new_columns']->getColumns(); |
| 185 | 185 | $obsoleteColumns = $comparedColumns['obsolete_columns']->getColumns(); |
| 186 | 186 | if (empty($newColumns) && empty($obsoleteColumns)) |
| 187 | 187 | { |
| 188 | - $alteredColumns = $comparedColumns['altered_columns']->getColumns(); |
|
| 189 | - if (empty($alteredColumns)) |
|
| 190 | - { |
|
| 188 | + $alteredColumns = $comparedColumns['altered_columns']->getColumns(); |
|
| 189 | + if (empty($alteredColumns)) |
|
| 190 | + { |
|
| 191 | 191 | $this->createTriggers($additionalSql); |
| 192 | - } |
|
| 192 | + } |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | return $comparedColumns; |
| 196 | - } |
|
| 197 | - |
|
| 198 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 199 | - /** |
|
| 200 | - * Adds new columns to audit table. |
|
| 201 | - * |
|
| 202 | - * @param Columns $columns Columns array |
|
| 203 | - */ |
|
| 204 | - private function addNewColumns($columns) |
|
| 205 | - { |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 199 | + /** |
|
| 200 | + * Adds new columns to audit table. |
|
| 201 | + * |
|
| 202 | + * @param Columns $columns Columns array |
|
| 203 | + */ |
|
| 204 | + private function addNewColumns($columns) |
|
| 205 | + { |
|
| 206 | 206 | DataLayer::addNewColumns($this->auditSchemaName, $this->tableName, $columns); |
| 207 | - } |
|
| 208 | - |
|
| 209 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 210 | - /** |
|
| 211 | - * Creates a triggers for this table. |
|
| 212 | - * |
|
| 213 | - * @param string $action The trigger action (INSERT, DELETE, or UPDATE). |
|
| 214 | - * @param string|null $skipVariable |
|
| 215 | - * @param string[] $additionSql The additional SQL statements to be included in triggers. |
|
| 216 | - */ |
|
| 217 | - private function createTableTrigger($action, $skipVariable, $additionSql) |
|
| 218 | - { |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 210 | + /** |
|
| 211 | + * Creates a triggers for this table. |
|
| 212 | + * |
|
| 213 | + * @param string $action The trigger action (INSERT, DELETE, or UPDATE). |
|
| 214 | + * @param string|null $skipVariable |
|
| 215 | + * @param string[] $additionSql The additional SQL statements to be included in triggers. |
|
| 216 | + */ |
|
| 217 | + private function createTableTrigger($action, $skipVariable, $additionSql) |
|
| 218 | + { |
|
| 219 | 219 | $triggerName = $this->getTriggerName($action); |
| 220 | 220 | |
| 221 | 221 | $this->io->logVerbose('Creating trigger <dbo>%s.%s</dbo> on table <dbo>%s.%s</dbo>', |
| 222 | - $this->dataSchemaName, |
|
| 223 | - $triggerName, |
|
| 224 | - $this->dataSchemaName, |
|
| 225 | - $this->tableName); |
|
| 222 | + $this->dataSchemaName, |
|
| 223 | + $triggerName, |
|
| 224 | + $this->dataSchemaName, |
|
| 225 | + $this->tableName); |
|
| 226 | 226 | |
| 227 | 227 | DataLayer::createAuditTrigger($this->dataSchemaName, |
| 228 | - $this->auditSchemaName, |
|
| 229 | - $this->tableName, |
|
| 230 | - $triggerName, |
|
| 231 | - $action, |
|
| 232 | - $this->auditColumns, |
|
| 233 | - $this->dataTableColumnsDatabase, |
|
| 234 | - $skipVariable, |
|
| 235 | - $additionSql); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 239 | - /** |
|
| 240 | - * Drops all triggers from this table. |
|
| 241 | - */ |
|
| 242 | - private function dropTriggers() |
|
| 243 | - { |
|
| 228 | + $this->auditSchemaName, |
|
| 229 | + $this->tableName, |
|
| 230 | + $triggerName, |
|
| 231 | + $action, |
|
| 232 | + $this->auditColumns, |
|
| 233 | + $this->dataTableColumnsDatabase, |
|
| 234 | + $skipVariable, |
|
| 235 | + $additionSql); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 239 | + /** |
|
| 240 | + * Drops all triggers from this table. |
|
| 241 | + */ |
|
| 242 | + private function dropTriggers() |
|
| 243 | + { |
|
| 244 | 244 | $triggers = DataLayer::getTableTriggers($this->dataSchemaName, $this->tableName); |
| 245 | 245 | foreach ($triggers as $trigger) |
| 246 | 246 | { |
| 247 | - $this->io->logVerbose('Dropping trigger <dbo>%s</dbo> on <dbo>%s.%s</dbo>', |
|
| 247 | + $this->io->logVerbose('Dropping trigger <dbo>%s</dbo> on <dbo>%s.%s</dbo>', |
|
| 248 | 248 | $trigger['trigger_name'], |
| 249 | 249 | $this->dataSchemaName, |
| 250 | 250 | $this->tableName); |
| 251 | 251 | |
| 252 | - DataLayer::dropTrigger($this->dataSchemaName, $trigger['trigger_name']); |
|
| 252 | + DataLayer::dropTrigger($this->dataSchemaName, $trigger['trigger_name']); |
|
| 253 | + } |
|
| 253 | 254 | } |
| 254 | - } |
|
| 255 | - |
|
| 256 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 257 | - /** |
|
| 258 | - * Compares columns types from table in data_schema with columns in config file. |
|
| 259 | - * |
|
| 260 | - * @return Columns |
|
| 261 | - */ |
|
| 262 | - private function getAlteredColumns() |
|
| 263 | - { |
|
| 255 | + |
|
| 256 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 257 | + /** |
|
| 258 | + * Compares columns types from table in data_schema with columns in config file. |
|
| 259 | + * |
|
| 260 | + * @return Columns |
|
| 261 | + */ |
|
| 262 | + private function getAlteredColumns() |
|
| 263 | + { |
|
| 264 | 264 | $alteredColumnsTypes = Columns::differentColumnTypes($this->dataTableColumnsDatabase, |
| 265 | - $this->dataTableColumnsConfig); |
|
| 265 | + $this->dataTableColumnsConfig); |
|
| 266 | 266 | |
| 267 | 267 | return $alteredColumnsTypes; |
| 268 | - } |
|
| 269 | - |
|
| 270 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 271 | - /** |
|
| 272 | - * Selects and returns the metadata of the columns of this table from information_schema. |
|
| 273 | - * |
|
| 274 | - * @return array[] |
|
| 275 | - */ |
|
| 276 | - private function getColumnsFromInformationSchema() |
|
| 277 | - { |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 271 | + /** |
|
| 272 | + * Selects and returns the metadata of the columns of this table from information_schema. |
|
| 273 | + * |
|
| 274 | + * @return array[] |
|
| 275 | + */ |
|
| 276 | + private function getColumnsFromInformationSchema() |
|
| 277 | + { |
|
| 278 | 278 | $result = DataLayer::getTableColumns($this->dataSchemaName, $this->tableName); |
| 279 | 279 | |
| 280 | 280 | return $result; |
| 281 | - } |
|
| 282 | - |
|
| 283 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 284 | - /** |
|
| 285 | - * Compare columns from table in data_schema with columns in config file. |
|
| 286 | - * |
|
| 287 | - * @return \array[] |
|
| 288 | - */ |
|
| 289 | - private function getTableColumnInfo() |
|
| 290 | - { |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 284 | + /** |
|
| 285 | + * Compare columns from table in data_schema with columns in config file. |
|
| 286 | + * |
|
| 287 | + * @return \array[] |
|
| 288 | + */ |
|
| 289 | + private function getTableColumnInfo() |
|
| 290 | + { |
|
| 291 | 291 | $columnActual = new Columns(DataLayer::getTableColumns($this->auditSchemaName, $this->tableName)); |
| 292 | 292 | $columnsConfig = Columns::combine($this->auditColumns, $this->dataTableColumnsConfig); |
| 293 | 293 | $columnsTarget = Columns::combine($this->auditColumns, $this->dataTableColumnsDatabase); |
@@ -303,112 +303,112 @@ discard block |
||
| 303 | 303 | 'new_columns' => $newColumns, |
| 304 | 304 | 'obsolete_columns' => $obsoleteColumns, |
| 305 | 305 | 'altered_columns' => $alteredColumns]; |
| 306 | - } |
|
| 307 | - |
|
| 308 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 309 | - /** |
|
| 310 | - * Check for know what columns array returns. |
|
| 311 | - * |
|
| 312 | - * @param Columns $newColumns |
|
| 313 | - * @param Columns $obsoleteColumns |
|
| 314 | - * |
|
| 315 | - * @return Columns |
|
| 316 | - */ |
|
| 317 | - private function getTableColumnsFromConfig($newColumns, $obsoleteColumns) |
|
| 318 | - { |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 309 | + /** |
|
| 310 | + * Check for know what columns array returns. |
|
| 311 | + * |
|
| 312 | + * @param Columns $newColumns |
|
| 313 | + * @param Columns $obsoleteColumns |
|
| 314 | + * |
|
| 315 | + * @return Columns |
|
| 316 | + */ |
|
| 317 | + private function getTableColumnsFromConfig($newColumns, $obsoleteColumns) |
|
| 318 | + { |
|
| 319 | 319 | $new = $newColumns->getColumns(); |
| 320 | 320 | $obsolete = $obsoleteColumns->getColumns(); |
| 321 | 321 | if (!empty($new) && !empty($obsolete)) |
| 322 | 322 | { |
| 323 | - return $this->dataTableColumnsConfig; |
|
| 323 | + return $this->dataTableColumnsConfig; |
|
| 324 | 324 | } |
| 325 | 325 | |
| 326 | 326 | return $this->dataTableColumnsDatabase; |
| 327 | - } |
|
| 328 | - |
|
| 329 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 330 | - /** |
|
| 331 | - * Create and return trigger name. |
|
| 332 | - * |
|
| 333 | - * @param string $action Trigger on action (Insert, Update, Delete) |
|
| 334 | - * |
|
| 335 | - * @return string |
|
| 336 | - */ |
|
| 337 | - private function getTriggerName($action) |
|
| 338 | - { |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 330 | + /** |
|
| 331 | + * Create and return trigger name. |
|
| 332 | + * |
|
| 333 | + * @param string $action Trigger on action (Insert, Update, Delete) |
|
| 334 | + * |
|
| 335 | + * @return string |
|
| 336 | + */ |
|
| 337 | + private function getTriggerName($action) |
|
| 338 | + { |
|
| 339 | 339 | return strtolower(sprintf('trg_%s_%s', $this->alias, $action)); |
| 340 | - } |
|
| 341 | - |
|
| 342 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 343 | - /** |
|
| 344 | - * Lock the table to prevent insert, updates, or deletes between dropping and creating triggers. |
|
| 345 | - * |
|
| 346 | - * @param string $tableName Name of table |
|
| 347 | - */ |
|
| 348 | - private function lockTable($tableName) |
|
| 349 | - { |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 343 | + /** |
|
| 344 | + * Lock the table to prevent insert, updates, or deletes between dropping and creating triggers. |
|
| 345 | + * |
|
| 346 | + * @param string $tableName Name of table |
|
| 347 | + */ |
|
| 348 | + private function lockTable($tableName) |
|
| 349 | + { |
|
| 350 | 350 | DataLayer::lockTable($tableName); |
| 351 | - } |
|
| 352 | - |
|
| 353 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 354 | - /** |
|
| 355 | - * Logging new and obsolete columns. |
|
| 356 | - * |
|
| 357 | - * @param Columns $newColumns |
|
| 358 | - * @param Columns $obsoleteColumns |
|
| 359 | - * @param Columns $alteredColumns |
|
| 360 | - */ |
|
| 361 | - private function loggingColumnInfo($newColumns, $obsoleteColumns, $alteredColumns) |
|
| 362 | - { |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 354 | + /** |
|
| 355 | + * Logging new and obsolete columns. |
|
| 356 | + * |
|
| 357 | + * @param Columns $newColumns |
|
| 358 | + * @param Columns $obsoleteColumns |
|
| 359 | + * @param Columns $alteredColumns |
|
| 360 | + */ |
|
| 361 | + private function loggingColumnInfo($newColumns, $obsoleteColumns, $alteredColumns) |
|
| 362 | + { |
|
| 363 | 363 | $new = $newColumns->getColumns(); |
| 364 | 364 | $obsolete = $obsoleteColumns->getColumns(); |
| 365 | 365 | if (!empty($new) && !empty($obsolete)) |
| 366 | 366 | { |
| 367 | - $this->io->logInfo('Found both new and obsolete columns for table %s', $this->tableName); |
|
| 368 | - $this->io->logInfo('No action taken'); |
|
| 367 | + $this->io->logInfo('Found both new and obsolete columns for table %s', $this->tableName); |
|
| 368 | + $this->io->logInfo('No action taken'); |
|
| 369 | 369 | |
| 370 | - /** @var ColumnType $column */ |
|
| 371 | - foreach ($newColumns->getColumns() as $column) |
|
| 372 | - { |
|
| 370 | + /** @var ColumnType $column */ |
|
| 371 | + foreach ($newColumns->getColumns() as $column) |
|
| 372 | + { |
|
| 373 | 373 | $this->io->logInfo('New column %s', $column->getProperty('column_name')); |
| 374 | - } |
|
| 375 | - foreach ($obsoleteColumns->getColumns() as $column) |
|
| 376 | - { |
|
| 374 | + } |
|
| 375 | + foreach ($obsoleteColumns->getColumns() as $column) |
|
| 376 | + { |
|
| 377 | 377 | $this->io->logInfo('Obsolete column %s', $column->getProperty('column_name')); |
| 378 | - } |
|
| 378 | + } |
|
| 379 | 379 | } |
| 380 | 380 | |
| 381 | 381 | /** @var ColumnType $column */ |
| 382 | 382 | foreach ($obsoleteColumns->getColumns() as $column) |
| 383 | 383 | { |
| 384 | - $this->io->logInfo('Obsolete column %s.%s', $this->tableName, $column->getProperty('column_name')); |
|
| 384 | + $this->io->logInfo('Obsolete column %s.%s', $this->tableName, $column->getProperty('column_name')); |
|
| 385 | 385 | } |
| 386 | 386 | |
| 387 | 387 | /** @var ColumnType $column */ |
| 388 | 388 | foreach ($newColumns->getColumns() as $column) |
| 389 | 389 | { |
| 390 | - $this->io->logInfo('New column %s.%s', $this->tableName, $column->getProperty('column_name')); |
|
| 390 | + $this->io->logInfo('New column %s.%s', $this->tableName, $column->getProperty('column_name')); |
|
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | foreach ($alteredColumns->getColumns() as $column) |
| 394 | 394 | { |
| 395 | - $this->io->logInfo('Type of <dbo>%s.%s</dbo> has been altered to <dbo>%s</dbo>', |
|
| 396 | - $this->tableName, |
|
| 397 | - $column['column_name'], |
|
| 398 | - $column['column_type']); |
|
| 395 | + $this->io->logInfo('Type of <dbo>%s.%s</dbo> has been altered to <dbo>%s</dbo>', |
|
| 396 | + $this->tableName, |
|
| 397 | + $column['column_name'], |
|
| 398 | + $column['column_type']); |
|
| 399 | + } |
|
| 399 | 400 | } |
| 400 | - } |
|
| 401 | - |
|
| 402 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 403 | - /** |
|
| 404 | - * Releases all table locks. |
|
| 405 | - */ |
|
| 406 | - private function unlockTables() |
|
| 407 | - { |
|
| 401 | + |
|
| 402 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 403 | + /** |
|
| 404 | + * Releases all table locks. |
|
| 405 | + */ |
|
| 406 | + private function unlockTables() |
|
| 407 | + { |
|
| 408 | 408 | DataLayer::unlockTables(); |
| 409 | - } |
|
| 409 | + } |
|
| 410 | 410 | |
| 411 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 411 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 412 | 412 | } |
| 413 | 413 | |
| 414 | 414 | //---------------------------------------------------------------------------------------------------------------------- |