diff options
Diffstat (limited to 'src/illuminate/FirebirdConnection.php')
| -rwxr-xr-x | src/illuminate/FirebirdConnection.php | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/src/illuminate/FirebirdConnection.php b/src/illuminate/FirebirdConnection.php new file mode 100755 index 0000000..b279632 --- /dev/null +++ b/src/illuminate/FirebirdConnection.php @@ -0,0 +1,171 @@ +<?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\Builder + */ + 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\IlluminateQuery\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(); + } +} |
