Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DEFAULT parens wrapping #84

Merged
merged 2 commits into from
Jun 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/DBSteward/sql_format/pgsql8/pgsql8.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ public static function column_value_default($node_schema, $node_table, $data_col
}
// don't esacape columns marked literal sql values
else if (isset($node_col['sql']) && strcasecmp($node_col['sql'], 'true') == 0) {
$value = '(' . $node_col . ')';
if (strcasecmp($node_col, 'default') === 0) {
$value = 'DEFAULT';
} else {
$value = '(' . $node_col . ')';
}
}
// else if col is zero length, make it default, or DB NULL
else if (strlen($node_col) == 0) {
Expand Down
97 changes: 97 additions & 0 deletions tests/pgsql8/ColumnValueDefaultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* Tests functionality of pgsql8::column_value_default()
*
* @package DBSteward
* @license http://www.opensource.org/licenses/bsd-license.php Simplified BSD License
* @author Austin Hyde <austin109@gmail.com>
*/

require_once __DIR__ . '/../dbstewardUnitTestBase.php';

/**
* @group pgsql8
*/
class ColumnValueDefaultTest extends PHPUnit_Framework_TestCase {
public function setUp() {
dbsteward::set_sql_format('pgsql8');
}

public function testNullColumnReturnsNull() {
$value = $this->getColumnValue(
'<column name="foo" type="text"/>',
'<col null="true">adsf</col>');

$this->assertEquals('NULL', $value, 'Expected NULL if null="true" is specified');
}

public function testEmptyColumnGivesEmptyString() {
$value = $this->getColumnValue(
'<column name="foo" type="text"/>',
'<col empty="true">asdf</col>');

$expected = pgsql8::E_ESCAPE ? "E''" : "''";

$this->assertEquals($expected, $value, "Expected $expected if empty=\"true\" is specified");
}

public function testSQLGivesLiteralButWrapped() {
$value = $this->getColumnValue(
'<column name="foo" type="text"/>',
'<col sql="true">some_function()</col>');

$this->assertEquals('(some_function())', $value, 'Expected literal column value wrapped in parens if sql="true"');
}

public function testSQLWithDefaultDoesNotWrap() {
$value = $this->getColumnValue(
'<column name="foo" type="text"/>',
'<col sql="true">DEFAULT</col>');

$this->assertEquals('DEFAULT', $value, 'Expected DEFAULT without wrapping parens if sql="true" and column value is DEFAULT');
}

public function testUsesColumnDefaultIfEmpty() {
$value = $this->getColumnValue(
'<column name="foo" type="text" default="asdf"/>',
'<col></col>');

$this->assertEquals('asdf', $value, 'Expected column default if data was empty');
}

public function testUsesLiteralForInt() {
$value = $this->getColumnValue(
'<column name="foo" type="int"/>',
'<col>42</col>');

$this->assertEquals('42', $value, 'Expected literal int value for integers');
}

public function testQuotesStrings() {
$value = $this->getColumnValue(
'<column name="foo" type="text"/>',
'<col>asdf</col>');

$expected = pgsql8::E_ESCAPE ? "E'asdf'" : "'asdf'";
$this->assertEquals($expected, $value, "Expected $expected for a string value");
}

private function getColumnValue($def, $data) {
$defNode = simplexml_load_string($def);
$defNodeName = $defNode['name'];

$schemaXml = <<<XML
<schema name="test_schema">
<table name="test_table" primaryKey="$defNodeName">
$def
<rows columns="$defNodeName">
<row>$data</row>
</rows>
</table>
</schema>
XML;

$schema = simplexml_load_string($schemaXml);
return pgsql8::column_value_default($schema, $schema->table, $defNodeName, $schema->table->rows->row->col);
}
}