Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Rault committed Apr 22, 2015
0 parents commit 3e0acb4
Show file tree
Hide file tree
Showing 13 changed files with 1,696 additions and 0 deletions.
Empty file added LICENSE.md
Empty file.
130 changes: 130 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# laravel-db2

[![Latest Stable Version](https://poser.pugx.org/cooperl/laravel-db2/v/stable)](https://packagist.org/packages/cooperl/laravel-db2)
[![Total Downloads](https://poser.pugx.org/cooperl/laravel-db2/downloads)](https://packagist.org/packages/cooperl/laravel-db2)
[![Latest Unstable Version](https://poser.pugx.org/cooperl/laravel-db2/v/unstable)](https://packagist.org/packages/cooperl/laravel-db2)
[![License](https://poser.pugx.org/cooperl/laravel-db2/license)](https://packagist.org/packages/cooperl/laravel-db2)

laravel-db2 is a simple DB2 service provider for Laravel.
It provides DB2 Connection by extending the Illuminate Database component of the laravel framework.

---

- [Installation](#installation)
- [Registering the Package](#registering-the-package)
- [Configuration](#configuration)
- [Usage](#usage)

## Installation

Add laravel-db2 to your composer.json file:

```
"require": {
"cooperl/laravel-db2": "~1.0"
}
```

Use [composer](http://getcomposer.org) to install this package.

```
$ composer update
```

### Registering the Package

Add the laravel-db2 Service Provider to your config in ``app/config/app.php``:

```php
'providers' => [
'Cooperl\Database\DB2\DB2ServiceProvider'
],
```

### Configuration

There are two ways to configure laravel-db2. You can choose the most convenient way for you. You can put your DB2 credentials into ``app/config/database.php`` (option 1) file or use package config file which you can be generated through command line by artisan (option 2).

#### Option 1: Configure DB2 using ``app/config/database.php`` file

Simply add this code at the end of your ``app/config/database.php`` file:

```php
/*
|--------------------------------------------------------------------------
| DB2 Databases
|--------------------------------------------------------------------------
*/

'odbc' => [
'driver' => 'odbc',
'host' => '',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'ccsid' => 1208,
'prefix' => '',
'schema' => '',
'i5_libl' => '',
'i5_lib' => '',
'i5_commit' => 0,
'i5_naming' => 0,
'i5_date_fmt' => 5,
'i5_date_sep' => 0,
'i5_decimal_sep' => 0,
'i5_time_fmt' => 0,
'i5_time_sep' => 0,
'options' => [
PDO::ATTR_CASE => PDO::CASE_LOWER,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_PERSISTENT => false
]
],

'ibm' => [
'driver' => 'ibm',
'host' => '',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'ccsid' => 1208,
'prefix' => '',
'schema' => '',
'i5_libl' => '',
'i5_lib' => '',
'i5_commit' => 0,
'i5_naming' => 0,
'i5_date_fmt' => 5,
'i5_date_sep' => 0,
'i5_decimal_sep' => 0,
'i5_time_fmt' => 0,
'i5_time_sep' => 0,
'options' => [
PDO::ATTR_CASE => PDO::CASE_LOWER,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_PERSISTENT => false
]
],

```
driver setting is either 'odbc' for ODBC connection or 'ibm' for pdo_ibm connection
Then if driver is 'odbc', database must be set to ODBC connection name.
if driver is 'ibm', database must be set to IBMi database name (WRKRDBDIRE).

#### Option 2: Configure DB2 using package config file

Run on the command line from the root of your project:

```
$ php artisan config:publish cooperl/laravel-db2
```

Set your laravel-db2 credentials in ``app/config/packages/cooperl/laravel-db2/config.php``
the same way as above


## Usage

Consult the [Laravel framework documentation](http://laravel.com/docs).
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "cooperl/laravel-db2",
"description": "laravel-db2 is a simple DB2 service provider for Laravel. It provides DB2 Connection by extending the Illuminate Database component of the laravel framework.",
"keywords": ["laravel", "DB2", "Database", "PDO", "ODBC"],
"license": "MIT",
"authors": [
{
"name": "Maxime Rault",
"role": "Developer"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "~4.2",
"illuminate/database": "~4.2"
},
"require-dev": {
},
"autoload": {
"psr-4": {
"Cooperl\\Database\\DB2\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}
34 changes: 34 additions & 0 deletions src/Connectors/IBMConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace Cooperl\Database\DB2\Connectors;

use Illuminate\Database\Connectors\Connector;
use Illuminate\Database\Connectors\ConnectorInterface;

class IBMConnector extends Connector implements ConnectorInterface
{

public function connect(array $config)
{
$dsn = $this->getDsn($config);

$options = $this->getOptions($config);

$connection = $this->createConnection($dsn, $config, $options);

if (isset($config['schema']))
{
$schema = $config['schema'];

$connection->prepare("set schema $schema")->execute();
}

return $connection;
}

protected function getDsn(array $config) {
extract($config);
$dsn = "ibm:$database";
return $dsn;
}

}
46 changes: 46 additions & 0 deletions src/Connectors/ODBCConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace Cooperl\Database\DB2\Connectors;

use Illuminate\Database\Connectors\Connector;
use Illuminate\Database\Connectors\ConnectorInterface;

class ODBCConnector extends Connector implements ConnectorInterface
{

public function connect(array $config)
{
$dsn = $this->getDsn($config);

$options = $this->getOptions($config);

$connection = $this->createConnection($dsn, $config, $options);

if (isset($config['schema']))
{
$schema = $config['schema'];

$connection->prepare("set schema $schema")->execute();
}

return $connection;
}

protected function getDsn(array $config) {
extract($config);

$dsn = "odbc:DRIVER={iSeries Access ODBC Driver};"
. "SYSTEM=$database;"
. "NAM=$i5_naming;"
. "DATABASE=$i5_lib;"
. "DBQ=$i5_libl;"
. "DFT=$i5_date_fmt;"
. "DSP=$i5_date_sep;"
. "DEC=$i5_decimal_sep;"
. "TFT=$i5_time_fmt;"
. "TSP=$i5_time_sep;"
. "CCSID=$ccsid";

return $dsn;
}

}
100 changes: 100 additions & 0 deletions src/DB2Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
namespace Cooperl\Database\DB2;

use PDO;

use Illuminate\Database\Connection;

use Cooperl\Database\DB2\Schema\Builder;
use Cooperl\Database\DB2\Query\Processors\DB2Processor;
use Cooperl\Database\DB2\Query\Grammars\DB2Grammar as QueryGrammar;
use Cooperl\Database\DB2\Schema\Grammars\DB2Grammar as SchemaGrammar;

class DB2Connection extends Connection
{

/**
* The name of the default schema.
*
* @var string
*/
protected $defaultSchema;

public function __construct(PDO $pdo, $database = '', $tablePrefix = '', array $config = [])
{
parent::__construct($pdo, $database, $tablePrefix, $config);
$this->currentSchema = $this->defaultSchema = strtoupper($config['schema']);
}

/**
* Get the name of the default schema.
*
* @return string
*/
public function getDefaultSchema()
{
return $this->defaultSchema;
}

/**
* Reset to default the current schema.
*
* @return string
*/
public function resetCurrentSchema()
{
$this->setCurrentSchema($this->getDefaultSchema());
}

/**
* Set the name of the current schema.
*
* @return string
*/
public function setCurrentSchema($schema)
{
//$this->currentSchema = $schema;
$this->statement('SET SCHEMA ?', [strtoupper($schema)]);
}

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

return new Builder($this);
}

/**
* @return Query\Grammars\Grammar
*/
protected function getDefaultQueryGrammar()
{
return $this->withTablePrefix(new QueryGrammar);
}

/**
* Default grammar for specified Schema
* @return Schema\Grammars\Grammar
*/
protected function getDefaultSchemaGrammar()
{

return $this->withTablePrefix(new SchemaGrammar);
}

/**
* Get the default post processor instance.
*
* @return \Illuminate\Database\Query\Processors\PostgresProcessor
*/
protected function getDefaultPostProcessor()
{
return new DB2Processor;
}

}
Loading

0 comments on commit 3e0acb4

Please sign in to comment.