Overview

Namespaces

  • None
  • Yajra
    • Oci8
      • Auth
      • Connectors
      • Eloquent
      • Query
        • Grammars
        • Processors
      • Schema
        • Grammars

Classes

  • Yajra\Oci8\Auth\OracleUserProvider
  • Yajra\Oci8\Connectors\OracleConnector
  • Yajra\Oci8\Eloquent\OracleEloquent
  • Yajra\Oci8\Oci8Connection
  • Yajra\Oci8\Oci8ServiceProvider
  • Yajra\Oci8\Query\Grammars\OracleGrammar
  • Yajra\Oci8\Query\OracleBuilder
  • Yajra\Oci8\Query\Processors\OracleProcessor
  • Yajra\Oci8\Schema\Comment
  • Yajra\Oci8\Schema\Grammars\OracleGrammar
  • Yajra\Oci8\Schema\OracleAutoIncrementHelper
  • Yajra\Oci8\Schema\OracleBlueprint
  • Yajra\Oci8\Schema\OracleBuilder
  • Yajra\Oci8\Schema\Sequence
  • Yajra\Oci8\Schema\Trigger

Traits

  • Yajra\Oci8\OracleReservedWords

Functions

  • config_path
  • Overview
  • Namespace
  • Class
  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:      * 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:     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:      * Create a DSN string from a configuration.
 42:      *
 43:      * @param  array $config
 44:      * @return string
 45:      */
 46:     protected function getDsn(array $config)
 47:     {
 48:         if (! empty($config['tns'])) {
 49:             return $config['tns'];
 50:         }
 51: 
 52:         // parse configuration
 53:         $config = $this->parseConfig($config);
 54: 
 55:         // check multiple connections/host, comma delimiter
 56:         $config = $this->checkMultipleHostDsn($config);
 57: 
 58:         // return generated tns
 59:         return $config['tns'];
 60:     }
 61: 
 62:     /**
 63:      * Parse configurations.
 64:      *
 65:      * @param array $config
 66:      * @return array
 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:      * Set host from config.
 82:      *
 83:      * @param array $config
 84:      * @return array
 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:      * Set port from config.
 95:      *
 96:      * @param array $config
 97:      * @return array
 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:      * Set protocol from config.
108:      *
109:      * @param array $config
110:      * @return array
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:      * Set service id from config.
121:      *
122:      * @param array $config
123:      * @return array
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:      * Set tns from config.
136:      *
137:      * @param array $config
138:      * @return array
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:      * Set charset from config.
149:      *
150:      * @param array $config
151:      * @return array
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:      * Set DSN host from config.
164:      *
165:      * @param array $config
166:      * @return array
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:             // create a tns with multiple address connection
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:      * 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)
195:     {
196:         // add fallback in case driver is not set, will use pdo instead
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: 
API documentation generated by ApiGen