Issues (68)

src/Drivers/SQLitePDO.php (1 issue)

Severity
1
<?php
2
/**
3
 * Class SQLitePDO
4
 *
5
 * @filesource   SQLiteDriver.php
6
 * @created      28.06.2017
7
 * @package      chillerlan\Database\Drivers
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2018 Smiley
10
 * @license      MIT
11
 *
12
 * @noinspection PhpComposerExtensionStubsInspection
13
 */
14
15
namespace chillerlan\Database\Drivers;
16
17
use chillerlan\Database\Dialects\SQLite;
18
use chillerlan\Database\Result;
19
use Exception, PDO;
20
21
/**
22
 * @property \PDO $db
23
 */
24
class SQLitePDO extends PDODriverAbstract{
25
26
	protected string $drivername = 'sqlite';
27
	protected string $dialect    = SQLite::class;
28
29
	/**
30
	 * @inheritdoc
31
	 *
32
	 * @link http://php.net/manual/ref.pdo-sqlite.connection.php
33
	 */
34
	protected function getDSN():string {
35
		return $this->drivername.':'.$this->options->database;
36
	}
37
38
	/** @inheritdoc */
39
	public function getServerInfo():?string {
40
		return $this->drivername.', connected to: '.$this->options->database.' (PDO::ATTR_SERVER_INFO not available)';
41
	}
42
43
	/** @inheritdoc */
44
	public function connect():DriverInterface{
45
46
		if($this->db instanceof PDO){
0 ignored issues
show
$this->db is always a sub-type of PDO.
Loading history...
47
			return $this;
48
		}
49
50
		$db = $this->options->database;
51
52
		try{
53
54
			if($db !== ':memory:' && !is_file($db)){
55
				trigger_error('file not found');
56
			}
57
58
			if($db === ':memory:'){
59
				$this->pdo_options += [PDO::ATTR_PERSISTENT => true];
60
			}
61
62
			$this->db = new PDO($this->getDSN(), null, null, $this->pdo_options);
63
64
			return $this;
65
		}
66
		catch(Exception $e){
67
			throw new DriverException('db error: [SQLitePDO]: '.$e->getMessage());
68
		}
69
	}
70
71
/*
72
	protected function getResult(callable $callable, array $args, string $index = null, bool $assoc = null){
73
		$out = new Result(null, $this->convert_encoding_src, $this->convert_encoding_dest);
74
		$i   = 0;
75
76
		while($row = call_user_func_array($callable, $args)){
77
			$key = $assoc && !empty($index) ? $row[$index] : $i;
78
79
			foreach($row as $k => $v){
80
				switch(true){
81
					case is_numeric($v): $row[$k] = $v + 0; break;
82
				}
83
			}
84
85
			$out[$key] = $row;
86
			$i++;
87
		}
88
89
		return $i === 0 ? true : $out;
90
	}
91
*/
92
93
}
94