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
|
<?php
namespace Xgrz\Firebird;
use Illuminate\Database\Connection as DatabaseConnection;
use Illuminate\Support\Collection;
use Xgrz\Firebird\Query\FirebirdBuilder as FirebirdQueryBuilder;
use Xgrz\Firebird\Query\Grammars\FirebirdGrammar as FirebirdQueryGrammar;
use Xgrz\Firebird\Query\Processors\FirebirdProcessor as FirebirdQueryProcessor;
use Xgrz\Firebird\Schema\FirebirdBuilder as FirebirdSchemaBuilder;
use Xgrz\Firebird\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 \Xgrz\Firebird\Schema\Builder
*/
public function getSchemaBuilder(): FirebirdSchemaBuilder
{
if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}
return new FirebirdSchemaBuilder($this);
}
/**
* Get the default schema grammar instance.
*
* @return \Xgrz\Firebird\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 \Xgrz\Firebird\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 : [];
});
}
/**
* 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();
}
}
|