Completed
Push — master ( e7eda2...d37324 )
by P.R.
06:29
created

AuditDataLayer::createAuditTrigger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 13
cts 13
cp 1
rs 9.0856
cc 1
eloc 20
nc 1
nop 9
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
//----------------------------------------------------------------------------------------------------------------------
3
namespace SetBased\Audit\MySql;
4
5
use SetBased\Audit\MySql\Metadata\TableColumnsMetadata;
6
use SetBased\Audit\MySql\Sql\AlterAuditTableAddColumns;
7
use SetBased\Audit\MySql\Sql\CreateAuditTable;
8
use SetBased\Audit\MySql\Sql\CreateAuditTrigger;
9
use SetBased\Helper\CodeStore\MySqlCompoundSyntaxCodeStore;
10
use SetBased\Stratum\MySql\StaticDataLayer;
11
use SetBased\Stratum\Style\StratumStyle;
12
13
//----------------------------------------------------------------------------------------------------------------------
14
/**
15
 * Class for executing SQL statements and retrieving metadata from MySQL.
16
 */
17
class AuditDataLayer extends StaticDataLayer
18
{
19
  //--------------------------------------------------------------------------------------------------------------------
20
  /**
21
   * The Output decorator.
22
   *
23
   * @var StratumStyle
24
   */
25
  private static $io;
26
27
  //--------------------------------------------------------------------------------------------------------------------
28
  /**
29
   * Adds new columns to an audit table.
30
   *
31
   * @param string               $auditSchemaName The name of audit schema.
32
   * @param string               $tableName       The name of the table.
33
   * @param TableColumnsMetadata $columns         The metadata of the new columns.
34
   */
35 1
  public static function addNewColumns($auditSchemaName, $tableName, $columns)
36
  {
37 1
    $helper = new AlterAuditTableAddColumns($auditSchemaName, $tableName, $columns);
38 1
    $sql    = $helper->buildStatement();
39
40 1
    self::executeNone($sql);
41 1
  }
42
43
  //--------------------------------------------------------------------------------------------------------------------
44
  /**
45
   * Creates an audit table.
46
   *
47
   * @param string               $dataSchemaName  The name of the data schema.
48
   * @param string               $auditSchemaName The name of the audit schema.
49
   * @param string               $tableName       The name of the table.
50
   * @param TableColumnsMetadata $columns         The metadata of the columns of the audit table (i.e. the audit
51
   *                                              columns and columns of the data table).
52
   */
53 13
  public static function createAuditTable($dataSchemaName, $auditSchemaName, $tableName, $columns)
54
  {
55 13
    $helper = new CreateAuditTable($dataSchemaName, $auditSchemaName, $tableName, $columns);
56 13
    $sql    = $helper->buildStatement();
57
58 13
    self::executeNone($sql);
59 13
  }
60
61
  //--------------------------------------------------------------------------------------------------------------------
62
  /**
63
   * Creates a trigger on a table.
64
   *
65
   * @param string               $dataSchemaName  The name of the data schema.
66
   * @param string               $auditSchemaName The name of the audit schema.
67
   * @param string               $tableName       The name of the table.
68
   * @param string               $triggerAction   The trigger action (i.e. INSERT, UPDATE, or DELETE).
69
   * @param string               $triggerName     The name of the trigger.
70
   * @param TableColumnsMetadata $auditColumns    The audit table columns.
71
   * @param TableColumnsMetadata $tableColumns    The data table columns.
72
   * @param string               $skipVariable    The skip variable.
73
   * @param string[]             $additionSql     Additional SQL statements.
74
   */
75 16
  public static function createAuditTrigger($dataSchemaName,
76
                                            $auditSchemaName,
77
                                            $tableName,
78
                                            $triggerName,
79
                                            $triggerAction,
80
                                            $auditColumns,
81
                                            $tableColumns,
82
                                            $skipVariable,
83
                                            $additionSql)
84
  {
85 16
    $helper = new CreateAuditTrigger($dataSchemaName,
86 16
                                     $auditSchemaName,
87 16
                                     $tableName,
88 16
                                     $triggerName,
89 16
                                     $triggerAction,
90 16
                                     $auditColumns,
91 16
                                     $tableColumns,
92 16
                                     $skipVariable,
93 16
                                     $additionSql);
94 16
    $sql    = $helper->buildStatement();
95
96 16
    self::executeNone($sql);
97 16
  }
98
99
  //--------------------------------------------------------------------------------------------------------------------
100
  /**
101
   * Create temp table for getting column type information for audit columns.
102
   *
103
   * @param string  $schemaName   The name of the table schema.
104
   * @param string  $tableName    The table name.
105
   * @param array[] $auditColumns Audit columns from config file.
106
   */
107 9
  public static function createTemporaryTable($schemaName, $tableName, $auditColumns)
108
  {
109 9
    $sql = new MySqlCompoundSyntaxCodeStore();
110 9
    $sql->append(sprintf('create table `%s`.`%s` (', $schemaName, $tableName));
111 9
    foreach ($auditColumns as $column)
112
    {
113 9
      $sql->append(sprintf('%s %s', $column['column_name'], $column['column_type']));
114 9
      if (end($auditColumns)!==$column)
115 9
      {
116 9
        $sql->appendToLastLine(',');
117 9
      }
118 9
    }
119 9
    $sql->append(')');
120
121 9
    self::executeNone($sql->getCode());
122 9
  }
123
124
  //--------------------------------------------------------------------------------------------------------------------
125
  /**
126
   * Drop table.
127
   *
128
   * @param string $schemaName The name of the table schema.
129
   * @param string $tableName  The name of the table.
130
   */
131 9
  public static function dropTemporaryTable($schemaName, $tableName)
132
  {
133 9
    $sql = sprintf('drop table `%s`.`%s`', $schemaName, $tableName);
134
135 9
    self::executeNone($sql);
136 9
  }
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 6
  public static function dropTrigger($triggerSchema, $triggerName)
146
  {
147 6
    $sql = sprintf('drop trigger `%s`.`%s`', $triggerSchema, $triggerName);
148
149 6
    self::executeNone($sql);
150 6
  }
151
152
  //--------------------------------------------------------------------------------------------------------------------
153
  /**
154
   * {@inheritdoc}
155
   */
156
  public static function executeBulk($bulkHandler, $query)
157
  {
158
    self::logQuery($query);
159
160
    parent::executeBulk($bulkHandler, $query);
161
  }
162
163
  //--------------------------------------------------------------------------------------------------------------------
164
  /**
165
   * {@inheritdoc}
166
   */
167 16
  public static function executeNone($query)
168
  {
169 16
    self::logQuery($query);
170
171 16
    return parent::executeNone($query);
172
  }
173
174
  //--------------------------------------------------------------------------------------------------------------------
175
  /**
176
   * {@inheritdoc}
177
   */
178
  public static function executeRow0($query)
179
  {
180
    self::logQuery($query);
181
182
    return parent::executeRow0($query);
183
  }
184
185
  //--------------------------------------------------------------------------------------------------------------------
186
  /**
187
   * {@inheritdoc}
188
   */
189 13
  public static function executeRow1($query)
190
  {
191 13
    self::logQuery($query);
192
193 13
    return parent::executeRow1($query);
194
  }
195
196
  //--------------------------------------------------------------------------------------------------------------------
197
  /**
198
   * {@inheritdoc}
199
   */
200 17
  public static function executeRows($query)
201
  {
202 17
    self::logQuery($query);
203
204 17
    return parent::executeRows($query);
205
  }
206
207
  //--------------------------------------------------------------------------------------------------------------------
208
  /**
209
   * {@inheritdoc}
210
   */
211
  public static function executeSingleton0($query)
212
  {
213
    self::logQuery($query);
214
215
    return parent::executeSingleton0($query);
216
  }
217
218
  //--------------------------------------------------------------------------------------------------------------------
219
  /**
220
   * {@inheritdoc}
221
   */
222
  public static function executeSingleton1($query)
223
  {
224
    self::logQuery($query);
225
226
    return parent::executeSingleton1($query);
227
  }
228
229
  //--------------------------------------------------------------------------------------------------------------------
230
  /**
231
   * {@inheritdoc}
232
   */
233
  public static function executeTable($query)
234
  {
235
    self::logQuery($query);
236
237
    return parent::executeTable($query);
238
  }
239
240
  //--------------------------------------------------------------------------------------------------------------------
241
  /**
242
   * Selects metadata of all columns of table.
243
   *
244
   * @param string $schemaName The name of the table schema.
245
   * @param string $tableName  The name of the table.
246
   *
247
   * @return \array[]
248
   */
249 16
  public static function getTableColumns($schemaName, $tableName)
250
  {
251 16
    $sql = sprintf('
252
select COLUMN_NAME        as column_name
253
,      COLUMN_TYPE        as column_type
254
,      IS_NULLABLE        as is_nullable
255
,      CHARACTER_SET_NAME as character_set_name
256
,      COLLATION_NAME     as collation_name
257
from   information_schema.COLUMNS
258
where  TABLE_SCHEMA = %s
259
and    TABLE_NAME   = %s
260 16
order by ORDINAL_POSITION',
261 16
                   parent::quoteString($schemaName),
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (quoteString() instead of getTableColumns()). Are you sure this is correct? If so, you might want to change this to $this->quoteString().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
262 16
                   parent::quoteString($tableName));
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (quoteString() instead of getTableColumns()). Are you sure this is correct? If so, you might want to change this to $this->quoteString().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
263
264 16
    return self::executeRows($sql);
265
  }
266
267
  //--------------------------------------------------------------------------------------------------------------------
268
  /**
269
   * Selects table engine, character_set_name and table_collation.
270
   *
271
   * @param string $schemaName The name of the table schema.
272
   * @param string $tableName  The name of the table.
273
   *
274
   * @return array
275
   */
276 13
  public static function getTableOptions($schemaName, $tableName)
277
  {
278 13
    $sql = sprintf('
279
SELECT t1.TABLE_COLLATION    as table_collation
280
,      t1.ENGINE             as engine
281
,      t2.CHARACTER_SET_NAME as character_set_name
282
FROM       information_schema.TABLES                                t1
283
inner join information_schema.COLLATION_CHARACTER_SET_APPLICABILITY t2  on  t2.COLLATION_NAME = t1.TABLE_COLLATION
284
WHERE t1.TABLE_SCHEMA = %s
285 13
AND   t1.TABLE_NAME   = %s',
286 13
                   parent::quoteString($schemaName),
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (quoteString() instead of getTableOptions()). Are you sure this is correct? If so, you might want to change this to $this->quoteString().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
287 13
                   parent::quoteString($tableName));
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (quoteString() instead of getTableOptions()). Are you sure this is correct? If so, you might want to change this to $this->quoteString().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
288
289 13
    return self::executeRow1($sql);
290
  }
291
292
  //--------------------------------------------------------------------------------------------------------------------
293
  /**
294
   * Selects all triggers on a table.
295
   *
296
   * @param string $schemaName The name of the table schema.
297
   * @param string $tableName  The name of the table.
298
   *
299
   * @return \array[]
300
   */
301 16
  public static function getTableTriggers($schemaName, $tableName)
302
  {
303 16
    $sql = sprintf('
304
select TRIGGER_NAME as trigger_name
305
from   information_schema.TRIGGERS
306
where  TRIGGER_SCHEMA     = %s
307
and    EVENT_OBJECT_TABLE = %s
308 16
order by Trigger_Name',
309 16
                   parent::quoteString($schemaName),
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (quoteString() instead of getTableTriggers()). Are you sure this is correct? If so, you might want to change this to $this->quoteString().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
310 16
                   parent::quoteString($tableName));
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (quoteString() instead of getTableTriggers()). Are you sure this is correct? If so, you might want to change this to $this->quoteString().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
311
312 16
    return self::executeRows($sql);
313
  }
314
315
  //--------------------------------------------------------------------------------------------------------------------
316
  /**
317
   * Selects all table names in a schema.
318
   *
319
   * @param string $schemaName The name of the schema.
320
   *
321
   * @return \array[]
322
   */
323 17
  public static function getTablesNames($schemaName)
324
  {
325 17
    $sql = sprintf("
326
select TABLE_NAME as table_name
327
from   information_schema.TABLES
328
where  TABLE_SCHEMA = %s
329
and    TABLE_TYPE   = 'BASE TABLE'
330 17
order by TABLE_NAME", parent::quoteString($schemaName));
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (quoteString() instead of getTablesNames()). Are you sure this is correct? If so, you might want to change this to $this->quoteString().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
331
332 17
    return self::executeRows($sql);
333
  }
334
335
  //--------------------------------------------------------------------------------------------------------------------
336
  /**
337
   * Selects all triggers in a schema
338
   *
339
   * @param string $schemaName The name of the table schema.
340
   *
341
   * @return \array[]
342
   */
343
  public static function getTriggers($schemaName)
344
  {
345
    $sql = sprintf('
346
select EVENT_OBJECT_TABLE as table_name
347
,      TRIGGER_NAME       as trigger_name
348
from   information_schema.TRIGGERS
349
where  TRIGGER_SCHEMA     = %s
350
order by EVENT_OBJECT_TABLE
351
,        TRIGGER_NAME',
352
                   parent::quoteString($schemaName));
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (quoteString() instead of getTriggers()). Are you sure this is correct? If so, you might want to change this to $this->quoteString().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
353
354
    return self::executeRows($sql);
355
  }
356
357
  //--------------------------------------------------------------------------------------------------------------------
358
  /**
359
   * Acquires a write lock on a table.
360
   *
361
   * @param string $tableName The table name.
362
   */
363 16
  public static function lockTable($tableName)
364
  {
365 16
    $sql = sprintf('lock tables `%s` write', $tableName);
366
367 16
    self::executeNone($sql);
368 16
  }
369
370
  //--------------------------------------------------------------------------------------------------------------------
371
  /**
372
   * {@inheritdoc}
373
   */
374
  public static function multiQuery($queries)
375
  {
376
    self::logQuery($queries);
377
378
    return parent::multiQuery($queries);
379
  }
380
381
  //--------------------------------------------------------------------------------------------------------------------
382
  /**
383
   * {@inheritdoc}
384
   */
385
  public static function query($query)
386
  {
387
    self::logQuery($query);
388
389
    return parent::multiQuery($query);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (multiQuery() instead of query()). Are you sure this is correct? If so, you might want to change this to $this->multiQuery().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
390
  }
391
392
  //--------------------------------------------------------------------------------------------------------------------
393
  /**
394
   * Sets the Output decorator.
395
   *
396
   * @param StratumStyle $io The Output decorator.
397
   */
398 17
  public static function setIo($io)
399
  {
400 17
    self::$io = $io;
401 17
  }
402
403
  //--------------------------------------------------------------------------------------------------------------------
404
  /**
405
   * Drop table.
406
   *
407
   * @param string $schemaName The name of the table schema.
408
   * @param string $tableName  The name of the table.
409
   *
410
   * @return \array[]
411
   */
412
  public static function showColumns($schemaName, $tableName)
413
  {
414
    $sql = sprintf('SHOW COLUMNS FROM `%s`.`%s`', $schemaName, $tableName);
415
416
    return self::executeRows($sql);
417
  }
418
419
  //--------------------------------------------------------------------------------------------------------------------
420
  /**
421
   * Releases all table locks.
422
   */
423 16
  public static function unlockTables()
424
  {
425 16
    $sql = 'unlock tables';
426
427 16
    self::executeNone($sql);
428 16
  }
429
430
  //--------------------------------------------------------------------------------------------------------------------
431
  /**
432
   * {@inheritdoc}
433
   */
434
  protected static function realQuery($query)
435
  {
436
    self::logQuery($query);
437
438
    return parent::realQuery($query);
439
  }
440
441
  //--------------------------------------------------------------------------------------------------------------------
442
  /**
443
   * Logs the query on the console.
444
   *
445
   * @param string $query The query.
446
   */
447 17
  private static function logQuery($query)
448
  {
449 17
    $query = trim($query);
450
451 17
    if (strpos($query, "\n")!==false)
452 17
    {
453
      // Query is a multi line query.
454 17
      self::$io->logVeryVerbose('Executing query:');
455 17
      self::$io->logVeryVerbose('<sql>%s</sql>', $query);
456 17
    }
457
    else
458
    {
459
      // Query is a single line query.
460 16
      self::$io->logVeryVerbose('Executing query: <sql>%s</sql>', $query);
461
    }
462 17
  }
463
464
  //--------------------------------------------------------------------------------------------------------------------
465
}
466
467
//----------------------------------------------------------------------------------------------------------------------
468