Skip to content

Commit 3e0acb4

Browse files
author
Maxime Rault
committed
first commit
0 parents  commit 3e0acb4

13 files changed

+1696
-0
lines changed

LICENSE.md

Whitespace-only changes.

README.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# laravel-db2
2+
3+
[![Latest Stable Version](https://poser.pugx.org/cooperl/laravel-db2/v/stable)](https://packagist.org/packages/cooperl/laravel-db2)
4+
[![Total Downloads](https://poser.pugx.org/cooperl/laravel-db2/downloads)](https://packagist.org/packages/cooperl/laravel-db2)
5+
[![Latest Unstable Version](https://poser.pugx.org/cooperl/laravel-db2/v/unstable)](https://packagist.org/packages/cooperl/laravel-db2)
6+
[![License](https://poser.pugx.org/cooperl/laravel-db2/license)](https://packagist.org/packages/cooperl/laravel-db2)
7+
8+
laravel-db2 is a simple DB2 service provider for Laravel.
9+
It provides DB2 Connection by extending the Illuminate Database component of the laravel framework.
10+
11+
---
12+
13+
- [Installation](#installation)
14+
- [Registering the Package](#registering-the-package)
15+
- [Configuration](#configuration)
16+
- [Usage](#usage)
17+
18+
## Installation
19+
20+
Add laravel-db2 to your composer.json file:
21+
22+
```
23+
"require": {
24+
"cooperl/laravel-db2": "~1.0"
25+
}
26+
```
27+
28+
Use [composer](http://getcomposer.org) to install this package.
29+
30+
```
31+
$ composer update
32+
```
33+
34+
### Registering the Package
35+
36+
Add the laravel-db2 Service Provider to your config in ``app/config/app.php``:
37+
38+
```php
39+
'providers' => [
40+
'Cooperl\Database\DB2\DB2ServiceProvider'
41+
],
42+
```
43+
44+
### Configuration
45+
46+
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).
47+
48+
#### Option 1: Configure DB2 using ``app/config/database.php`` file
49+
50+
Simply add this code at the end of your ``app/config/database.php`` file:
51+
52+
```php
53+
/*
54+
|--------------------------------------------------------------------------
55+
| DB2 Databases
56+
|--------------------------------------------------------------------------
57+
*/
58+
59+
'odbc' => [
60+
'driver' => 'odbc',
61+
'host' => '',
62+
'database' => '',
63+
'username' => '',
64+
'password' => '',
65+
'charset' => 'utf8',
66+
'ccsid' => 1208,
67+
'prefix' => '',
68+
'schema' => '',
69+
'i5_libl' => '',
70+
'i5_lib' => '',
71+
'i5_commit' => 0,
72+
'i5_naming' => 0,
73+
'i5_date_fmt' => 5,
74+
'i5_date_sep' => 0,
75+
'i5_decimal_sep' => 0,
76+
'i5_time_fmt' => 0,
77+
'i5_time_sep' => 0,
78+
'options' => [
79+
PDO::ATTR_CASE => PDO::CASE_LOWER,
80+
PDO::ATTR_EMULATE_PREPARES => false,
81+
PDO::ATTR_PERSISTENT => false
82+
]
83+
],
84+
85+
'ibm' => [
86+
'driver' => 'ibm',
87+
'host' => '',
88+
'database' => '',
89+
'username' => '',
90+
'password' => '',
91+
'charset' => 'utf8',
92+
'ccsid' => 1208,
93+
'prefix' => '',
94+
'schema' => '',
95+
'i5_libl' => '',
96+
'i5_lib' => '',
97+
'i5_commit' => 0,
98+
'i5_naming' => 0,
99+
'i5_date_fmt' => 5,
100+
'i5_date_sep' => 0,
101+
'i5_decimal_sep' => 0,
102+
'i5_time_fmt' => 0,
103+
'i5_time_sep' => 0,
104+
'options' => [
105+
PDO::ATTR_CASE => PDO::CASE_LOWER,
106+
PDO::ATTR_EMULATE_PREPARES => false,
107+
PDO::ATTR_PERSISTENT => false
108+
]
109+
],
110+
111+
```
112+
driver setting is either 'odbc' for ODBC connection or 'ibm' for pdo_ibm connection
113+
Then if driver is 'odbc', database must be set to ODBC connection name.
114+
if driver is 'ibm', database must be set to IBMi database name (WRKRDBDIRE).
115+
116+
#### Option 2: Configure DB2 using package config file
117+
118+
Run on the command line from the root of your project:
119+
120+
```
121+
$ php artisan config:publish cooperl/laravel-db2
122+
```
123+
124+
Set your laravel-db2 credentials in ``app/config/packages/cooperl/laravel-db2/config.php``
125+
the same way as above
126+
127+
128+
## Usage
129+
130+
Consult the [Laravel framework documentation](http://laravel.com/docs).

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "cooperl/laravel-db2",
3+
"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.",
4+
"keywords": ["laravel", "DB2", "Database", "PDO", "ODBC"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Maxime Rault",
9+
"role": "Developer"
10+
}
11+
],
12+
"require": {
13+
"php": ">=5.4.0",
14+
"illuminate/support": "~4.2",
15+
"illuminate/database": "~4.2"
16+
},
17+
"require-dev": {
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"Cooperl\\Database\\DB2\\": "src/"
22+
}
23+
},
24+
"extra": {
25+
"branch-alias": {
26+
"dev-master": "1.0-dev"
27+
}
28+
}
29+
}

src/Connectors/IBMConnector.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace Cooperl\Database\DB2\Connectors;
3+
4+
use Illuminate\Database\Connectors\Connector;
5+
use Illuminate\Database\Connectors\ConnectorInterface;
6+
7+
class IBMConnector extends Connector implements ConnectorInterface
8+
{
9+
10+
public function connect(array $config)
11+
{
12+
$dsn = $this->getDsn($config);
13+
14+
$options = $this->getOptions($config);
15+
16+
$connection = $this->createConnection($dsn, $config, $options);
17+
18+
if (isset($config['schema']))
19+
{
20+
$schema = $config['schema'];
21+
22+
$connection->prepare("set schema $schema")->execute();
23+
}
24+
25+
return $connection;
26+
}
27+
28+
protected function getDsn(array $config) {
29+
extract($config);
30+
$dsn = "ibm:$database";
31+
return $dsn;
32+
}
33+
34+
}

src/Connectors/ODBCConnector.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
namespace Cooperl\Database\DB2\Connectors;
3+
4+
use Illuminate\Database\Connectors\Connector;
5+
use Illuminate\Database\Connectors\ConnectorInterface;
6+
7+
class ODBCConnector extends Connector implements ConnectorInterface
8+
{
9+
10+
public function connect(array $config)
11+
{
12+
$dsn = $this->getDsn($config);
13+
14+
$options = $this->getOptions($config);
15+
16+
$connection = $this->createConnection($dsn, $config, $options);
17+
18+
if (isset($config['schema']))
19+
{
20+
$schema = $config['schema'];
21+
22+
$connection->prepare("set schema $schema")->execute();
23+
}
24+
25+
return $connection;
26+
}
27+
28+
protected function getDsn(array $config) {
29+
extract($config);
30+
31+
$dsn = "odbc:DRIVER={iSeries Access ODBC Driver};"
32+
. "SYSTEM=$database;"
33+
. "NAM=$i5_naming;"
34+
. "DATABASE=$i5_lib;"
35+
. "DBQ=$i5_libl;"
36+
. "DFT=$i5_date_fmt;"
37+
. "DSP=$i5_date_sep;"
38+
. "DEC=$i5_decimal_sep;"
39+
. "TFT=$i5_time_fmt;"
40+
. "TSP=$i5_time_sep;"
41+
. "CCSID=$ccsid";
42+
43+
return $dsn;
44+
}
45+
46+
}

src/DB2Connection.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
namespace Cooperl\Database\DB2;
3+
4+
use PDO;
5+
6+
use Illuminate\Database\Connection;
7+
8+
use Cooperl\Database\DB2\Schema\Builder;
9+
use Cooperl\Database\DB2\Query\Processors\DB2Processor;
10+
use Cooperl\Database\DB2\Query\Grammars\DB2Grammar as QueryGrammar;
11+
use Cooperl\Database\DB2\Schema\Grammars\DB2Grammar as SchemaGrammar;
12+
13+
class DB2Connection extends Connection
14+
{
15+
16+
/**
17+
* The name of the default schema.
18+
*
19+
* @var string
20+
*/
21+
protected $defaultSchema;
22+
23+
public function __construct(PDO $pdo, $database = '', $tablePrefix = '', array $config = [])
24+
{
25+
parent::__construct($pdo, $database, $tablePrefix, $config);
26+
$this->currentSchema = $this->defaultSchema = strtoupper($config['schema']);
27+
}
28+
29+
/**
30+
* Get the name of the default schema.
31+
*
32+
* @return string
33+
*/
34+
public function getDefaultSchema()
35+
{
36+
return $this->defaultSchema;
37+
}
38+
39+
/**
40+
* Reset to default the current schema.
41+
*
42+
* @return string
43+
*/
44+
public function resetCurrentSchema()
45+
{
46+
$this->setCurrentSchema($this->getDefaultSchema());
47+
}
48+
49+
/**
50+
* Set the name of the current schema.
51+
*
52+
* @return string
53+
*/
54+
public function setCurrentSchema($schema)
55+
{
56+
//$this->currentSchema = $schema;
57+
$this->statement('SET SCHEMA ?', [strtoupper($schema)]);
58+
}
59+
60+
/**
61+
* Get a schema builder instance for the connection.
62+
*
63+
* @return \Illuminate\Database\Schema\MySqlBuilder
64+
*/
65+
public function getSchemaBuilder()
66+
{
67+
if (is_null($this->schemaGrammar)) { $this->useDefaultSchemaGrammar(); }
68+
69+
return new Builder($this);
70+
}
71+
72+
/**
73+
* @return Query\Grammars\Grammar
74+
*/
75+
protected function getDefaultQueryGrammar()
76+
{
77+
return $this->withTablePrefix(new QueryGrammar);
78+
}
79+
80+
/**
81+
* Default grammar for specified Schema
82+
* @return Schema\Grammars\Grammar
83+
*/
84+
protected function getDefaultSchemaGrammar()
85+
{
86+
87+
return $this->withTablePrefix(new SchemaGrammar);
88+
}
89+
90+
/**
91+
* Get the default post processor instance.
92+
*
93+
* @return \Illuminate\Database\Query\Processors\PostgresProcessor
94+
*/
95+
protected function getDefaultPostProcessor()
96+
{
97+
return new DB2Processor;
98+
}
99+
100+
}

0 commit comments

Comments
 (0)