| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SetBased\Audit\MySql\Sql; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SetBased\Audit\MySql\AuditDataLayer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SetBased\Audit\MySql\Metadata\TableColumnsMetadata; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SetBased\Helper\CodeStore\MySqlCompoundSyntaxCodeStore; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Class for creating SQL statements for creating audit tables. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class CreateAuditTable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |   //-------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |    * The name of the audit schema. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |    * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |   private $auditSchemaName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |    * The name of the table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |    * @var TableColumnsMetadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |   private $columns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |    * The name of the data schema. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |    * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |   private $dataSchemaName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |    * The name of the table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |    * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |   private $tableName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 TableColumnsMetadata $columns         The metadata of the columns of the audit table (i.e. the audit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |    *                                              columns and columns of the data table). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 22 |  |   public function __construct($dataSchemaName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                               $auditSchemaName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                               $tableName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                               $columns) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 22 |  |     $this->dataSchemaName  = $dataSchemaName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 22 |  |     $this->auditSchemaName = $auditSchemaName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 22 |  |     $this->tableName       = $tableName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 22 |  |     $this->columns         = $columns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 22 |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |   //-------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |    * Returns a SQL statement for creating the audit table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |    * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 22 |  |   public function buildStatement() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 22 |  |     $code = new MySqlCompoundSyntaxCodeStore(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 22 |  |     $code->append(sprintf('create table `%s`.`%s`', $this->auditSchemaName, $this->tableName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     // Create SQL for columns. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 22 |  |     $code->append('('); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 22 |  |     $code->append($this->getColumnDefinitions()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     // Create SQL for table options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 22 |  |     $tableOptions = AuditDataLayer::getTableOptions($this->dataSchemaName, $this->tableName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 22 |  |     $code->append(sprintf(') engine=%s character set=%s collate=%s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 22 |  |                           $tableOptions['engine'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 22 |  |                           $tableOptions['character_set_name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 22 |  |                           $tableOptions['table_collation'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 22 |  |     return $code->getCode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |   //-------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |    * Returns an array with SQL code for column definitions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |    * @return string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 22 |  |   private function getColumnDefinitions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 22 |  |     $lines = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 22 |  |     $columns   = $this->columns->getColumns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 22 |  |     $maxLength = $this->columns->getLongestColumnNameLength(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 22 |  |     foreach ($columns as $column) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 22 |  |       $name   = $column->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 22 |  |       $filler = str_repeat(' ', $maxLength - mb_strlen($name) + 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 22 |  |       $line = sprintf('  `%s`%s%s', $name, $filler, $column->getColumnDefinition()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 22 |  |       if (end($columns)!==$column) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |       { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 22 |  |         $line .= ','; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |       } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 22 |  |       $lines[] = $line; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 22 |  |     return $lines; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |   //-------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | //---------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  |