Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 63 additions & 5 deletions idiorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class ORM implements ArrayAccess {

const DEFAULT_CONNECTION = 'default';

// Limit clause style
const LIMIT_STYLE_TOP_N = "top";
const LIMIT_STYLE_LIMIT = "limit";

// ------------------------ //
// --- CLASS PROPERTIES --- //
// ------------------------ //
Expand All @@ -64,6 +68,7 @@ class ORM implements ArrayAccess {
'password' => null,
'driver_options' => null,
'identifier_quote_character' => null, // if this is null, will be autodetected
'limit_clause_style' => null, // if this is null, will be autodetected
'logging' => false,
'caching' => false,
'return_result_sets' => false,
Expand Down Expand Up @@ -196,6 +201,13 @@ public static function configure($key, $value = null, $connection_name = self::D
}
}

/**
* Delete all configs in _config array.
*/
public static function reset_config() {
self::$_config = array();
}

/**
* Despite its slightly odd name, this is actually the factory
* method used to acquire instances of the class. It is named
Expand Down Expand Up @@ -254,6 +266,7 @@ public static function set_db($db, $connection_name = self::DEFAULT_CONNECTION)
self::_setup_db_config($connection_name);
self::$_db[$connection_name] = $db;
self::_setup_identifier_quote_character($connection_name);
self::_setup_limit_clause_style($connection_name);
}

/**
Expand All @@ -266,7 +279,19 @@ public static function set_db($db, $connection_name = self::DEFAULT_CONNECTION)
protected static function _setup_identifier_quote_character($connection_name) {
if (is_null(self::$_config[$connection_name]['identifier_quote_character'])) {
self::$_config[$connection_name]['identifier_quote_character'] =
self::_detect_identifier_quote_character($connection_name);
self::_detect_identifier_quote_character($connection_name);
}
}

/**
* Detect and initialise the limit clause style ("SELECT TOP 5" /
* "... LIMIT 5"). If this has been specified manually using
* ORM::configure('limit_clause_style', 'top'), this will do nothing.
*/
public static function _setup_limit_clause_style($connection_name) {
if (is_null(self::$_config[$connection_name]['limit_clause_style'])) {
self::$_config[$connection_name]['limit_clause_style'] =
self::_detect_limit_clause_style($connection_name);
}
}

Expand All @@ -281,8 +306,11 @@ protected static function _detect_identifier_quote_character($connection_name) {
case 'pgsql':
case 'sqlsrv':
case 'dblib':
return '';
case 'mssql':
return '';
case 'sybase':
return '';
case 'firebird':
return '"';
case 'mysql':
Expand All @@ -293,6 +321,22 @@ protected static function _detect_identifier_quote_character($connection_name) {
}
}

/**
* Returns a constant for the "limit clause style" for MS Sql Server
* versus grown up databases.
*/
protected static function _detect_limit_clause_style($connection_name) {
switch(self::$_db[$connection_name]->getAttribute(PDO::ATTR_DRIVER_NAME)) {
case 'sqlsrv':
case 'dblib':
return ORM::LIMIT_STYLE_TOP_N;
case 'mssql':
return ORM::LIMIT_STYLE_TOP_N;
default:
return ORM::LIMIT_STYLE_LIMIT;
}
}

/**
* Returns the PDO instance used by the the ORM to communicate with
* the database. This can be called if any low-level DB access is
Expand Down Expand Up @@ -438,6 +482,13 @@ public static function get_connection_names() {
return array_keys(self::$_db);
}

/**
* Delete all registered PDO objects in _db array.
*/
public static function reset_db() {
self::$_db = array();
}

// ------------------------ //
// --- INSTANCE METHODS --- //
// ------------------------ //
Expand Down Expand Up @@ -1289,13 +1340,18 @@ protected function _build_select() {
* Build the start of the SELECT statement
*/
protected function _build_select_start() {
$top = "";
$result_columns = join(', ', $this->_result_columns);

if (self::$_config[$this->_connection_name]['limit_clause_style'] == ORM::LIMIT_STYLE_TOP_N && !is_null($this->_limit)) {
$top = "TOP {$this->_limit} ";
}

if ($this->_distinct) {
$result_columns = 'DISTINCT ' . $result_columns;
}

$fragment = "SELECT {$result_columns} FROM " . $this->_quote_identifier($this->_table_name);
$fragment = "SELECT {$top}{$result_columns} FROM " . $this->_quote_identifier($this->_table_name);

if (!is_null($this->_table_alias)) {
$fragment .= " " . $this->_quote_identifier($this->_table_alias);
Expand Down Expand Up @@ -1374,11 +1430,13 @@ protected function _build_order_by() {
*/
protected function _build_limit() {
if (!is_null($this->_limit)) {
$clause = 'LIMIT';
if (self::$_config[$this->_connection_name]['limit_clause_style'] == ORM::LIMIT_STYLE_LIMIT) {
return 'LIMIT ' . $this->_limit;
}

if (self::$_db[$this->_connection_name]->getAttribute(PDO::ATTR_DRIVER_NAME) == 'firebird') {
$clause = 'ROWS';
return 'ROWS ' . $this->_limit;
}
return "$clause " . $this->_limit;
}
return '';
}
Expand Down
8 changes: 2 additions & 6 deletions test/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ public function setUp() {
}

public function tearDown() {
ORM::configure('logging', false);
ORM::configure('logging', false, self::ALTERNATE);
ORM::configure('caching', false);
ORM::configure('caching', false, self::ALTERNATE);
ORM::set_db(null);
ORM::set_db(null, self::ALTERNATE);
ORM::reset_config();
ORM::reset_db();
}

// Test caching. This is a bit of a hack.
Expand Down
6 changes: 2 additions & 4 deletions test/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ public function setUp() {
}

public function tearDown() {
ORM::configure('logging', false);
ORM::set_db(null);

ORM::configure('id_column', 'id');
ORM::reset_config();
ORM::reset_db();
}

protected function setUpIdColumnOverrides() {
Expand Down
4 changes: 2 additions & 2 deletions test/IdiormResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public function setUp() {
}

public function tearDown() {
ORM::configure('logging', false);
ORM::set_db(null);
ORM::reset_config();
ORM::reset_db();
}

public function testGet() {
Expand Down
6 changes: 2 additions & 4 deletions test/MulitpleConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ public function setUp() {
}

public function tearDown() {
ORM::configure('logging', false);
ORM::configure('logging', false, self::ALTERNATE);
ORM::set_db(null);
ORM::set_db(null, self::ALTERNATE);
ORM::reset_config();
ORM::reset_db();
}

public function testMultiplePdoConnections() {
Expand Down
4 changes: 2 additions & 2 deletions test/ORMTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public function setUp() {
}

public function tearDown() {
ORM::configure('logging', false);
ORM::set_db(null);
ORM::reset_config();
ORM::reset_db();
}

public function testStaticAtrributes() {
Expand Down
Loading