Completed
Push — master ( 02aa65...135f7a )
by P.R.
03:53
created

AuditDataLayer::executeSingleton1()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
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
  public static function addNewColumns($auditSchemaName, $tableName, $columns)
36
  {
37
    $helper = new AlterAuditTableAddColumns($auditSchemaName, $tableName, $columns);
38
    $sql    = $helper->buildStatement();
39
40
    self::executeNone($sql);
41
  }
42 1
43
  //--------------------------------------------------------------------------------------------------------------------
44 1
  /**
45 1
   * Creates an audit table.
46
   *
47 1
   * @param string               $dataSchemaName  The name of the data schema.
48 1
   * @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
  public static function createAuditTable($dataSchemaName, $auditSchemaName, $tableName, $columns)
54
  {
55
    $helper = new CreateAuditTable($dataSchemaName, $auditSchemaName, $tableName, $columns);
56
    $sql    = $helper->buildStatement();
57
58
    self::executeNone($sql);
59
  }
60
61
  //--------------------------------------------------------------------------------------------------------------------
62
  /**
63 17
   * Creates a trigger on a table.
64
   *
65 17
   * @param string               $dataSchemaName  The name of the data schema.
66
   * @param string               $auditSchemaName The name of the audit schema.
67 17
   * @param string               $tableName       The name of the table.
68 17
   * @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
  public static function createAuditTrigger($dataSchemaName,
76
                                            $auditSchemaName,
77
                                            $tableName,
78
                                            $triggerName,
79
                                            $triggerAction,
80 13
                                            $auditColumns,
81
                                            $tableColumns,
82 13
                                            $skipVariable,
83 13
                                            $additionSql)
84
  {
85 13
    $helper = new CreateAuditTrigger($dataSchemaName,
86 13
                                     $auditSchemaName,
87
                                     $tableName,
88
                                     $triggerName,
89
                                     $triggerAction,
90
                                     $auditColumns,
91
                                     $tableColumns,
92
                                     $skipVariable,
93
                                     $additionSql);
94
    $sql    = $helper->buildStatement();
95
96
    self::executeNone($sql);
97
  }
98
99
  //--------------------------------------------------------------------------------------------------------------------
100
  /**
101
   * Create temp table for getting column type information for audit columns.
102 16
   *
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
  public static function createTemporaryTable($schemaName, $tableName, $auditColumns)
108
  {
109
    $sql = new MySqlCompoundSyntaxCodeStore();
110
    $sql->append(sprintf('create table `%s`.`%s` (', $schemaName, $tableName));
111
    foreach ($auditColumns as $column)
112 16
    {
113 16
      $sql->append(sprintf('%s %s', $column['column_name'], $column['column_type']));
114 16
      if (end($auditColumns)!==$column)
115 16
      {
116 16
        $sql->appendToLastLine(',');
117 16
      }
118 16
    }
119 16
    $sql->append(')');
120 16
121 16
    self::executeNone($sql->getCode());
122
  }
123 16
124 16
  //--------------------------------------------------------------------------------------------------------------------
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
  public static function dropTemporaryTable($schemaName, $tableName)
132
  {
133
    $sql = sprintf('drop table `%s`.`%s`', $schemaName, $tableName);
134 9
135
    self::executeNone($sql);
136 9
  }
137 9
138 9
  //--------------------------------------------------------------------------------------------------------------------
139
  /**
140 9
   * Drops a trigger.
141 9
   *
142 9
   * @param string $triggerSchema The name of the trigger schema.
143 9
   * @param string $triggerName   The mame of trigger.
144 9
   */
145 9
  public static function dropTrigger($triggerSchema, $triggerName)
146 9
  {
147
    $sql = sprintf('drop trigger `%s`.`%s`', $triggerSchema, $triggerName);
148 9
149 9
    self::executeNone($sql);
150
  }
151
152
  //--------------------------------------------------------------------------------------------------------------------
153
  /**
154
   * {@inheritdoc}
155 17
   */
156
  public static function executeBulk($bulkHandler, $query)
157 17
  {
158 17
    self::logQuery($query);
159 17
160 17
    parent::executeBulk($bulkHandler, $query);
161 17
  }
162 17
163
  //--------------------------------------------------------------------------------------------------------------------
164
  /**
165
   * {@inheritdoc}
166
   */
167
  public static function executeNone($query)
168
  {
169
    self::logQuery($query);
170
171 9
    return parent::executeNone($query);
172
  }
173 9
174
  //--------------------------------------------------------------------------------------------------------------------
175 9
  /**
176 9
   * {@inheritdoc}
177
   */
178
  public static function executeRow0($query)
179
  {
180
    self::logQuery($query);
181
182
    return parent::executeRow0($query);
183
  }
184
185 6
  //--------------------------------------------------------------------------------------------------------------------
186
  /**
187 6
   * {@inheritdoc}
188
   */
189 6
  public static function executeRow1($query)
190 6
  {
191
    self::logQuery($query);
192
193
    return parent::executeRow1($query);
194
  }
195
196
  //--------------------------------------------------------------------------------------------------------------------
197
  /**
198 16
   * {@inheritdoc}
199
   */
200 16
  public static function executeRows($query)
201
  {
202 16
    self::logQuery($query);
203
204
    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 13
  /**
231
   * {@inheritdoc}
232 13
   */
233
  public static function executeTable($query)
234 13
  {
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 17
   * @param string $tableName  The name of the table.
246
   *
247 17
   * @return \array[]
248
   */
249 17
  public static function getTableColumns($schemaName, $tableName)
250
  {
251
    $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
order by ORDINAL_POSITION',
261
                   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
                   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
    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
  public static function getTableOptions($schemaName, $tableName)
277
  {
278
    $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
AND   t1.TABLE_NAME   = %s',
286
                   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
                   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
    return self::executeRow1($sql);
290
  }
291
292
  //--------------------------------------------------------------------------------------------------------------------
293 16
  /**
294
   * Selects all triggers on a table.
295 16
   *
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
  public static function getTableTriggers($schemaName, $tableName)
302
  {
303
    $sql = sprintf('
304 16
select TRIGGER_NAME as trigger_name
305 16
from   information_schema.TRIGGERS
306 16
where  TRIGGER_SCHEMA     = %s
307
and    EVENT_OBJECT_TABLE = %s
308 16
order by Trigger_Name',
309
                   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
                   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
    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 13
   *
321
   * @return \array[]
322 13
   */
323
  public static function getTablesNames($schemaName)
324
  {
325
    $sql = sprintf("
326
select TABLE_NAME as table_name
327
from   information_schema.TABLES
328
where  TABLE_SCHEMA = %s
329 13
and    TABLE_TYPE   = 'BASE TABLE'
330 13
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 13
332
    return self::executeRows($sql);
333 13
  }
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 16
    $sql = sprintf('
346
select EVENT_OBJECT_TABLE as table_name
347 16
,      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 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 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 16
354 16
    return self::executeRows($sql);
355
  }
356 16
357
  //--------------------------------------------------------------------------------------------------------------------
358
  /**
359
   * Acquires a write lock on a table.
360
   *
361
   * @param string $tableName The table name.
362
   */
363
  public static function lockTable($tableName)
364
  {
365
    $sql = sprintf('lock tables `%s` write', $tableName);
366
367 17
    self::executeNone($sql);
368
  }
369 17
370
  //--------------------------------------------------------------------------------------------------------------------
371
  /**
372
   * {@inheritdoc}
373
   */
374 17
  public static function multiQuery($queries)
375
  {
376 17
    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
  public static function setIo($io)
399
  {
400
    self::$io = $io;
401
  }
402
403
  //--------------------------------------------------------------------------------------------------------------------
404
  /**
405
   * Drop table.
406
   *
407 16
   * @param string $schemaName The name of the table schema.
408
   * @param string $tableName  The name of the table.
409 16
   *
410
   * @return \array[]
411 16
   */
412 16
  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 17
  /**
421
   * Releases all table locks.
422 17
   */
423 17
  public static function unlockTables()
424
  {
425
    $sql = 'unlock tables';
426
427
    self::executeNone($sql);
428
  }
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 16
   * @param string $query The query.
446
   */
447 16
  private static function logQuery($query)
448
  {
449 16
    $query = trim($query);
450 16
451
    if (strpos($query, "\n")!==false)
452
    {
453
      // Query is a multi line query.
454
      self::$io->logVeryVerbose('Executing query:');
455
      self::$io->logVeryVerbose('<sql>%s</sql>', $query);
456
    }
457
    else
458 17
    {
459
      // Query is a single line query.
460 17
      self::$io->logVeryVerbose('Executing query: <sql>%s</sql>', $query);
461
    }
462 17
  }
463 17
464
  //--------------------------------------------------------------------------------------------------------------------
465 17
}
466 17
467
//----------------------------------------------------------------------------------------------------------------------
468