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
|
|
|
const DSN_MYSQL = 'mysql:host=%s;port=%d;dbname=%s'; |
14
|
|
|
const DSN_SQLITE = 'sqlite:%s'; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
private $user; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $password; |
21
|
|
|
|
22
|
|
|
/** @var PDO */ |
23
|
|
|
private $currentConnection; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $provisioningTable; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private $criteriaColumn; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
private $dsn; |
33
|
|
|
|
34
|
|
|
/** @var ConnectionFactory */ |
35
|
|
|
private $connectionFactory; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param ConnectionFactory $connectionFactory |
41
|
|
|
*/ |
42
|
3 |
|
public function __construct(ConnectionFactory $connectionFactory) |
43
|
|
|
{ |
44
|
3 |
|
$this->connectionFactory = $connectionFactory; |
45
|
3 |
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $databaseName |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
1 |
|
public function useSqlite($databaseName = ':memory:') |
54
|
|
|
{ |
55
|
1 |
|
$this->dsn = sprintf(self::DSN_SQLITE, $databaseName); |
56
|
|
|
|
57
|
1 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $host |
64
|
|
|
* @param integer $port |
65
|
|
|
* @param string $databaseName |
66
|
|
|
* @param string $databaseUser |
67
|
|
|
* @param string $databasePassword |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
1 |
|
public function useMysql($host, $port, $databaseName, $databaseUser, $databasePassword) |
71
|
|
|
{ |
72
|
1 |
|
$this->dsn = sprintf(self::DSN_MYSQL, $host, $port, $databaseName); |
73
|
1 |
|
$this->user = $databaseUser; |
74
|
1 |
|
$this->password = $databasePassword; |
75
|
|
|
|
76
|
1 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return PDO |
81
|
|
|
*/ |
82
|
2 |
|
public function getCurrentConnection() |
83
|
|
|
{ |
84
|
2 |
|
if (null === $this->currentConnection) { |
85
|
2 |
|
$this->currentConnection = $this->connectionFactory->build($this->dsn, $this->user, $this->password); |
86
|
2 |
|
} |
87
|
|
|
|
88
|
2 |
|
return $this->currentConnection; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $provisioningTable |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
2 |
|
public function setProvisioningTable($provisioningTable) |
98
|
|
|
{ |
99
|
2 |
|
$this->provisioningTable = $provisioningTable; |
100
|
|
|
|
101
|
2 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $criteriaColumn |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
2 |
|
public function setCriteriaColumn($criteriaColumn) |
111
|
|
|
{ |
112
|
2 |
|
$this->criteriaColumn = $criteriaColumn; |
113
|
|
|
|
114
|
2 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function getProvisioningTable() |
123
|
|
|
{ |
124
|
|
|
return $this->provisioningTable; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getCriteriaColumn() |
133
|
|
|
{ |
134
|
|
|
return $this->criteriaColumn; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|