<commit_before><?php namespace yajra\Oci8\Connectors;

use Illuminate\Database\Connectors;
use Illuminate\Database\Connectors\Connector;
use Illuminate\Database\Connectors\ConnectorInterface;

class OracleConnector extends Connector implements ConnectorInterface
{

    /**
     * The default PDO connection options.
     *
     * @var array
     */
    protected $options = array(
        \PDO::ATTR_CASE => \PDO::CASE_LOWER,
        \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
        \PDO::ATTR_ORACLE_NULLS => \PDO::NULL_NATURAL,
        );

    /**
     * Create a new PDO connection.
     *
     * @param  string  $tns
     * @param  array   $config
     * @param  array   $options
     * @return \PDO
     */
    public function createConnection($tns, array $config, array $options)
    {
        // add fallback in case driver is not set, will use pdo instead
        if ( !in_array($config['driver'], array('oci8', 'pdo-via-oci8', 'oracle')) ) {
            return parent::createConnection($tns, $config, $options);
        }

        // check charset
        if (!isset($config['charset'])) {
            $config['charset'] = '';
        }

        $options['charset'] = $config['charset'];
        return new \yajra\Pdo\Oci8($tns, $config['username'], $config['password'], $options);
    }

    /**
     * Establish a database connection.
     *
     * @return \PDO
     */
    public function connect(array $config)
    {
        $tns = $this->getDsn($config);

        $options = $this->getOptions($config);

        $connection = $this->createConnection($tns, $config, $options);

        return $connection;
    }

    /**
     * Create a DSN string from a configuration.
     *
     * @param  array   $config
     * @return string
     */
    protected function getDsn(array $config)
    {
        // check port
        if (!isset($config['port'])) {
            $config['port'] = '1521';
        }

        // check host config
        if (isset($config['hostname']) and !isset($config['host']))
            $config['host'] = $config['hostname'];

        // check tns
        if (empty($config['tns'])) {
             //Create a description to locate the database to connect to
            $config['tns'] = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = {$config['host']})(PORT = {$config['port']})) (CONNECT_DATA =(SID = {$config['database']})))";
        }

        // check multiple connections/host, comma delimiter
        if( isset($config['host']) ){
            $host = explode(',', $config['host']);
            if (count($host) > 1) {
                $address  = "";

                for($i = 0;$i < count($host); $i++){
                   $address .= '(ADDRESS = (PROTOCOL = TCP)(HOST = '.trim($host[$i]).')(PORT = '.$config['port'].'))';<commit_msg>Optional use of SID<commit_after><?php namespace yajra\Oci8\Connectors;

use Illuminate\Database\Connectors;
use Illuminate\Database\Connectors\Connector;
use Illuminate\Database\Connectors\ConnectorInterface;

class OracleConnector extends Connector implements ConnectorInterface
{

    /**
     * The default PDO connection options.
     *
     * @var array
     */
    protected $options = array(
        \PDO::ATTR_CASE => \PDO::CASE_LOWER,
        \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
        \PDO::ATTR_ORACLE_NULLS => \PDO::NULL_NATURAL,
        );

    /**
     * Create a new PDO connection.
     *
     * @param  string  $tns
     * @param  array   $config
     * @param  array   $options
     * @return \PDO
     */
    public function createConnection($tns, array $config, array $options)
    {
        // add fallback in case driver is not set, will use pdo instead
        if ( !in_array($config['driver'], array('oci8', 'pdo-via-oci8', 'oracle')) ) {
            return parent::createConnection($tns, $config, $options);
        }

        // check charset
        if (!isset($config['charset'])) {
            $config['charset'] = '';
        }

        $options['charset'] = $config['charset'];
        return new \yajra\Pdo\Oci8($tns, $config['username'], $config['password'], $options);
    }

    /**
     * Establish a database connection.
     *
     * @return \PDO
     */
    public function connect(array $config)
    {
        $tns = $this->getDsn($config);

        $options = $this->getOptions($config);

        $connection = $this->createConnection($tns, $config, $options);

        return $connection;
    }

    /**
     * Create a DSN string from a configuration.
     *
     * @param  array   $config
     * @return string
     */
    protected function getDsn(array $config)
    {
        // check port
        if (!isset($config['port'])) {
            $config['port'] = '1521';
        }

        // check host config
        if (isset($config['hostname']) and !isset($config['host']))
            $config['host'] = $config['hostname'];

        // check tns
        if (empty($config['tns'])) {
             //Create a description to locate the database to connect to
            
            if(!empty($config['sid'])) $service_param = 'SID = '.$config['sid'];
            else $service_param = 'SERVICE_NAME = '.$config['database'];
            
            $config['tns'] = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = {$config['host']})(PORT = {$config['port']})) (CONNECT_DATA =($service_param)))";
        }

        // check multiple connections/host, comma delimiter
        if( isset($config['host']) ){
            $host = explode(',', $config['host']);
            if (count($host) > 1) {
                $address  = "";

                for($i = 0;$i < count($host); $i++){
                   $address .= '(ADDRESS = (PROTOCOL = TCP)(HOST = '.trim($host[$i]).')(PORT = '.$config['port'].'))';
{'fraction_non_alphanumeric': 0.1658833768494343, 'fraction_numerical': 0.0034812880765883376, 'mean_word_length': 2.2244668911335577, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2549999', 'n_tokens_mistral': 1727, 'n_tokens_neox': 1693, 'n_words': 383}