1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Oci8\Connectors; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Connectors; |
6
|
|
|
use Illuminate\Database\Connectors\Connector; |
7
|
|
|
use Illuminate\Database\Connectors\ConnectorInterface; |
8
|
|
|
use PDO; |
9
|
|
|
use Yajra\Pdo\Oci8; |
10
|
|
|
|
11
|
|
|
class OracleConnector extends Connector implements ConnectorInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The default PDO connection options. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $options = [ |
19
|
|
|
PDO::ATTR_CASE => PDO::CASE_LOWER, |
20
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
21
|
|
|
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Establish a database connection. |
26
|
|
|
* |
27
|
|
|
* @param array $config |
28
|
|
|
* @return PDO |
29
|
|
|
*/ |
30
|
|
|
public function connect(array $config) |
31
|
|
|
{ |
32
|
|
|
$tns = ! empty($config['tns']) ? $config['tns'] : $this->getDsn($config); |
33
|
|
|
|
34
|
|
|
$options = $this->getOptions($config); |
35
|
|
|
|
36
|
|
|
$connection = $this->createConnection($tns, $config, $options); |
37
|
|
|
|
38
|
|
|
return $connection; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create a DSN string from a configuration. |
43
|
|
|
* |
44
|
|
|
* @param array $config |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
protected function getDsn(array $config) |
48
|
|
|
{ |
49
|
|
|
if (! empty($config['tns'])) { |
50
|
|
|
return $config['tns']; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// parse configuration |
54
|
|
|
$config = $this->parseConfig($config); |
55
|
|
|
|
56
|
|
|
// check multiple connections/host, comma delimiter |
57
|
|
|
$config = $this->checkMultipleHostDsn($config); |
58
|
|
|
|
59
|
|
|
// return generated tns |
60
|
|
|
return $config['tns']; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $config |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
private function parseConfig(array $config) |
68
|
|
|
{ |
69
|
|
|
$config = $this->setHost($config); |
70
|
|
|
$config = $this->setPort($config); |
71
|
|
|
$config = $this->setProtocol($config); |
72
|
|
|
$config = $this->setServiceId($config); |
73
|
|
|
$config = $this->setTNS($config); |
74
|
|
|
$config = $this->setCharset($config); |
75
|
|
|
|
76
|
|
|
return $config; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $config |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
private function setHost(array $config) |
84
|
|
|
{ |
85
|
|
|
$config['host'] = isset($config['host']) ? $config['host'] : $config['hostname']; |
86
|
|
|
|
87
|
|
|
return $config; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $config |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
private function setPort(array $config) |
95
|
|
|
{ |
96
|
|
|
$config['port'] = isset($config['port']) ? $config['port'] : '1521'; |
97
|
|
|
|
98
|
|
|
return $config; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array $config |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
private function setProtocol(array $config) |
106
|
|
|
{ |
107
|
|
|
$config['protocol'] = isset($config['protocol']) ? $config['protocol'] : 'TCP'; |
108
|
|
|
|
109
|
|
|
return $config; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $config |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
private function setServiceId(array $config) |
117
|
|
|
{ |
118
|
|
|
$config['service'] = empty($config['service_name']) |
119
|
|
|
? $service_param = 'SID = ' . $config['database'] |
120
|
|
|
: $service_param = 'SERVICE_NAME = ' . $config['service_name']; |
121
|
|
|
|
122
|
|
|
return $config; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param array $config |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
private function setTNS(array $config) |
130
|
|
|
{ |
131
|
|
|
$config['tns'] = "(DESCRIPTION = (ADDRESS = (PROTOCOL = {$config['protocol']})(HOST = {$config['host']})(PORT = {$config['port']})) (CONNECT_DATA =({$config['service']})))"; |
132
|
|
|
|
133
|
|
|
return $config; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array $config |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
private function setCharset(array $config) |
141
|
|
|
{ |
142
|
|
|
if (! isset($config['charset'])) { |
143
|
|
|
$config['charset'] = 'AL32UTF8'; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $config; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param array $config |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
private function checkMultipleHostDsn(array $config) |
154
|
|
|
{ |
155
|
|
|
$host = is_array($config['host']) ? $config['host'] : explode(',', $config['host']); |
156
|
|
|
|
157
|
|
|
$count = count($host); |
158
|
|
|
if ($count > 1) { |
159
|
|
|
$address = ""; |
160
|
|
|
for ($i = 0; $i < $count; $i++) { |
161
|
|
|
$address .= '(ADDRESS = (PROTOCOL = ' . $config["protocol"] . ')(HOST = ' . trim($host[$i]) . ')(PORT = ' . $config['port'] . '))'; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// create a tns with multiple address connection |
165
|
|
|
$config['tns'] = "(DESCRIPTION = {$address} (LOAD_BALANCE = yes) (FAILOVER = on) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = {$config['database']})))"; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $config; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Create a new PDO connection. |
173
|
|
|
* |
174
|
|
|
* @param string $tns |
175
|
|
|
* @param array $config |
176
|
|
|
* @param array $options |
177
|
|
|
* @return PDO |
178
|
|
|
*/ |
179
|
|
|
public function createConnection($tns, array $config, array $options) |
180
|
|
|
{ |
181
|
|
|
// add fallback in case driver is not set, will use pdo instead |
182
|
|
|
if (! in_array($config['driver'], ['oci8', 'pdo-via-oci8', 'oracle'])) { |
183
|
|
|
return parent::createConnection($tns, $config, $options); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$config = $this->setCharset($config); |
187
|
|
|
$options['charset'] = $config['charset']; |
188
|
|
|
|
189
|
|
|
return new Oci8($tns, $config['username'], $config['password'], $options); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|