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: 15: 16: 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: 26: 27: 28: 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: 43: 44: 45: 46:
47: protected function getDsn(array $config)
48: {
49: if (! empty($config['tns'])) {
50: return $config['tns'];
51: }
52:
53:
54: $config = $this->parseConfig($config);
55:
56:
57: $config = $this->checkMultipleHostDsn($config);
58:
59:
60: return $config['tns'];
61: }
62:
63: 64: 65: 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: 81: 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: 92: 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: 103: 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: 114: 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: 127: 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: 138: 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: 151: 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:
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: 173: 174: 175: 176: 177: 178:
179: public function createConnection($tns, array $config, array $options)
180: {
181:
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: