1 | <?php |
||
10 | class OracleConnector extends Connector implements ConnectorInterface |
||
11 | { |
||
12 | /** |
||
13 | * The default PDO connection options. |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $options = [ |
||
18 | PDO::ATTR_CASE => PDO::CASE_LOWER, |
||
19 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
20 | PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, |
||
21 | ]; |
||
22 | |||
23 | /** |
||
24 | * Establish a database connection. |
||
25 | * |
||
26 | * @param array $config |
||
27 | * @return PDO |
||
28 | */ |
||
29 | 42 | public function connect(array $config) |
|
39 | |||
40 | /** |
||
41 | * Create a DSN string from a configuration. |
||
42 | * |
||
43 | * @param array $config |
||
44 | * @return string |
||
45 | */ |
||
46 | 24 | protected function getDsn(array $config) |
|
61 | |||
62 | /** |
||
63 | * Parse configurations. |
||
64 | * |
||
65 | * @param array $config |
||
66 | * @return array |
||
67 | */ |
||
68 | 24 | protected function parseConfig(array $config) |
|
69 | { |
||
70 | 24 | $config = $this->setHost($config); |
|
71 | 24 | $config = $this->setPort($config); |
|
72 | 24 | $config = $this->setProtocol($config); |
|
73 | 24 | $config = $this->setServiceId($config); |
|
74 | 24 | $config = $this->setTNS($config); |
|
75 | 24 | $config = $this->setCharset($config); |
|
76 | |||
77 | 24 | return $config; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Set host from config. |
||
82 | * |
||
83 | * @param array $config |
||
84 | * @return array |
||
85 | */ |
||
86 | 24 | protected function setHost(array $config) |
|
87 | { |
||
88 | 24 | $config['host'] = isset($config['host']) ? $config['host'] : $config['hostname']; |
|
89 | |||
90 | 24 | return $config; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Set port from config. |
||
95 | * |
||
96 | * @param array $config |
||
97 | * @return array |
||
98 | */ |
||
99 | 24 | private function setPort(array $config) |
|
105 | |||
106 | /** |
||
107 | * Set protocol from config. |
||
108 | * |
||
109 | * @param array $config |
||
110 | * @return array |
||
111 | */ |
||
112 | 24 | private function setProtocol(array $config) |
|
113 | { |
||
114 | 24 | $config['protocol'] = isset($config['protocol']) ? $config['protocol'] : 'TCP'; |
|
115 | |||
116 | 24 | return $config; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Set service id from config. |
||
121 | * |
||
122 | * @param array $config |
||
123 | * @return array |
||
124 | */ |
||
125 | 24 | protected function setServiceId(array $config) |
|
133 | |||
134 | /** |
||
135 | * Set tns from config. |
||
136 | * |
||
137 | * @param array $config |
||
138 | * @return array |
||
139 | */ |
||
140 | 24 | protected function setTNS(array $config) |
|
146 | |||
147 | /** |
||
148 | * Set charset from config. |
||
149 | * |
||
150 | * @param array $config |
||
151 | * @return array |
||
152 | */ |
||
153 | 24 | protected function setCharset(array $config) |
|
161 | |||
162 | /** |
||
163 | * Set DSN host from config. |
||
164 | * |
||
165 | * @param array $config |
||
166 | * @return array |
||
167 | */ |
||
168 | 24 | protected function checkMultipleHostDsn(array $config) |
|
169 | { |
||
170 | 24 | $host = is_array($config['host']) ? $config['host'] : explode(',', $config['host']); |
|
171 | |||
172 | 24 | $count = count($host); |
|
173 | 24 | if ($count > 1) { |
|
174 | 6 | $address = ""; |
|
175 | 6 | for ($i = 0; $i < $count; $i++) { |
|
176 | 6 | $address .= '(ADDRESS = (PROTOCOL = ' . $config["protocol"] . ')(HOST = ' . trim($host[$i]) . ')(PORT = ' . $config['port'] . '))'; |
|
177 | 6 | } |
|
178 | |||
179 | // create a tns with multiple address connection |
||
180 | 6 | $config['tns'] = "(DESCRIPTION = {$address} (LOAD_BALANCE = yes) (FAILOVER = on) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = {$config['database']})))"; |
|
181 | 6 | } |
|
182 | |||
183 | 24 | return $config; |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * Create a new PDO connection. |
||
188 | * |
||
189 | * @param string $tns |
||
190 | * @param array $config |
||
191 | * @param array $options |
||
192 | * @return PDO |
||
193 | */ |
||
194 | public function createConnection($tns, array $config, array $options) |
||
206 | } |
||
207 |