Completed
Push — master ( 612476...c205d6 )
by smiley
04:22
created

PDOFirebirdDriver::connect()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 7
nop 0
1
<?php
2
/**
3
 * Class PDOFirebirdDriver
4
 *
5
 * @filesource   PDOFirebirdDriver.php
6
 * @created      04.11.2015
7
 * @package      chillerlan\Database\Drivers\PDO
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2015 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database\Drivers\PDO;
14
15
use chillerlan\Database\DBException;
16
use chillerlan\Database\Drivers\DBDriverInterface;
17
use PDO;
18
19
/**
20
 * @todo: STRTOLOWER COLUMN NAMES
21
 */
22
class PDOFirebirdDriver extends PDODriverAbstract{
23
24
	/**
25
	 * The PDO drivername which is being used in the DSN
26
	 *
27
	 * @var string
28
	 */
29
	protected $drivername = 'firebird';
30
31
	/**
32
	 * Returns a DSN string using the given options
33
	 * @see http://php.net/manual/en/ref.pdo-firebird.connection.php
34
	 *
35
	 * @return string DSN
36
	 */
37
	protected function getDSN():string {
38
		$dsn = $this->drivername.':dbname=';
39
40
		if(!empty($this->options->host)){
41
			$dsn .= $this->options->host;
42
43
			if(is_numeric($this->options->port)){
44
				$dsn .= '/'.$this->options->port;
45
			}
46
47
			$dsn .= ':';
48
		}
49
50
		$dsn .= $this->options->database;
51
52
		return $dsn;
53
	}
54
55
	/**
56
	 * Establishes a database connection and returns the connection object
57
	 *
58
	 * @return \chillerlan\Database\Drivers\DBDriverInterface
59
	 * @throws \chillerlan\Database\DBException
60
	 */
61
	public function connect():DBDriverInterface{
62
63
		if($this->db instanceof PDO){
64
			return $this;
65
		}
66
67
		try{
68
69
			if(!is_file($this->options->database)){
70
				trigger_error('file not found');
71
			}
72
73
			$this->db = new PDO($this->getDSN(), $this->options->username, $this->options->password, $this->pdo_options);
74
75
			return $this;
76
		}
77
		catch(\Exception $e){
78
			throw new DBException('db error: [PDOFirebirdDriver]: '.$e->getMessage()); // @codeCoverageIgnore
79
		}
80
	}
81
82
	/**
83
	 * Returns the last insert id (if present)
84
	 *
85
	 * Firebird -> SQLSTATE[IM001]: driver does not support lastInsertId()
86
	 *
87
	 * @link http://php.net/manual/pdo.lastinsertid.php
88
	 * @return string
89
	 * @codeCoverageIgnore
90
	 */
91
	protected function insertID():string {
92
		return '';
93
	}
94
95
}
96