Skip to content

Commit

Permalink
Merge pull request #8 from sartor/master
Browse files Browse the repository at this point in the history
Column builder
  • Loading branch information
bashkarev authored Aug 15, 2018
2 parents 3b3cd3e + dbcc10d commit 8fe6ba7
Show file tree
Hide file tree
Showing 5 changed files with 401 additions and 2 deletions.
96 changes: 96 additions & 0 deletions ColumnSchemaBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace bashkarev\clickhouse;

use yii\db\ColumnSchemaBuilder as AbstractColumnSchemaBuilder;

/**
* ColumnSchemaBuilder is the schema builder for ClickHouse databases.
*
* @author Sartor <[email protected]>
*/
class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder
{
/**
* @var bool whether the column is or not nullable.
* If this is `false`, a `Nullable` type wrapper will be added.
*/
protected $isNotNull = true;

/**
* {@inheritdoc}
*/
public function defaultValue($default)
{
$this->default = $default;
return $this;
}

/**
* {@inheritdoc}
*/
protected function buildNotNullString()
{
if ($this->isNotNull !== true) {
return ' NULL';
}

return '';
}

/**
* {@inheritdoc}
*/
protected function buildUnsignedString()
{
return $this->isUnsigned ? 'U' : '';
}

/**
* {@inheritdoc}
*/
protected function buildAfterString()
{
return $this->after !== null ?
' AFTER ' . $this->db->quoteColumnName($this->after) :
'';
}

/**
* {@inheritdoc}
*/
protected function buildFirstString()
{
return $this->isFirst ? ' FIRST' : '';
}

/**
* {@inheritdoc}
*/
protected function buildDefaultString()
{
$result = parent::buildDefaultString();

if ($this->default === null && $this->isNotNull === false) {
return '';
}

return $result;
}

/**
* {@inheritdoc}
*/
public function __toString()
{
switch ($this->getTypeCategory()) {
case self::CATEGORY_NUMERIC:
$format = "{unsigned}{type}{notnull}{default}{append}{pos}";
break;
default:
$format = "{type}{length}{notnull}{default}{check}{append}{pos}";
}

return $this->buildCompleteString($format);
}
}
34 changes: 34 additions & 0 deletions QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,33 @@
*/
class QueryBuilder extends \yii\db\QueryBuilder
{
/**
* @var array mapping from abstract column types (keys) to physical column types (values).
*/
public $typeMap = [
Schema::TYPE_CHAR => 'FixedString(1)',
Schema::TYPE_STRING => 'String',
Schema::TYPE_TEXT => 'String',
Schema::TYPE_TINYINT => 'Int8',
Schema::TYPE_SMALLINT => 'Int16',
Schema::TYPE_INTEGER => 'Int32',
Schema::TYPE_BIGINT => 'Int64',
'U'.Schema::TYPE_TINYINT => 'UInt8',
'U'.Schema::TYPE_SMALLINT => 'UInt16',
'U'.Schema::TYPE_INTEGER => 'UInt32',
'U'.Schema::TYPE_BIGINT => 'UInt64',
Schema::TYPE_FLOAT => 'Float32',
Schema::TYPE_DOUBLE => 'Float64',
Schema::TYPE_DECIMAL => 'Float64',
Schema::TYPE_DATETIME => 'DateTime',
Schema::TYPE_TIMESTAMP => 'DateTime',
Schema::TYPE_TIME => 'DateTime',
Schema::TYPE_DATE => 'Date',
Schema::TYPE_BINARY => 'String',
Schema::TYPE_BOOLEAN => 'UInt8',
Schema::TYPE_MONEY => 'Float64',
Schema::TYPE_JSON => 'String'
];

/**
* @inheritdoc
Expand All @@ -26,6 +53,13 @@ public function createTable($table, $columns, $options = null)
return parent::createTable($table, $columns, $options);
}


public function getColumnType($type)
{
// Replacing NULL to Nullable() wrapper
return preg_replace('/^(\w+)(\(\d+\))? NULL(.*)$/i', 'Nullable(\1\2)\3', parent::getColumnType($type));
}

/**
* @inheritdoc
*/
Expand Down
8 changes: 8 additions & 0 deletions Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,12 @@ public function createQueryBuilder()
{
return new QueryBuilder($this->db);
}

/**
* {@inheritdoc}
*/
public function createColumnSchemaBuilder($type, $length = null)
{
return new ColumnSchemaBuilder($type, $length, $this->db);
}
}
4 changes: 2 additions & 2 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testQuery()
$this->assertInstanceOf(\Generator::class, $reader);

// queryAll
$rows = $db->createCommand('SELECT * FROM {{customer}}')->queryAll();
$rows = $db->createCommand('SELECT * FROM {{customer}} ORDER BY [[id]]')->queryAll();
$this->assertCount(3, $rows);
$row = $rows[2];
$this->assertEquals(3, $row['id']);
Expand All @@ -60,7 +60,7 @@ public function testQuery()
$command = $db->createCommand($sql);
$this->assertFalse($command->queryOne());
// queryColumn
$sql = 'SELECT * FROM {{customer}}';
$sql = 'SELECT * FROM {{customer}} ORDER BY [[id]]';
$column = $db->createCommand($sql)->queryColumn();
$this->assertEquals(range(1, 3), $column);
$command = $db->createCommand('SELECT [[id]] FROM {{customer}} WHERE [[id]] = 10');
Expand Down
Loading

0 comments on commit 8fe6ba7

Please sign in to comment.