1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tworzenieweb\SqlProvisioner\Database; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Luke Adamczewski |
9
|
|
|
* @package Tworzenieweb\SqlProvisioner\Database |
10
|
|
|
*/ |
11
|
|
|
class Connection |
12
|
|
|
{ |
13
|
|
|
/** @var string */ |
14
|
|
|
private $host; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
private $port; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $user; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
private $password; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $databaseName; |
27
|
|
|
|
28
|
|
|
/** @var PDO */ |
29
|
|
|
private $currentConnection; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
private $provisioningTable; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
private $criteriaColumn; |
36
|
|
|
|
37
|
|
|
private $dsn; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $host |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
|
|
public function setHost($host) |
45
|
|
|
{ |
46
|
|
|
$this->host = $host; |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $port |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function setPort($port) |
58
|
|
|
{ |
59
|
|
|
$this->port = $port; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $user |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function setUser($user) |
71
|
|
|
{ |
72
|
|
|
$this->user = $user; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $password |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function setPassword($password) |
84
|
|
|
{ |
85
|
|
|
$this->password = $password; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $databaseName |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function setDatabaseName($databaseName) |
97
|
|
|
{ |
98
|
|
|
$this->databaseName = $databaseName; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function useSqlite($useMemoryStorage = false) |
104
|
|
|
{ |
105
|
|
|
$this->dsn = $useMemoryStorage ? 'sqlite::memory:' : 'sqlite:%s'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function useMysql() |
109
|
|
|
{ |
110
|
|
|
$this->dsn = 'mysql:host=%s;port=%d;dbname=%s'; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return PDO |
115
|
|
|
*/ |
116
|
|
|
public function getCurrentConnection() |
117
|
|
|
{ |
118
|
|
|
if (null === $this->currentConnection) { |
119
|
|
|
$this->currentConnection = new PDO( |
120
|
|
|
sprintf($this->dsn, $this->host, $this->port, $this->databaseName), $this->user, $this->password |
121
|
|
|
); |
122
|
|
|
$this->currentConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
123
|
|
|
$this->currentConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); |
124
|
|
|
$this->currentConnection->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, true); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->currentConnection; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $provisioningTable |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function setProvisioningTable($provisioningTable) |
137
|
|
|
{ |
138
|
|
|
$this->provisioningTable = $provisioningTable; |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $criteriaColumn |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
|
|
public function setCriteriaColumn($criteriaColumn) |
150
|
|
|
{ |
151
|
|
|
$this->criteriaColumn = $criteriaColumn; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function getProvisioningTable() |
162
|
|
|
{ |
163
|
|
|
return $this->provisioningTable; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function getCriteriaColumn() |
172
|
|
|
{ |
173
|
|
|
return $this->criteriaColumn; |
174
|
|
|
} |
175
|
|
|
} |