Skip to content

Commit

Permalink
Apparently not all files were included in previous commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Gregory committed Jan 22, 2013
1 parent 98b8066 commit a49bd88
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 110 deletions.
22 changes: 15 additions & 7 deletions docs/connections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,11 @@ calls in the chain use the correct connection.
$person = ORM::for_table('different_person', 'remote')->find_one(5);
// Last query on *any* connection
ORM::get_last_query(); // returns query on 'different_person' using 'remote'
// returns query on 'person' using default
ORM::get_last_query(ORM::DEFAULT_CONNECTION);

Supported Methods
^^^^^^^^^^^^^^^^^
In each of these cases, the ``$connection_name`` parameter is optional, and is an arbitrary
key identifying the named connection.
In each of these cases, the ``$connection_name`` parameter is optional, and is
an arbitrary key identifying the named connection.

* ``ORM::configure($key, $value, $connection_name)``
* ``ORM::for_table($table_name, $connection_name)``
Expand All @@ -49,6 +44,19 @@ key identifying the named connection.
* ``ORM::get_last_query($connection_name)``
* ``ORM::get_query_log($connection_name)``

Of these methods, only ``ORM::get_last_query($connection_name)`` does *not*
fallback to the default connection when no connection name is passed.
Instead, passing no connection name (or ``null``) returns the most recent
query on *any* connection.

::

// Last query on *any* connection
ORM::get_last_query(); // returns query on 'different_person' using 'remote'
// returns query on 'person' using default
ORM::get_last_query(ORM::DEFAULT_CONNECTION);

Notes
~~~~~
* **There is no support for joins across connections**
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Contents:
querying
models
transactions

connections


Indices and tables
Expand Down
7 changes: 4 additions & 3 deletions idiorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class ORM implements ArrayAccess {
'return_result_sets' => false,
);

// Map of configuration settings
protected static $_config = array();

// Map of database connections, instances of the PDO class
Expand Down Expand Up @@ -172,7 +173,7 @@ class ORM implements ArrayAccess {
* you wish to configure, another shortcut is to pass an array
* of settings (and omit the second argument).
*/
public static function configure(
public static function configure(
$key,
$value = null,
$connection_name = self::DEFAULT_CONNECTION
Expand All @@ -186,7 +187,7 @@ public static function configure(
self::configure($conf_key, $conf_value, $connection_name);
}
} else {
if (empty($value)) {
if (is_null($value)) {
// Shortcut: If only one string argument is passed,
// assume it's a connection string
$value = $key;
Expand Down Expand Up @@ -512,7 +513,7 @@ public function find_one($id=null) {
* @return array|\IdiormResultSet
*/
public function find_many() {
if(self::$_config['return_result_sets']) {
if(self::$_config[$this->_connection_name]['return_result_sets']) {
return $this->find_result_set();
}
return $this->_find_many();
Expand Down
22 changes: 18 additions & 4 deletions test/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@

class CacheTest extends PHPUnit_Framework_TestCase {

const ALTERNATE = 'alternate'; // Used as name of alternate connection

public function setUp() {
// Set up the dummy database connections
ORM::set_db(new MockPDO('sqlite::memory:'));
ORM::set_db(new MockDifferentPDO('sqlite::memory:'), self::ALTERNATE);

// Enable logging
ORM::configure('logging', true);
ORM::configure('logging', true, self::ALTERNATE);
ORM::configure('caching', true);

// Set up the dummy database connection
$db = new MockPDO('sqlite::memory:');
ORM::set_db($db);
ORM::configure('caching', true, self::ALTERNATE);
}

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);
}

// Test caching. This is a bit of a hack.
Expand All @@ -25,5 +32,12 @@ public function testQueryGenerationOnlyOccursOnce() {
$expected = ORM::get_last_query();
ORM::for_table('widget')->where('name', 'Fred')->where('age', 17)->find_one(); // this shouldn't run a query!
$this->assertEquals($expected, ORM::get_last_query());

// Test caching with multiple connections (also a bit of a hack)
ORM::for_table('widget', self::ALTERNATE)->where('name', 'Steve')->where('age', 80)->find_one();
ORM::for_table('widget', self::ALTERNATE)->where('name', 'Tom')->where('age', 120)->find_one();
$expectedToo = ORM::get_last_query();
ORM::for_table('widget', self::ALTERNATE)->where('name', 'Steve')->where('age', 80)->find_one(); // this shouldn't run a query!
$this->assertEquals($expectedToo, ORM::get_last_query(self::ALTERNATE));
}
}
34 changes: 34 additions & 0 deletions test/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,38 @@ public function prepare($statement, $driver_options=array()) {
$this->last_query = new MockPDOStatement($statement);
return $this->last_query;
}
}

/**
* Another mock PDOStatement class, used for testing multiple connections
*/
class MockDifferentPDOStatement extends PDOStatement {

private $current_row = 0;
/**
* Return some dummy data
*/
public function fetch($fetch_style=PDO::FETCH_BOTH, $cursor_orientation=PDO::FETCH_ORI_NEXT, $cursor_offset=0) {
if ($this->current_row == 5) {
return false;
} else {
$this->current_row++;
return array('name' => 'Steve', 'age' => 80, 'id' => "{$this->current_row}");
}
}
}

/**
* A different mock database class, for testing multiple connections
* Mock database class implementing a subset of the PDO API.
*/
class MockDifferentPDO extends PDO {

/**
* Return a dummy PDO statement
*/
public function prepare($statement, $driver_options = array()) {
$this->last_query = new MockDifferentPDOStatement($statement);
return $this->last_query;
}
}
6 changes: 3 additions & 3 deletions test/test_classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function prepare($statement, $driver_options=array()) {
/**
* Another mock PDOStatement class, for testing multiple connections
*/
class DummyDifferentPDOStatement extends PDOStatement {
class MockDifferentPDOStatement extends PDOStatement {

private $current_row = 0;
/**
Expand All @@ -69,13 +69,13 @@ public function fetch(
* A different mock database class, for testing multiple connections
* Mock database class implementing a subset of the PDO API.
*/
class DummyDifferentPDO extends PDO {
class MockDifferentPDO extends PDO {

/**
* Return a dummy PDO statement
*/
public function prepare($statement, $driver_options = array()) {
$this->last_query = new DummyDifferentPDOStatement($statement);
$this->last_query = new MockDifferentPDOStatement($statement);
return $this->last_query;
}
}
Expand Down
Loading

0 comments on commit a49bd88

Please sign in to comment.