QueryBuilderTest.php 10 KB
Newer Older
1 2 3 4
<?php

namespace yiiunit\framework\db;

5
use yii\db\Query;
6 7 8 9 10
use yii\db\QueryBuilder;
use yii\db\Schema;
use yii\db\mysql\QueryBuilder as MysqlQueryBuilder;
use yii\db\sqlite\QueryBuilder as SqliteQueryBuilder;
use yii\db\mssql\QueryBuilder as MssqlQueryBuilder;
11
use yii\db\pgsql\QueryBuilder as PgsqlQueryBuilder;
Carsten Brandt committed
12
use yii\db\cubrid\QueryBuilder as CubridQueryBuilder;
13

14 15 16 17
/**
 * @group db
 * @group mysql
 */
18 19 20 21 22 23 24 25
class QueryBuilderTest extends DatabaseTestCase
{
	/**
	 * @throws \Exception
	 * @return QueryBuilder
	 */
	protected function getQueryBuilder()
	{
26
		switch ($this->driverName) {
27 28 29 30 31 32
			case 'mysql':
				return new MysqlQueryBuilder($this->getConnection());
			case 'sqlite':
				return new SqliteQueryBuilder($this->getConnection());
			case 'mssql':
				return new MssqlQueryBuilder($this->getConnection());
33 34
			case 'pgsql':
				return new PgsqlQueryBuilder($this->getConnection());
Carsten Brandt committed
35 36
			case 'cubrid':
				return new CubridQueryBuilder($this->getConnection());
37 38 39 40 41 42 43 44 45 46
		}
		throw new \Exception('Test is not implemented for ' . $this->driverName);
	}

	/**
	 * this is not used as a dataprovider for testGetColumnType to speed up the test
	 * when used as dataprovider every single line will cause a reconnect with the database which is not needed here
	 */
	public function columnTypes()
	{
Alexander Makarov committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
		return [
			[Schema::TYPE_PK, 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY'],
			[Schema::TYPE_PK . '(8)', 'int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY'],
			[Schema::TYPE_PK . ' CHECK (value > 5)', 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY CHECK (value > 5)'],
			[Schema::TYPE_PK . '(8) CHECK (value > 5)', 'int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY CHECK (value > 5)'],
			[Schema::TYPE_STRING, 'varchar(255)'],
			[Schema::TYPE_STRING . '(32)', 'varchar(32)'],
			[Schema::TYPE_STRING . ' CHECK (value LIKE "test%")', 'varchar(255) CHECK (value LIKE "test%")'],
			[Schema::TYPE_STRING . '(32) CHECK (value LIKE "test%")', 'varchar(32) CHECK (value LIKE "test%")'],
			[Schema::TYPE_STRING . ' NOT NULL', 'varchar(255) NOT NULL'],
			[Schema::TYPE_TEXT, 'text'],
			[Schema::TYPE_TEXT . '(255)', 'text'],
			[Schema::TYPE_TEXT . ' CHECK (value LIKE "test%")', 'text CHECK (value LIKE "test%")'],
			[Schema::TYPE_TEXT . '(255) CHECK (value LIKE "test%")', 'text CHECK (value LIKE "test%")'],
			[Schema::TYPE_TEXT . ' NOT NULL', 'text NOT NULL'],
			[Schema::TYPE_TEXT . '(255) NOT NULL', 'text NOT NULL'],
			[Schema::TYPE_SMALLINT, 'smallint(6)'],
			[Schema::TYPE_SMALLINT . '(8)', 'smallint(8)'],
			[Schema::TYPE_INTEGER, 'int(11)'],
			[Schema::TYPE_INTEGER . '(8)', 'int(8)'],
			[Schema::TYPE_INTEGER . ' CHECK (value > 5)', 'int(11) CHECK (value > 5)'],
			[Schema::TYPE_INTEGER . '(8) CHECK (value > 5)', 'int(8) CHECK (value > 5)'],
			[Schema::TYPE_INTEGER . ' NOT NULL', 'int(11) NOT NULL'],
			[Schema::TYPE_BIGINT, 'bigint(20)'],
			[Schema::TYPE_BIGINT . '(8)', 'bigint(8)'],
			[Schema::TYPE_BIGINT . ' CHECK (value > 5)', 'bigint(20) CHECK (value > 5)'],
			[Schema::TYPE_BIGINT . '(8) CHECK (value > 5)', 'bigint(8) CHECK (value > 5)'],
			[Schema::TYPE_BIGINT . ' NOT NULL', 'bigint(20) NOT NULL'],
			[Schema::TYPE_FLOAT, 'float'],
			[Schema::TYPE_FLOAT . '(16,5)', 'float'],
			[Schema::TYPE_FLOAT . ' CHECK (value > 5.6)', 'float CHECK (value > 5.6)'],
			[Schema::TYPE_FLOAT . '(16,5) CHECK (value > 5.6)', 'float CHECK (value > 5.6)'],
			[Schema::TYPE_FLOAT . ' NOT NULL', 'float NOT NULL'],
			[Schema::TYPE_DECIMAL, 'decimal(10,0)'],
			[Schema::TYPE_DECIMAL . '(12,4)', 'decimal(12,4)'],
			[Schema::TYPE_DECIMAL . ' CHECK (value > 5.6)', 'decimal(10,0) CHECK (value > 5.6)'],
			[Schema::TYPE_DECIMAL . '(12,4) CHECK (value > 5.6)', 'decimal(12,4) CHECK (value > 5.6)'],
			[Schema::TYPE_DECIMAL . ' NOT NULL', 'decimal(10,0) NOT NULL'],
			[Schema::TYPE_DATETIME, 'datetime'],
			[Schema::TYPE_DATETIME . " CHECK(value BETWEEN '2011-01-01' AND '2013-01-01')", "datetime CHECK(value BETWEEN '2011-01-01' AND '2013-01-01')"],
			[Schema::TYPE_DATETIME . ' NOT NULL', 'datetime NOT NULL'],
			[Schema::TYPE_TIMESTAMP, 'timestamp'],
			[Schema::TYPE_TIMESTAMP . " CHECK(value BETWEEN '2011-01-01' AND '2013-01-01')", "timestamp CHECK(value BETWEEN '2011-01-01' AND '2013-01-01')"],
			[Schema::TYPE_TIMESTAMP . ' NOT NULL', 'timestamp NOT NULL'],
			[Schema::TYPE_TIME, 'time'],
			[Schema::TYPE_TIME . " CHECK(value BETWEEN '12:00:00' AND '13:01:01')", "time CHECK(value BETWEEN '12:00:00' AND '13:01:01')"],
			[Schema::TYPE_TIME . ' NOT NULL', 'time NOT NULL'],
			[Schema::TYPE_DATE, 'date'],
			[Schema::TYPE_DATE . " CHECK(value BETWEEN '2011-01-01' AND '2013-01-01')", "date CHECK(value BETWEEN '2011-01-01' AND '2013-01-01')"],
			[Schema::TYPE_DATE . ' NOT NULL', 'date NOT NULL'],
			[Schema::TYPE_BINARY, 'blob'],
			[Schema::TYPE_BOOLEAN, 'tinyint(1)'],
			[Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT 1', 'tinyint(1) NOT NULL DEFAULT 1'],
			[Schema::TYPE_MONEY, 'decimal(19,4)'],
			[Schema::TYPE_MONEY . '(16,2)', 'decimal(16,2)'],
			[Schema::TYPE_MONEY . ' CHECK (value > 0.0)', 'decimal(19,4) CHECK (value > 0.0)'],
			[Schema::TYPE_MONEY . '(16,2) CHECK (value > 0.0)', 'decimal(16,2) CHECK (value > 0.0)'],
			[Schema::TYPE_MONEY . ' NOT NULL', 'decimal(19,4) NOT NULL'],
		];
106 107 108 109 110
	}

	public function testGetColumnType()
	{
		$qb = $this->getQueryBuilder();
111
		foreach ($this->columnTypes() as $item) {
112 113 114 115
			list ($column, $expected) = $item;
			$this->assertEquals($expected, $qb->getColumnType($column));
		}
	}
116

117
	public function testAddDropPrimaryKey()
118 119 120 121 122 123
	{
		$tableName = 'tbl_constraints';
		$pkeyName = $tableName . "_pkey";
		
		// ADD
		$qb = $this->getQueryBuilder();
Alexander Makarov committed
124
		$qb->db->createCommand()->addPrimaryKey($pkeyName, $tableName, ['id'])->execute();
125 126 127 128
		$tableSchema = $qb->db->getSchema()->getTableSchema($tableName);
		$this->assertEquals(1, count($tableSchema->primaryKey));

		//DROP
129
		$qb->db->createCommand()->dropPrimaryKey($pkeyName, $tableName)->execute();
130 131
		$qb = $this->getQueryBuilder(); // resets the schema
		$tableSchema = $qb->db->getSchema()->getTableSchema($tableName);
Alexander Makarov committed
132
		$this->assertEquals(0, count($tableSchema->primaryKey));
133
	}
134

Qiang Xue committed
135
	/* qiangxue: the following tests are commented because they vary by different DB drivers. need a better test scheme.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	public function testBuildWhereExists()
	{
		$expectedQuerySql = "SELECT `id` FROM `TotalExample` `t` WHERE EXISTS (SELECT `1` FROM `Website` `w`)";
		$expectedQueryParams = null;

		$subQuery = new Query();
		$subQuery->select('1')
			->from('Website w');

		$query = new Query();
		$query->select('id')
			->from('TotalExample t')
			->where(['exists', $subQuery]);

		list($actualQuerySql, $actualQueryParams) = $this->getQueryBuilder()->build($query);
		$this->assertEquals($expectedQuerySql, $actualQuerySql);
		$this->assertEquals($expectedQueryParams, $actualQueryParams);
	}

	public function testBuildWhereNotExists()
	{
		$expectedQuerySql = "SELECT `id` FROM `TotalExample` `t` WHERE NOT EXISTS (SELECT `1` FROM `Website` `w`)";
		$expectedQueryParams = null;

		$subQuery = new Query();
		$subQuery->select('1')
			->from('Website w');

		$query = new Query();
		$query->select('id')
			->from('TotalExample t')
			->where(['not exists', $subQuery]);

		list($actualQuerySql, $actualQueryParams) = $this->getQueryBuilder()->build($query);
		$this->assertEquals($expectedQuerySql, $actualQuerySql);
		$this->assertEquals($expectedQueryParams, $actualQueryParams);
	}

	public function testBuildWhereExistsWithParameters()
	{
		$expectedQuerySql = "SELECT `id` FROM `TotalExample` `t` WHERE (EXISTS (SELECT `1` FROM `Website` `w` WHERE (w.id = t.website_id) AND (w.merchant_id = :merchant_id))) AND (t.some_column = :some_value)";
		$expectedQueryParams = [':some_value' => "asd", ':merchant_id' => 6];

		$subQuery = new Query();
		$subQuery->select('1')
			->from('Website w')
			->where('w.id = t.website_id')
			->andWhere('w.merchant_id = :merchant_id', [':merchant_id' => 6]);

		$query = new Query();
		$query->select('id')
			->from('TotalExample t')
			->where(['exists', $subQuery])
			->andWhere('t.some_column = :some_value', [':some_value' => "asd"]);

		list($actualQuerySql, $queryParams) = $this->getQueryBuilder()->build($query);
		$this->assertEquals($expectedQuerySql, $actualQuerySql);
		$this->assertEquals($expectedQueryParams, $queryParams);
	}

	public function testBuildWhereExistsWithArrayParameters()
	{
		$expectedQuerySql = "SELECT `id` FROM `TotalExample` `t` WHERE (EXISTS (SELECT `1` FROM `Website` `w` WHERE (w.id = t.website_id) AND ((`w`.`merchant_id`=:qp0) AND (`w`.`user_id`=:qp1)))) AND (`t`.`some_column`=:qp2)";
		$expectedQueryParams = [':qp0' => 6, ':qp1' => 210, ':qp2' => 'asd'];

		$subQuery = new Query();
		$subQuery->select('1')
			->from('Website w')
			->where('w.id = t.website_id')
			->andWhere(['w.merchant_id' => 6, 'w.user_id' => '210']);

		$query = new Query();
		$query->select('id')
			->from('TotalExample t')
			->where(['exists', $subQuery])
			->andWhere(['t.some_column' => "asd"]);

		list($actualQuerySql, $queryParams) = $this->getQueryBuilder()->build($query);
		$this->assertEquals($expectedQuerySql, $actualQuerySql);
		$this->assertEquals($expectedQueryParams, $queryParams);
	}
Qiang Xue committed
217
	*/
218 219 220 221
	
	/*
	This test contains three select queries connected with UNION and UNION ALL constructions.
	It could be useful to use "phpunit --group=db --filter testBuildUnion" command for run it.
222
	
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	 public function testBuildUnion()
	 {
	 	$expectedQuerySql = "SELECT `id` FROM `TotalExample` `t1` WHERE (w > 0) AND (x < 2) UNION ( SELECT `id` FROM `TotalTotalExample` `t2` WHERE w > 5 ) UNION ALL ( SELECT `id` FROM `TotalTotalExample` `t3` WHERE w = 3 )";
    		$query = new Query();
    		$secondQuery = new Query();
    		$secondQuery->select('id')
      			->from('TotalTotalExample t2')
      			->where('w > 5');
    		$thirdQuery = new Query();
    		$thirdQuery->select('id')
      			->from('TotalTotalExample t3')
      			->where('w = 3');
    		$query->select('id')
      			->from('TotalExample t1')
      			->where(['and', 'w > 0', 'x < 2'])
      			->union($secondQuery)
      			->union($thirdQuery, TRUE);
    		list($actualQuerySql, $queryParams) = $this->getQueryBuilder()->build($query);
    		$this->assertEquals($expectedQuerySql, $actualQuerySql);
242
  	 }*/
243

244
}