2010-10-13 06:06:50 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\Connect\MySQLiConnector;
|
|
|
|
use SilverStripe\ORM\Queries\SQLUpdate;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Dev\TestOnly;
|
|
|
|
|
2010-10-13 06:06:50 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-10-13 06:06:50 +02:00
|
|
|
* @subpackage testing
|
|
|
|
*/
|
|
|
|
|
|
|
|
class MySQLDatabaseTest extends SapphireTest {
|
2015-07-27 12:38:16 +02:00
|
|
|
|
2015-04-29 07:32:57 +02:00
|
|
|
protected static $fixture_file = 'MySQLDatabaseTest.yml';
|
|
|
|
|
2010-10-13 06:06:50 +02:00
|
|
|
protected $extraDataObjects = array(
|
2015-04-29 07:32:57 +02:00
|
|
|
'MySQLDatabaseTest_Data'
|
2010-10-13 06:06:50 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-04-29 07:32:57 +02:00
|
|
|
public function testPreparedStatements() {
|
|
|
|
if(!(DB::get_connector() instanceof MySQLiConnector)) {
|
|
|
|
$this->markTestSkipped('This test requires the current DB connector is MySQLi');
|
2010-10-13 06:06:50 +02:00
|
|
|
}
|
2015-04-29 07:32:57 +02:00
|
|
|
|
|
|
|
// Test preparation of equivalent statemetns
|
|
|
|
$result1 = DB::get_connector()->preparedQuery(
|
|
|
|
'SELECT "Sort", "Title" FROM "MySQLDatabaseTest_Data" WHERE "Sort" > ? ORDER BY "Sort"',
|
|
|
|
array(0)
|
|
|
|
);
|
|
|
|
|
|
|
|
$result2 = DB::get_connector()->preparedQuery(
|
|
|
|
'SELECT "Sort", "Title" FROM "MySQLDatabaseTest_Data" WHERE "Sort" > ? ORDER BY "Sort"',
|
|
|
|
array(2)
|
|
|
|
);
|
2016-06-15 06:03:16 +02:00
|
|
|
$this->assertInstanceOf('SilverStripe\\ORM\\Connect\\MySQLStatement', $result1);
|
|
|
|
$this->assertInstanceOf('SilverStripe\\ORM\\Connect\\MySQLStatement', $result2);
|
2015-04-29 07:32:57 +02:00
|
|
|
|
|
|
|
// Also select non-prepared statement
|
|
|
|
$result3 = DB::get_connector()->query('SELECT "Sort", "Title" FROM "MySQLDatabaseTest_Data" ORDER BY "Sort"');
|
2016-06-15 06:03:16 +02:00
|
|
|
$this->assertInstanceOf('SilverStripe\\ORM\\Connect\\MySQLQuery', $result3);
|
2015-04-29 07:32:57 +02:00
|
|
|
|
|
|
|
// Iterating one level should not buffer, but return the right result
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Sort' => 1,
|
|
|
|
'Title' => 'First Item'
|
|
|
|
),
|
|
|
|
$result1->next()
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Sort' => 2,
|
|
|
|
'Title' => 'Second Item'
|
|
|
|
),
|
|
|
|
$result1->next()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test first
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Sort' => 1,
|
|
|
|
'Title' => 'First Item'
|
|
|
|
),
|
|
|
|
$result1->first()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test seek
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Sort' => 2,
|
|
|
|
'Title' => 'Second Item'
|
|
|
|
),
|
|
|
|
$result1->seek(1)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test count
|
|
|
|
$this->assertEquals(4, $result1->numRecords());
|
|
|
|
|
|
|
|
// Test second statement
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Sort' => 3,
|
|
|
|
'Title' => 'Third Item'
|
|
|
|
),
|
|
|
|
$result2->next()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test non-prepared query
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'Sort' => 1,
|
|
|
|
'Title' => 'First Item'
|
|
|
|
),
|
|
|
|
$result3->next()
|
|
|
|
);
|
2010-10-13 06:06:50 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-04-29 07:32:57 +02:00
|
|
|
public function testAffectedRows() {
|
|
|
|
if(!(DB::get_connector() instanceof MySQLiConnector)) {
|
|
|
|
$this->markTestSkipped('This test requires the current DB connector is MySQLi');
|
2010-10-13 06:06:50 +02:00
|
|
|
}
|
2015-04-29 07:32:57 +02:00
|
|
|
|
|
|
|
$query = new SQLUpdate('MySQLDatabaseTest_Data');
|
|
|
|
$query->setAssignments(array('Title' => 'New Title'));
|
|
|
|
|
|
|
|
// Test update which affects no rows
|
|
|
|
$query->setWhere(array('Title' => 'Bob'));
|
|
|
|
$result = $query->execute();
|
2016-06-15 06:03:16 +02:00
|
|
|
$this->assertInstanceOf('SilverStripe\\ORM\\Connect\\MySQLQuery', $result);
|
2015-04-29 07:32:57 +02:00
|
|
|
$this->assertEquals(0, DB::affected_rows());
|
|
|
|
|
|
|
|
// Test update which affects some rows
|
|
|
|
$query->setWhere(array('Title' => 'First Item'));
|
|
|
|
$result = $query->execute();
|
2016-06-15 06:03:16 +02:00
|
|
|
$this->assertInstanceOf('SilverStripe\\ORM\\Connect\\MySQLQuery', $result);
|
2015-04-29 07:32:57 +02:00
|
|
|
$this->assertEquals(1, DB::affected_rows());
|
2010-10-13 06:06:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 07:32:57 +02:00
|
|
|
class MySQLDatabaseTest_Data extends DataObject implements TestOnly {
|
|
|
|
private static $db = array(
|
|
|
|
'Title' => 'Varchar',
|
|
|
|
'Description' => 'Text',
|
|
|
|
'Enabled' => 'Boolean',
|
|
|
|
'Sort' => 'Int'
|
|
|
|
);
|
2010-10-13 06:06:50 +02:00
|
|
|
|
2015-04-29 07:32:57 +02:00
|
|
|
private static $default_sort = '"Sort" ASC';
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|