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

PDOSQLiteDriver::getServerInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @filesource   PDOSQLiteDriver.php
4
 * @created      05.01.2016
5
 * @package      chillerlan\Database\Drivers\PDO
6
 * @author       Smiley <[email protected]>
7
 * @copyright    2016 Smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\Database\Drivers\PDO;
12
13
use chillerlan\Database\DBException;
14
use chillerlan\Database\Drivers\DBDriverInterface;
15
use PDO;
16
17
/**
18
 * Class PDOSQLiteDriver
19
 */
20
class PDOSQLiteDriver extends PDODriverAbstract{
21
22
	/**
23
	 * The PDO drivername which is being used in the DSN
24
	 *
25
	 * @var string
26
	 */
27
	protected $drivername = 'sqlite';
28
29
	/**
30
	 * Returns a DSN string using the given options
31
	 * @see http://php.net/manual/ref.pdo-sqlite.connection.php
32
	 *
33
	 * @return string DSN
34
	 */
35
	protected function getDSN():string {
36
		return $this->drivername.':'.$this->options->database;
37
	}
38
39
	/**
40
	 * Returns info about the database server
41
	 *
42
	 * @return string database's serverinfo string
43
	 */
44
	public function getServerInfo():string {
45
		return $this->drivername.' (PDO::ATTR_SERVER_INFO not available)';
46
	}
47
48
	/**
49
	 * Establishes a database connection and returns the connection object
50
	 *
51
	 * @return \chillerlan\Database\Drivers\DBDriverInterface
52
	 * @throws \chillerlan\Database\DBException
53
	 */
54
	public function connect():DBDriverInterface{
55
56
		if($this->db instanceof PDO){
57
			return $this;
58
		}
59
60
		try{
61
62
			if($this->options->database !== ':memory:' && !is_file($this->options->database)){
63
				trigger_error('file not found');
64
			}
65
66
			if($this->options->database === ':memory:'){
67
				$this->pdo_options += [PDO::ATTR_PERSISTENT => true];
68
			}
69
70
			$this->db = new PDO($this->getDSN(), null, null, $this->pdo_options);
71
72
			return $this;
73
		}
74
		catch(\Exception $e){
75
			throw new DBException('db error: [PDOSQLiteDriver]: '.$e->getMessage());
76
		}
77
	}
78
79
80
}
81