1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CloudyCity\LaravelBuilderMacros\Library\Database\Query; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
6
|
|
|
use Illuminate\Database\Query\Builder as QueryBuilder; |
7
|
|
|
|
8
|
|
|
class MySqlBuilder extends QueryBuilder |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The database query grammar instance. |
12
|
|
|
* |
13
|
|
|
* @var Grammars\MySqlGrammar |
14
|
|
|
*/ |
15
|
|
|
public $grammar; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Insert a new record into the database, replace on primary key conflict. |
19
|
|
|
* |
20
|
|
|
* @param array $values |
21
|
|
|
* @return bool |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
public function replace(array $values) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
if (empty($values)) { |
26
|
|
|
return true; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// Since every insert gets treated like a batch insert, we will make sure the |
30
|
|
|
// bindings are structured in a way that is convenient for building these |
31
|
|
|
// inserts statements by verifying the elements are actually an array. |
32
|
|
|
if (! is_array(reset($values))) { |
33
|
|
|
$values = [$values]; |
34
|
|
|
} else { // Sort the keys in each row alphabetically for consistency |
35
|
|
|
foreach ($values as $key => $value) { |
36
|
|
|
ksort($value); |
37
|
|
|
$values[$key] = $value; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// We'll treat every insert like a batch insert so we can easily insert each |
42
|
|
|
// of the records into the database consistently. This will make it much |
43
|
|
|
// easier on the grammars to just handle one type of record insertion. |
44
|
|
|
$bindings = []; |
45
|
|
|
|
46
|
|
|
foreach ($values as $record) { |
47
|
|
|
foreach ($record as $value) { |
48
|
|
|
$bindings[] = $value; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$sql = $this->grammar->compileReplace($this, $values); |
53
|
|
|
|
54
|
|
|
// Once we have compiled the insert statement's SQL we can execute it on the |
55
|
|
|
// connection and return a result as a boolean success indicator as that |
56
|
|
|
// is the same type of result returned by the raw connection instance. |
57
|
|
|
$bindings = $this->cleanBindings($bindings); |
58
|
|
|
|
59
|
|
|
return $this->connection->insert($sql, $bindings); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Insert a new record into the database, update on primary key conflict. |
64
|
|
|
* |
65
|
|
|
* @param array $values |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function insertUpdate(array $values) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
if (empty($values)) { |
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Since every insert gets treated like a batch insert, we will make sure the |
75
|
|
|
// bindings are structured in a way that is convenient for building these |
76
|
|
|
// inserts statements by verifying the elements are actually an array. |
77
|
|
|
if (! is_array(reset($values))) { |
78
|
|
|
$values = [$values]; |
79
|
|
|
} else { // Sort the keys in each row alphabetically for consistency |
80
|
|
|
foreach ($values as $key => $value) { |
81
|
|
|
ksort($value); |
82
|
|
|
$values[$key] = $value; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// We'll treat every insert like a batch insert so we can easily insert each |
87
|
|
|
// of the records into the database consistently. This will make it much |
88
|
|
|
// easier on the grammars to just handle one type of record insertion. |
89
|
|
|
$bindings = []; |
90
|
|
|
|
91
|
|
|
foreach ($values as $record) { |
92
|
|
|
foreach ($record as $value) { |
93
|
|
|
$bindings[] = $value; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$sql = $this->grammar->compileInsertUpdate($this, $values); |
98
|
|
|
|
99
|
|
|
// Once we have compiled the insert statement's SQL we can execute it on the |
100
|
|
|
// connection and return a result as a boolean success indicator as that |
101
|
|
|
// is the same type of result returned by the raw connection instance. |
102
|
|
|
$bindings = $this->cleanBindings($bindings); |
103
|
|
|
|
104
|
|
|
return $this->connection->insert($sql, $bindings); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Insert a new record into the database, discard on primary key conflict. |
109
|
|
|
* |
110
|
|
|
* @param array $values |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function insertIgnore(array $values) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
if (empty($values)) { |
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Since every insert gets treated like a batch insert, we will make sure the |
120
|
|
|
// bindings are structured in a way that is convenient for building these |
121
|
|
|
// inserts statements by verifying the elements are actually an array. |
122
|
|
|
if (! is_array(reset($values))) { |
123
|
|
|
$values = [$values]; |
124
|
|
|
} else { |
125
|
|
|
foreach ($values as $key => $value) { |
126
|
|
|
ksort($value); |
127
|
|
|
$values[$key] = $value; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// We'll treat every insert like a batch insert so we can easily insert each |
132
|
|
|
// of the records into the database consistently. This will make it much |
133
|
|
|
// easier on the grammars to just handle one type of record insertion. |
134
|
|
|
$bindings = []; |
135
|
|
|
|
136
|
|
|
foreach ($values as $record) { |
137
|
|
|
foreach ($record as $value) { |
138
|
|
|
$bindings[] = $value; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$sql = $this->grammar->compileInsertIgnore($this, $values); |
143
|
|
|
|
144
|
|
|
// Once we have compiled the insert statement's SQL we can execute it on the |
145
|
|
|
// connection and return a result as a boolean success indicator as that |
146
|
|
|
// is the same type of result returned by the raw connection instance. |
147
|
|
|
|
148
|
|
|
$bindings = $this->cleanBindings($bindings); |
149
|
|
|
|
150
|
|
|
return $this->connection->insert($sql, $bindings); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Add a "where in raw" clause for integer values to the query. |
155
|
|
|
* |
156
|
|
|
* @param string $column |
157
|
|
|
* @param \Illuminate\Contracts\Support\Arrayable|array $values |
158
|
|
|
* @param string $boolean |
159
|
|
|
* @param bool $not |
160
|
|
|
* @return \Illuminate\Database\Query\Builder |
161
|
|
|
*/ |
162
|
|
|
public function whereIntegerInRaw($column, $values, $boolean = 'and', $not = false) |
163
|
|
|
{ |
164
|
|
|
$type = $not ? 'NotInRaw' : 'InRaw'; |
165
|
|
|
|
166
|
|
|
if ($values instanceof Arrayable) { |
167
|
|
|
$values = $values->toArray(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
foreach ($values as &$value) { |
171
|
|
|
$value = (int) $value; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$this->wheres[] = compact('type', 'column', 'values', 'boolean'); |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.