|
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
|
|
View Code Duplication |
class SQlServer 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
|
|
|
$server = $this->dsn[0]; |
|
66
|
|
|
$database = $this->dsn[1]; |
|
67
|
|
|
|
|
68
|
|
|
$this->loginData['username'] = $username; |
|
69
|
|
|
$this->loginData['password'] = $password; |
|
70
|
|
|
|
|
71
|
|
|
try |
|
72
|
|
|
{ |
|
73
|
|
|
$this->connectionObject = new \PDO("sqlsrv:Server=$server;Database=$database;ConnectionPooling=0", $username, $password); |
|
74
|
|
|
} |
|
75
|
|
|
catch (\PDOException $e) |
|
76
|
|
|
{ |
|
77
|
|
|
throw new SQLException("Unable to connect to database", 400, $e); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$dsn = $this->dsn; |
|
81
|
|
|
unset($dsn[0], $dsn[1]); |
|
82
|
|
|
|
|
83
|
|
|
foreach ($dsn as $attribute) |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
foreach ($attribute as $key=>$value) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->connectionObject->setAttribute($key, $value); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Returns an instance a pdo intance of the connection object |
|
94
|
|
|
* |
|
95
|
|
|
* @return \PDO |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getConnection() : \PDO |
|
98
|
|
|
{ |
|
99
|
|
|
if ($this->connectionObject instanceof \PDO) |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->connectionObject; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$this->connect( |
|
105
|
|
|
$this->loginData['username'] ?? '', |
|
106
|
|
|
$this->loginData['password'] ?? '' |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
return $this->connectionObject; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Closes connection |
|
114
|
|
|
*/ |
|
115
|
|
|
public function disableConnection() |
|
116
|
|
|
{ |
|
117
|
|
|
$this->connectionObject = null; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Sets attributes for the PDO object |
|
122
|
|
|
* |
|
123
|
|
|
* @param \PDO $attribute |
|
124
|
|
|
* @param \PDO $value |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setAttribute(\PDO $attribute, \PDO $value) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->connectionObject->setAttribute($attribute, $value); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.