|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* @license MIT |
|
4
|
|
|
* @author Samuel Adeshina <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of the EmmetBlue project, please read the license document |
|
7
|
|
|
* available in the root level of the project |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace EmmetBlue\Core\Connection\Adapters; |
|
10
|
|
|
|
|
11
|
|
|
use EmmetBlue\Core\Connection\ConnectableInterface; |
|
12
|
|
|
use EmmetBlue\Core\Exception\SQLException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* class ConnectionAdapter. |
|
16
|
|
|
* Instantiates a new instance of ConnectableInterface |
|
17
|
|
|
* |
|
18
|
|
|
* @author Samuel Adeshina <[email protected]> |
|
19
|
|
|
* |
|
20
|
|
|
* @since v0.0.1 08/06/2016 14:20 |
|
21
|
|
|
*/ |
|
22
|
|
|
class SQLite implements ConnectableInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string $dsn |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $dsn; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var \PDO $connectionObject |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $connectionObject; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array loginData |
|
36
|
|
|
*/ |
|
37
|
|
|
private $loginData = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Setter for the DSN string {@see $dsn} |
|
41
|
|
|
* |
|
42
|
|
|
* MSSQl Server DSN Structure: sqlsrv:Server={srv};Database={db}" |
|
43
|
|
|
* `$dsnArray[0] = {srv}` |
|
44
|
|
|
* `$dsnArray[1] = {db}` |
|
45
|
|
|
* Attributes = [2 - infinity] |
|
46
|
|
|
* |
|
47
|
|
|
* @param array $dsnArray |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
public function setDsn(array $dsnArray) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->dsn = $dsnArray; |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Establishes connection |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $username |
|
59
|
|
|
* @param string $password optional |
|
60
|
|
|
* @throws \EmmetBlue\Core\Exception\SQLException |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
public function connect(string $username="", string $password="") |
|
64
|
|
|
{ |
|
65
|
|
|
$dbLocation = $this->dsn[0]; |
|
66
|
|
|
|
|
67
|
|
|
try |
|
68
|
|
|
{ |
|
69
|
|
|
$this->connectionObject = new \PDO("sqlite:$dbLocation"); |
|
70
|
|
|
} |
|
71
|
|
|
catch (\PDOException $e) |
|
72
|
|
|
{ |
|
73
|
|
|
throw new SQLException("Unable to connect to database", 400, $e); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$dsn = $this->dsn; |
|
77
|
|
|
unset($dsn[0]); |
|
78
|
|
|
|
|
79
|
|
|
foreach ($dsn as $attribute) |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
foreach ($attribute as $key=>$value) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->connectionObject->setAttribute($key, $value); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns an instance a pdo intance of the connection object |
|
90
|
|
|
* |
|
91
|
|
|
* @return \PDO |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getConnection() : \PDO |
|
94
|
|
|
{ |
|
95
|
|
|
if ($this->connectionObject instanceof \PDO) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->connectionObject; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->connect( |
|
101
|
|
|
$this->loginData['username'] ?? '', |
|
102
|
|
|
$this->loginData['password'] ?? '' |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
return $this->connectionObject; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Closes connection |
|
110
|
|
|
*/ |
|
111
|
|
|
public function disableConnection() |
|
112
|
|
|
{ |
|
113
|
|
|
$this->connectionObject = null; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Sets attributes for the PDO object |
|
118
|
|
|
* |
|
119
|
|
|
* @param \PDO $attribute |
|
120
|
|
|
* @param \PDO $value |
|
121
|
|
|
*/ |
|
122
|
|
|
public function setAttribute(\PDO $attribute, \PDO $value) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->connectionObject->setAttribute($attribute, $value); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..