@@ -56,7 +56,7 @@ |
||
| 56 | 56 | */ |
| 57 | 57 | public function appendTableOption($theOption, $theName = null) |
| 58 | 58 | { |
| 59 | - if ($theName===null) |
|
| 59 | + if ($theName === null) |
|
| 60 | 60 | { |
| 61 | 61 | $theName = $theOption; |
| 62 | 62 | } |
@@ -11,96 +11,96 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class TableHelper |
| 13 | 13 | { |
| 14 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 15 | - /** |
|
| 16 | - * Array with rows for table. |
|
| 17 | - * |
|
| 18 | - * @var \array[] |
|
| 19 | - */ |
|
| 20 | - private $rows = []; |
|
| 14 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 15 | + /** |
|
| 16 | + * Array with rows for table. |
|
| 17 | + * |
|
| 18 | + * @var \array[] |
|
| 19 | + */ |
|
| 20 | + private $rows = []; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Table options from audit schema. |
|
| 24 | - * |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 27 | - private $auditTableOptions; |
|
| 22 | + /** |
|
| 23 | + * Table options from audit schema. |
|
| 24 | + * |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | + private $auditTableOptions; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Table options from data schema. |
|
| 31 | - * |
|
| 32 | - * @var array |
|
| 33 | - */ |
|
| 34 | - private $dataTableOptions; |
|
| 29 | + /** |
|
| 30 | + * Table options from data schema. |
|
| 31 | + * |
|
| 32 | + * @var array |
|
| 33 | + */ |
|
| 34 | + private $dataTableOptions; |
|
| 35 | 35 | |
| 36 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 37 | - /** |
|
| 38 | - * Object constructor. |
|
| 39 | - * |
|
| 40 | - * @param string $dataSchema Data schema name. |
|
| 41 | - * @param string $auditSchema Audit schema name. |
|
| 42 | - * @param string $tableName The table name. |
|
| 43 | - */ |
|
| 44 | - public function __construct($dataSchema, $auditSchema, $tableName) |
|
| 45 | - { |
|
| 36 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 37 | + /** |
|
| 38 | + * Object constructor. |
|
| 39 | + * |
|
| 40 | + * @param string $dataSchema Data schema name. |
|
| 41 | + * @param string $auditSchema Audit schema name. |
|
| 42 | + * @param string $tableName The table name. |
|
| 43 | + */ |
|
| 44 | + public function __construct($dataSchema, $auditSchema, $tableName) |
|
| 45 | + { |
|
| 46 | 46 | $this->auditTableOptions = DataLayer::getTableOptions($auditSchema, $tableName); |
| 47 | 47 | $this->dataTableOptions = DataLayer::getTableOptions($dataSchema, $tableName); |
| 48 | - } |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 51 | - /** |
|
| 52 | - * Append row with table option. |
|
| 53 | - * |
|
| 54 | - * @param string $theOption The option. |
|
| 55 | - * @param null|string $theName Display name. |
|
| 56 | - */ |
|
| 57 | - public function appendTableOption($theOption, $theName = null) |
|
| 58 | - { |
|
| 50 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 51 | + /** |
|
| 52 | + * Append row with table option. |
|
| 53 | + * |
|
| 54 | + * @param string $theOption The option. |
|
| 55 | + * @param null|string $theName Display name. |
|
| 56 | + */ |
|
| 57 | + public function appendTableOption($theOption, $theName = null) |
|
| 58 | + { |
|
| 59 | 59 | if ($theName===null) |
| 60 | 60 | { |
| 61 | - $theName = $theOption; |
|
| 61 | + $theName = $theOption; |
|
| 62 | 62 | } |
| 63 | 63 | $this->rows[$theOption] = ['column_name' => $theName, |
| 64 | - 'data_table_type' => $this->dataTableOptions[$theOption], |
|
| 65 | - 'audit_table_type' => $this->auditTableOptions[$theOption], |
|
| 66 | - 'config_type' => null]; |
|
| 67 | - } |
|
| 64 | + 'data_table_type' => $this->dataTableOptions[$theOption], |
|
| 65 | + 'audit_table_type' => $this->auditTableOptions[$theOption], |
|
| 66 | + 'config_type' => null]; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 70 | - /** |
|
| 71 | - * Appends rows. |
|
| 72 | - * |
|
| 73 | - * @param \array[] $theRows Rows array. |
|
| 74 | - * @param bool $theOptionFlag If not null append table options to rows. |
|
| 75 | - */ |
|
| 76 | - public function appendRows($theRows, $theOptionFlag) |
|
| 77 | - { |
|
| 69 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 70 | + /** |
|
| 71 | + * Appends rows. |
|
| 72 | + * |
|
| 73 | + * @param \array[] $theRows Rows array. |
|
| 74 | + * @param bool $theOptionFlag If not null append table options to rows. |
|
| 75 | + */ |
|
| 76 | + public function appendRows($theRows, $theOptionFlag) |
|
| 77 | + { |
|
| 78 | 78 | foreach ($theRows as $row) |
| 79 | 79 | { |
| 80 | - $this->rows[] = $row; |
|
| 80 | + $this->rows[] = $row; |
|
| 81 | 81 | } |
| 82 | 82 | if ($theOptionFlag) |
| 83 | 83 | { |
| 84 | - $this->rows[] = new TableSeparator(); |
|
| 85 | - $this->appendTableOption('engine'); |
|
| 86 | - $this->appendTableOption('character_set_name', 'character set'); |
|
| 87 | - $this->appendTableOption('table_collation', 'collation'); |
|
| 84 | + $this->rows[] = new TableSeparator(); |
|
| 85 | + $this->appendTableOption('engine'); |
|
| 86 | + $this->appendTableOption('character_set_name', 'character set'); |
|
| 87 | + $this->appendTableOption('table_collation', 'collation'); |
|
| 88 | + } |
|
| 88 | 89 | } |
| 89 | - } |
|
| 90 | 90 | |
| 91 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 92 | - /** |
|
| 93 | - * Get rows. |
|
| 94 | - * |
|
| 95 | - * @return \array[] |
|
| 96 | - */ |
|
| 97 | - public function getRows() |
|
| 98 | - { |
|
| 91 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 92 | + /** |
|
| 93 | + * Get rows. |
|
| 94 | + * |
|
| 95 | + * @return \array[] |
|
| 96 | + */ |
|
| 97 | + public function getRows() |
|
| 98 | + { |
|
| 99 | 99 | return $this->rows; |
| 100 | - } |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | 102 | |
| 103 | - //-------------------------------------------------------------------------------------------------------------------- |
|
| 103 | + //-------------------------------------------------------------------------------------------------------------------- |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -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 | //---------------------------------------------------------------------------------------------------------------------- |
@@ -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 | //---------------------------------------------------------------------------------------------------------------------- |