1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\db\sqlite; |
9
|
|
|
|
10
|
|
|
use yii\db\SqlToken; |
11
|
|
|
use yii\helpers\StringHelper; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Command represents an SQLite's SQL statement to be executed against a database. |
15
|
|
|
* |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
* |
18
|
|
|
* @author Sergey Makinen <[email protected]> |
19
|
|
|
* @since 2.0.14 |
20
|
|
|
*/ |
21
|
|
|
class Command extends \yii\db\Command |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @inheritdoc |
25
|
|
|
*/ |
26
|
192 |
|
public function execute() |
27
|
|
|
{ |
28
|
192 |
|
$sql = $this->getSql(); |
29
|
192 |
|
$params = $this->params; |
30
|
192 |
|
$statements = $this->splitStatements($sql, $params); |
31
|
192 |
|
if ($statements === false) { |
32
|
191 |
|
return parent::execute(); |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
$result = null; |
36
|
1 |
|
foreach ($statements as $statement) { |
37
|
1 |
|
[$statementSql, $statementParams] = $statement; |
|
|
|
|
38
|
1 |
|
$this->setSql($statementSql)->bindValues($statementParams); |
|
|
|
|
39
|
1 |
|
$result = parent::execute(); |
40
|
|
|
} |
41
|
1 |
|
$this->setSql($sql)->bindValues($params); |
42
|
1 |
|
return $result; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
368 |
|
protected function queryInternal($method, $fetchMode = null) |
49
|
|
|
{ |
50
|
368 |
|
$sql = $this->getSql(); |
51
|
368 |
|
$params = $this->params; |
52
|
368 |
|
$statements = $this->splitStatements($sql, $params); |
53
|
368 |
|
if ($statements === false) { |
54
|
368 |
|
return parent::queryInternal($method, $fetchMode); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
[$lastStatementSql, $lastStatementParams] = array_pop($statements); |
|
|
|
|
58
|
1 |
|
foreach ($statements as $statement) { |
59
|
1 |
|
[$statementSql, $statementParams] = $statement; |
|
|
|
|
60
|
1 |
|
$this->setSql($statementSql)->bindValues($statementParams); |
|
|
|
|
61
|
1 |
|
parent::execute(); |
|
|
|
|
62
|
|
|
} |
63
|
1 |
|
$this->setSql($lastStatementSql)->bindValues($lastStatementParams); |
|
|
|
|
64
|
1 |
|
$result = parent::queryInternal($method, $fetchMode); |
65
|
1 |
|
$this->setSql($sql)->bindValues($params); |
66
|
1 |
|
return $result; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Splits the specified SQL code into individual SQL statements and returns them |
71
|
|
|
* or `false` if there's a single statement. |
72
|
|
|
* @param string $sql |
73
|
|
|
* @param array $params |
74
|
|
|
* @return string[]|false |
75
|
|
|
*/ |
76
|
385 |
|
private function splitStatements($sql, $params) |
77
|
|
|
{ |
78
|
385 |
|
$semicolonIndex = strpos($sql, ';'); |
79
|
385 |
|
if ($semicolonIndex === false || $semicolonIndex === StringHelper::byteLength($sql) - 1) { |
80
|
385 |
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
$tokenizer = new SqlTokenizer($sql); |
84
|
1 |
|
$codeToken = $tokenizer->tokenize(); |
85
|
1 |
|
if (count($codeToken->getChildren()) === 1) { |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
$statements = []; |
90
|
1 |
|
foreach ($codeToken->getChildren() as $statement) { |
91
|
1 |
|
$statements[] = [$statement->getSql(), $this->extractUsedParams($statement, $params)]; |
92
|
|
|
} |
93
|
1 |
|
return $statements; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns named bindings used in the specified statement token. |
98
|
|
|
* @param SqlToken $statement |
99
|
|
|
* @param array $params |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
1 |
|
private function extractUsedParams(SqlToken $statement, $params) |
103
|
|
|
{ |
104
|
1 |
|
preg_match_all('/(?P<placeholder>[:][a-zA-Z0-9_]+)/', $statement->getSql(), $matches, PREG_SET_ORDER); |
105
|
1 |
|
$result = []; |
106
|
1 |
|
foreach ($matches as $match) { |
|
|
|
|
107
|
1 |
|
$phName = ltrim($match['placeholder'], ':'); |
108
|
1 |
|
if (isset($params[$phName])) { |
109
|
1 |
|
$result[$phName] = $params[$phName]; |
110
|
|
|
} elseif (isset($params[':' . $phName])) { |
111
|
1 |
|
$result[':' . $phName] = $params[':' . $phName]; |
112
|
|
|
} |
113
|
|
|
} |
114
|
1 |
|
return $result; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.