Skip to content

Commit

Permalink
#44: extended COL and COLS
Browse files Browse the repository at this point in the history
  • Loading branch information
vasa-c committed Mar 27, 2016
1 parent 95f1bd5 commit c539f17
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 28 deletions.
70 changes: 42 additions & 28 deletions goDB/Helpers/Templater.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private function replacementT($value, array $modifiers)

if (isset($value[0])) {
$value = array(
'db' => $value[0],
'db' => $value[0],
'table' => $value[1],
);
}
Expand All @@ -280,40 +280,54 @@ private function replacementT($value, array $modifiers)
*/
private function replacementC($value, array $modifiers)
{
if (is_array($value)) {
if (isset($value[0])) {
$value = array(
'table' => $value[0],
'col' => $value[1],
);
if (!is_array($value)) {
return $this->implementation->reprCol($this->connection, $value);
}
if (isset($value[0])) {
if ($this->prefix !== null) {
$t = count($value) - 2;
if (isset($value[$t])) {
$value[$t] = $this->prefix . $value[$t];
}
}
if (isset($value['col'])) {
$chain = array($value['col']);
if (isset($value['table'])) {
$chain[] = $this->prefix.$value['table'];
if (isset($value['db'])) {
$chain[] = $value['db'];
return $this->implementation->reprChainFields($this->connection, $value);
}
if (isset($value['col'])) {
$chain = [];
foreach (['db', 'table', 'col'] as $f) {
if (isset($value[$f])) {
if (is_array($value[$f])) {
$chain = array_merge($chain, $value[$f]);
} else {
$chain[] = $value[$f];
}
}
$chain = array_reverse($chain);
$result = $this->implementation->reprChainFields($this->connection, $chain);
} elseif (isset($value['value'])) {
if (is_int($value['value'])) {
$result = $this->implementation->reprInt($this->connection, $value['value']);
} else {
$result = $this->implementation->reprString($this->connection, $value['value']);
}
} else {
throw new DataInvalidFormat('col', 'required `col` or `value` field');
}
if (isset($value['func'])) {
$result = $value['func'].'('.$result.')';
if ($this->prefix !== null) {
$t = count($chain) - 2;
if (isset($chain[$t])) {
$chain[$t] = $this->prefix . $chain[$t];
}
}
if (isset($value['as'])) {
$result .= ' AS '.$this->implementation->reprCol($this->connection, $value['as']);
$result = $this->implementation->reprChainFields($this->connection, $chain);
} elseif (isset($value['value'])) {
if (is_int($value['value'])) {
$result = $this->implementation->reprInt($this->connection, $value['value']);
} else {
$result = $this->implementation->reprString($this->connection, $value['value']);
}
$value['value'] = null;
} else {
$result = $this->implementation->reprCol($this->connection, $value);
throw new DataInvalidFormat('col', 'required `col` or `value` field');
}
if (isset($value['func'])) {
$result = $value['func'].'('.$result.')';
}
if (isset($value['value'])) {
$result .= (($value['value'] > 0) ? '+' : '').$value['value'];
}
if (isset($value['as'])) {
$result .= ' AS '.$this->implementation->reprCol($this->connection, $value['as']);
}
return $result;
}
Expand Down
138 changes: 138 additions & 0 deletions tests/Helpers/Templater/ColTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/**
* @package go\DB
* @subpackage Tests
*/

namespace go\Tests\DB\Helpers\Templater;

/**
* coversDefaultClass go\DB\Helpers\Templater
* @author Oleg Grigoriev <go.vasac@gmail.com>
*/
final class ColTest extends Base
{
/**
* {@inheritdoc}
*/
public function providerTemplater()
{
return [
'string' => [
'SELECT ?c',
['id'],
'SELECT `id`',
],
'list' => [
'SELECT ?c',
[
['one', 'two', 'three', 'four']
],
'SELECT `one`.`two`.`three`.`four`',
],
'a_string' => [
'SELECT ?c',
[
[
'col' => 'id'
],
],
'SELECT `id`',
],
'a_list' => [
'SELECT ?c',
[
[
'col' => ['a', 'b'],
],
],
'SELECT `a`.`b`',
],
'a_merge' => [
'SELECT ?c',
[
[
'col' => ['a', 'b'],
'table' => 'c',
'db' => ['d', 'e'],
],
],
'SELECT `d`.`e`.`c`.`a`.`b`',
],
'func' => [
'SELECT ?c',
[
[
'col' => ['a', 'b'],
'func' => 'SUM',
],
],
'SELECT SUM(`a`.`b`)',
],
'value' => [
'SELECT ?c',
[
[
'col' => 'col',
'value' => 1,
],
],
'SELECT `col`+1',
],
'as' => [
'SELECT ?c',
[
[
'col' => 'col',
'as' => 'id',
],
],
'SELECT `col` AS `id`',
],
'mixed' => [
'SELECT ?c',
[
[
'db' => 'dbname',
'table' => 'test',
'col' => 'value',
'func' => 'COUNT',
'value' => -3,
'as' => 'q',
],
],
'SELECT COUNT(`dbname`.`test`.`value`)-3 AS `q`',
],
'only_value' => [
'SELECT ?c',
[
[
'value' => 2,
'as' => 'two',
],
],
'SELECT 2 AS `two`',
],
'prefix_list' => [
'SELECT ?c',
[
['a', 'b', 'c']
],
'SELECT `a`.`prefix_b`.`c`',
'prefix_',
],
'prefix_table' => [
'SELECT ?c',
[
[
'col' => 'a',
'table' => ['b', 'c'],
'db' => 'd',
],
],
'SELECT `d`.`b`.`prefix_c`.`a`',
'prefix_',
],
];
}
}
39 changes: 39 additions & 0 deletions tests/Helpers/Templater/ColsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* @package go\DB
* @subpackage Tests
*/

namespace go\Tests\DB\Helpers\Templater;

/**
* coversDefaultClass go\DB\Helpers\Templater
* @author Oleg Grigoriev <go.vasac@gmail.com>
*/
final class ColsTest extends Base
{
/**
* {@inheritdoc}
*/
public function providerTemplater()
{
return [
'cols' => [
'SELECT ?cols',
[
[
'id',
['a', 'b'],
[
'col' => 'value',
'func' => 'SUM',
'as' => 's',
],
],
],
'SELECT `id`,`p_a`.`b`,SUM(`value`) AS `s`',
'p_'
],
];
}
}

0 comments on commit c539f17

Please sign in to comment.