Skip to content

Commit

Permalink
Merge pull request #62 from digitalmoon/master
Browse files Browse the repository at this point in the history
Represent placeholder data according to its php type #61
  • Loading branch information
vasa-c committed Dec 20, 2016
2 parents 2cf8078 + 0609fc9 commit d86c50d
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 29 deletions.
1 change: 1 addition & 0 deletions goDB/Compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static function getOpt($key, $default = null)
*/
private static $opts = array(
'null' => true,
'types' => true,
);

/**
Expand Down
37 changes: 34 additions & 3 deletions goDB/Helpers/Templater.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace go\DB\Helpers;

use go\DB\Compat;
use go\DB\Exceptions\DataMuch;
use go\DB\Exceptions\DataNotEnough;
use go\DB\Exceptions\DataNamed;
Expand Down Expand Up @@ -146,8 +147,10 @@ protected function placeholderClb($matches)
*/
private function valueModification($value, array $modifiers)
{
if ($modifiers['n'] && is_null($value)) {
return $this->implementation->reprNULL($this->connection);
if (is_null($value)) {
if ($modifiers['n'] || Compat::getOpt('types')) {
return $this->implementation->reprNULL($this->connection);
}
}
if ($modifiers['i']) {
return $this->implementation->reprInt($this->connection, $value);
Expand All @@ -156,11 +159,21 @@ private function valueModification($value, array $modifiers)
} elseif ($modifiers['b']) {
return $this->implementation->reprBool($this->connection, $value);
}
if (Compat::getOpt('types')) {
$type = gettype($value);
if ($type === 'integer') {
return $this->implementation->reprInt($this->connection, $value);
} elseif ($type === 'double') {
return $this->implementation->reprFloat($this->connection, $value);
} elseif ($type === 'boolean') {
return $this->implementation->reprBool($this->connection, $value);
}
}
return $this->implementation->reprString($this->connection, $value);
}

/**
* ?, ?string, ?scalar
* ?, ?scalar
*
* @param mixed $value
* @param array $modifiers
Expand All @@ -173,6 +186,24 @@ protected function replacement($value, array $modifiers)
}
return $this->valueModification($value, $modifiers);
}

/**
* ?string
* @param mixed $value
* @param array $modifiers
* @throws DataInvalidFormat
* @return string
*/
protected function replacementSTRING($value, array $modifiers)
{
if (is_array($value)) {
throw new DataInvalidFormat('', 'required scalar given');
}
if (($modifiers['n'] || Compat::getOpt('types')) && is_null($value)) {
return $this->implementation->reprNULL($this->connection);
}
return $this->implementation->reprString($this->connection, $value);
}

/**
* ?l, ?list
Expand Down
8 changes: 8 additions & 0 deletions goDB/Implementations/Pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ public function reprString($connection, $value)
return '\'' . $this->escapeString($connection, $value) . '\'';
}

/**
* {@inheritdoc}
*/
public function reprBool($connection, $value)
{
return $value ? 'true' : 'false';
}

/**
* {@inheritdoc}
*/
Expand Down
2 changes: 1 addition & 1 deletion goDB/_config/placeholders.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/* The list of long synonyms */
'longs' => array(
'string' => '',
'string' => 'string',
'scalar' => '',
'list' => 'l',
'set' => 's',
Expand Down
27 changes: 22 additions & 5 deletions tests/CompatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,34 @@ public function tearDown()
{
parent::tearDown();
Compat::setOpt('null', true);
Compat::setOpt('types', true);
}

public function testSetOpt()
{
$db = DB::create(['host' => 'localhost'], 'test');
$pattern = 'INSERT ?, ?i, ?n, ?in, ?s, ?sn';
$data = [null, null, null, null, ['x' => null], ['y' => null]];
$expectedDef = 'INSERT NULL, NULL, NULL, NULL, `x`=NULL, `y`=NULL';
$pattern = 'INSERT ?, ?string, ?, ?i, ?n, ?in, ?s, ?sn';
$data = [2, null, null, null, null, null, ['x' => null], ['y' => null]];
$expectedDef = 'INSERT 2, NULL, NULL, NULL, NULL, NULL, `x`=NULL, `y`=NULL';
$this->assertSame($expectedDef, $db->makeQuery($pattern, $data));
Compat::setOpt('null', false);
$expectedOld = 'INSERT "", 0, NULL, NULL, `x`="", `y`=NULL';
Compat::setOpt('types', false);
$expectedOld = 'INSERT "2", "", "", 0, NULL, NULL, `x`="", `y`=NULL';
$this->assertSame($expectedOld, $db->makeQuery($pattern, $data));
Compat::setOpt('null', true);
Compat::setOpt('types', false);
$this->assertSame(
'INSERT "2", NULL, NULL, NULL, NULL, NULL, `x`=NULL, `y`=NULL',
$db->makeQuery($pattern, $data)
);
Compat::setOpt('null', false);
Compat::setOpt('types', true);
$this->assertSame(
'INSERT 2, NULL, NULL, NULL, NULL, NULL, `x`=NULL, `y`=NULL',
$db->makeQuery($pattern, $data)
);
Compat::setOpt('null', true);
Compat::setOpt('types', true);
$this->assertSame($expectedDef, $db->makeQuery($pattern, $data));
}

Expand All @@ -57,13 +72,15 @@ public function testTableSet()
public function testSysParamCompat()
{
$db = DB::create(['host' => 'localhost'], 'test');
$dbO = DB::create(['host' => 'localhost', '_compat' => ['null' => false]], 'test');
$dbO = DB::create(['host' => 'localhost', '_compat' => ['null' => false, 'types' => false]], 'test');
$this->assertSame('NULL', $db->makeQuery('?', [null]));
$this->assertSame('""', $dbO->makeQuery('?', [null]));
Compat::setOpt('null', false);
Compat::setOpt('types', false);
$this->assertSame('""', $db->makeQuery('?', [null]));
$this->assertSame('""', $dbO->makeQuery('?', [null]));
Compat::setOpt('null', true);
Compat::setOpt('types', true);
$this->assertSame('NULL', $db->makeQuery('?', [null]));
$this->assertSame('""', $dbO->makeQuery('?', [null]));
}
Expand Down
1 change: 0 additions & 1 deletion tests/Helpers/Fetchers/ArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ public function testObjects()
25 => (object)array('id' => 3, 'a' => 25, 'b' => null),
);
$this->assertEquals($expected, $this->fetcher->objects('a'));

}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Helpers/ParserPHTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function providerParse()
array('c', 'c', 'n'),
array('e', 'e', 'n'),
array('q', 'q', 'n'),
array('string', '', 'n'),
array('string', 'string', 'n'),
array('scalar', '', 'n'),
array('list', 'l', 'n'),
array('set', 's', 'n'),
Expand Down
6 changes: 3 additions & 3 deletions tests/Helpers/Templater/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ public function providerTemplater()
'short' => [
'INSERT INTO `table` VALUES (?l)',
[$list],
'INSERT INTO `table` VALUES ("str\"ing", "1", NULL, "3.5")',
'INSERT INTO `table` VALUES ("str\"ing", 1, NULL, "3.5")',
],
'full' => [
'INSERT INTO `table` VALUES (?list)',
[$list],
'INSERT INTO `table` VALUES ("str\"ing", "1", NULL, "3.5")',
'INSERT INTO `table` VALUES ("str\"ing", 1, NULL, "3.5")',
],
'null' => [
'INSERT INTO `table` VALUES (?ln)',
[$list],
'INSERT INTO `table` VALUES ("str\"ing", "1", NULL, "3.5")',
'INSERT INTO `table` VALUES ("str\"ing", 1, NULL, "3.5")',
],
'int' => [
'INSERT INTO `table` VALUES (?li)',
Expand Down
37 changes: 26 additions & 11 deletions tests/Helpers/Templater/ScalarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,47 @@ final class ScalarTest extends Base
*/
public function providerTemplater()
{
$data = ['str"ing', 1, null, '3.5'];
$data = ['str"ing', 1, null, '3.5', 2.5];
return [
'escape' => [
'INSERT INTO `table` VALUES (?, ?scalar, ?, ?string)',
'INSERT INTO `table` VALUES (?, ?scalar, ?, ?string, ?)',
$data,
'INSERT INTO `table` VALUES ("str\"ing", "1", NULL, "3.5")',
'INSERT INTO `table` VALUES ("str\"ing", 1, NULL, "3.5", 2.5)',
],
'null' => [
'INSERT INTO `table` VALUES (?null, ?null, ?null, ?null)',
'INSERT INTO `table` VALUES (?null, ?null, ?null, ?null, ?null)',
$data,
'INSERT INTO `table` VALUES ("str\"ing", "1", NULL, "3.5")',
'INSERT INTO `table` VALUES ("str\"ing", 1, NULL, "3.5", 2.5)',
],
'int' => [
'INSERT INTO `table` VALUES (?i, ?i, ?i, ?i)',
'INSERT INTO `table` VALUES (?i, ?i, ?i, ?i, ?i)',
$data,
'INSERT INTO `table` VALUES (0, 1, NULL, 3)',
'INSERT INTO `table` VALUES (0, 1, NULL, 3, 2)',
],
'int-null' => [
'INSERT INTO `table` VALUES (?in, ?in, ?in, ?in)',
'INSERT INTO `table` VALUES (?in, ?in, ?in, ?in, ?in)',
$data,
'INSERT INTO `table` VALUES (0, 1, NULL, 3)',
'INSERT INTO `table` VALUES (0, 1, NULL, 3, 2)',
],
'float' => [
'INSERT INTO `table` VALUES (?f, ?f, ?f, ?f, ?f)',
$data,
'INSERT INTO `table` VALUES (0, 1, NULL, 3.5, 2.5)',
],
'string' => [
'INSERT INTO `table` VALUES (?string, ?string, ?string, ?string, ?string)',
$data,
'INSERT INTO `table` VALUES ("str\"ing", "1", NULL, "3.5", "2.5")',
],
'string-null' => [
'INSERT INTO `table` VALUES (?string-null, ?string-null, ?string-null, ?string-null, ?string-null)',
$data,
'INSERT INTO `table` VALUES ("str\"ing", "1", NULL, "3.5", "2.5")',
],
'full' => [
'INSERT INTO `table` VALUES (?string, ?scalar-int, ?scalar-null, ?scalar-int-null)',
'INSERT INTO `table` VALUES (?string, ?scalar-int, ?scalar-null, ?scalar-int-null, ?scalar-float)',
$data,
'INSERT INTO `table` VALUES ("str\"ing", 1, NULL, 3)',
'INSERT INTO `table` VALUES ("str\"ing", 1, NULL, 3, 2.5)',
],
];
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Helpers/Templater/ValuesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public function providerTemplater()
'values' => [
'INSERT INTO `table` VALUES ?values;',
[$values],
'INSERT INTO `table` VALUES ("0", "1", "2"), ("one", NULL, "three")',
'INSERT INTO `table` VALUES (0, 1, 2), ("one", NULL, "three")',
],
'null' => [
'INSERT INTO `table` VALUES ?vn',
[$values],
'INSERT INTO `table` VALUES ("0", "1", "2"), ("one", NULL, "three")',
'INSERT INTO `table` VALUES (0, 1, 2), ("one", NULL, "three")',
],
'int' => [
'INSERT INTO `table` VALUES ?values-int',
Expand Down
3 changes: 3 additions & 0 deletions tests/Real/Mysql/MysqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public function testDB()

$trickyName = 'qu`ote"d na\'me';
$this->assertEquals('str', $db->query("SELECT 'str' as ?c", array($trickyName))->el($trickyName));

$this->assertEquals('1', $db->makeQuery('?', array(true)));
$this->assertEquals('0', $db->makeQuery('?', array(false)));
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/Real/Mysqlold/MysqloldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public function testDB()

$trickyName = 'qu`ote"d na\'me';
$this->assertEquals('str', $db->query("SELECT 'str' as ?c", array($trickyName))->el($trickyName));

$this->assertEquals('1', $db->makeQuery('?', array(true)));
$this->assertEquals('0', $db->makeQuery('?', array(false)));
}

public function testErrorConnect()
Expand Down
3 changes: 3 additions & 0 deletions tests/Real/Pgsql/PgsqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public function testDB()

$trickyName = 'qu`ote"d na\'me';
$this->assertEquals('str', $db->query("SELECT 'str' as ?c", array($trickyName))->el($trickyName));

$this->assertEquals('true', $db->makeQuery('?', array(true)));
$this->assertEquals('false', $db->makeQuery('?', array(false)));
}

public function testErrorConnect()
Expand Down
3 changes: 3 additions & 0 deletions tests/Real/Sqlite/SqliteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public function testDB()

$trickyName = 'qu`ote"d na\'me';
$this->assertEquals('str', $db->query("SELECT 'str' as ?c", array($trickyName))->el($trickyName));

$this->assertEquals('1', $db->makeQuery('?', array(true)));
$this->assertEquals('0', $db->makeQuery('?', array(false)));
}

public function testErrorConnect()
Expand Down
2 changes: 0 additions & 2 deletions tests/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ public function testHighSelect()

$table->update(array('id' => $col), array('id'=>5));
$this->assertSame(1, $table->getCount(null, array('id' => 6)));


}

/**
Expand Down

0 comments on commit d86c50d

Please sign in to comment.