You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
404 lines
14 KiB
404 lines
14 KiB
13 years ago
|
<?php
|
||
|
|
||
12 years ago
|
namespace yiiunit\framework\db;
|
||
13 years ago
|
|
||
10 years ago
|
use yii\caching\FileCache;
|
||
|
use yii\db\Connection;
|
||
12 years ago
|
use yii\db\DataReader;
|
||
13 years ago
|
|
||
11 years ago
|
/**
|
||
|
* @group db
|
||
|
* @group mysql
|
||
|
*/
|
||
12 years ago
|
class CommandTest extends DatabaseTestCase
|
||
13 years ago
|
{
|
||
11 years ago
|
public function testConstruct()
|
||
|
{
|
||
|
$db = $this->getConnection(false);
|
||
|
|
||
|
// null
|
||
|
$command = $db->createCommand();
|
||
|
$this->assertEquals(null, $command->sql);
|
||
|
|
||
|
// string
|
||
11 years ago
|
$sql = 'SELECT * FROM customer';
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
|
$this->assertEquals($sql, $command->sql);
|
||
|
}
|
||
|
|
||
|
public function testGetSetSql()
|
||
|
{
|
||
|
$db = $this->getConnection(false);
|
||
|
|
||
11 years ago
|
$sql = 'SELECT * FROM customer';
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
|
$this->assertEquals($sql, $command->sql);
|
||
|
|
||
11 years ago
|
$sql2 = 'SELECT * FROM order';
|
||
11 years ago
|
$command->sql = $sql2;
|
||
|
$this->assertEquals($sql2, $command->sql);
|
||
|
}
|
||
|
|
||
|
public function testAutoQuoting()
|
||
|
{
|
||
|
$db = $this->getConnection(false);
|
||
|
|
||
11 years ago
|
$sql = 'SELECT [[id]], [[t.name]] FROM {{customer}} t';
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
11 years ago
|
$this->assertEquals("SELECT `id`, `t`.`name` FROM `customer` t", $command->sql);
|
||
11 years ago
|
}
|
||
|
|
||
|
public function testPrepareCancel()
|
||
|
{
|
||
|
$db = $this->getConnection(false);
|
||
|
|
||
10 years ago
|
$command = $db->createCommand('SELECT * FROM {{customer}}');
|
||
11 years ago
|
$this->assertEquals(null, $command->pdoStatement);
|
||
|
$command->prepare();
|
||
|
$this->assertNotEquals(null, $command->pdoStatement);
|
||
|
$command->cancel();
|
||
|
$this->assertEquals(null, $command->pdoStatement);
|
||
|
}
|
||
|
|
||
|
public function testExecute()
|
||
|
{
|
||
|
$db = $this->getConnection();
|
||
|
|
||
10 years ago
|
$sql = 'INSERT INTO {{customer}}([[email]], [[name]], [[address]]) VALUES (\'user4@example.com\', \'user4\', \'address4\')';
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
|
$this->assertEquals(1, $command->execute());
|
||
|
|
||
10 years ago
|
$sql = 'SELECT COUNT(*) FROM {{customer}} WHERE [[name]] = \'user4\'';
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
|
$this->assertEquals(1, $command->queryScalar());
|
||
|
|
||
|
$command = $db->createCommand('bad SQL');
|
||
|
$this->setExpectedException('\yii\db\Exception');
|
||
|
$command->execute();
|
||
|
}
|
||
|
|
||
|
public function testQuery()
|
||
|
{
|
||
|
$db = $this->getConnection();
|
||
|
|
||
|
// query
|
||
10 years ago
|
$sql = 'SELECT * FROM {{customer}}';
|
||
11 years ago
|
$reader = $db->createCommand($sql)->query();
|
||
|
$this->assertTrue($reader instanceof DataReader);
|
||
|
|
||
|
// queryAll
|
||
10 years ago
|
$rows = $db->createCommand('SELECT * FROM {{customer}}')->queryAll();
|
||
11 years ago
|
$this->assertEquals(3, count($rows));
|
||
|
$row = $rows[2];
|
||
|
$this->assertEquals(3, $row['id']);
|
||
|
$this->assertEquals('user3', $row['name']);
|
||
|
|
||
10 years ago
|
$rows = $db->createCommand('SELECT * FROM {{customer}} WHERE [[id]] = 10')->queryAll();
|
||
11 years ago
|
$this->assertEquals([], $rows);
|
||
|
|
||
|
// queryOne
|
||
10 years ago
|
$sql = 'SELECT * FROM {{customer}} ORDER BY [[id]]';
|
||
11 years ago
|
$row = $db->createCommand($sql)->queryOne();
|
||
|
$this->assertEquals(1, $row['id']);
|
||
|
$this->assertEquals('user1', $row['name']);
|
||
|
|
||
10 years ago
|
$sql = 'SELECT * FROM {{customer}} ORDER BY [[id]]';
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
|
$command->prepare();
|
||
|
$row = $command->queryOne();
|
||
|
$this->assertEquals(1, $row['id']);
|
||
|
$this->assertEquals('user1', $row['name']);
|
||
|
|
||
10 years ago
|
$sql = 'SELECT * FROM {{customer}} WHERE [[id]] = 10';
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
|
$this->assertFalse($command->queryOne());
|
||
|
|
||
|
// queryColumn
|
||
10 years ago
|
$sql = 'SELECT * FROM {{customer}}';
|
||
11 years ago
|
$column = $db->createCommand($sql)->queryColumn();
|
||
|
$this->assertEquals(range(1, 3), $column);
|
||
|
|
||
10 years ago
|
$command = $db->createCommand('SELECT [[id]] FROM {{customer}} WHERE [[id]] = 10');
|
||
11 years ago
|
$this->assertEquals([], $command->queryColumn());
|
||
|
|
||
|
// queryScalar
|
||
10 years ago
|
$sql = 'SELECT * FROM {{customer}} ORDER BY [[id]]';
|
||
11 years ago
|
$this->assertEquals($db->createCommand($sql)->queryScalar(), 1);
|
||
|
|
||
10 years ago
|
$sql = 'SELECT [[id]] FROM {{customer}} ORDER BY [[id]]';
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
|
$command->prepare();
|
||
|
$this->assertEquals(1, $command->queryScalar());
|
||
|
|
||
10 years ago
|
$command = $db->createCommand('SELECT [[id]] FROM {{customer}} WHERE [[id]] = 10');
|
||
11 years ago
|
$this->assertFalse($command->queryScalar());
|
||
|
|
||
|
$command = $db->createCommand('bad SQL');
|
||
|
$this->setExpectedException('\yii\db\Exception');
|
||
|
$command->query();
|
||
|
}
|
||
|
|
||
|
public function testBindParamValue()
|
||
|
{
|
||
|
$db = $this->getConnection();
|
||
|
|
||
|
// bindParam
|
||
10 years ago
|
$sql = 'INSERT INTO {{customer}}([[email]], [[name]], [[address]]) VALUES (:email, :name, :address)';
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
|
$email = 'user4@example.com';
|
||
|
$name = 'user4';
|
||
|
$address = 'address4';
|
||
|
$command->bindParam(':email', $email);
|
||
|
$command->bindParam(':name', $name);
|
||
|
$command->bindParam(':address', $address);
|
||
|
$command->execute();
|
||
|
|
||
10 years ago
|
$sql = 'SELECT [[name]] FROM {{customer}} WHERE [[email]] = :email';
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
|
$command->bindParam(':email', $email);
|
||
|
$this->assertEquals($name, $command->queryScalar());
|
||
|
|
||
10 years ago
|
$sql = <<<SQL
|
||
|
INSERT INTO {{type}} ([[int_col]], [[char_col]], [[float_col]], [[blob_col]], [[numeric_col]], [[bool_col]])
|
||
|
VALUES (:int_col, :char_col, :float_col, :blob_col, :numeric_col, :bool_col)
|
||
|
SQL;
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
|
$intCol = 123;
|
||
10 years ago
|
$charCol = str_repeat('abc', 33) . 'x'; // a 100 char string
|
||
11 years ago
|
$boolCol = false;
|
||
10 years ago
|
$command->bindParam(':int_col', $intCol, \PDO::PARAM_INT);
|
||
11 years ago
|
$command->bindParam(':char_col', $charCol);
|
||
10 years ago
|
$command->bindParam(':bool_col', $boolCol, \PDO::PARAM_BOOL);
|
||
|
if ($this->driverName === 'oci') {
|
||
|
// can't bind floats without support from a custom PDO driver
|
||
|
$floatCol = 2;
|
||
|
$numericCol = 3;
|
||
|
// can't use blobs without support from a custom PDO driver
|
||
|
$blobCol = null;
|
||
|
$command->bindParam(':float_col', $floatCol, \PDO::PARAM_INT);
|
||
|
$command->bindParam(':numeric_col', $numericCol, \PDO::PARAM_INT);
|
||
|
$command->bindParam(':blob_col', $blobCol);
|
||
|
} else {
|
||
|
$floatCol = 1.23;
|
||
|
$numericCol = '1.23';
|
||
|
$blobCol = "\x10\x11\x12";
|
||
|
$command->bindParam(':float_col', $floatCol);
|
||
|
$command->bindParam(':numeric_col', $numericCol);
|
||
|
$command->bindParam(':blob_col', $blobCol);
|
||
|
}
|
||
11 years ago
|
$this->assertEquals(1, $command->execute());
|
||
|
|
||
10 years ago
|
$command = $db->createCommand('SELECT [[int_col]], [[char_col]], [[float_col]], [[blob_col]], [[numeric_col]], [[bool_col]] FROM {{type}}');
|
||
10 years ago
|
// $command->prepare();
|
||
|
// $command->pdoStatement->bindColumn('blob_col', $bc, \PDO::PARAM_LOB);
|
||
|
$row = $command->queryOne();
|
||
11 years ago
|
$this->assertEquals($intCol, $row['int_col']);
|
||
|
$this->assertEquals($charCol, $row['char_col']);
|
||
|
$this->assertEquals($floatCol, $row['float_col']);
|
||
10 years ago
|
if ($this->driverName === 'mysql' || $this->driverName === 'sqlite' || $this->driverName === 'oci') {
|
||
10 years ago
|
$this->assertEquals($blobCol, $row['blob_col']);
|
||
|
} else {
|
||
|
$this->assertTrue(is_resource($row['blob_col']));
|
||
|
$this->assertEquals($blobCol, stream_get_contents($row['blob_col']));
|
||
|
}
|
||
11 years ago
|
$this->assertEquals($numericCol, $row['numeric_col']);
|
||
10 years ago
|
if ($this->driverName === 'mysql' || (defined('HHVM_VERSION') && $this->driverName === 'sqlite') || $this->driverName === 'oci') {
|
||
10 years ago
|
$this->assertEquals($boolCol, (int) $row['bool_col']);
|
||
10 years ago
|
} else {
|
||
|
$this->assertEquals($boolCol, $row['bool_col']);
|
||
|
}
|
||
11 years ago
|
|
||
|
// bindValue
|
||
10 years ago
|
$sql = 'INSERT INTO {{customer}}([[email]], [[name]], [[address]]) VALUES (:email, \'user5\', \'address5\')';
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
|
$command->bindValue(':email', 'user5@example.com');
|
||
|
$command->execute();
|
||
|
|
||
10 years ago
|
$sql = 'SELECT [[email]] FROM {{customer}} WHERE [[name]] = :name';
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
|
$command->bindValue(':name', 'user5');
|
||
|
$this->assertEquals('user5@example.com', $command->queryScalar());
|
||
|
}
|
||
|
|
||
|
public function testFetchMode()
|
||
|
{
|
||
|
$db = $this->getConnection();
|
||
|
|
||
|
// default: FETCH_ASSOC
|
||
10 years ago
|
$sql = 'SELECT * FROM {{customer}}';
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
|
$result = $command->queryOne();
|
||
|
$this->assertTrue(is_array($result) && isset($result['id']));
|
||
|
|
||
|
// FETCH_OBJ, customized via fetchMode property
|
||
10 years ago
|
$sql = 'SELECT * FROM {{customer}}';
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
|
$command->fetchMode = \PDO::FETCH_OBJ;
|
||
|
$result = $command->queryOne();
|
||
|
$this->assertTrue(is_object($result));
|
||
|
|
||
|
// FETCH_NUM, customized in query method
|
||
10 years ago
|
$sql = 'SELECT * FROM {{customer}}';
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
|
$result = $command->queryOne([], \PDO::FETCH_NUM);
|
||
|
$this->assertTrue(is_array($result) && isset($result[0]));
|
||
|
}
|
||
|
|
||
|
public function testBatchInsert()
|
||
|
{
|
||
|
$command = $this->getConnection()->createCommand();
|
||
10 years ago
|
$command->batchInsert(
|
||
|
'{{customer}}',
|
||
|
['email', 'name', 'address'],
|
||
|
[
|
||
11 years ago
|
['t1@example.com', 't1', 't1 address'],
|
||
|
['t2@example.com', null, false],
|
||
|
]
|
||
|
);
|
||
|
$this->assertEquals(2, $command->execute());
|
||
|
}
|
||
|
|
||
10 years ago
|
/*
|
||
11 years ago
|
public function testInsert()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public function testUpdate()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public function testDelete()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public function testCreateTable()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public function testRenameTable()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public function testDropTable()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public function testTruncateTable()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public function testAddColumn()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public function testDropColumn()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public function testRenameColumn()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public function testAlterColumn()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public function testAddForeignKey()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public function testDropForeignKey()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public function testCreateIndex()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public function testDropIndex()
|
||
|
{
|
||
|
}
|
||
10 years ago
|
*/
|
||
11 years ago
|
|
||
|
public function testIntegrityViolation()
|
||
|
{
|
||
|
$this->setExpectedException('\yii\db\IntegrityException');
|
||
|
|
||
|
$db = $this->getConnection();
|
||
|
|
||
10 years ago
|
$sql = 'INSERT INTO {{profile}}([[id]], [[description]]) VALUES (123, \'duplicate\')';
|
||
11 years ago
|
$command = $db->createCommand($sql);
|
||
|
$command->execute();
|
||
|
$command->execute();
|
||
|
}
|
||
10 years ago
|
|
||
|
public function testQueryCache()
|
||
|
{
|
||
|
$db = $this->getConnection();
|
||
|
$db->enableQueryCache = true;
|
||
|
$db->queryCache = new FileCache(['cachePath' => '@yiiunit/runtime/cache']);
|
||
10 years ago
|
$command = $db->createCommand('SELECT [[name]] FROM {{customer}} WHERE [[id]] = :id');
|
||
10 years ago
|
|
||
|
$this->assertEquals('user1', $command->bindValue(':id', 1)->queryScalar());
|
||
10 years ago
|
$update = $db->createCommand('UPDATE {{customer}} SET [[name]] = :name WHERE [[id]] = :id');
|
||
10 years ago
|
$update->bindValues([':id' => 1, ':name' => 'user11'])->execute();
|
||
|
$this->assertEquals('user11', $command->bindValue(':id', 1)->queryScalar());
|
||
|
|
||
|
$db->cache(function (Connection $db) use ($command, $update) {
|
||
|
$this->assertEquals('user2', $command->bindValue(':id', 2)->queryScalar());
|
||
|
$update->bindValues([':id' => 2, ':name' => 'user22'])->execute();
|
||
|
$this->assertEquals('user2', $command->bindValue(':id', 2)->queryScalar());
|
||
|
|
||
|
$db->noCache(function () use ($command) {
|
||
|
$this->assertEquals('user22', $command->bindValue(':id', 2)->queryScalar());
|
||
|
});
|
||
|
|
||
|
$this->assertEquals('user2', $command->bindValue(':id', 2)->queryScalar());
|
||
|
}, 10);
|
||
|
|
||
|
$db->enableQueryCache = false;
|
||
|
$db->cache(function ($db) use ($command, $update) {
|
||
|
$this->assertEquals('user22', $command->bindValue(':id', 2)->queryScalar());
|
||
|
$update->bindValues([':id' => 2, ':name' => 'user2'])->execute();
|
||
|
$this->assertEquals('user2', $command->bindValue(':id', 2)->queryScalar());
|
||
|
}, 10);
|
||
|
|
||
|
$db->enableQueryCache = true;
|
||
10 years ago
|
$command = $db->createCommand('SELECT [[name]] FROM {{customer}} WHERE [[id]] = :id')->cache();
|
||
10 years ago
|
$this->assertEquals('user11', $command->bindValue(':id', 1)->queryScalar());
|
||
|
$update->bindValues([':id' => 1, ':name' => 'user1'])->execute();
|
||
|
$this->assertEquals('user11', $command->bindValue(':id', 1)->queryScalar());
|
||
|
$this->assertEquals('user1', $command->noCache()->bindValue(':id', 1)->queryScalar());
|
||
|
|
||
10 years ago
|
$command = $db->createCommand('SELECT [[name]] FROM {{customer}} WHERE [[id]] = :id');
|
||
10 years ago
|
$db->cache(function (Connection $db) use ($command, $update) {
|
||
|
$this->assertEquals('user11', $command->bindValue(':id', 1)->queryScalar());
|
||
|
$this->assertEquals('user1', $command->noCache()->bindValue(':id', 1)->queryScalar());
|
||
|
}, 10);
|
||
|
}
|
||
10 years ago
|
|
||
|
public function testColumnCase()
|
||
|
{
|
||
|
$db = $this->getConnection(false);
|
||
|
$this->assertEquals(\PDO::CASE_NATURAL, $db->slavePdo->getAttribute(\PDO::ATTR_CASE));
|
||
|
|
||
|
$sql = 'SELECT [[customer_id]], [[total]] FROM {{order}}';
|
||
|
$rows = $db->createCommand($sql)->queryAll();
|
||
|
$this->assertTrue(isset($rows[0]));
|
||
|
$this->assertTrue(isset($rows[0]['customer_id']));
|
||
|
$this->assertTrue(isset($rows[0]['total']));
|
||
|
|
||
|
$db->slavePdo->setAttribute(\PDO::ATTR_CASE, \PDO::CASE_LOWER);
|
||
|
$rows = $db->createCommand($sql)->queryAll();
|
||
|
$this->assertTrue(isset($rows[0]));
|
||
|
$this->assertTrue(isset($rows[0]['customer_id']));
|
||
|
$this->assertTrue(isset($rows[0]['total']));
|
||
|
|
||
|
$db->slavePdo->setAttribute(\PDO::ATTR_CASE, \PDO::CASE_UPPER);
|
||
|
$rows = $db->createCommand($sql)->queryAll();
|
||
|
$this->assertTrue(isset($rows[0]));
|
||
|
$this->assertTrue(isset($rows[0]['CUSTOMER_ID']));
|
||
|
$this->assertTrue(isset($rows[0]['TOTAL']));
|
||
|
}
|
||
12 years ago
|
}
|