Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermeagirardi committed Feb 21, 2021
1 parent 9034957 commit 72f1057
Show file tree
Hide file tree
Showing 16 changed files with 2,360 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/vendor
.DS_Store
.*.sw?
.idea
/nbproject
/node_modules
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Laravel Firebird

---
![GitHub last commit](https://img.shields.io/github/last-commit/ejetar/laravel-firebird)
![GitHub release (latest by date)](https://img.shields.io/github/v/release/ejetar/laravel-firebird)
![GitHub](https://img.shields.io/github/license/ejetar/laravel-firebird)

* [About](#about)
* [Compability](#compability)
* [Installation](#installation)
* [Changelog](#changelog)
* [Contributing](#contributing)
* [Credits](#credits)
* [License](#license)

## About
With this package you can use Eloquent and QueryBuilder with a Firebird database. 🔥

## Compability
Support for Laravel 5.5 to 8.x with PHP 7.1+ and Firebird 1.5, 2.5, 3.0.

## Installation

1. Install/enable the Firebird PDO driver for PHP (`pdo_firebird`);
2. Install the package with composer:
```bash
composer require ejetar/laravel-firebird
```
3. As of Laravel 5.5, it is not necessary to inform service providers in `config/app.php`. But if you want to inform, enter the file `config/app.php` and include the class below in the section `providers`:
```php
Ejetar\LaravelFirebird\FirebirdServiceProvider::class
```
4. Declare your connection in section `connections` in file `config/database.php`, using firebird driver:
```php
'firebird' => [
'driver' => 'firebird',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE','/storage/firebird/APPLICATION.FDB'),
'username' => env('DB_USERNAME', 'sysdba'),
'password' => env('DB_PASSWORD', 'masterkey'),
'charset' => env('DB_CHARSET', 'UTF8'),
'role' => 'RDB$ADMIN',
//'engine_version' => '3.0', //it will be discovered automatically
]
```
If you do not enter `engine_version`, it will be discovered automatically.

## Changelog
Nothing for now...

## Contributing
Contribute to this wonderful project, it will be a pleasure to have you with us. Let's help the free software community. You are invited to incorporate new features, make corrections, report bugs, and any other form of support. Don't forget to star in this repository! 😀

## Credits
This package was based on the repository [marcha/laravel-firebird](https://github.com/marcha/laravel-firebird) and its predecessors, forked and extended:
* [sim1984/laravel-firebird](https://github.com/sim1984/laravel-firebird)
* [jacquestvanzuydam/laravel-firebird](https://github.com/jacquestvanzuydam/laravel-firebird)
* [KKSzymanowski/laravel-6-firebird](https://github.com/KKSzymanowski/laravel-6-firebird)
* [harrygulliford/laravel-firebird](https://github.com/harrygulliford/laravel-firebird)

## License
This library is a open-source software licensed under the MIT license.
35 changes: 35 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "ejetar/laravel-firebird",
"description": "Use Eloquent and QueryBuilder with a Firebird database \uD83D\uDD25",
"license": "MIT",
"authors": [
{
"name": "Guilherme A. Girardi",
"email": "guilhermeagirardi@gmail.com"
}
],
"require": {
"php": ">=7.1",
"illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0",
"illuminate/container": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0",
"illuminate/database": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0",
"illuminate/events": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0"
},
"require-dev": {
"mockery/mockery": "~1.0",
"phpunit/phpunit": "~7.0|^8.0|^9.0",
"orchestra/testbench": "~3.7|^4.0|^5.0|^6.0"
},
"autoload": {
"psr-4": {
"Ejetar\\LaravelFirebird\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Ejetar\\LaravelFirebird\\FirebirdServiceProvider"
]
}
}
}
203 changes: 203 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php

namespace Ejetar\LaravelFirebird;

use Ejetar\LaravelFirebird\Query\Builder as QueryBuilder;
use Ejetar\LaravelFirebird\Query\Grammars\Firebird15Grammar as QueryGrammar10;
use Ejetar\LaravelFirebird\Query\Grammars\Firebird25Grammar as QueryGrammar20;
use Ejetar\LaravelFirebird\Query\Grammars\Firebird30Grammar as QueryGrammar30;
use Ejetar\LaravelFirebird\Query\Processors\FirebirdProcessor as Processor;
use Ejetar\LaravelFirebird\Schema\Builder as SchemaBuilder;
use Ejetar\LaravelFirebird\Schema\Grammars\FirebirdGrammar as SchemaGrammar;
use PDO;

class Connection extends \Illuminate\Database\Connection
{

/**
* Firebird Engine version
*
* @var string
*/
private $engine_version = null;

/**
* Get engine version
*
* @return string
*/
protected function getEngineVersion()
{
if (!$this->engine_version) {
$this->engine_version = isset($this->config['engine_version']) ? $this->config['engine_version'] : null;
}
if (!$this->engine_version) {
$sql = "SELECT RDB\$GET_CONTEXT(?, ?) FROM RDB\$DATABASE";
$sth = $this->getPdo()->prepare($sql);
$sth->execute(['SYSTEM', 'ENGINE_VERSION']);
$this->engine_version = $sth->fetchColumn();
$sth->closeCursor();
}
return $this->engine_version;
}

/**
* Get major engine version
* It allows you to determine the features of the engine.
*
* @return int
*/
protected function getMajorEngineVersion()
{
$version = $this->getEngineVersion();
$parts = explode('.', $version);
return (int)$parts[0];
}

/**
* Get the default query grammar instance
*
* @return QueryGrammar10|QueryGrammar20|QueryGrammar30
*/
protected function getDefaultQueryGrammar()
{
switch ($this->getMajorEngineVersion()){
case 1:
return new QueryGrammar10;
break;
case 3:
return new QueryGrammar30;
break;
default:
return new QueryGrammar20;
break;
}
}

/**
* Get the default post processor instance.
*
* @return \Ejetar\LaravelFirebird\Query\Processors\FirebirdProcessor
*/
protected function getDefaultPostProcessor()
{
return new Processor;
}

/**
* Get a schema builder instance for this connection.
*
* @return \Ejetar\LaravelFirebird\Schema\Builder
*/
public function getSchemaBuilder()
{
if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}

return new SchemaBuilder($this);
}

/**
* Get the default schema grammar instance.
*
* @return \Illuminate\Database\Grammar
*/
protected function getDefaultSchemaGrammar()
{
return $this->withTablePrefix(new SchemaGrammar);
}

/**
* Get query builder
*
* @return \Ejetar\LaravelFirebird\Query\Builder
*/
protected function getQueryBuilder()
{
$processor = $this->getPostProcessor();
$grammar = $this->getQueryGrammar();

return new QueryBuilder($this, $grammar, $processor);
}

/**
* Get a new query builder instance.
*
* @return \Ejetar\LaravelFirebird\Query\Builder
*/
public function query()
{
return $this->getQueryBuilder();
}

/**
* Execute stored function
*
* @param string $function
* @param array $values
* @return mixed
*/
public function executeFunction($function, array $values = null)
{
$query = $this->getQueryBuilder();

return $query->executeFunction($function, $values);
}

/**
* Execute stored procedure
*
* @param string $procedure
* @param array $values
*/
public function executeProcedure($procedure, array $values = null)
{
$query = $this->getQueryBuilder();

$query->executeProcedure($procedure, $values);
}

/**
* Start a new database transaction.
*
* @return void
* @throws \Exception
*/
public function beginTransaction()
{
if ($this->transactions == 0 && $this->pdo->getAttribute(PDO::ATTR_AUTOCOMMIT) == 1) {
$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, 0);
}
parent::beginTransaction();
}

/**
* Commit the active database transaction.
*
* @return void
*/
public function commit()
{
parent::commit();
if ($this->transactions == 0 && $this->pdo->getAttribute(PDO::ATTR_AUTOCOMMIT) == 0) {
$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, 1);
}
}

/**
* Rollback the active database transaction.
*
* @param int|null $toLevel
* @return void
* @throws \Exception
*/
public function rollBack($toLevel = null)
{
parent::rollBack($toLevel);
if ($this->transactions == 0 && $this->pdo->getAttribute(PDO::ATTR_AUTOCOMMIT) == 0) {
$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, 1);
}
}

}
65 changes: 65 additions & 0 deletions src/Eloquent/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Ejetar\LaravelFirebird\Eloquent;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model as BaseModel;

class Model extends BaseModel
{

/**
* The sequence for the model.
*
* @var string
*/
protected $sequence = null;

/**
* Get sequence name
*
* @return string
*/
protected function getSequence()
{
$autoSequence = substr('seq_' . $this->getTable(), 0, 31);
return $this->sequence ? $this->sequence : $autoSequence;
}

/**
* Get next sequence value
*
* @param string $sequence
*
* @return int
*/
protected function nextSequenceValue($sequence = null)
{
$query = $this->getConnection()->getQueryBuilder();

$id = $query->nextSequenceValue($sequence ? $sequence : $this->getSequence());

return $id;
}

/**
* Insert the given attributes and set the ID on the model.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array $attributes
* @return void
*/
protected function insertAndSetId(Builder $query, $attributes)
{
$id = $this->nextSequenceValue();

$keyName = $this->getKeyName();

$attributes[$keyName] = $id;

$query->insert($attributes);

$this->setAttribute($keyName, $id);
}

}
Loading

0 comments on commit 72f1057

Please sign in to comment.