-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathQueryBuilder.php
158 lines (137 loc) · 5.57 KB
/
QueryBuilder.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\db\pgsql;
use craft\db\Connection;
use craft\helpers\Json;
/**
* @inheritdoc
* @property Connection $db Connection the DB connection that this command is associated with.
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0.0
*/
class QueryBuilder extends \yii\db\pgsql\QueryBuilder
{
/**
* Builds a SQL statement for dropping a DB table if it exists.
*
* @param string $table The table to be dropped. The name will be properly quoted by the method.
* @return string The SQL statement for dropping a DB table.
*/
public function dropTableIfExists(string $table): string
{
return 'DROP TABLE IF EXISTS ' . $this->db->quoteTableName($table);
}
/**
* Builds a SQL statement for renaming a DB sequence.
*
* @param string $oldName the sequence to be renamed. The name will be properly quoted by the method.
* @param string $newName the new sequence name. The name will be properly quoted by the method.
* @return string the SQL statement for renaming a DB table.
*/
public function renameSequence(string $oldName, string $newName): string
{
return 'ALTER SEQUENCE ' . $this->db->quoteTableName($oldName) . ' RENAME TO ' . $this->db->quoteTableName($newName);
}
/**
* Builds a SQL statement for replacing some text with other text in a given table column.
*
* @param string $table The table to be updated.
* @param string $column The column to be searched.
* @param string $find The text to be searched for.
* @param string $replace The replacement text.
* @param array|string $condition The condition that will be put in the WHERE part. Please
* refer to [[Query::where()]] on how to specify condition.
* @param array $params The binding parameters that will be generated by this method.
* They should be bound to the DB command later.
* @return string The SQL statement for replacing some text in a given table.
*/
public function replace(string $table, string $column, string $find, string $replace, array|string $condition, array &$params): string
{
$column = $this->db->quoteColumnName($column);
$findPhName = self::PARAM_PREFIX . count($params);
$params[$findPhName] = $find;
$replacePhName = self::PARAM_PREFIX . count($params);
$params[$replacePhName] = $replace;
$sql = 'UPDATE ' . $table .
" SET $column = REPLACE($column, $findPhName, $replacePhName)";
$where = $this->buildWhere($condition, $params);
return $where === '' ? $sql : $sql . ' ' . $where;
}
/**
* Builds the SQL expression used to return a DB result in a fixed order.
*
* http://stackoverflow.com/a/1310188/684
*
* @param string $column The column name that contains the values.
* @param array $values The column values, in the order in which the rows should be returned in.
* @return string The SQL expression.
*/
public function fixedOrder(string $column, array $values): string
{
$schema = $this->db->getSchema();
$sql = 'CASE';
$key = -1;
foreach ($values as $key => $value) {
$sql .= sprintf(' WHEN %s=%s THEN %s', $schema->quoteColumnName($column), $schema->quoteValue($value), $schema->quoteValue($key));
}
$sql .= sprintf(' ELSE %s END', $schema->quoteValue((string)($key + 1)));
return $sql;
}
/**
* Builds the SQL expression used to delete duplicate rows from a table.
*
* @param string $table The table where the data will be deleted from
* @param string[] $columns The column names that contain duplicate data
* @param string $pk The primary key column name
* @return string The SQL expression
* @since 3.5.2
*/
public function deleteDuplicates(string $table, array $columns, string $pk = 'id'): string
{
$table = $this->db->quoteTableName($table);
$pk = $this->db->quoteColumnName($pk);
$a = $this->db->quoteColumnName('a');
$b = $this->db->quoteColumnName('b');
$sql = "DELETE FROM $table $a" .
" USING $table $b" .
" WHERE $a.$pk > $b.$pk";
foreach ($columns as $column) {
$column = $this->db->quoteColumnName($column);
$sql .= " AND $a.$column = $b.$column";
}
return $sql;
}
/**
* Builds the SQL expression used to extract a value from a JSON column.
*
* @param string $column The column name to extract from
* @param string[] $path The path to the value to extract
* @return string
* @since 5.0.0
*/
public function jsonExtract(string $column, array $path): string
{
$column = $this->db->quoteColumnName($column);
$path = $this->db->quoteValue(
sprintf('{%s}', implode(',', array_map(fn(string $seg) => sprintf('"%s"', $seg), $path)))
);
return "($column#>>$path)";
}
/**
* Builds the SQL expression used to check whether a given value is contained by a target JSON value.
*
* @param string $targetSql SQL that expresses the JSON value
* @param mixed $value The value to check for (**not** JSON-encoded)
* @return string
* @since 5.0.0
*/
public function jsonContains(string $targetSql, mixed $value): string
{
$value = $this->db->quoteValue(Json::encode($value));
return "($targetSql @> $value::jsonb)";
}
}