Skip to content

Commit

Permalink
SqlServerConnector
Browse files Browse the repository at this point in the history
  • Loading branch information
nticaric committed May 11, 2017
1 parent 90010c8 commit 163817a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/Connectors/SqlServerConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace TeamTNT\TNTSearch\Connectors;

use PDO;

class SqlServerConnector extends Connector implements ConnectorInterface {

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

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

return $this->createConnection($this->getDsn($config), $config, $options);
}

/**
* Create a DSN string from a configuration.
*
* @param array $config
* @return string
*/
protected function getDsn(array $config)
{
extract($config);

// First we will create the basic DSN setup as well as the port if it is in
// in the configuration options. This will give us the basic DSN we will
// need to establish the PDO connections and return them back for use.
$port = isset($config['port']) ? ','.$port : '';

if (in_array('dblib', $this->getAvailableDrivers()))
{
return "dblib:host={$host}{$port};dbname={$database}";
}
else
{
$dbName = $database != '' ? ";Database={$database}" : '';

return "sqlsrv:Server={$host}{$port}{$dbName}";
}
}

/**
* Get the available PDO drivers.
*
* @return array
*/
protected function getAvailableDrivers()
{
return PDO::getAvailableDrivers();
}

}
3 changes: 3 additions & 0 deletions src/Indexer/TNTIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use TeamTNT\TNTSearch\Connectors\MySqlConnector;
use TeamTNT\TNTSearch\Connectors\PostgresConnector;
use TeamTNT\TNTSearch\Connectors\SQLiteConnector;
use TeamTNT\TNTSearch\Connectors\SqlServerConnector;
use TeamTNT\TNTSearch\FileReaders\TextFileReader;
use TeamTNT\TNTSearch\Stemmer\CroatianStemmer;
use TeamTNT\TNTSearch\Stemmer\PorterStemmer;
Expand Down Expand Up @@ -181,6 +182,8 @@ public function createConnector(array $config)
return new PostgresConnector;
case 'sqlite':
return new SQLiteConnector;
case 'sqlsrv':
return new SqlServerConnector;
case 'filesystem':
return new FileSystemConnector;
}
Expand Down

0 comments on commit 163817a

Please sign in to comment.