-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParametrizedSqlMigrationQuery.php
72 lines (63 loc) · 1.8 KB
/
ParametrizedSqlMigrationQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace Effiana\MigrationBundle\Migration;
use Psr\Log\LoggerInterface;
class ParametrizedSqlMigrationQuery extends ParametrizedMigrationQuery
{
/**
* @var array value = [sql, parameters, types]
*/
protected $queries = [];
/**
* Adds SQL query
*
* @param string $query The SQL query
* @param array $params The parameters to bind to the query, if any.
* @param array $types The types the previous parameters are in.
*/
public function __construct($query = null, array $params = [], array $types = [])
{
if (!empty($query)) {
$this->queries[] = [$query, $params, $types];
}
}
/**
* Adds SQL query
*
* @param string $query The SQL query
* @param array $params The parameters to bind to the query, if any.
* @param array $types The types the previous parameters are in.
*/
public function addSql($query, array $params = [], array $types = [])
{
$this->queries[] = [$query, $params, $types];
}
/**
* {@inheritdoc}
*/
public function getDescription()
{
$logger = new ArrayLogger();
$this->processQueries($logger, true);
return $logger->getMessages();
}
/**
* {@inheritdoc}
*/
public function execute(LoggerInterface $logger)
{
$this->processQueries($logger);
}
/**
* @param LoggerInterface $logger
* @param bool $dryRun
*/
protected function processQueries(LoggerInterface $logger, $dryRun = false)
{
foreach ($this->queries as $query) {
$this->logQuery($logger, $query[0], $query[1], $query[2]);
if (!$dryRun) {
$this->connection->executeUpdate($query[0], $query[1], $query[2]);
}
}
}
}