Commit f0ffcfaf authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge pull request #80 from FabioBatSilva/bit-comparison

bit comparison
parents 5a827d7c 728ef4a1
......@@ -786,6 +786,30 @@ abstract class AbstractPlatform
throw DBALException::notSupported(__METHOD__);
}
/**
* Gets SQL bit AND comparison expression
*
* @param string $value1
* @param string $value2
* @return string
*/
public function getBitAndComparisonExpression($value1, $value2)
{
return '(' . $value1 . ' & ' . $value2 . ')';
}
/**
* Gets SQL bit OR comparison expression
*
* @param string $value1
* @param string $value2
* @return string
*/
public function getBitOrComparisonExpression($value1, $value2)
{
return '(' . $value1 . ' | ' . $value2 . ')';
}
public function getForUpdateSQL()
{
return 'FOR UPDATE';
......
......@@ -116,26 +116,56 @@ class OraclePlatform extends AbstractPlatform
return "TRUNC(TO_NUMBER(SUBSTR((" . $date1 . "-" . $date2 . "), 1, INSTR(" . $date1 . "-" . $date2 .", ' '))))";
}
/**
* {@inheritdoc}
*/
public function getDateAddDaysExpression($date, $days)
{
return '(' . $date . '+' . $days . ')';
}
/**
* {@inheritdoc}
*/
public function getDateSubDaysExpression($date, $days)
{
return '(' . $date . '-' . $days . ')';
}
/**
* {@inheritdoc}
*/
public function getDateAddMonthExpression($date, $months)
{
return "ADD_MONTHS(" . $date . ", " . $months . ")";
}
/**
* {@inheritdoc}
*/
public function getDateSubMonthExpression($date, $months)
{
return "ADD_MONTHS(" . $date . ", -" . $months . ")";
}
/**
* {@inheritdoc}
*/
public function getBitAndComparisonExpression($value1, $value2)
{
return 'BITAND('.$value1 . ', ' . $value2 . ')';
}
/**
* {@inheritdoc}
*/
public function getBitOrComparisonExpression($value1, $value2)
{
return '(' . $value1 . '-' .
$this->getBitAndComparisonExpression($value1, $value2)
. '+' . $value2 . ')';
}
/**
* Gets the SQL used to create a sequence that starts with a given value
* and increments by the given allocation size.
......
......@@ -319,6 +319,57 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this->assertEquals(0, count($rows), "no result should be returned, otherwise SQL injection is possible");
}
/**
* @group DDC-1213
*/
public function testBitComparisonExpressionSupport()
{
$this->_conn->executeQuery('DELETE FROM fetch_table')->execute();
$platform = $this->_conn->getDatabasePlatform();
$bitmap = array();
for ($i = 2; $i < 9; $i = $i + 2) {
$bitmap[$i] = array(
'bit_or' => ($i | 2),
'bit_and' => ($i & 2)
);
$this->_conn->insert('fetch_table', array(
'test_int' => $i,
'test_string' => json_encode($bitmap[$i]),
'test_datetime' => '2010-01-01 10:10:10'
));
}
$sql[] = 'SELECT ';
$sql[] = 'test_int, ';
$sql[] = 'test_string, ';
$sql[] = $platform->getBitOrComparisonExpression('test_int', 2) . ' AS bit_or, ';
$sql[] = $platform->getBitAndComparisonExpression('test_int', 2) . ' AS bit_and ';
$sql[] = 'FROM fetch_table';
$stmt = $this->_conn->executeQuery(implode(PHP_EOL, $sql));
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$this->assertEquals(4, count($data));
$this->assertEquals(count($bitmap), count($data));
foreach ($data as $row) {
$this->assertArrayHasKey('test_int', $row);
$id = $row['test_int'];
$this->assertArrayHasKey($id, $bitmap);
$this->assertArrayHasKey($id, $bitmap);
$this->assertArrayHasKey('bit_or', $row);
$this->assertArrayHasKey('bit_and', $row);
$this->assertEquals($row['bit_or'], $bitmap[$id]['bit_or']);
$this->assertEquals($row['bit_and'], $bitmap[$id]['bit_and']);
}
}
public function testSetDefaultFetchMode()
{
$stmt = $this->_conn->query("SELECT * FROM fetch_table");
......
......@@ -131,6 +131,34 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
$this->assertEquals($this->getGenerateConstraintForeignKeySql(), $sql);
}
protected function getBitAndComparisonExpressionSql($value1, $value2)
{
return '(' . $value1 . ' & ' . $value2 . ')';
}
/**
* @group DDC-1213
*/
public function testGeneratesBitAndComparisonExpressionSql()
{
$sql = $this->_platform->getBitAndComparisonExpression(2, 4);
$this->assertEquals($this->getBitAndComparisonExpressionSql(2, 4), $sql);
}
protected function getBitOrComparisonExpressionSql($value1, $value2)
{
return '(' . $value1 . ' | ' . $value2 . ')';
}
/**
* @group DDC-1213
*/
public function testGeneratesBitOrComparisonExpressionSql()
{
$sql = $this->_platform->getBitOrComparisonExpression(2, 4);
$this->assertEquals($this->getBitOrComparisonExpressionSql(2, 4), $sql);
}
public function getGenerateConstraintUniqueIndexSql()
{
return 'ALTER TABLE test ADD CONSTRAINT constraint_name UNIQUE (test)';
......
......@@ -204,4 +204,16 @@ class OraclePlatformTest extends AbstractPlatformTestCase
"COMMENT ON COLUMN mytable.baz IS 'B comment'",
);
}
public function getBitAndComparisonExpressionSql($value1, $value2)
{
return 'BITAND('.$value1 . ', ' . $value2 . ')';
}
public function getBitOrComparisonExpressionSql($value1, $value2)
{
return '(' . $value1 . '-' .
$this->getBitAndComparisonExpressionSql($value1, $value2)
. '+' . $value2 . ')';
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment