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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
<?php
namespace Firebird\Illuminate;
use Illuminate\Database\Connection as DatabaseConnection;
use Illuminate\Support\Collection;
use Firebird\Illuminate\Query\FirebirdBuilder as FirebirdQueryBuilder;
use Firebird\Illuminate\Query\Grammars\FirebirdGrammar as FirebirdQueryGrammar;
use Firebird\Illuminate\Query\Processors\FirebirdProcessor as FirebirdQueryProcessor;
use Firebird\Illuminate\Schema\FirebirdBuilder as FirebirdSchemaBuilder;
use Firebird\Illuminate\Schema\Grammars\FirebirdGrammar as FirebirdSchemaGrammar;
class FirebirdConnection extends DatabaseConnection
{
/**
* The last inserted ID generated by the server.
* RETURNED by the insert statement
*
* @var string|int|null
*/
protected $lastInsertId;
/**
* Get the default query grammar instance.
*
* @return \Illuminate\Database\Query\Grammars\Grammar
*/
protected function getDefaultQueryGrammar(): FirebirdQueryGrammar
{
return new FirebirdQueryGrammar($this);
}
/**
* Get the default post processor instance.
*
* @return \Illuminate\Database\Query\Processors\Processor
*/
protected function getDefaultPostProcessor(): FirebirdQueryProcessor
{
return new FirebirdQueryProcessor;
}
/**
* Get a schema builder instance for this connection.
*
* @return \Firebird\Illuminate\Schema\FirebirdBuilder
*/
public function getSchemaBuilder(): FirebirdSchemaBuilder
{
if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}
return new FirebirdSchemaBuilder($this);
}
/**
* Get the default schema grammar instance.
*
* @return \Firebird\Illuminate\Schema\Grammars\FirebirdGrammar
*/
protected function getDefaultSchemaGrammar()
{
return new FirebirdSchemaGrammar($this); // $this->withTablePrefix()
}
/**
* Get the connection's last insert ID.
*
* @return string|int|null
*/
public function getLastInsertId()
{
return $this->lastInsertId;
}
/**
* Get a new query builder instance.
*
* @return \Firebird\Illuminate\Query\FirebirdBuilder
*/
public function query()
{
return new FirebirdQueryBuilder(
$this, $this->getQueryGrammar(), $this->getPostProcessor()
);
}
/**
* Run an insert statement against the database.
*
* @param string $query
* @param array $bindings
* @param string|null $sequence
* @return array
*/
public function insert($query, $bindings = [], $sequence = null)
{
return $this->run($query, $bindings, function ($query, $bindings) use ($sequence) {
if ($this->pretending()) {
return true;
}
$statement = $this->getPdo()->prepare($query);
$this->bindValues($statement, $this->prepareBindings($bindings));
$this->recordsHaveBeenModified();
$result = $statement->execute();
// Fetch the RETURNING clause result to get the actual ID used
if ($result) {
$row = $statement->fetch(\PDO::FETCH_ASSOC);
if ($row) {
// Get the first (and typically only) returned value
$this->lastInsertId = reset($row);
}
}
return $result;
});
}
/**
* Run a select statement against the database.
*
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
* @return array
*/
public function select($query, $bindings = [], $useReadPdo = true)
{
return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) {
if ($this->pretending()) {
return [];
}
// For select statements, we'll simply execute the query and return an array
// of the database result set. Each element in the array will be a single
// row from the database table, and will either be an array or objects.
$statement = $this->prepared(
$this->getPdoForSelect($useReadPdo)->prepare($query)
);
$this->bindValues($statement, $this->prepareBindings($bindings));
$statement->execute();
$result = [];
while ($row = $statement->fetch(\PDO::FETCH_ASSOC)) {
$lowerCaseRow = array_change_key_case((array) $row, CASE_LOWER);
$result[] = (object) $lowerCaseRow;
}
return count($result) > 0 ? $result : [];
});
}
/**
* Run a select statement against the database and returns a generator.
*
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
* @return \Generator<int, \stdClass>
*/
public function cursor($query, $bindings = [], $useReadPdo = true)
{
$statement = $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) {
if ($this->pretending()) {
return [];
}
// First we will create a statement for the query. Then, we will set the fetch
// mode and prepare the bindings for the query. Once that's done we will be
// ready to execute the query against the database and return the cursor.
$statement = $this->prepared($this->getPdoForSelect($useReadPdo)
->prepare($query));
$this->bindValues(
$statement, $this->prepareBindings($bindings)
);
// Next, we'll execute the query against the database and return the statement
// so we can return the cursor. The cursor will use a PHP generator to give
// back one row at a time without using a bunch of memory to render them.
$statement->execute();
return $statement;
});
while ($record = $statement->fetch()) {
$lowerCaseRow = array_change_key_case((array) $record, CASE_LOWER);
yield (object) $lowerCaseRow;
}
}
/**
* Execute a stored procedure.
*
* @param string $procedure
* @param array $values
* @return Collection
*/
public function executeProcedure($procedure, array $values = []): Collection
{
return $this->query()->fromProcedure($procedure, $values)->get();
}
}
|