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