@@ -268,7 +268,7 @@ |
||
| 268 | 268 | /** |
| 269 | 269 | * Selects and returns the metadata of the columns of this table from information_schema. |
| 270 | 270 | * |
| 271 | - * @return array[] |
|
| 271 | + * @return \array[] |
|
| 272 | 272 | */ |
| 273 | 273 | private function getColumnsFromInformationSchema() |
| 274 | 274 | { |
@@ -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,108 +303,108 @@ 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'); |
|
| 369 | - foreach ($newColumns->getColumns() as $column) |
|
| 370 | - { |
|
| 367 | + $this->io->logInfo('Found both new and obsolete columns for table %s', $this->tableName); |
|
| 368 | + $this->io->logInfo('No action taken'); |
|
| 369 | + foreach ($newColumns->getColumns() as $column) |
|
| 370 | + { |
|
| 371 | 371 | $this->io->logInfo('New column %s', $column['column_name']); |
| 372 | - } |
|
| 373 | - foreach ($obsoleteColumns->getColumns() as $column) |
|
| 374 | - { |
|
| 372 | + } |
|
| 373 | + foreach ($obsoleteColumns->getColumns() as $column) |
|
| 374 | + { |
|
| 375 | 375 | $this->io->logInfo('Obsolete column %s', $column['column_name']); |
| 376 | - } |
|
| 376 | + } |
|
| 377 | 377 | } |
| 378 | 378 | |
| 379 | 379 | foreach ($obsoleteColumns->getColumns() as $column) |
| 380 | 380 | { |
| 381 | - $this->io->logInfo('Obsolete column %s.%s', $this->tableName, $column['column_name']); |
|
| 381 | + $this->io->logInfo('Obsolete column %s.%s', $this->tableName, $column['column_name']); |
|
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | foreach ($newColumns->getColumns() as $column) |
| 385 | 385 | { |
| 386 | - $this->io->logInfo('New column %s.%s', $this->tableName, $column['column_name']); |
|
| 386 | + $this->io->logInfo('New column %s.%s', $this->tableName, $column['column_name']); |
|
| 387 | 387 | } |
| 388 | 388 | |
| 389 | 389 | foreach ($alteredColumns->getColumns() as $column) |
| 390 | 390 | { |
| 391 | - $this->io->logInfo('Type of <dbo>%s.%s</dbo> has been altered to <dbo>%s</dbo>', |
|
| 392 | - $this->tableName, |
|
| 393 | - $column['column_name'], |
|
| 394 | - $column['column_type']); |
|
| 391 | + $this->io->logInfo('Type of <dbo>%s.%s</dbo> has been altered to <dbo>%s</dbo>', |
|
| 392 | + $this->tableName, |
|
| 393 | + $column['column_name'], |
|
| 394 | + $column['column_type']); |
|
| 395 | + } |
|
| 395 | 396 | } |
| 396 | - } |
|
| 397 | - |
|
| 398 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 399 | - /** |
|
| 400 | - * Releases all table locks. |
|
| 401 | - */ |
|
| 402 | - private function unlockTables() |
|
| 403 | - { |
|
| 397 | + |
|
| 398 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 399 | + /** |
|
| 400 | + * Releases all table locks. |
|
| 401 | + */ |
|
| 402 | + private function unlockTables() |
|
| 403 | + { |
|
| 404 | 404 | DataLayer::unlockTables(); |
| 405 | - } |
|
| 405 | + } |
|
| 406 | 406 | |
| 407 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 407 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 408 | 408 | } |
| 409 | 409 | |
| 410 | 410 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -114,7 +114,7 @@ |
||
| 114 | 114 | if ($trim) $line = trim($line); |
| 115 | 115 | |
| 116 | 116 | $words = explode(' ', $line); |
| 117 | - if (count($words)>0) |
|
| 117 | + if (count($words) > 0) |
|
| 118 | 118 | { |
| 119 | 119 | switch ($words[0]) |
| 120 | 120 | { |
@@ -10,150 +10,150 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class CompoundSyntaxStore |
| 12 | 12 | { |
| 13 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 14 | - /** |
|
| 15 | - * The the number of spaces per indent level. |
|
| 16 | - * |
|
| 17 | - * @var int |
|
| 18 | - */ |
|
| 19 | - public static $indentation = 2; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * The current indent level of the code. |
|
| 23 | - * |
|
| 24 | - * @var int |
|
| 25 | - */ |
|
| 26 | - private $indentLevel = 0; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The source code. Each element is a line. |
|
| 30 | - * |
|
| 31 | - * @var string[] |
|
| 32 | - */ |
|
| 33 | - private $lines = []; |
|
| 34 | - |
|
| 35 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 36 | - /** |
|
| 37 | - * Object constructor. |
|
| 38 | - */ |
|
| 39 | - public function __construct() |
|
| 40 | - { |
|
| 13 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 14 | + /** |
|
| 15 | + * The the number of spaces per indent level. |
|
| 16 | + * |
|
| 17 | + * @var int |
|
| 18 | + */ |
|
| 19 | + public static $indentation = 2; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * The current indent level of the code. |
|
| 23 | + * |
|
| 24 | + * @var int |
|
| 25 | + */ |
|
| 26 | + private $indentLevel = 0; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The source code. Each element is a line. |
|
| 30 | + * |
|
| 31 | + * @var string[] |
|
| 32 | + */ |
|
| 33 | + private $lines = []; |
|
| 34 | + |
|
| 35 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 36 | + /** |
|
| 37 | + * Object constructor. |
|
| 38 | + */ |
|
| 39 | + public function __construct() |
|
| 40 | + { |
|
| 41 | 41 | // Nothing to do. |
| 42 | - } |
|
| 43 | - |
|
| 44 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 45 | - /** |
|
| 46 | - * Appends a line or lines of code this this code. |
|
| 47 | - * |
|
| 48 | - * @param null|string|string[] $line The line or lines of code to be appended. |
|
| 49 | - * @param bool $trim If true the line or lines of code will be trimmed before appending. |
|
| 50 | - */ |
|
| 51 | - public function append($line, $trim = true) |
|
| 52 | - { |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 45 | + /** |
|
| 46 | + * Appends a line or lines of code this this code. |
|
| 47 | + * |
|
| 48 | + * @param null|string|string[] $line The line or lines of code to be appended. |
|
| 49 | + * @param bool $trim If true the line or lines of code will be trimmed before appending. |
|
| 50 | + */ |
|
| 51 | + public function append($line, $trim = true) |
|
| 52 | + { |
|
| 53 | 53 | switch (true) |
| 54 | 54 | { |
| 55 | - case is_string($line): |
|
| 55 | + case is_string($line): |
|
| 56 | 56 | $this->appendLine($line, $trim); |
| 57 | 57 | break; |
| 58 | 58 | |
| 59 | - case is_array($line): |
|
| 59 | + case is_array($line): |
|
| 60 | 60 | $this->appendLines($line, $trim); |
| 61 | 61 | break; |
| 62 | 62 | |
| 63 | - case is_null($line): |
|
| 63 | + case is_null($line): |
|
| 64 | 64 | // Nothing to do. |
| 65 | 65 | break; |
| 66 | 66 | |
| 67 | - default: |
|
| 67 | + default: |
|
| 68 | 68 | throw new RuntimeException('Nor a string or array.'); |
| 69 | 69 | } |
| 70 | - } |
|
| 71 | - |
|
| 72 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 73 | - /** |
|
| 74 | - * Appends a part of code to the last line of code. |
|
| 75 | - * |
|
| 76 | - * @param string $part The part of code to be to the last line. |
|
| 77 | - */ |
|
| 78 | - public function appendToLastLine($part) |
|
| 79 | - { |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 73 | + /** |
|
| 74 | + * Appends a part of code to the last line of code. |
|
| 75 | + * |
|
| 76 | + * @param string $part The part of code to be to the last line. |
|
| 77 | + */ |
|
| 78 | + public function appendToLastLine($part) |
|
| 79 | + { |
|
| 80 | 80 | $this->lines[count($this->lines) - 1] .= $part; |
| 81 | - } |
|
| 82 | - |
|
| 83 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 84 | - /** |
|
| 85 | - * Returns the generated code as a single string. |
|
| 86 | - */ |
|
| 87 | - public function getCode() |
|
| 88 | - { |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 84 | + /** |
|
| 85 | + * Returns the generated code as a single string. |
|
| 86 | + */ |
|
| 87 | + public function getCode() |
|
| 88 | + { |
|
| 89 | 89 | return implode(PHP_EOL, $this->lines); |
| 90 | - } |
|
| 91 | - |
|
| 92 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 93 | - /** |
|
| 94 | - * Returns a line of code with the proper amount of indentation. |
|
| 95 | - * |
|
| 96 | - * @param string $line The line of code. |
|
| 97 | - * |
|
| 98 | - * @return string The indented line of code. |
|
| 99 | - */ |
|
| 100 | - private function addIndentation($line) |
|
| 101 | - { |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 93 | + /** |
|
| 94 | + * Returns a line of code with the proper amount of indentation. |
|
| 95 | + * |
|
| 96 | + * @param string $line The line of code. |
|
| 97 | + * |
|
| 98 | + * @return string The indented line of code. |
|
| 99 | + */ |
|
| 100 | + private function addIndentation($line) |
|
| 101 | + { |
|
| 102 | 102 | return str_repeat(' ', self::$indentation * $this->indentLevel).$line; |
| 103 | - } |
|
| 104 | - |
|
| 105 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 106 | - /** |
|
| 107 | - * Appends a line of code this this code. |
|
| 108 | - * |
|
| 109 | - * @param string $line The line of code to be appended. |
|
| 110 | - * @param bool $trim If true the line of code will be trimmed before appending. |
|
| 111 | - */ |
|
| 112 | - private function appendLine($line, $trim) |
|
| 113 | - { |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 106 | + /** |
|
| 107 | + * Appends a line of code this this code. |
|
| 108 | + * |
|
| 109 | + * @param string $line The line of code to be appended. |
|
| 110 | + * @param bool $trim If true the line of code will be trimmed before appending. |
|
| 111 | + */ |
|
| 112 | + private function appendLine($line, $trim) |
|
| 113 | + { |
|
| 114 | 114 | if ($trim) $line = trim($line); |
| 115 | 115 | |
| 116 | 116 | $words = explode(' ', $line); |
| 117 | 117 | if (count($words)>0) |
| 118 | 118 | { |
| 119 | - switch ($words[0]) |
|
| 120 | - { |
|
| 119 | + switch ($words[0]) |
|
| 120 | + { |
|
| 121 | 121 | case 'begin': |
| 122 | 122 | case 'if': |
| 123 | 123 | $line = $this->addIndentation($line); |
| 124 | - $this->indentLevel += 1; |
|
| 125 | - break; |
|
| 124 | + $this->indentLevel += 1; |
|
| 125 | + break; |
|
| 126 | 126 | |
| 127 | 127 | case 'end': |
| 128 | 128 | $this->indentLevel = max(0, $this->indentLevel - 1); |
| 129 | - $line = $this->addIndentation($line); |
|
| 130 | - break; |
|
| 129 | + $line = $this->addIndentation($line); |
|
| 130 | + break; |
|
| 131 | 131 | |
| 132 | 132 | default: |
| 133 | 133 | $line = $this->addIndentation($line); |
| 134 | - break; |
|
| 135 | - } |
|
| 134 | + break; |
|
| 135 | + } |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | $this->lines[] = $line; |
| 139 | - } |
|
| 140 | - |
|
| 141 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 142 | - /** |
|
| 143 | - * Appends lines of code this this code. |
|
| 144 | - * |
|
| 145 | - * @param string[] $lines The lines of code to be appended. |
|
| 146 | - * @param bool $trim If true the lines of code will be trimmed before appending. |
|
| 147 | - */ |
|
| 148 | - private function appendLines($lines, $trim) |
|
| 149 | - { |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 142 | + /** |
|
| 143 | + * Appends lines of code this this code. |
|
| 144 | + * |
|
| 145 | + * @param string[] $lines The lines of code to be appended. |
|
| 146 | + * @param bool $trim If true the lines of code will be trimmed before appending. |
|
| 147 | + */ |
|
| 148 | + private function appendLines($lines, $trim) |
|
| 149 | + { |
|
| 150 | 150 | foreach ($lines as $line) |
| 151 | 151 | { |
| 152 | - $this->appendLine($line, $trim); |
|
| 152 | + $this->appendLine($line, $trim); |
|
| 153 | + } |
|
| 153 | 154 | } |
| 154 | - } |
|
| 155 | 155 | |
| 156 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 156 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -111,7 +111,9 @@ |
||
| 111 | 111 | */ |
| 112 | 112 | private function appendLine($line, $trim) |
| 113 | 113 | { |
| 114 | - if ($trim) $line = trim($line); |
|
| 114 | + if ($trim) { |
|
| 115 | + $line = trim($line); |
|
| 116 | + } |
|
| 115 | 117 | |
| 116 | 118 | $words = explode(' ', $line); |
| 117 | 119 | if (count($words)>0) |
@@ -153,7 +153,7 @@ |
||
| 153 | 153 | $this->config['audit_columns'], |
| 154 | 154 | $this->config['tables'][$table['table_name']]['alias'], |
| 155 | 155 | $this->config['tables'][$table['table_name']]['skip']); |
| 156 | - $res = StaticDataLayer::searchInRowSet('table_name', $currentTable->getTableName(), $this->auditSchemaTables); |
|
| 156 | + $res = StaticDataLayer::searchInRowSet('table_name', $currentTable->getTableName(), $this->auditSchemaTables); |
|
| 157 | 157 | if (!isset($res)) |
| 158 | 158 | { |
| 159 | 159 | $currentTable->createMissingAuditTable(); |
@@ -17,113 +17,113 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class AuditCommand extends MySqlCommand |
| 19 | 19 | { |
| 20 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 21 | - /** |
|
| 22 | - * All tables in the in the audit schema. |
|
| 23 | - * |
|
| 24 | - * @var array |
|
| 25 | - */ |
|
| 26 | - protected $auditSchemaTables; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Array of tables from data schema. |
|
| 30 | - * |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - protected $dataSchemaTables; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * If true remove all column information from config file. |
|
| 37 | - * |
|
| 38 | - * @var boolean |
|
| 39 | - */ |
|
| 40 | - private $pruneOption; |
|
| 41 | - |
|
| 42 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 43 | - /** |
|
| 44 | - * Compares the tables listed in the config file and the tables found in the audit schema |
|
| 45 | - * |
|
| 46 | - * @param string $tableName Name of table |
|
| 47 | - * @param Columns $columns The table columns. |
|
| 48 | - */ |
|
| 49 | - public function getColumns($tableName, $columns) |
|
| 50 | - { |
|
| 20 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 21 | + /** |
|
| 22 | + * All tables in the in the audit schema. |
|
| 23 | + * |
|
| 24 | + * @var array |
|
| 25 | + */ |
|
| 26 | + protected $auditSchemaTables; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Array of tables from data schema. |
|
| 30 | + * |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + protected $dataSchemaTables; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * If true remove all column information from config file. |
|
| 37 | + * |
|
| 38 | + * @var boolean |
|
| 39 | + */ |
|
| 40 | + private $pruneOption; |
|
| 41 | + |
|
| 42 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 43 | + /** |
|
| 44 | + * Compares the tables listed in the config file and the tables found in the audit schema |
|
| 45 | + * |
|
| 46 | + * @param string $tableName Name of table |
|
| 47 | + * @param Columns $columns The table columns. |
|
| 48 | + */ |
|
| 49 | + public function getColumns($tableName, $columns) |
|
| 50 | + { |
|
| 51 | 51 | $newColumns = []; |
| 52 | 52 | foreach ($columns->getColumns() as $column) |
| 53 | 53 | { |
| 54 | - $newColumns[] = ['column_name' => $column['column_name'], |
|
| 55 | - 'column_type' => $column['column_type']]; |
|
| 54 | + $newColumns[] = ['column_name' => $column['column_name'], |
|
| 55 | + 'column_type' => $column['column_type']]; |
|
| 56 | 56 | } |
| 57 | 57 | $this->config['table_columns'][$tableName] = $newColumns; |
| 58 | 58 | |
| 59 | 59 | if ($this->pruneOption) |
| 60 | 60 | { |
| 61 | - $this->config['table_columns'] = []; |
|
| 61 | + $this->config['table_columns'] = []; |
|
| 62 | + } |
|
| 62 | 63 | } |
| 63 | - } |
|
| 64 | - |
|
| 65 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 66 | - /** |
|
| 67 | - * Getting list of all tables from information_schema of database from config file. |
|
| 68 | - */ |
|
| 69 | - public function listOfTables() |
|
| 70 | - { |
|
| 64 | + |
|
| 65 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 66 | + /** |
|
| 67 | + * Getting list of all tables from information_schema of database from config file. |
|
| 68 | + */ |
|
| 69 | + public function listOfTables() |
|
| 70 | + { |
|
| 71 | 71 | $this->dataSchemaTables = DataLayer::getTablesNames($this->config['database']['data_schema']); |
| 72 | 72 | |
| 73 | 73 | $this->auditSchemaTables = DataLayer::getTablesNames($this->config['database']['audit_schema']); |
| 74 | - } |
|
| 75 | - |
|
| 76 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 77 | - /** |
|
| 78 | - * Found tables in config file |
|
| 79 | - * |
|
| 80 | - * Compares the tables listed in the config file and the tables found in the data schema |
|
| 81 | - */ |
|
| 82 | - public function unknownTables() |
|
| 83 | - { |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 77 | + /** |
|
| 78 | + * Found tables in config file |
|
| 79 | + * |
|
| 80 | + * Compares the tables listed in the config file and the tables found in the data schema |
|
| 81 | + */ |
|
| 82 | + public function unknownTables() |
|
| 83 | + { |
|
| 84 | 84 | foreach ($this->dataSchemaTables as $table) |
| 85 | 85 | { |
| 86 | - if (isset($this->config['tables'][$table['table_name']])) |
|
| 87 | - { |
|
| 86 | + if (isset($this->config['tables'][$table['table_name']])) |
|
| 87 | + { |
|
| 88 | 88 | if (!isset($this->config['tables'][$table['table_name']]['audit'])) |
| 89 | 89 | { |
| 90 | - $this->io->writeln(sprintf('<info>AuditApplication flag is not set in table %s</info>', $table['table_name'])); |
|
| 90 | + $this->io->writeln(sprintf('<info>AuditApplication flag is not set in table %s</info>', $table['table_name'])); |
|
| 91 | 91 | } |
| 92 | 92 | else |
| 93 | 93 | { |
| 94 | - if ($this->config['tables'][$table['table_name']]['audit']) |
|
| 95 | - { |
|
| 94 | + if ($this->config['tables'][$table['table_name']]['audit']) |
|
| 95 | + { |
|
| 96 | 96 | $this->config['tables'][$table['table_name']]['alias'] = Table::getRandomAlias(); |
| 97 | - } |
|
| 97 | + } |
|
| 98 | + } |
|
| 98 | 99 | } |
| 99 | - } |
|
| 100 | - else |
|
| 101 | - { |
|
| 100 | + else |
|
| 101 | + { |
|
| 102 | 102 | $this->io->writeln(sprintf('<info>Found new table %s</info>', $table['table_name'])); |
| 103 | 103 | $this->config['tables'][$table['table_name']] = ['audit' => false, |
| 104 | - 'alias' => null, |
|
| 105 | - 'skip' => null]; |
|
| 106 | - } |
|
| 104 | + 'alias' => null, |
|
| 105 | + 'skip' => null]; |
|
| 106 | + } |
|
| 107 | 107 | } |
| 108 | - } |
|
| 109 | - |
|
| 110 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 111 | - /** |
|
| 112 | - * {@inheritdoc} |
|
| 113 | - */ |
|
| 114 | - protected function configure() |
|
| 115 | - { |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 111 | + /** |
|
| 112 | + * {@inheritdoc} |
|
| 113 | + */ |
|
| 114 | + protected function configure() |
|
| 115 | + { |
|
| 116 | 116 | $this->setName('audit') |
| 117 | - ->setDescription('Create (missing) audit table and (re)creates audit triggers') |
|
| 118 | - ->addArgument('config file', InputArgument::OPTIONAL, 'The audit configuration file', 'etc/audit.json'); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 122 | - /** |
|
| 123 | - * {@inheritdoc} |
|
| 124 | - */ |
|
| 125 | - protected function execute(InputInterface $input, OutputInterface $output) |
|
| 126 | - { |
|
| 117 | + ->setDescription('Create (missing) audit table and (re)creates audit triggers') |
|
| 118 | + ->addArgument('config file', InputArgument::OPTIONAL, 'The audit configuration file', 'etc/audit.json'); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 122 | + /** |
|
| 123 | + * {@inheritdoc} |
|
| 124 | + */ |
|
| 125 | + protected function execute(InputInterface $input, OutputInterface $output) |
|
| 126 | + { |
|
| 127 | 127 | $this->io = new StratumStyle($input, $output); |
| 128 | 128 | |
| 129 | 129 | $this->configFileName = $input->getArgument('config file'); |
@@ -138,52 +138,52 @@ discard block |
||
| 138 | 138 | |
| 139 | 139 | foreach ($this->dataSchemaTables as $table) |
| 140 | 140 | { |
| 141 | - if ($this->config['tables'][$table['table_name']]['audit']) |
|
| 142 | - { |
|
| 141 | + if ($this->config['tables'][$table['table_name']]['audit']) |
|
| 142 | + { |
|
| 143 | 143 | $tableColumns = []; |
| 144 | 144 | if (isset($this->config['table_columns'][$table['table_name']])) |
| 145 | 145 | { |
| 146 | - $tableColumns = $this->config['table_columns'][$table['table_name']]; |
|
| 146 | + $tableColumns = $this->config['table_columns'][$table['table_name']]; |
|
| 147 | 147 | } |
| 148 | 148 | $currentTable = new Table($this->io, |
| 149 | - $table['table_name'], |
|
| 150 | - $this->config['database']['data_schema'], |
|
| 151 | - $this->config['database']['audit_schema'], |
|
| 152 | - $tableColumns, |
|
| 153 | - $this->config['audit_columns'], |
|
| 154 | - $this->config['tables'][$table['table_name']]['alias'], |
|
| 155 | - $this->config['tables'][$table['table_name']]['skip']); |
|
| 149 | + $table['table_name'], |
|
| 150 | + $this->config['database']['data_schema'], |
|
| 151 | + $this->config['database']['audit_schema'], |
|
| 152 | + $tableColumns, |
|
| 153 | + $this->config['audit_columns'], |
|
| 154 | + $this->config['tables'][$table['table_name']]['alias'], |
|
| 155 | + $this->config['tables'][$table['table_name']]['skip']); |
|
| 156 | 156 | $res = StaticDataLayer::searchInRowSet('table_name', $currentTable->getTableName(), $this->auditSchemaTables); |
| 157 | 157 | if (!isset($res)) |
| 158 | 158 | { |
| 159 | - $currentTable->createMissingAuditTable(); |
|
| 159 | + $currentTable->createMissingAuditTable(); |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | $columns = $currentTable->main($this->config['additional_sql']); |
| 163 | 163 | $alteredColumns = $columns['altered_columns']->getColumns(); |
| 164 | 164 | if (empty($alteredColumns)) |
| 165 | 165 | { |
| 166 | - $this->getColumns($currentTable->getTableName(), $columns['full_columns']); |
|
| 166 | + $this->getColumns($currentTable->getTableName(), $columns['full_columns']); |
|
| 167 | + } |
|
| 167 | 168 | } |
| 168 | - } |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | // Drop database connection |
| 172 | 172 | DataLayer::disconnect(); |
| 173 | 173 | |
| 174 | 174 | $this->rewriteConfig(); |
| 175 | - } |
|
| 176 | - |
|
| 177 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 178 | - /** |
|
| 179 | - * Write new data to config file. |
|
| 180 | - */ |
|
| 181 | - private function rewriteConfig() |
|
| 182 | - { |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 178 | + /** |
|
| 179 | + * Write new data to config file. |
|
| 180 | + */ |
|
| 181 | + private function rewriteConfig() |
|
| 182 | + { |
|
| 183 | 183 | $this->writeTwoPhases($this->configFileName, json_encode($this->config, JSON_PRETTY_PRINT)); |
| 184 | - } |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 186 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -88,16 +88,14 @@ |
||
| 88 | 88 | if (!isset($this->config['tables'][$table['table_name']]['audit'])) |
| 89 | 89 | { |
| 90 | 90 | $this->io->writeln(sprintf('<info>AuditApplication flag is not set in table %s</info>', $table['table_name'])); |
| 91 | - } |
|
| 92 | - else |
|
| 91 | + } else |
|
| 93 | 92 | { |
| 94 | 93 | if ($this->config['tables'][$table['table_name']]['audit']) |
| 95 | 94 | { |
| 96 | 95 | $this->config['tables'][$table['table_name']]['alias'] = Table::getRandomAlias(); |
| 97 | 96 | } |
| 98 | 97 | } |
| 99 | - } |
|
| 100 | - else |
|
| 98 | + } else |
|
| 101 | 99 | { |
| 102 | 100 | $this->io->writeln(sprintf('<info>Found new table %s</info>', $table['table_name'])); |
| 103 | 101 | $this->config['tables'][$table['table_name']] = ['audit' => false, |
@@ -34,7 +34,7 @@ |
||
| 34 | 34 | $this->io->write(<<<EOT |
| 35 | 35 | <info>AuditApplication - Database Auditing</info> |
| 36 | 36 | <comment>Creates audit tables and triggers to track data changes in databases.</comment> |
| 37 | -EOT |
|
| 37 | +eot |
|
| 38 | 38 | ); |
| 39 | 39 | } |
| 40 | 40 | |
@@ -12,23 +12,23 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | class AboutCommand extends BaseCommand |
| 14 | 14 | { |
| 15 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - protected function configure() |
|
| 20 | - { |
|
| 15 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + protected function configure() |
|
| 20 | + { |
|
| 21 | 21 | $this->setName('about') |
| 22 | - ->setDescription('Short information about AuditApplication') |
|
| 23 | - ->setHelp('<info>audit about</info>'); |
|
| 24 | - } |
|
| 22 | + ->setDescription('Short information about AuditApplication') |
|
| 23 | + ->setHelp('<info>audit about</info>'); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 27 | - /** |
|
| 28 | - * {@inheritdoc} |
|
| 29 | - */ |
|
| 30 | - protected function execute(InputInterface $input, OutputInterface $output) |
|
| 31 | - { |
|
| 26 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 27 | + /** |
|
| 28 | + * {@inheritdoc} |
|
| 29 | + */ |
|
| 30 | + protected function execute(InputInterface $input, OutputInterface $output) |
|
| 31 | + { |
|
| 32 | 32 | $this->io = $this->io = new StratumStyle($input, $output); |
| 33 | 33 | |
| 34 | 34 | $this->io->write(<<<EOT |
@@ -36,9 +36,9 @@ discard block |
||
| 36 | 36 | <comment>Creates audit tables and triggers to track data changes in databases.</comment> |
| 37 | 37 | EOT |
| 38 | 38 | ); |
| 39 | - } |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 41 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | * |
| 123 | 123 | * @param array[] $columns The metadata of the columns. |
| 124 | 124 | * |
| 125 | - * @return array[] |
|
| 125 | + * @return \array[] |
|
| 126 | 126 | */ |
| 127 | 127 | private function addHighlighting($columns) |
| 128 | 128 | { |
@@ -253,7 +253,7 @@ discard block |
||
| 253 | 253 | /** |
| 254 | 254 | * Add not null to audit columns if it not nullable. |
| 255 | 255 | * |
| 256 | - * @param array $theColumns Audit columns. |
|
| 256 | + * @param \array[] $theColumns Audit columns. |
|
| 257 | 257 | * |
| 258 | 258 | * @return array |
| 259 | 259 | */ |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | $cleaned = []; |
| 58 | 58 | foreach ($columns as $column) |
| 59 | 59 | { |
| 60 | - if (($column['data_table_type']!=$column['audit_table_type'] && $column['audit_table_type']!=$column['config_type']) || ($column['audit_table_type']!=$column['config_type'] && !empty($column['config_type']))) |
|
| 60 | + if (($column['data_table_type'] != $column['audit_table_type'] && $column['audit_table_type'] != $column['config_type']) || ($column['audit_table_type'] != $column['config_type'] && !empty($column['config_type']))) |
|
| 61 | 61 | { |
| 62 | 62 | $cleaned[] = $column; |
| 63 | 63 | } |
@@ -264,7 +264,7 @@ discard block |
||
| 264 | 264 | $auditColumn = StaticDataLayer::searchInRowSet('column_name', $modifiedColumn['column_name'], $this->config['audit_columns']); |
| 265 | 265 | if (isset($auditColumn)) |
| 266 | 266 | { |
| 267 | - if ($modifiedColumn['is_nullable']==='NO') |
|
| 267 | + if ($modifiedColumn['is_nullable'] === 'NO') |
|
| 268 | 268 | { |
| 269 | 269 | $modifiedColumn['column_type'] = sprintf('%s not null', $modifiedColumn['column_type']); |
| 270 | 270 | } |
@@ -136,19 +136,16 @@ discard block |
||
| 136 | 136 | { |
| 137 | 137 | $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']); |
| 138 | 138 | $styledColumn['data_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['data_table_type']); |
| 139 | - } |
|
| 140 | - else if (!isset($column['data_table_type']) && isset($column['audit_table_type'])) |
|
| 139 | + } else if (!isset($column['data_table_type']) && isset($column['audit_table_type'])) |
|
| 141 | 140 | { |
| 142 | 141 | $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['audit_table_type']); |
| 143 | - } |
|
| 144 | - else if (strcmp($column['data_table_type'], $column['audit_table_type'])) |
|
| 142 | + } else if (strcmp($column['data_table_type'], $column['audit_table_type'])) |
|
| 145 | 143 | { |
| 146 | 144 | $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']); |
| 147 | 145 | $styledColumn['data_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['data_table_type']); |
| 148 | 146 | $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['audit_table_type']); |
| 149 | 147 | } |
| 150 | - } |
|
| 151 | - else |
|
| 148 | + } else |
|
| 152 | 149 | { |
| 153 | 150 | // Highlighting for audit table column types and audit_columns in config file. |
| 154 | 151 | $searchColumn = StaticDataLayer::searchInRowSet('column_name', $styledColumn['column_name'], $this->config['audit_columns']); |
@@ -159,12 +156,10 @@ discard block |
||
| 159 | 156 | { |
| 160 | 157 | $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']); |
| 161 | 158 | $styledColumn['config_type'] = sprintf('<mm_type>%s</>', $styledColumn['config_type']); |
| 162 | - } |
|
| 163 | - else if (!isset($configType) && isset($column['audit_table_type'])) |
|
| 159 | + } else if (!isset($configType) && isset($column['audit_table_type'])) |
|
| 164 | 160 | { |
| 165 | 161 | $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $column['audit_table_type']); |
| 166 | - } |
|
| 167 | - else if (strcmp($configType, $column['audit_table_type'])) |
|
| 162 | + } else if (strcmp($configType, $column['audit_table_type'])) |
|
| 168 | 163 | { |
| 169 | 164 | $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']); |
| 170 | 165 | $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $column['audit_table_type']); |
@@ -289,8 +284,7 @@ discard block |
||
| 289 | 284 | if ($table['audit'] && !isset($res)) |
| 290 | 285 | { |
| 291 | 286 | $output->writeln(sprintf('<miss_table>%s</>', $tableName)); |
| 292 | - } |
|
| 293 | - else if (!$table['audit'] && isset($res)) |
|
| 287 | + } else if (!$table['audit'] && isset($res)) |
|
| 294 | 288 | { |
| 295 | 289 | $output->writeln(sprintf('<obsolete_table>%s</>', $tableName)); |
| 296 | 290 | } |
@@ -322,8 +316,7 @@ discard block |
||
| 322 | 316 | if ($first) |
| 323 | 317 | { |
| 324 | 318 | $first = false; |
| 325 | - } |
|
| 326 | - else |
|
| 319 | + } else |
|
| 327 | 320 | { |
| 328 | 321 | $output->writeln(''); |
| 329 | 322 | } |
@@ -21,70 +21,70 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | class DiffCommand extends AuditCommand |
| 23 | 23 | { |
| 24 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 25 | - /** |
|
| 26 | - * Array with columns for each table. |
|
| 27 | - * array [ |
|
| 28 | - * table_name [ |
|
| 29 | - * column [ |
|
| 30 | - * data table type, |
|
| 31 | - * audit table type |
|
| 32 | - * ], |
|
| 33 | - * ... |
|
| 34 | - * ] |
|
| 35 | - * ] |
|
| 36 | - * |
|
| 37 | - * @var array[] |
|
| 38 | - */ |
|
| 39 | - private $diffColumns; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * If set all tables and columns are shown. |
|
| 43 | - * |
|
| 44 | - * @var string |
|
| 45 | - */ |
|
| 46 | - private $full; |
|
| 47 | - |
|
| 48 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 49 | - /** |
|
| 50 | - * Check full full and return array without new or obsolete columns if full not set. |
|
| 51 | - * |
|
| 52 | - * @param array[] $columns The metadata of the columns of a table. |
|
| 53 | - * |
|
| 54 | - * @return array[] |
|
| 55 | - */ |
|
| 56 | - private static function removeMatchingColumns($columns) |
|
| 57 | - { |
|
| 24 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 25 | + /** |
|
| 26 | + * Array with columns for each table. |
|
| 27 | + * array [ |
|
| 28 | + * table_name [ |
|
| 29 | + * column [ |
|
| 30 | + * data table type, |
|
| 31 | + * audit table type |
|
| 32 | + * ], |
|
| 33 | + * ... |
|
| 34 | + * ] |
|
| 35 | + * ] |
|
| 36 | + * |
|
| 37 | + * @var array[] |
|
| 38 | + */ |
|
| 39 | + private $diffColumns; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * If set all tables and columns are shown. |
|
| 43 | + * |
|
| 44 | + * @var string |
|
| 45 | + */ |
|
| 46 | + private $full; |
|
| 47 | + |
|
| 48 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 49 | + /** |
|
| 50 | + * Check full full and return array without new or obsolete columns if full not set. |
|
| 51 | + * |
|
| 52 | + * @param array[] $columns The metadata of the columns of a table. |
|
| 53 | + * |
|
| 54 | + * @return array[] |
|
| 55 | + */ |
|
| 56 | + private static function removeMatchingColumns($columns) |
|
| 57 | + { |
|
| 58 | 58 | $cleaned = []; |
| 59 | 59 | foreach ($columns as $column) |
| 60 | 60 | { |
| 61 | - if (($column['data_table_type']!=$column['audit_table_type'] && $column['audit_table_type']!=$column['config_type']) || ($column['audit_table_type']!=$column['config_type'] && !empty($column['config_type']))) |
|
| 62 | - { |
|
| 61 | + if (($column['data_table_type']!=$column['audit_table_type'] && $column['audit_table_type']!=$column['config_type']) || ($column['audit_table_type']!=$column['config_type'] && !empty($column['config_type']))) |
|
| 62 | + { |
|
| 63 | 63 | $cleaned[] = $column; |
| 64 | - } |
|
| 64 | + } |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | return $cleaned; |
| 68 | - } |
|
| 69 | - |
|
| 70 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 71 | - /** |
|
| 72 | - * {@inheritdoc} |
|
| 73 | - */ |
|
| 74 | - protected function configure() |
|
| 75 | - { |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 71 | + /** |
|
| 72 | + * {@inheritdoc} |
|
| 73 | + */ |
|
| 74 | + protected function configure() |
|
| 75 | + { |
|
| 76 | 76 | $this->setName('diff') |
| 77 | - ->setDescription('Compares data tables and audit tables') |
|
| 78 | - ->addArgument('config file', InputArgument::OPTIONAL, 'The audit configuration file', 'etc/audit.json') |
|
| 79 | - ->addOption('full', 'f', InputOption::VALUE_NONE, 'Show all columns'); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 83 | - /** |
|
| 84 | - * {@inheritdoc} |
|
| 85 | - */ |
|
| 86 | - protected function execute(InputInterface $input, OutputInterface $output) |
|
| 87 | - { |
|
| 77 | + ->setDescription('Compares data tables and audit tables') |
|
| 78 | + ->addArgument('config file', InputArgument::OPTIONAL, 'The audit configuration file', 'etc/audit.json') |
|
| 79 | + ->addOption('full', 'f', InputOption::VALUE_NONE, 'Show all columns'); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 83 | + /** |
|
| 84 | + * {@inheritdoc} |
|
| 85 | + */ |
|
| 86 | + protected function execute(InputInterface $input, OutputInterface $output) |
|
| 87 | + { |
|
| 88 | 88 | $this->io = new StratumStyle($input, $output); |
| 89 | 89 | |
| 90 | 90 | // Style for column names with miss matched column types. |
@@ -114,239 +114,239 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | $this->getDiff(); |
| 116 | 116 | $this->printDiff($output); |
| 117 | - } |
|
| 118 | - |
|
| 119 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 120 | - /** |
|
| 121 | - * Add highlighting to columns. |
|
| 122 | - * |
|
| 123 | - * @param array[] $columns The metadata of the columns. |
|
| 124 | - * |
|
| 125 | - * @return array[] |
|
| 126 | - */ |
|
| 127 | - private function addHighlighting($columns) |
|
| 128 | - { |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 120 | + /** |
|
| 121 | + * Add highlighting to columns. |
|
| 122 | + * |
|
| 123 | + * @param array[] $columns The metadata of the columns. |
|
| 124 | + * |
|
| 125 | + * @return array[] |
|
| 126 | + */ |
|
| 127 | + private function addHighlighting($columns) |
|
| 128 | + { |
|
| 129 | 129 | $styledColumns = []; |
| 130 | 130 | foreach ($columns as $column) |
| 131 | 131 | { |
| 132 | - $styledColumn = $column; |
|
| 133 | - // Highlighting for data table column types and audit. |
|
| 134 | - if (!empty($column['data_table_type'])) |
|
| 135 | - { |
|
| 132 | + $styledColumn = $column; |
|
| 133 | + // Highlighting for data table column types and audit. |
|
| 134 | + if (!empty($column['data_table_type'])) |
|
| 135 | + { |
|
| 136 | 136 | if (isset($column['data_table_type']) && !isset($column['audit_table_type'])) |
| 137 | 137 | { |
| 138 | - $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']); |
|
| 139 | - $styledColumn['data_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['data_table_type']); |
|
| 138 | + $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']); |
|
| 139 | + $styledColumn['data_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['data_table_type']); |
|
| 140 | 140 | } |
| 141 | 141 | else if (!isset($column['data_table_type']) && isset($column['audit_table_type'])) |
| 142 | 142 | { |
| 143 | - $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['audit_table_type']); |
|
| 143 | + $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['audit_table_type']); |
|
| 144 | 144 | } |
| 145 | 145 | else if (strcmp($column['data_table_type'], $column['audit_table_type'])) |
| 146 | 146 | { |
| 147 | - $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']); |
|
| 148 | - $styledColumn['data_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['data_table_type']); |
|
| 149 | - $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['audit_table_type']); |
|
| 147 | + $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']); |
|
| 148 | + $styledColumn['data_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['data_table_type']); |
|
| 149 | + $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['audit_table_type']); |
|
| 150 | 150 | } |
| 151 | - } |
|
| 152 | - else |
|
| 153 | - { |
|
| 151 | + } |
|
| 152 | + else |
|
| 153 | + { |
|
| 154 | 154 | // Highlighting for audit table column types and audit_columns in config file. |
| 155 | 155 | $searchColumn = StaticDataLayer::searchInRowSet('column_name', $styledColumn['column_name'], $this->config['audit_columns']); |
| 156 | 156 | if (isset($searchColumn)) |
| 157 | 157 | { |
| 158 | - $configType = $this->config['audit_columns'][$searchColumn]['column_type']; |
|
| 159 | - if (isset($configType) && !isset($column['audit_table_type'])) |
|
| 160 | - { |
|
| 158 | + $configType = $this->config['audit_columns'][$searchColumn]['column_type']; |
|
| 159 | + if (isset($configType) && !isset($column['audit_table_type'])) |
|
| 160 | + { |
|
| 161 | 161 | $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']); |
| 162 | 162 | $styledColumn['config_type'] = sprintf('<mm_type>%s</>', $styledColumn['config_type']); |
| 163 | - } |
|
| 164 | - else if (!isset($configType) && isset($column['audit_table_type'])) |
|
| 165 | - { |
|
| 163 | + } |
|
| 164 | + else if (!isset($configType) && isset($column['audit_table_type'])) |
|
| 165 | + { |
|
| 166 | 166 | $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $column['audit_table_type']); |
| 167 | - } |
|
| 168 | - else if (strcmp($configType, $column['audit_table_type'])) |
|
| 169 | - { |
|
| 167 | + } |
|
| 168 | + else if (strcmp($configType, $column['audit_table_type'])) |
|
| 169 | + { |
|
| 170 | 170 | $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']); |
| 171 | 171 | $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $column['audit_table_type']); |
| 172 | 172 | $styledColumn['config_type'] = sprintf('<mm_type>%s</>', $styledColumn['config_type']); |
| 173 | - } |
|
| 173 | + } |
|
| 174 | + } |
|
| 174 | 175 | } |
| 175 | - } |
|
| 176 | - $styledColumns[] = $styledColumn; |
|
| 176 | + $styledColumns[] = $styledColumn; |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | return $styledColumns; |
| 180 | - } |
|
| 181 | - |
|
| 182 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 183 | - /** |
|
| 184 | - * Get the difference between data and audit tables. |
|
| 185 | - * |
|
| 186 | - * @param Columns $dataColumns The table columns from data schema. |
|
| 187 | - * @param Columns $auditColumns The table columns from audit schema. |
|
| 188 | - * |
|
| 189 | - * @return array[] |
|
| 190 | - */ |
|
| 191 | - private function createDiffArray($dataColumns, $auditColumns) |
|
| 192 | - { |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 183 | + /** |
|
| 184 | + * Get the difference between data and audit tables. |
|
| 185 | + * |
|
| 186 | + * @param Columns $dataColumns The table columns from data schema. |
|
| 187 | + * @param Columns $auditColumns The table columns from audit schema. |
|
| 188 | + * |
|
| 189 | + * @return array[] |
|
| 190 | + */ |
|
| 191 | + private function createDiffArray($dataColumns, $auditColumns) |
|
| 192 | + { |
|
| 193 | 193 | $diff = []; |
| 194 | 194 | |
| 195 | 195 | foreach ($this->config['audit_columns'] as $column) |
| 196 | 196 | { |
| 197 | - $diff[$column['column_name']] = ['column_name' => $column['column_name'], |
|
| 198 | - 'data_table_type' => null, |
|
| 199 | - 'audit_table_type' => null, |
|
| 200 | - 'config_type' => $column['column_type']]; |
|
| 197 | + $diff[$column['column_name']] = ['column_name' => $column['column_name'], |
|
| 198 | + 'data_table_type' => null, |
|
| 199 | + 'audit_table_type' => null, |
|
| 200 | + 'config_type' => $column['column_type']]; |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | foreach ($auditColumns->getColumns() as $column) |
| 204 | 204 | { |
| 205 | - $config_type = isset($diff[$column['column_name']]) ? $diff[$column['column_name']]['config_type'] : null; |
|
| 205 | + $config_type = isset($diff[$column['column_name']]) ? $diff[$column['column_name']]['config_type'] : null; |
|
| 206 | 206 | |
| 207 | - $diff[$column['column_name']] = ['column_name' => $column['column_name'], |
|
| 208 | - 'data_table_type' => null, |
|
| 209 | - 'audit_table_type' => $auditColumns->getColumnTypeWithCharSetCollation($column['column_name']), |
|
| 210 | - 'config_type' => $config_type]; |
|
| 207 | + $diff[$column['column_name']] = ['column_name' => $column['column_name'], |
|
| 208 | + 'data_table_type' => null, |
|
| 209 | + 'audit_table_type' => $auditColumns->getColumnTypeWithCharSetCollation($column['column_name']), |
|
| 210 | + 'config_type' => $config_type]; |
|
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | foreach ($dataColumns->getColumns() as $column) |
| 214 | 214 | { |
| 215 | - $config_type = isset($diff[$column['column_name']]) ? $diff[$column['column_name']]['config_type'] : null; |
|
| 216 | - $audit_table_type = isset($diff[$column['column_name']]) ? $diff[$column['column_name']]['audit_table_type'] : null; |
|
| 215 | + $config_type = isset($diff[$column['column_name']]) ? $diff[$column['column_name']]['config_type'] : null; |
|
| 216 | + $audit_table_type = isset($diff[$column['column_name']]) ? $diff[$column['column_name']]['audit_table_type'] : null; |
|
| 217 | 217 | |
| 218 | - $diff[$column['column_name']] = ['column_name' => $column['column_name'], |
|
| 219 | - 'data_table_type' => $dataColumns->getColumnTypeWithCharSetCollation($column['column_name']), |
|
| 220 | - 'audit_table_type' => $audit_table_type, |
|
| 221 | - 'config_type' => $config_type]; |
|
| 218 | + $diff[$column['column_name']] = ['column_name' => $column['column_name'], |
|
| 219 | + 'data_table_type' => $dataColumns->getColumnTypeWithCharSetCollation($column['column_name']), |
|
| 220 | + 'audit_table_type' => $audit_table_type, |
|
| 221 | + 'config_type' => $config_type]; |
|
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | return $diff; |
| 225 | - } |
|
| 226 | - |
|
| 227 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 228 | - /** |
|
| 229 | - * Computes the difference between data and audit tables. |
|
| 230 | - */ |
|
| 231 | - private function getDiff() |
|
| 232 | - { |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 228 | + /** |
|
| 229 | + * Computes the difference between data and audit tables. |
|
| 230 | + */ |
|
| 231 | + private function getDiff() |
|
| 232 | + { |
|
| 233 | 233 | foreach ($this->dataSchemaTables as $table) |
| 234 | 234 | { |
| 235 | - if ($this->config['tables'][$table['table_name']]['audit']) |
|
| 236 | - { |
|
| 235 | + if ($this->config['tables'][$table['table_name']]['audit']) |
|
| 236 | + { |
|
| 237 | 237 | $res = StaticDataLayer::searchInRowSet('table_name', $table['table_name'], $this->auditSchemaTables); |
| 238 | 238 | if (isset($res)) |
| 239 | 239 | { |
| 240 | - $dataColumns = new Columns(DataLayer::getTableColumns($this->config['database']['data_schema'], $table['table_name'])); |
|
| 241 | - $auditColumns = DataLayer::getTableColumns($this->config['database']['audit_schema'], $table['table_name']); |
|
| 242 | - $auditColumns = $this->addNotNull($auditColumns); |
|
| 243 | - $auditColumns = new Columns($auditColumns); |
|
| 240 | + $dataColumns = new Columns(DataLayer::getTableColumns($this->config['database']['data_schema'], $table['table_name'])); |
|
| 241 | + $auditColumns = DataLayer::getTableColumns($this->config['database']['audit_schema'], $table['table_name']); |
|
| 242 | + $auditColumns = $this->addNotNull($auditColumns); |
|
| 243 | + $auditColumns = new Columns($auditColumns); |
|
| 244 | 244 | |
| 245 | - $this->diffColumns[$table['table_name']] = $this->createDiffArray($dataColumns, $auditColumns); |
|
| 245 | + $this->diffColumns[$table['table_name']] = $this->createDiffArray($dataColumns, $auditColumns); |
|
| 246 | + } |
|
| 246 | 247 | } |
| 247 | - } |
|
| 248 | 248 | } |
| 249 | - } |
|
| 250 | - |
|
| 251 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 252 | - /** |
|
| 253 | - * Add not null to audit columns if it not nullable. |
|
| 254 | - * |
|
| 255 | - * @param array $theColumns Audit columns. |
|
| 256 | - * |
|
| 257 | - * @return array |
|
| 258 | - */ |
|
| 259 | - private function addNotNull($theColumns) |
|
| 260 | - { |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 252 | + /** |
|
| 253 | + * Add not null to audit columns if it not nullable. |
|
| 254 | + * |
|
| 255 | + * @param array $theColumns Audit columns. |
|
| 256 | + * |
|
| 257 | + * @return array |
|
| 258 | + */ |
|
| 259 | + private function addNotNull($theColumns) |
|
| 260 | + { |
|
| 261 | 261 | $modifiedColumns = []; |
| 262 | 262 | foreach ($theColumns as $column) |
| 263 | 263 | { |
| 264 | - $modifiedColumn = $column; |
|
| 265 | - $auditColumn = StaticDataLayer::searchInRowSet('column_name', $modifiedColumn['column_name'], $this->config['audit_columns']); |
|
| 266 | - if (isset($auditColumn)) |
|
| 267 | - { |
|
| 264 | + $modifiedColumn = $column; |
|
| 265 | + $auditColumn = StaticDataLayer::searchInRowSet('column_name', $modifiedColumn['column_name'], $this->config['audit_columns']); |
|
| 266 | + if (isset($auditColumn)) |
|
| 267 | + { |
|
| 268 | 268 | if ($modifiedColumn['is_nullable']==='NO') |
| 269 | 269 | { |
| 270 | - $modifiedColumn['column_type'] = sprintf('%s not null', $modifiedColumn['column_type']); |
|
| 270 | + $modifiedColumn['column_type'] = sprintf('%s not null', $modifiedColumn['column_type']); |
|
| 271 | + } |
|
| 271 | 272 | } |
| 272 | - } |
|
| 273 | - $modifiedColumns[] = $modifiedColumn; |
|
| 273 | + $modifiedColumns[] = $modifiedColumn; |
|
| 274 | 274 | } |
| 275 | 275 | |
| 276 | 276 | return $modifiedColumns; |
| 277 | - } |
|
| 278 | - |
|
| 279 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 280 | - /** |
|
| 281 | - * Writes the difference between the audit tables and metadata tables to the output. |
|
| 282 | - * |
|
| 283 | - * @param OutputInterface $output The output. |
|
| 284 | - */ |
|
| 285 | - private function diffTables($output) |
|
| 286 | - { |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 280 | + /** |
|
| 281 | + * Writes the difference between the audit tables and metadata tables to the output. |
|
| 282 | + * |
|
| 283 | + * @param OutputInterface $output The output. |
|
| 284 | + */ |
|
| 285 | + private function diffTables($output) |
|
| 286 | + { |
|
| 287 | 287 | foreach ($this->config['tables'] as $tableName => $table) |
| 288 | 288 | { |
| 289 | - $res = StaticDataLayer::searchInRowSet('table_name', $tableName, $this->auditSchemaTables); |
|
| 290 | - if ($table['audit'] && !isset($res)) |
|
| 291 | - { |
|
| 289 | + $res = StaticDataLayer::searchInRowSet('table_name', $tableName, $this->auditSchemaTables); |
|
| 290 | + if ($table['audit'] && !isset($res)) |
|
| 291 | + { |
|
| 292 | 292 | $output->writeln(sprintf('<miss_table>%s</>', $tableName)); |
| 293 | - } |
|
| 294 | - else if (!$table['audit'] && isset($res)) |
|
| 295 | - { |
|
| 293 | + } |
|
| 294 | + else if (!$table['audit'] && isset($res)) |
|
| 295 | + { |
|
| 296 | 296 | $output->writeln(sprintf('<obsolete_table>%s</>', $tableName)); |
| 297 | - } |
|
| 297 | + } |
|
| 298 | + } |
|
| 298 | 299 | } |
| 299 | - } |
|
| 300 | - |
|
| 301 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 302 | - /** |
|
| 303 | - * Writes the difference between the audit and data tables to the output. |
|
| 304 | - * |
|
| 305 | - * @param OutputInterface $output The output. |
|
| 306 | - */ |
|
| 307 | - private function printDiff($output) |
|
| 308 | - { |
|
| 300 | + |
|
| 301 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 302 | + /** |
|
| 303 | + * Writes the difference between the audit and data tables to the output. |
|
| 304 | + * |
|
| 305 | + * @param OutputInterface $output The output. |
|
| 306 | + */ |
|
| 307 | + private function printDiff($output) |
|
| 308 | + { |
|
| 309 | 309 | $first = true; |
| 310 | 310 | if (isset($this->diffColumns)) |
| 311 | 311 | { |
| 312 | - foreach ($this->diffColumns as $tableName => $columns) |
|
| 313 | - { |
|
| 312 | + foreach ($this->diffColumns as $tableName => $columns) |
|
| 313 | + { |
|
| 314 | 314 | // Remove matching columns unless the full option is used. |
| 315 | 315 | if (!$this->full) |
| 316 | 316 | { |
| 317 | - $columns = self::removeMatchingColumns($columns); |
|
| 317 | + $columns = self::removeMatchingColumns($columns); |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | if (!empty($columns)) |
| 321 | 321 | { |
| 322 | - // Add an empty line between tables. |
|
| 323 | - if ($first) |
|
| 324 | - { |
|
| 322 | + // Add an empty line between tables. |
|
| 323 | + if ($first) |
|
| 324 | + { |
|
| 325 | 325 | $first = false; |
| 326 | - } |
|
| 327 | - else |
|
| 328 | - { |
|
| 326 | + } |
|
| 327 | + else |
|
| 328 | + { |
|
| 329 | 329 | $output->writeln(''); |
| 330 | - } |
|
| 330 | + } |
|
| 331 | 331 | |
| 332 | - // Write table name. |
|
| 333 | - $output->writeln($tableName); |
|
| 332 | + // Write table name. |
|
| 333 | + $output->writeln($tableName); |
|
| 334 | 334 | |
| 335 | - // Write table with columns. |
|
| 336 | - $columns = $this->addHighlighting($columns); |
|
| 337 | - $rows = new TableHelper($this->config['database']['data_schema'], $this->config['database']['audit_schema'], $tableName); |
|
| 338 | - $rows->appendRows($columns, $this->full); |
|
| 339 | - $table = new Table($output); |
|
| 340 | - $table->setHeaders(['column', 'data table', 'audit table', 'config']) |
|
| 335 | + // Write table with columns. |
|
| 336 | + $columns = $this->addHighlighting($columns); |
|
| 337 | + $rows = new TableHelper($this->config['database']['data_schema'], $this->config['database']['audit_schema'], $tableName); |
|
| 338 | + $rows->appendRows($columns, $this->full); |
|
| 339 | + $table = new Table($output); |
|
| 340 | + $table->setHeaders(['column', 'data table', 'audit table', 'config']) |
|
| 341 | 341 | ->setRows($rows->getRows()); |
| 342 | - $table->render(); |
|
| 342 | + $table->render(); |
|
| 343 | + } |
|
| 343 | 344 | } |
| 344 | - } |
|
| 345 | 345 | } |
| 346 | 346 | $this->diffTables($output); |
| 347 | - } |
|
| 347 | + } |
|
| 348 | 348 | |
| 349 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 349 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 350 | 350 | } |
| 351 | 351 | |
| 352 | 352 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | $auditColumns, |
| 118 | 118 | $skipVariable, |
| 119 | 119 | $additionSql); |
| 120 | - $sql = $helper->buildStatement(); |
|
| 120 | + $sql = $helper->buildStatement(); |
|
| 121 | 121 | |
| 122 | 122 | self::executeNone($sql); |
| 123 | 123 | } |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | */ |
| 129 | 129 | public static function disconnect() |
| 130 | 130 | { |
| 131 | - if (self::$dl!==null) |
|
| 131 | + if (self::$dl !== null) |
|
| 132 | 132 | { |
| 133 | 133 | self::$dl->disconnect(); |
| 134 | 134 | self::$dl = null; |
@@ -381,7 +381,7 @@ discard block |
||
| 381 | 381 | { |
| 382 | 382 | $query = trim($query); |
| 383 | 383 | |
| 384 | - if (strpos($query, "\n")!==false) |
|
| 384 | + if (strpos($query, "\n") !== false) |
|
| 385 | 385 | { |
| 386 | 386 | // Query is a multi line query. |
| 387 | 387 | self::$io->logVeryVerbose('Executing query:'); |
@@ -386,8 +386,7 @@ |
||
| 386 | 386 | // Query is a multi line query. |
| 387 | 387 | self::$io->logVeryVerbose('Executing query:'); |
| 388 | 388 | self::$io->logVeryVerbose('<sql>%s</sql>', $query); |
| 389 | - } |
|
| 390 | - else |
|
| 389 | + } else |
|
| 391 | 390 | { |
| 392 | 391 | // Query is a single line query. |
| 393 | 392 | self::$io->logVeryVerbose('Executing query: <sql>%s</sql>', $query); |
@@ -15,90 +15,90 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | class DataLayer |
| 17 | 17 | { |
| 18 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 19 | - /** |
|
| 20 | - * The connection to the MySQL instance. |
|
| 21 | - * |
|
| 22 | - * @var StaticDataLayer |
|
| 23 | - */ |
|
| 24 | - private static $dl; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * The Output decorator. |
|
| 28 | - * |
|
| 29 | - * @var StratumStyle |
|
| 30 | - */ |
|
| 31 | - private static $io; |
|
| 32 | - |
|
| 33 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 34 | - /** |
|
| 35 | - * Adds new columns to an audit table. |
|
| 36 | - * |
|
| 37 | - * @param string $auditSchemaName The name of audit schema. |
|
| 38 | - * @param string $tableName The name of the table. |
|
| 39 | - * @param Columns $columns The metadata of the new columns. |
|
| 40 | - */ |
|
| 41 | - public static function addNewColumns($auditSchemaName, $tableName, $columns) |
|
| 42 | - { |
|
| 18 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 19 | + /** |
|
| 20 | + * The connection to the MySQL instance. |
|
| 21 | + * |
|
| 22 | + * @var StaticDataLayer |
|
| 23 | + */ |
|
| 24 | + private static $dl; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * The Output decorator. |
|
| 28 | + * |
|
| 29 | + * @var StratumStyle |
|
| 30 | + */ |
|
| 31 | + private static $io; |
|
| 32 | + |
|
| 33 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 34 | + /** |
|
| 35 | + * Adds new columns to an audit table. |
|
| 36 | + * |
|
| 37 | + * @param string $auditSchemaName The name of audit schema. |
|
| 38 | + * @param string $tableName The name of the table. |
|
| 39 | + * @param Columns $columns The metadata of the new columns. |
|
| 40 | + */ |
|
| 41 | + public static function addNewColumns($auditSchemaName, $tableName, $columns) |
|
| 42 | + { |
|
| 43 | 43 | $helper = new AlterAuditTableAddColumns($auditSchemaName, $tableName, $columns); |
| 44 | 44 | $sql = $helper->buildStatement(); |
| 45 | 45 | |
| 46 | 46 | self::executeNone($sql); |
| 47 | - } |
|
| 48 | - |
|
| 49 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 50 | - /** |
|
| 51 | - * Connects to a MySQL instance. |
|
| 52 | - * |
|
| 53 | - * Wrapper around [mysqli::__construct](http://php.net/manual/mysqli.construct.php), however on failure an exception |
|
| 54 | - * is thrown. |
|
| 55 | - * |
|
| 56 | - * @param string $host The hostname. |
|
| 57 | - * @param string $user The MySQL user name. |
|
| 58 | - * @param string $passWord The password. |
|
| 59 | - * @param string $database The default database. |
|
| 60 | - * @param int $port The port number. |
|
| 61 | - */ |
|
| 62 | - public static function connect($host, $user, $passWord, $database, $port = 3306) |
|
| 63 | - { |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 50 | + /** |
|
| 51 | + * Connects to a MySQL instance. |
|
| 52 | + * |
|
| 53 | + * Wrapper around [mysqli::__construct](http://php.net/manual/mysqli.construct.php), however on failure an exception |
|
| 54 | + * is thrown. |
|
| 55 | + * |
|
| 56 | + * @param string $host The hostname. |
|
| 57 | + * @param string $user The MySQL user name. |
|
| 58 | + * @param string $passWord The password. |
|
| 59 | + * @param string $database The default database. |
|
| 60 | + * @param int $port The port number. |
|
| 61 | + */ |
|
| 62 | + public static function connect($host, $user, $passWord, $database, $port = 3306) |
|
| 63 | + { |
|
| 64 | 64 | self::$dl = new StaticDataLayer(); |
| 65 | 65 | |
| 66 | 66 | self::$dl->connect($host, $user, $passWord, $database, $port); |
| 67 | - } |
|
| 68 | - |
|
| 69 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 70 | - /** |
|
| 71 | - * Creates an audit table. |
|
| 72 | - * |
|
| 73 | - * @param string $dataSchemaName The name of the data schema. |
|
| 74 | - * @param string $auditSchemaName The name of the audit schema. |
|
| 75 | - * @param string $tableName The name of the table. |
|
| 76 | - * @param Columns $columns The metadata of the columns of the audit table (i.e. the audit columns and columns |
|
| 77 | - * of the data table). |
|
| 78 | - */ |
|
| 79 | - public static function createAuditTable($dataSchemaName, $auditSchemaName, $tableName, $columns) |
|
| 80 | - { |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 70 | + /** |
|
| 71 | + * Creates an audit table. |
|
| 72 | + * |
|
| 73 | + * @param string $dataSchemaName The name of the data schema. |
|
| 74 | + * @param string $auditSchemaName The name of the audit schema. |
|
| 75 | + * @param string $tableName The name of the table. |
|
| 76 | + * @param Columns $columns The metadata of the columns of the audit table (i.e. the audit columns and columns |
|
| 77 | + * of the data table). |
|
| 78 | + */ |
|
| 79 | + public static function createAuditTable($dataSchemaName, $auditSchemaName, $tableName, $columns) |
|
| 80 | + { |
|
| 81 | 81 | $helper = new CreateAuditTable($dataSchemaName, $auditSchemaName, $tableName, $columns); |
| 82 | 82 | $sql = $helper->buildStatement(); |
| 83 | 83 | |
| 84 | 84 | self::executeNone($sql); |
| 85 | - } |
|
| 86 | - |
|
| 87 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 88 | - /** |
|
| 89 | - * Creates a trigger on a table. |
|
| 90 | - * |
|
| 91 | - * @param string $dataSchemaName The name of the data schema. |
|
| 92 | - * @param string $auditSchemaName The name of the audit schema. |
|
| 93 | - * @param string $tableName The name of the table. |
|
| 94 | - * @param string $triggerAction The trigger action (i.e. INSERT, UPDATE, or DELETE). |
|
| 95 | - * @param string $triggerName The name of the trigger. |
|
| 96 | - * @param Columns $tableColumns The data table columns. |
|
| 97 | - * @param Columns $auditColumns The audit table columns. |
|
| 98 | - * @param string $skipVariable The skip variable. |
|
| 99 | - * @param string[] $additionSql Additional SQL statements. |
|
| 100 | - */ |
|
| 101 | - public static function createAuditTrigger($dataSchemaName, |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 88 | + /** |
|
| 89 | + * Creates a trigger on a table. |
|
| 90 | + * |
|
| 91 | + * @param string $dataSchemaName The name of the data schema. |
|
| 92 | + * @param string $auditSchemaName The name of the audit schema. |
|
| 93 | + * @param string $tableName The name of the table. |
|
| 94 | + * @param string $triggerAction The trigger action (i.e. INSERT, UPDATE, or DELETE). |
|
| 95 | + * @param string $triggerName The name of the trigger. |
|
| 96 | + * @param Columns $tableColumns The data table columns. |
|
| 97 | + * @param Columns $auditColumns The audit table columns. |
|
| 98 | + * @param string $skipVariable The skip variable. |
|
| 99 | + * @param string[] $additionSql Additional SQL statements. |
|
| 100 | + */ |
|
| 101 | + public static function createAuditTrigger($dataSchemaName, |
|
| 102 | 102 | $auditSchemaName, |
| 103 | 103 | $tableName, |
| 104 | 104 | $triggerAction, |
@@ -107,151 +107,151 @@ discard block |
||
| 107 | 107 | $auditColumns, |
| 108 | 108 | $skipVariable, |
| 109 | 109 | $additionSql) |
| 110 | - { |
|
| 110 | + { |
|
| 111 | 111 | $helper = new CreateAuditTrigger($dataSchemaName, |
| 112 | - $auditSchemaName, |
|
| 113 | - $tableName, |
|
| 114 | - $triggerAction, |
|
| 115 | - $triggerName, |
|
| 116 | - $tableColumns, |
|
| 117 | - $auditColumns, |
|
| 118 | - $skipVariable, |
|
| 119 | - $additionSql); |
|
| 112 | + $auditSchemaName, |
|
| 113 | + $tableName, |
|
| 114 | + $triggerAction, |
|
| 115 | + $triggerName, |
|
| 116 | + $tableColumns, |
|
| 117 | + $auditColumns, |
|
| 118 | + $skipVariable, |
|
| 119 | + $additionSql); |
|
| 120 | 120 | $sql = $helper->buildStatement(); |
| 121 | 121 | |
| 122 | 122 | self::executeNone($sql); |
| 123 | - } |
|
| 124 | - |
|
| 125 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 126 | - /** |
|
| 127 | - * Closes the connection to the MySQL instance, if connected. |
|
| 128 | - */ |
|
| 129 | - public static function disconnect() |
|
| 130 | - { |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 126 | + /** |
|
| 127 | + * Closes the connection to the MySQL instance, if connected. |
|
| 128 | + */ |
|
| 129 | + public static function disconnect() |
|
| 130 | + { |
|
| 131 | 131 | if (self::$dl!==null) |
| 132 | 132 | { |
| 133 | - self::$dl->disconnect(); |
|
| 134 | - self::$dl = null; |
|
| 133 | + self::$dl->disconnect(); |
|
| 134 | + self::$dl = null; |
|
| 135 | + } |
|
| 135 | 136 | } |
| 136 | - } |
|
| 137 | - |
|
| 138 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 139 | - /** |
|
| 140 | - * Drops a trigger. |
|
| 141 | - * |
|
| 142 | - * @param string $triggerSchema The name of the trigger schema. |
|
| 143 | - * @param string $triggerName The mame of trigger. |
|
| 144 | - */ |
|
| 145 | - public static function dropTrigger($triggerSchema, $triggerName) |
|
| 146 | - { |
|
| 137 | + |
|
| 138 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 139 | + /** |
|
| 140 | + * Drops a trigger. |
|
| 141 | + * |
|
| 142 | + * @param string $triggerSchema The name of the trigger schema. |
|
| 143 | + * @param string $triggerName The mame of trigger. |
|
| 144 | + */ |
|
| 145 | + public static function dropTrigger($triggerSchema, $triggerName) |
|
| 146 | + { |
|
| 147 | 147 | $sql = sprintf('drop trigger `%s`.`%s`', $triggerSchema, $triggerName); |
| 148 | 148 | |
| 149 | 149 | self::executeNone($sql); |
| 150 | - } |
|
| 151 | - |
|
| 152 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 153 | - /** |
|
| 154 | - * @param string $query The SQL statement. |
|
| 155 | - * |
|
| 156 | - * @return int The number of affected rows (if any). |
|
| 157 | - */ |
|
| 158 | - public static function executeNone($query) |
|
| 159 | - { |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 153 | + /** |
|
| 154 | + * @param string $query The SQL statement. |
|
| 155 | + * |
|
| 156 | + * @return int The number of affected rows (if any). |
|
| 157 | + */ |
|
| 158 | + public static function executeNone($query) |
|
| 159 | + { |
|
| 160 | 160 | self::logQuery($query); |
| 161 | 161 | |
| 162 | 162 | return self::$dl->executeNone($query); |
| 163 | - } |
|
| 164 | - |
|
| 165 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 166 | - /** |
|
| 167 | - * Executes a query that returns 0 or 1 row. |
|
| 168 | - * Throws an exception if the query selects 2 or more rows. |
|
| 169 | - * |
|
| 170 | - * @param string $query The SQL statement. |
|
| 171 | - * |
|
| 172 | - * @return array|null The selected row. |
|
| 173 | - */ |
|
| 174 | - public static function executeRow0($query) |
|
| 175 | - { |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 166 | + /** |
|
| 167 | + * Executes a query that returns 0 or 1 row. |
|
| 168 | + * Throws an exception if the query selects 2 or more rows. |
|
| 169 | + * |
|
| 170 | + * @param string $query The SQL statement. |
|
| 171 | + * |
|
| 172 | + * @return array|null The selected row. |
|
| 173 | + */ |
|
| 174 | + public static function executeRow0($query) |
|
| 175 | + { |
|
| 176 | 176 | self::logQuery($query); |
| 177 | 177 | |
| 178 | 178 | return self::$dl->executeRow0($query); |
| 179 | - } |
|
| 180 | - |
|
| 181 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 182 | - /** |
|
| 183 | - * Executes a query that returns 1 and only 1 row. |
|
| 184 | - * Throws an exception if the query selects none, 2 or more rows. |
|
| 185 | - * |
|
| 186 | - * @param string $query The SQL statement. |
|
| 187 | - * |
|
| 188 | - * @return array The selected row. |
|
| 189 | - */ |
|
| 190 | - public static function executeRow1($query) |
|
| 191 | - { |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 182 | + /** |
|
| 183 | + * Executes a query that returns 1 and only 1 row. |
|
| 184 | + * Throws an exception if the query selects none, 2 or more rows. |
|
| 185 | + * |
|
| 186 | + * @param string $query The SQL statement. |
|
| 187 | + * |
|
| 188 | + * @return array The selected row. |
|
| 189 | + */ |
|
| 190 | + public static function executeRow1($query) |
|
| 191 | + { |
|
| 192 | 192 | self::logQuery($query); |
| 193 | 193 | |
| 194 | 194 | return self::$dl->executeRow1($query); |
| 195 | - } |
|
| 196 | - |
|
| 197 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 198 | - /** |
|
| 199 | - * Executes a query that returns 0 or more rows. |
|
| 200 | - * |
|
| 201 | - * @param string $query The SQL statement. |
|
| 202 | - * |
|
| 203 | - * @return \array[] |
|
| 204 | - */ |
|
| 205 | - public static function executeRows($query) |
|
| 206 | - { |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 198 | + /** |
|
| 199 | + * Executes a query that returns 0 or more rows. |
|
| 200 | + * |
|
| 201 | + * @param string $query The SQL statement. |
|
| 202 | + * |
|
| 203 | + * @return \array[] |
|
| 204 | + */ |
|
| 205 | + public static function executeRows($query) |
|
| 206 | + { |
|
| 207 | 207 | self::logQuery($query); |
| 208 | 208 | |
| 209 | 209 | return self::$dl->executeRows($query); |
| 210 | - } |
|
| 211 | - |
|
| 212 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 213 | - /** |
|
| 214 | - * Executes a query that returns 0 or 1 row. |
|
| 215 | - * Throws an exception if the query selects 2 or more rows. |
|
| 216 | - * |
|
| 217 | - * @param string $query The SQL statement. |
|
| 218 | - * |
|
| 219 | - * @return int|string|null The selected row. |
|
| 220 | - */ |
|
| 221 | - public static function executeSingleton0($query) |
|
| 222 | - { |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 213 | + /** |
|
| 214 | + * Executes a query that returns 0 or 1 row. |
|
| 215 | + * Throws an exception if the query selects 2 or more rows. |
|
| 216 | + * |
|
| 217 | + * @param string $query The SQL statement. |
|
| 218 | + * |
|
| 219 | + * @return int|string|null The selected row. |
|
| 220 | + */ |
|
| 221 | + public static function executeSingleton0($query) |
|
| 222 | + { |
|
| 223 | 223 | self::logQuery($query); |
| 224 | 224 | |
| 225 | 225 | return self::$dl->executeSingleton0($query); |
| 226 | - } |
|
| 227 | - |
|
| 228 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 229 | - /** |
|
| 230 | - * Executes a query that returns 1 and only 1 row with 1 column. |
|
| 231 | - * Throws an exception if the query selects none, 2 or more rows. |
|
| 232 | - * |
|
| 233 | - * @param string $query The SQL statement. |
|
| 234 | - * |
|
| 235 | - * @return int|string The selected row. |
|
| 236 | - */ |
|
| 237 | - public static function executeSingleton1($query) |
|
| 238 | - { |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 229 | + /** |
|
| 230 | + * Executes a query that returns 1 and only 1 row with 1 column. |
|
| 231 | + * Throws an exception if the query selects none, 2 or more rows. |
|
| 232 | + * |
|
| 233 | + * @param string $query The SQL statement. |
|
| 234 | + * |
|
| 235 | + * @return int|string The selected row. |
|
| 236 | + */ |
|
| 237 | + public static function executeSingleton1($query) |
|
| 238 | + { |
|
| 239 | 239 | self::logQuery($query); |
| 240 | 240 | |
| 241 | 241 | return self::$dl->executeSingleton1($query); |
| 242 | - } |
|
| 243 | - |
|
| 244 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 245 | - /** |
|
| 246 | - * Selects metadata of all columns of table. |
|
| 247 | - * |
|
| 248 | - * @param string $schemaName The name of the table schema. |
|
| 249 | - * @param string $tableName The name of the table. |
|
| 250 | - * |
|
| 251 | - * @return \array[] |
|
| 252 | - */ |
|
| 253 | - public static function getTableColumns($schemaName, $tableName) |
|
| 254 | - { |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 245 | + /** |
|
| 246 | + * Selects metadata of all columns of table. |
|
| 247 | + * |
|
| 248 | + * @param string $schemaName The name of the table schema. |
|
| 249 | + * @param string $tableName The name of the table. |
|
| 250 | + * |
|
| 251 | + * @return \array[] |
|
| 252 | + */ |
|
| 253 | + public static function getTableColumns($schemaName, $tableName) |
|
| 254 | + { |
|
| 255 | 255 | $sql = sprintf(' |
| 256 | 256 | select COLUMN_NAME as column_name |
| 257 | 257 | , COLUMN_TYPE as column_type |
@@ -262,23 +262,23 @@ discard block |
||
| 262 | 262 | where TABLE_SCHEMA = %s |
| 263 | 263 | and TABLE_NAME = %s |
| 264 | 264 | order by ORDINAL_POSITION', |
| 265 | - self::$dl->quoteString($schemaName), |
|
| 266 | - self::$dl->quoteString($tableName)); |
|
| 265 | + self::$dl->quoteString($schemaName), |
|
| 266 | + self::$dl->quoteString($tableName)); |
|
| 267 | 267 | |
| 268 | 268 | return self::$dl->executeRows($sql); |
| 269 | - } |
|
| 270 | - |
|
| 271 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 272 | - /** |
|
| 273 | - * Selects table engine, character_set_name and table_collation. |
|
| 274 | - * |
|
| 275 | - * @param string $schemaName The name of the table schema. |
|
| 276 | - * @param string $tableName The name of the table. |
|
| 277 | - * |
|
| 278 | - * @return array |
|
| 279 | - */ |
|
| 280 | - public static function getTableOptions($schemaName, $tableName) |
|
| 281 | - { |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 272 | + /** |
|
| 273 | + * Selects table engine, character_set_name and table_collation. |
|
| 274 | + * |
|
| 275 | + * @param string $schemaName The name of the table schema. |
|
| 276 | + * @param string $tableName The name of the table. |
|
| 277 | + * |
|
| 278 | + * @return array |
|
| 279 | + */ |
|
| 280 | + public static function getTableOptions($schemaName, $tableName) |
|
| 281 | + { |
|
| 282 | 282 | $sql = sprintf(' |
| 283 | 283 | SELECT t1.TABLE_COLLATION as table_collation |
| 284 | 284 | , t1.ENGINE as engine |
@@ -287,45 +287,45 @@ discard block |
||
| 287 | 287 | inner join information_schema.COLLATION_CHARACTER_SET_APPLICABILITY t2 on t2.COLLATION_NAME = t1.TABLE_COLLATION |
| 288 | 288 | WHERE t1.TABLE_SCHEMA = %s |
| 289 | 289 | AND t1.TABLE_NAME = %s', |
| 290 | - self::$dl->quoteString($schemaName), |
|
| 291 | - self::$dl->quoteString($tableName)); |
|
| 290 | + self::$dl->quoteString($schemaName), |
|
| 291 | + self::$dl->quoteString($tableName)); |
|
| 292 | 292 | |
| 293 | 293 | return self::$dl->executeRow1($sql); |
| 294 | - } |
|
| 295 | - |
|
| 296 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 297 | - /** |
|
| 298 | - * Selects all triggers on a table. |
|
| 299 | - * |
|
| 300 | - * @param string $schemaName The name of the table schema. |
|
| 301 | - * @param string $tableName The name of the table. |
|
| 302 | - * |
|
| 303 | - * @return \array[] |
|
| 304 | - */ |
|
| 305 | - public static function getTableTriggers($schemaName, $tableName) |
|
| 306 | - { |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 297 | + /** |
|
| 298 | + * Selects all triggers on a table. |
|
| 299 | + * |
|
| 300 | + * @param string $schemaName The name of the table schema. |
|
| 301 | + * @param string $tableName The name of the table. |
|
| 302 | + * |
|
| 303 | + * @return \array[] |
|
| 304 | + */ |
|
| 305 | + public static function getTableTriggers($schemaName, $tableName) |
|
| 306 | + { |
|
| 307 | 307 | $sql = sprintf(' |
| 308 | 308 | select Trigger_Name as trigger_name |
| 309 | 309 | from information_schema.TRIGGERS |
| 310 | 310 | where TRIGGER_SCHEMA = %s |
| 311 | 311 | and EVENT_OBJECT_TABLE = %s |
| 312 | 312 | order by Trigger_Name', |
| 313 | - self::$dl->quoteString($schemaName), |
|
| 314 | - self::$dl->quoteString($tableName)); |
|
| 313 | + self::$dl->quoteString($schemaName), |
|
| 314 | + self::$dl->quoteString($tableName)); |
|
| 315 | 315 | |
| 316 | 316 | return self::$dl->executeRows($sql); |
| 317 | - } |
|
| 318 | - |
|
| 319 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 320 | - /** |
|
| 321 | - * Selects all table names in a schema. |
|
| 322 | - * |
|
| 323 | - * @param string $schemaName The name of the schema. |
|
| 324 | - * |
|
| 325 | - * @return \array[] |
|
| 326 | - */ |
|
| 327 | - public static function getTablesNames($schemaName) |
|
| 328 | - { |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 320 | + /** |
|
| 321 | + * Selects all table names in a schema. |
|
| 322 | + * |
|
| 323 | + * @param string $schemaName The name of the schema. |
|
| 324 | + * |
|
| 325 | + * @return \array[] |
|
| 326 | + */ |
|
| 327 | + public static function getTablesNames($schemaName) |
|
| 328 | + { |
|
| 329 | 329 | $sql = sprintf(" |
| 330 | 330 | select TABLE_NAME as table_name |
| 331 | 331 | from information_schema.TABLES |
@@ -334,67 +334,67 @@ discard block |
||
| 334 | 334 | order by TABLE_NAME", self::$dl->quoteString($schemaName)); |
| 335 | 335 | |
| 336 | 336 | return self::$dl->executeRows($sql); |
| 337 | - } |
|
| 338 | - |
|
| 339 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 340 | - /** |
|
| 341 | - * Acquires a write lock on a table. |
|
| 342 | - * |
|
| 343 | - * @param string $tableName The table name. |
|
| 344 | - */ |
|
| 345 | - public static function lockTable($tableName) |
|
| 346 | - { |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 340 | + /** |
|
| 341 | + * Acquires a write lock on a table. |
|
| 342 | + * |
|
| 343 | + * @param string $tableName The table name. |
|
| 344 | + */ |
|
| 345 | + public static function lockTable($tableName) |
|
| 346 | + { |
|
| 347 | 347 | $sql = sprintf('lock tables `%s` write', $tableName); |
| 348 | 348 | |
| 349 | 349 | self::$dl->executeNone($sql); |
| 350 | - } |
|
| 351 | - |
|
| 352 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 353 | - /** |
|
| 354 | - * Sets the Output decorator. |
|
| 355 | - * |
|
| 356 | - * @param StratumStyle $io The Output decorator. |
|
| 357 | - */ |
|
| 358 | - public static function setIo($io) |
|
| 359 | - { |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 353 | + /** |
|
| 354 | + * Sets the Output decorator. |
|
| 355 | + * |
|
| 356 | + * @param StratumStyle $io The Output decorator. |
|
| 357 | + */ |
|
| 358 | + public static function setIo($io) |
|
| 359 | + { |
|
| 360 | 360 | self::$io = $io; |
| 361 | - } |
|
| 362 | - |
|
| 363 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 364 | - /** |
|
| 365 | - * Releases all table locks. |
|
| 366 | - */ |
|
| 367 | - public static function unlockTables() |
|
| 368 | - { |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 364 | + /** |
|
| 365 | + * Releases all table locks. |
|
| 366 | + */ |
|
| 367 | + public static function unlockTables() |
|
| 368 | + { |
|
| 369 | 369 | $sql = 'unlock tables'; |
| 370 | 370 | |
| 371 | 371 | self::$dl->executeNone($sql); |
| 372 | - } |
|
| 373 | - |
|
| 374 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 375 | - /** |
|
| 376 | - * Logs the query on the console. |
|
| 377 | - * |
|
| 378 | - * @param string $query The query. |
|
| 379 | - */ |
|
| 380 | - private static function logQuery($query) |
|
| 381 | - { |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 375 | + /** |
|
| 376 | + * Logs the query on the console. |
|
| 377 | + * |
|
| 378 | + * @param string $query The query. |
|
| 379 | + */ |
|
| 380 | + private static function logQuery($query) |
|
| 381 | + { |
|
| 382 | 382 | $query = trim($query); |
| 383 | 383 | |
| 384 | 384 | if (strpos($query, "\n")!==false) |
| 385 | 385 | { |
| 386 | - // Query is a multi line query. |
|
| 387 | - self::$io->logVeryVerbose('Executing query:'); |
|
| 388 | - self::$io->logVeryVerbose('<sql>%s</sql>', $query); |
|
| 386 | + // Query is a multi line query. |
|
| 387 | + self::$io->logVeryVerbose('Executing query:'); |
|
| 388 | + self::$io->logVeryVerbose('<sql>%s</sql>', $query); |
|
| 389 | 389 | } |
| 390 | 390 | else |
| 391 | 391 | { |
| 392 | - // Query is a single line query. |
|
| 393 | - self::$io->logVeryVerbose('Executing query: <sql>%s</sql>', $query); |
|
| 392 | + // Query is a single line query. |
|
| 393 | + self::$io->logVeryVerbose('Executing query: <sql>%s</sql>', $query); |
|
| 394 | + } |
|
| 394 | 395 | } |
| 395 | - } |
|
| 396 | 396 | |
| 397 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 397 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 398 | 398 | } |
| 399 | 399 | |
| 400 | 400 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -14,101 +14,101 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | class CreateAuditTrigger |
| 16 | 16 | { |
| 17 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 18 | - /** |
|
| 19 | - * Additional SQL statements. |
|
| 20 | - * |
|
| 21 | - * @var string[] |
|
| 22 | - */ |
|
| 23 | - private $additionalSql; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * AuditApplication columns from metadata. |
|
| 27 | - * |
|
| 28 | - * @var Columns |
|
| 29 | - */ |
|
| 30 | - private $auditColumns; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * The name of the audit schema. |
|
| 34 | - * |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - private $auditSchemaName; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * The generated code. |
|
| 41 | - * |
|
| 42 | - * @var CompoundSyntaxStore |
|
| 43 | - */ |
|
| 44 | - private $code; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The name of the data schema. |
|
| 48 | - * |
|
| 49 | - * @var string |
|
| 50 | - */ |
|
| 51 | - private $dataSchemaName; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * The skip variable. |
|
| 55 | - * |
|
| 56 | - * @var string |
|
| 57 | - */ |
|
| 58 | - private $skipVariable; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Table columns from metadata. |
|
| 62 | - * |
|
| 63 | - * @var Columns |
|
| 64 | - */ |
|
| 65 | - private $tableColumns; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * The name of the data table. |
|
| 69 | - * |
|
| 70 | - * @var string |
|
| 71 | - */ |
|
| 72 | - private $tableName; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * The trigger action (.e. INSERT, UPDATE, or DELETE). |
|
| 76 | - * |
|
| 77 | - * @var string |
|
| 78 | - */ |
|
| 79 | - private $triggerAction; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * The name of the trigger. |
|
| 83 | - * |
|
| 84 | - * @var string |
|
| 85 | - */ |
|
| 86 | - private $triggerName; |
|
| 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 Columns $tableColumns The data table columns. |
|
| 98 | - * @param Columns $auditColumns The audit table columns. |
|
| 99 | - * @param string $skipVariable The skip variable. |
|
| 100 | - * @param string[] $additionalSql Additional SQL statements. |
|
| 101 | - */ |
|
| 102 | - public function __construct($dataSchemaName, |
|
| 103 | - $auditSchemaName, |
|
| 104 | - $tableName, |
|
| 105 | - $triggerName, |
|
| 106 | - $triggerAction, |
|
| 107 | - $auditColumns, |
|
| 108 | - $tableColumns, |
|
| 109 | - $skipVariable, |
|
| 110 | - $additionalSql) |
|
| 111 | - { |
|
| 17 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 18 | + /** |
|
| 19 | + * Additional SQL statements. |
|
| 20 | + * |
|
| 21 | + * @var string[] |
|
| 22 | + */ |
|
| 23 | + private $additionalSql; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * AuditApplication columns from metadata. |
|
| 27 | + * |
|
| 28 | + * @var Columns |
|
| 29 | + */ |
|
| 30 | + private $auditColumns; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * The name of the audit schema. |
|
| 34 | + * |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + private $auditSchemaName; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * The generated code. |
|
| 41 | + * |
|
| 42 | + * @var CompoundSyntaxStore |
|
| 43 | + */ |
|
| 44 | + private $code; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The name of the data schema. |
|
| 48 | + * |
|
| 49 | + * @var string |
|
| 50 | + */ |
|
| 51 | + private $dataSchemaName; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * The skip variable. |
|
| 55 | + * |
|
| 56 | + * @var string |
|
| 57 | + */ |
|
| 58 | + private $skipVariable; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Table columns from metadata. |
|
| 62 | + * |
|
| 63 | + * @var Columns |
|
| 64 | + */ |
|
| 65 | + private $tableColumns; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * The name of the data table. |
|
| 69 | + * |
|
| 70 | + * @var string |
|
| 71 | + */ |
|
| 72 | + private $tableName; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * The trigger action (.e. INSERT, UPDATE, or DELETE). |
|
| 76 | + * |
|
| 77 | + * @var string |
|
| 78 | + */ |
|
| 79 | + private $triggerAction; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * The name of the trigger. |
|
| 83 | + * |
|
| 84 | + * @var string |
|
| 85 | + */ |
|
| 86 | + private $triggerName; |
|
| 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 Columns $tableColumns The data table columns. |
|
| 98 | + * @param Columns $auditColumns The audit table columns. |
|
| 99 | + * @param string $skipVariable The skip variable. |
|
| 100 | + * @param string[] $additionalSql Additional SQL statements. |
|
| 101 | + */ |
|
| 102 | + public function __construct($dataSchemaName, |
|
| 103 | + $auditSchemaName, |
|
| 104 | + $tableName, |
|
| 105 | + $triggerName, |
|
| 106 | + $triggerAction, |
|
| 107 | + $auditColumns, |
|
| 108 | + $tableColumns, |
|
| 109 | + $skipVariable, |
|
| 110 | + $additionalSql) |
|
| 111 | + { |
|
| 112 | 112 | $this->dataSchemaName = $dataSchemaName; |
| 113 | 113 | $this->auditSchemaName = $auditSchemaName; |
| 114 | 114 | $this->tableName = $tableName; |
@@ -118,35 +118,35 @@ discard block |
||
| 118 | 118 | $this->tableColumns = $tableColumns; |
| 119 | 119 | $this->auditColumns = $auditColumns; |
| 120 | 120 | $this->additionalSql = $additionalSql; |
| 121 | - } |
|
| 122 | - |
|
| 123 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 124 | - /** |
|
| 125 | - * Returns the SQL code for creating an audit trigger. |
|
| 126 | - * |
|
| 127 | - * @throws FallenException |
|
| 128 | - */ |
|
| 129 | - public function buildStatement() |
|
| 130 | - { |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 124 | + /** |
|
| 125 | + * Returns the SQL code for creating an audit trigger. |
|
| 126 | + * |
|
| 127 | + * @throws FallenException |
|
| 128 | + */ |
|
| 129 | + public function buildStatement() |
|
| 130 | + { |
|
| 131 | 131 | $this->code = new CompoundSyntaxStore(); |
| 132 | 132 | |
| 133 | 133 | $rowState = []; |
| 134 | 134 | switch ($this->triggerAction) |
| 135 | 135 | { |
| 136 | - case 'INSERT': |
|
| 136 | + case 'INSERT': |
|
| 137 | 137 | $rowState[] = 'NEW'; |
| 138 | 138 | break; |
| 139 | 139 | |
| 140 | - case 'DELETE': |
|
| 140 | + case 'DELETE': |
|
| 141 | 141 | $rowState[] = 'OLD'; |
| 142 | 142 | break; |
| 143 | 143 | |
| 144 | - case 'UPDATE': |
|
| 144 | + case 'UPDATE': |
|
| 145 | 145 | $rowState[] = 'OLD'; |
| 146 | 146 | $rowState[] = 'NEW'; |
| 147 | 147 | break; |
| 148 | 148 | |
| 149 | - default: |
|
| 149 | + default: |
|
| 150 | 150 | throw new FallenException('action', $this->triggerAction); |
| 151 | 151 | } |
| 152 | 152 | |
@@ -162,104 +162,104 @@ discard block |
||
| 162 | 162 | $this->createInsertStatement($rowState[0]); |
| 163 | 163 | if (sizeof($rowState)==2) |
| 164 | 164 | { |
| 165 | - $this->createInsertStatement($rowState[1]); |
|
| 165 | + $this->createInsertStatement($rowState[1]); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | if ($this->skipVariable!==null) $this->code->append('end if;'); |
| 169 | 169 | $this->code->append('end'); |
| 170 | 170 | |
| 171 | 171 | return $this->code->getCode(); |
| 172 | - } |
|
| 173 | - |
|
| 174 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 175 | - /** |
|
| 176 | - * Adds an insert SQL statement to SQL code for a trigger. |
|
| 177 | - * |
|
| 178 | - * @param string $rowState The row state (i.e. OLD or NEW). |
|
| 179 | - */ |
|
| 180 | - private function createInsertStatement($rowState) |
|
| 181 | - { |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 175 | + /** |
|
| 176 | + * Adds an insert SQL statement to SQL code for a trigger. |
|
| 177 | + * |
|
| 178 | + * @param string $rowState The row state (i.e. OLD or NEW). |
|
| 179 | + */ |
|
| 180 | + private function createInsertStatement($rowState) |
|
| 181 | + { |
|
| 182 | 182 | $this->createInsertStatementInto(); |
| 183 | 183 | $this->createInsertStatementValues($rowState); |
| 184 | - } |
|
| 185 | - |
|
| 186 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 187 | - /** |
|
| 188 | - * Adds the "insert into" part of an insert SQL statement to SQL code for a trigger. |
|
| 189 | - */ |
|
| 190 | - private function createInsertStatementInto() |
|
| 191 | - { |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 187 | + /** |
|
| 188 | + * Adds the "insert into" part of an insert SQL statement to SQL code for a trigger. |
|
| 189 | + */ |
|
| 190 | + private function createInsertStatementInto() |
|
| 191 | + { |
|
| 192 | 192 | $columnNames = ''; |
| 193 | 193 | |
| 194 | 194 | // First the audit columns. |
| 195 | 195 | foreach ($this->auditColumns->getColumns() as $column) |
| 196 | 196 | { |
| 197 | - if ($columnNames) $columnNames .= ','; |
|
| 198 | - $columnNames .= sprintf('`%s`', $column['column_name']); |
|
| 197 | + if ($columnNames) $columnNames .= ','; |
|
| 198 | + $columnNames .= sprintf('`%s`', $column['column_name']); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | // Second the audit columns. |
| 202 | 202 | foreach ($this->tableColumns->getColumns() as $column) |
| 203 | 203 | { |
| 204 | - if ($columnNames) $columnNames .= ','; |
|
| 205 | - $columnNames .= sprintf('`%s`', $column['column_name']); |
|
| 204 | + if ($columnNames) $columnNames .= ','; |
|
| 205 | + $columnNames .= sprintf('`%s`', $column['column_name']); |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | $this->code->append(sprintf('insert into `%s`.`%s`(%s)', $this->auditSchemaName, $this->tableName, $columnNames)); |
| 209 | - } |
|
| 210 | - |
|
| 211 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 212 | - /** |
|
| 213 | - * Adds the "values" part of an insert SQL statement to SQL code for a trigger. |
|
| 214 | - * |
|
| 215 | - * @param string $rowState The row state (i.e. OLD or NEW). |
|
| 216 | - */ |
|
| 217 | - private function createInsertStatementValues($rowState) |
|
| 218 | - { |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 212 | + /** |
|
| 213 | + * Adds the "values" part of an insert SQL statement to SQL code for a trigger. |
|
| 214 | + * |
|
| 215 | + * @param string $rowState The row state (i.e. OLD or NEW). |
|
| 216 | + */ |
|
| 217 | + private function createInsertStatementValues($rowState) |
|
| 218 | + { |
|
| 219 | 219 | $values = ''; |
| 220 | 220 | |
| 221 | 221 | // First the values for the audit columns. |
| 222 | 222 | foreach ($this->auditColumns->getColumns() as $column) |
| 223 | 223 | { |
| 224 | - if ($values) $values .= ','; |
|
| 225 | - switch (true) |
|
| 226 | - { |
|
| 224 | + if ($values) $values .= ','; |
|
| 225 | + switch (true) |
|
| 226 | + { |
|
| 227 | 227 | case (isset($column['audit_value_type'])): |
| 228 | 228 | switch ($column['audit_value_type']) |
| 229 | - { |
|
| 229 | + { |
|
| 230 | 230 | case 'ACTION': |
| 231 | 231 | $values .= StaticDataLayer::quoteString($this->triggerAction); |
| 232 | - break; |
|
| 232 | + break; |
|
| 233 | 233 | |
| 234 | 234 | case 'STATE': |
| 235 | 235 | $values .= StaticDataLayer::quoteString($rowState); |
| 236 | - break; |
|
| 236 | + break; |
|
| 237 | 237 | |
| 238 | 238 | default: |
| 239 | 239 | throw new FallenException('audit_value_type', ($column['audit_value_type'])); |
| 240 | - } |
|
| 241 | - break; |
|
| 240 | + } |
|
| 241 | + break; |
|
| 242 | 242 | |
| 243 | 243 | case (isset($column['audit_expression'])): |
| 244 | 244 | $values .= $column['audit_expression']; |
| 245 | - break; |
|
| 245 | + break; |
|
| 246 | 246 | |
| 247 | 247 | default: |
| 248 | 248 | throw new RuntimeException('None of audit_value_type and audit_expression are set.'); |
| 249 | - } |
|
| 249 | + } |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | // Second the values for the audit columns. |
| 253 | 253 | foreach ($this->tableColumns->getColumns() as $column) |
| 254 | 254 | { |
| 255 | - if ($values) $values .= ','; |
|
| 256 | - $values .= sprintf('%s.`%s`', $rowState, $column['column_name']); |
|
| 255 | + if ($values) $values .= ','; |
|
| 256 | + $values .= sprintf('%s.`%s`', $rowState, $column['column_name']); |
|
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | $this->code->append(sprintf('values(%s);', $values)); |
| 260 | - } |
|
| 260 | + } |
|
| 261 | 261 | |
| 262 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 262 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -155,17 +155,17 @@ |
||
| 155 | 155 | $this->code->append('for each row'); |
| 156 | 156 | $this->code->append('begin'); |
| 157 | 157 | |
| 158 | - if ($this->skipVariable!==null) $this->code->append(sprintf('if (%s is null) then', $this->skipVariable)); |
|
| 158 | + if ($this->skipVariable !== null) $this->code->append(sprintf('if (%s is null) then', $this->skipVariable)); |
|
| 159 | 159 | |
| 160 | 160 | $this->code->append($this->additionalSql); |
| 161 | 161 | |
| 162 | 162 | $this->createInsertStatement($rowState[0]); |
| 163 | - if (sizeof($rowState)==2) |
|
| 163 | + if (sizeof($rowState) == 2) |
|
| 164 | 164 | { |
| 165 | 165 | $this->createInsertStatement($rowState[1]); |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | - if ($this->skipVariable!==null) $this->code->append('end if;'); |
|
| 168 | + if ($this->skipVariable !== null) $this->code->append('end if;'); |
|
| 169 | 169 | $this->code->append('end'); |
| 170 | 170 | |
| 171 | 171 | return $this->code->getCode(); |
@@ -155,7 +155,9 @@ discard block |
||
| 155 | 155 | $this->code->append('for each row'); |
| 156 | 156 | $this->code->append('begin'); |
| 157 | 157 | |
| 158 | - if ($this->skipVariable!==null) $this->code->append(sprintf('if (%s is null) then', $this->skipVariable)); |
|
| 158 | + if ($this->skipVariable!==null) { |
|
| 159 | + $this->code->append(sprintf('if (%s is null) then', $this->skipVariable)); |
|
| 160 | + } |
|
| 159 | 161 | |
| 160 | 162 | $this->code->append($this->additionalSql); |
| 161 | 163 | |
@@ -165,7 +167,9 @@ discard block |
||
| 165 | 167 | $this->createInsertStatement($rowState[1]); |
| 166 | 168 | } |
| 167 | 169 | |
| 168 | - if ($this->skipVariable!==null) $this->code->append('end if;'); |
|
| 170 | + if ($this->skipVariable!==null) { |
|
| 171 | + $this->code->append('end if;'); |
|
| 172 | + } |
|
| 169 | 173 | $this->code->append('end'); |
| 170 | 174 | |
| 171 | 175 | return $this->code->getCode(); |
@@ -194,14 +198,18 @@ discard block |
||
| 194 | 198 | // First the audit columns. |
| 195 | 199 | foreach ($this->auditColumns->getColumns() as $column) |
| 196 | 200 | { |
| 197 | - if ($columnNames) $columnNames .= ','; |
|
| 201 | + if ($columnNames) { |
|
| 202 | + $columnNames .= ','; |
|
| 203 | + } |
|
| 198 | 204 | $columnNames .= sprintf('`%s`', $column['column_name']); |
| 199 | 205 | } |
| 200 | 206 | |
| 201 | 207 | // Second the audit columns. |
| 202 | 208 | foreach ($this->tableColumns->getColumns() as $column) |
| 203 | 209 | { |
| 204 | - if ($columnNames) $columnNames .= ','; |
|
| 210 | + if ($columnNames) { |
|
| 211 | + $columnNames .= ','; |
|
| 212 | + } |
|
| 205 | 213 | $columnNames .= sprintf('`%s`', $column['column_name']); |
| 206 | 214 | } |
| 207 | 215 | |
@@ -221,7 +229,9 @@ discard block |
||
| 221 | 229 | // First the values for the audit columns. |
| 222 | 230 | foreach ($this->auditColumns->getColumns() as $column) |
| 223 | 231 | { |
| 224 | - if ($values) $values .= ','; |
|
| 232 | + if ($values) { |
|
| 233 | + $values .= ','; |
|
| 234 | + } |
|
| 225 | 235 | switch (true) |
| 226 | 236 | { |
| 227 | 237 | case (isset($column['audit_value_type'])): |
@@ -252,7 +262,9 @@ discard block |
||
| 252 | 262 | // Second the values for the audit columns. |
| 253 | 263 | foreach ($this->tableColumns->getColumns() as $column) |
| 254 | 264 | { |
| 255 | - if ($values) $values .= ','; |
|
| 265 | + if ($values) { |
|
| 266 | + $values .= ','; |
|
| 267 | + } |
|
| 256 | 268 | $values .= sprintf('%s.`%s`', $rowState, $column['column_name']); |
| 257 | 269 | } |
| 258 | 270 | |
@@ -11,77 +11,77 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class AlterAuditTableAddColumns |
| 13 | 13 | { |
| 14 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 15 | - /** |
|
| 16 | - * The name of the audit schema. |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 20 | - private $auditSchemaName; |
|
| 14 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 15 | + /** |
|
| 16 | + * The name of the audit schema. |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | + private $auditSchemaName; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * The array of new columns for adding to table. |
|
| 24 | - * |
|
| 25 | - * @var Columns |
|
| 26 | - */ |
|
| 27 | - private $columns; |
|
| 22 | + /** |
|
| 23 | + * The array of new columns for adding to table. |
|
| 24 | + * |
|
| 25 | + * @var Columns |
|
| 26 | + */ |
|
| 27 | + private $columns; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The name of the audit table. |
|
| 31 | - * |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - private $tableName; |
|
| 29 | + /** |
|
| 30 | + * The name of the audit table. |
|
| 31 | + * |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + private $tableName; |
|
| 35 | 35 | |
| 36 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 37 | - /** |
|
| 38 | - * Object constructor. |
|
| 39 | - * |
|
| 40 | - * @param string $auditSchemaName The name of the audit schema. |
|
| 41 | - * @param string $tableName The name of the table. |
|
| 42 | - * @param Columns $columns The metadata of the new columns of the audit table (i.e. the audit columns and |
|
| 43 | - * columns of the data table). |
|
| 44 | - */ |
|
| 45 | - public function __construct($auditSchemaName, $tableName, $columns) |
|
| 46 | - { |
|
| 36 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 37 | + /** |
|
| 38 | + * Object constructor. |
|
| 39 | + * |
|
| 40 | + * @param string $auditSchemaName The name of the audit schema. |
|
| 41 | + * @param string $tableName The name of the table. |
|
| 42 | + * @param Columns $columns The metadata of the new columns of the audit table (i.e. the audit columns and |
|
| 43 | + * columns of the data table). |
|
| 44 | + */ |
|
| 45 | + public function __construct($auditSchemaName, $tableName, $columns) |
|
| 46 | + { |
|
| 47 | 47 | $this->auditSchemaName = $auditSchemaName; |
| 48 | 48 | $this->tableName = $tableName; |
| 49 | 49 | $this->columns = $columns; |
| 50 | - } |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 53 | - /** |
|
| 54 | - * Returns a SQL statement for adding new columns to the audit table. |
|
| 55 | - * |
|
| 56 | - * @return string |
|
| 57 | - */ |
|
| 58 | - public function buildStatement() |
|
| 59 | - { |
|
| 52 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 53 | + /** |
|
| 54 | + * Returns a SQL statement for adding new columns to the audit table. |
|
| 55 | + * |
|
| 56 | + * @return string |
|
| 57 | + */ |
|
| 58 | + public function buildStatement() |
|
| 59 | + { |
|
| 60 | 60 | $code = new CompoundSyntaxStore(); |
| 61 | 61 | |
| 62 | 62 | $code->append(sprintf('alter table `%s`.`%s`', $this->auditSchemaName, $this->tableName)); |
| 63 | 63 | foreach ($this->columns->getColumns() as $column) |
| 64 | 64 | { |
| 65 | - $code->append(sprintf(' add `%s` %s', $column['column_name'], $column['column_type']), false); |
|
| 66 | - if (isset($column['after'])) |
|
| 67 | - { |
|
| 65 | + $code->append(sprintf(' add `%s` %s', $column['column_name'], $column['column_type']), false); |
|
| 66 | + if (isset($column['after'])) |
|
| 67 | + { |
|
| 68 | 68 | $code->appendToLastLine(sprintf(' after `%s`', $column['after'])); |
| 69 | - } |
|
| 70 | - else |
|
| 71 | - { |
|
| 69 | + } |
|
| 70 | + else |
|
| 71 | + { |
|
| 72 | 72 | $code->appendToLastLine(' first'); |
| 73 | - } |
|
| 74 | - $columns = $this->columns->getColumns(); |
|
| 75 | - if (end($columns)!==$column) |
|
| 76 | - { |
|
| 73 | + } |
|
| 74 | + $columns = $this->columns->getColumns(); |
|
| 75 | + if (end($columns)!==$column) |
|
| 76 | + { |
|
| 77 | 77 | $code->appendToLastLine(','); |
| 78 | - } |
|
| 78 | + } |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | return $code->getCode(); |
| 82 | - } |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 84 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -88,7 +88,7 @@ |
||
| 88 | 88 | foreach ($columns as $column) |
| 89 | 89 | { |
| 90 | 90 | $code->append(sprintf($format, '`'.$column['column_name'].'`', $column['column_type']), false); |
| 91 | - if (end($columns)!==$column) |
|
| 91 | + if (end($columns) !== $column) |
|
| 92 | 92 | { |
| 93 | 93 | $code->appendToLastLine(','); |
| 94 | 94 | } |
@@ -62,8 +62,7 @@ |
||
| 62 | 62 | { |
| 63 | 63 | $columns[] = ['column_name' => $column['column_name'], |
| 64 | 64 | 'column_type' => $column['column_type']]; |
| 65 | - } |
|
| 66 | - else |
|
| 65 | + } else |
|
| 67 | 66 | { |
| 68 | 67 | $columns[] = ['column_name' => $column['column_name'], |
| 69 | 68 | 'column_type' => $column['column_type'].' NULL']; |
@@ -12,64 +12,64 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | class CreateAuditTable |
| 14 | 14 | { |
| 15 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 16 | - /** |
|
| 17 | - * The name of the audit schema. |
|
| 18 | - * |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - private $auditSchemaName; |
|
| 15 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 16 | + /** |
|
| 17 | + * The name of the audit schema. |
|
| 18 | + * |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + private $auditSchemaName; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * The name of the table. |
|
| 25 | - * |
|
| 26 | - * @var Columns |
|
| 27 | - */ |
|
| 28 | - private $columns; |
|
| 23 | + /** |
|
| 24 | + * The name of the table. |
|
| 25 | + * |
|
| 26 | + * @var Columns |
|
| 27 | + */ |
|
| 28 | + private $columns; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * The name of the data schema. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - private $dataSchemaName; |
|
| 30 | + /** |
|
| 31 | + * The name of the data schema. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + private $dataSchemaName; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * The name of the table. |
|
| 39 | - * |
|
| 40 | - * @var string |
|
| 41 | - */ |
|
| 42 | - private $tableName; |
|
| 37 | + /** |
|
| 38 | + * The name of the table. |
|
| 39 | + * |
|
| 40 | + * @var string |
|
| 41 | + */ |
|
| 42 | + private $tableName; |
|
| 43 | 43 | |
| 44 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 45 | - /** |
|
| 46 | - * Object constructor. |
|
| 47 | - * |
|
| 48 | - * @param string $dataSchemaName The name of the data schema. |
|
| 49 | - * @param string $auditSchemaName The name of the audit schema. |
|
| 50 | - * @param string $tableName The name of the table. |
|
| 51 | - * @param Columns $columns The metadata of the columns of the audit table (i.e. the audit columns and columns |
|
| 52 | - * of the data table). |
|
| 53 | - */ |
|
| 54 | - public function __construct($dataSchemaName, |
|
| 55 | - $auditSchemaName, |
|
| 56 | - $tableName, |
|
| 57 | - $columns) |
|
| 58 | - { |
|
| 44 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 45 | + /** |
|
| 46 | + * Object constructor. |
|
| 47 | + * |
|
| 48 | + * @param string $dataSchemaName The name of the data schema. |
|
| 49 | + * @param string $auditSchemaName The name of the audit schema. |
|
| 50 | + * @param string $tableName The name of the table. |
|
| 51 | + * @param Columns $columns The metadata of the columns of the audit table (i.e. the audit columns and columns |
|
| 52 | + * of the data table). |
|
| 53 | + */ |
|
| 54 | + public function __construct($dataSchemaName, |
|
| 55 | + $auditSchemaName, |
|
| 56 | + $tableName, |
|
| 57 | + $columns) |
|
| 58 | + { |
|
| 59 | 59 | $this->dataSchemaName = $dataSchemaName; |
| 60 | 60 | $this->auditSchemaName = $auditSchemaName; |
| 61 | 61 | $this->tableName = $tableName; |
| 62 | 62 | $this->columns = $columns; |
| 63 | - } |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 66 | - /** |
|
| 67 | - * Returns a SQL statement for creating the audit table. |
|
| 68 | - * |
|
| 69 | - * @return string |
|
| 70 | - */ |
|
| 71 | - public function buildStatement() |
|
| 72 | - { |
|
| 65 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 66 | + /** |
|
| 67 | + * Returns a SQL statement for creating the audit table. |
|
| 68 | + * |
|
| 69 | + * @return string |
|
| 70 | + */ |
|
| 71 | + public function buildStatement() |
|
| 72 | + { |
|
| 73 | 73 | $code = new CompoundSyntaxStore(); |
| 74 | 74 | |
| 75 | 75 | $code->append(sprintf('create table `%s`.`%s`', $this->auditSchemaName, $this->tableName)); |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | $width = 0; |
| 80 | 80 | foreach ($columns as $column) |
| 81 | 81 | { |
| 82 | - $width = max($width, mb_strlen($column['column_name'])); |
|
| 82 | + $width = max($width, mb_strlen($column['column_name'])); |
|
| 83 | 83 | } |
| 84 | 84 | $format = sprintf(' %%-%ds %%s', $width + 2); |
| 85 | 85 | |
@@ -87,24 +87,24 @@ discard block |
||
| 87 | 87 | $code->append('('); |
| 88 | 88 | foreach ($columns as $column) |
| 89 | 89 | { |
| 90 | - $code->append(sprintf($format, '`'.$column['column_name'].'`', $column['column_type']), false); |
|
| 91 | - if (end($columns)!==$column) |
|
| 92 | - { |
|
| 90 | + $code->append(sprintf($format, '`'.$column['column_name'].'`', $column['column_type']), false); |
|
| 91 | + if (end($columns)!==$column) |
|
| 92 | + { |
|
| 93 | 93 | $code->appendToLastLine(','); |
| 94 | - } |
|
| 94 | + } |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | // Create SQL for table options. |
| 98 | 98 | $tableOptions = DataLayer::getTableOptions($this->dataSchemaName, $this->tableName); |
| 99 | 99 | $code->append(sprintf(') engine=%s default charset=%s default collate=%s', |
| 100 | - $tableOptions['engine'], |
|
| 101 | - $tableOptions['character_set_name'], |
|
| 102 | - $tableOptions['table_collation'])); |
|
| 100 | + $tableOptions['engine'], |
|
| 101 | + $tableOptions['character_set_name'], |
|
| 102 | + $tableOptions['table_collation'])); |
|
| 103 | 103 | |
| 104 | 104 | return $code->getCode(); |
| 105 | - } |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 107 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -88,7 +88,7 @@ |
||
| 88 | 88 | foreach ($columns as $column) |
| 89 | 89 | { |
| 90 | 90 | $code->append(sprintf($format, '`'.$column['column_name'].'`', $column['column_type']), false); |
| 91 | - if (end($columns)!==$column) |
|
| 91 | + if (end($columns) !== $column) |
|
| 92 | 92 | { |
| 93 | 93 | $code->appendToLastLine(','); |
| 94 | 94 | } |