Skip to content

Commit

Permalink
standardize terminology
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul M. Jones committed Jan 23, 2017
1 parent c0fd386 commit 7e2ed50
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 136 deletions.
4 changes: 2 additions & 2 deletions src/AbstractExtendedPdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,10 @@ public function prepareWithValues($statement, array $values = [])
$query = $queries[0];

// prepare the statement
$sth = $this->pdo->prepare($query->getString());
$sth = $this->pdo->prepare($query->getStatement());

// for the placeholders we found, bind the corresponding data values
foreach ($query->getParameters() as $key => $val) {
foreach ($query->getValues() as $key => $val) {
$this->bindValue($sth, $key, $val);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Parser/AbstractParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function rebuild($string, array $parameters = [])
$charset = 'UTF-8';

/** @var State $state */
$state = new State($query->getString(), $query->getParameters(), $charset);
$state = new State($query->getStatement(), $query->getValues(), $charset);

$last_check_index = -1;

Expand Down
34 changes: 17 additions & 17 deletions src/Parser/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,49 @@

/**
*
* A rebuilt query string, with the parameters to bind to it.
* A rebuilt query statement, with the values to bind to it.
*
* @package aura/sql
*
*/
class Query
{
/**
* @var string A query string
* @var string A query statement
*/
private $string = '';
private $statement = '';

/**
* @var array Parameters associated with a query
* @var array values associated with a query
*/
private $parameters = array();
private $values = array();

/**
* Query constructor.
* @param string $string
* @param array $parameters
* @param string $statement
* @param array $values
*/
public function __construct($string, $parameters = array())
public function __construct($statement, $values = array())
{
$this->string = $string;
$this->parameters = $parameters;
$this->statement = $statement;
$this->values = $values;
}

/**
* Returns a Query SQL string
* @return string
* Returns a Query SQL statement
* @return statement
*/
public function getString()
public function getStatement()
{
return $this->string;
return $this->statement;
}

/**
* Returns a Query parameters
* Returns a Query values
* @return array
*/
public function getParameters()
public function getValues()
{
return $this->parameters;
return $this->values;
}
}
116 changes: 58 additions & 58 deletions tests/Parser/MysqlParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,65 +33,65 @@ public function testReplaceMultipleUsesOfNamedParameter()
$parameters = array('foo' => 'bar');
$sql = "SELECT :foo AS a, :foo AS b";
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$expectedSql = "SELECT :foo AS a, :foo_0 AS b";
$expectedParameters = array('foo' => 'bar', 'foo_0' => 'bar');
$this->assertEquals($expectedSql, $parsedQuery->getString());
$this->assertEquals($expectedParameters, $parsedQuery->getParameters());
$expectedStatement = "SELECT :foo AS a, :foo_0 AS b";
$expectedValues = array('foo' => 'bar', 'foo_0' => 'bar');
$this->assertEquals($expectedStatement, $parsedQuery->getStatement());
$this->assertEquals($expectedValues, $parsedQuery->getValues());
}

public function testReplaceNumberedParameter()
{
$parameters = array('bar', 'baz');
$sql = "SELECT ? AS a, ? AS b";
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$expectedSql = "SELECT :__numbered AS a, :__numbered_0 AS b";
$expectedParameters = array('__numbered' => 'bar', '__numbered_0' => 'baz');
$this->assertEquals($expectedSql, $parsedQuery->getString());
$this->assertEquals($expectedParameters, $parsedQuery->getParameters());
$expectedStatement = "SELECT :__numbered AS a, :__numbered_0 AS b";
$expectedValues = array('__numbered' => 'bar', '__numbered_0' => 'baz');
$this->assertEquals($expectedStatement, $parsedQuery->getStatement());
$this->assertEquals($expectedValues, $parsedQuery->getValues());
}

public function testReplaceArrayAsParameter()
{
$parameters = array('foo' => array('bar', 'baz'));
$sql = "SELECT :foo";
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$expectedSql = "SELECT :foo, :foo_0";
$expectedParameters = array('foo' => 'bar', 'foo_0' => 'baz');
$this->assertEquals($expectedSql, $parsedQuery->getString());
$this->assertEquals($expectedParameters, $parsedQuery->getParameters());
$expectedStatement = "SELECT :foo, :foo_0";
$expectedValues = array('foo' => 'bar', 'foo_0' => 'baz');
$this->assertEquals($expectedStatement, $parsedQuery->getStatement());
$this->assertEquals($expectedValues, $parsedQuery->getValues());

$parameters = array(array('bar', 'baz'));
$sql = "SELECT ?";
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$expectedSql = "SELECT :__numbered, :__numbered_0";
$expectedParameters = array('__numbered' => 'bar', '__numbered_0' => 'baz');
$this->assertEquals($expectedSql, $parsedQuery->getString());
$this->assertEquals($expectedParameters, $parsedQuery->getParameters());
$expectedStatement = "SELECT :__numbered, :__numbered_0";
$expectedValues = array('__numbered' => 'bar', '__numbered_0' => 'baz');
$this->assertEquals($expectedStatement, $parsedQuery->getStatement());
$this->assertEquals($expectedValues, $parsedQuery->getValues());
}

public function testSingleLineComment()
{
$parameters = array('foo' => array('bar', 'baz'));
$sql = "-- :foo";
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($sql, $parsedQuery->getString());
$this->assertEquals($sql, $parsedQuery->getStatement());

$sql = "SELECT 1 -- :foo = 1";
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($sql, $parsedQuery->getString());
$this->assertEquals($sql, $parsedQuery->getStatement());

$sql = "SELECT 1
-- :foo";
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($sql, $parsedQuery->getString());
$this->assertEquals($sql, $parsedQuery->getStatement());

// The space character after the two -- is mandatory for MySQL
$sql = "SELECT 1 --:foo";
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$expectedSql = "SELECT 1 --:foo, :foo_0";
$expectedParameters = array('foo' => 'bar', 'foo_0' => 'baz');
$this->assertEquals($expectedSql, $parsedQuery->getString());
$this->assertEquals($expectedParameters, $parsedQuery->getParameters());
$expectedStatement = "SELECT 1 --:foo, :foo_0";
$expectedValues = array('foo' => 'bar', 'foo_0' => 'baz');
$this->assertEquals($expectedStatement, $parsedQuery->getStatement());
$this->assertEquals($expectedValues, $parsedQuery->getValues());
}

public function testMultiLineComment()
Expand All @@ -103,7 +103,7 @@ public function testMultiLineComment()
*/
1";
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($sql, $parsedQuery->getString());
$this->assertEquals($sql, $parsedQuery->getStatement());

// MySQL does not handle nested comments
$sql = "SELECT
Expand All @@ -112,9 +112,9 @@ public function testMultiLineComment()
*/ :foo */
1";
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertNotEquals($sql, $parsedQuery->getString());
$expectedParameters = array('foo' => 'bar', 'foo_0' => 'baz');
$this->assertEquals($expectedParameters, $parsedQuery->getParameters());
$this->assertNotEquals($sql, $parsedQuery->getStatement());
$expectedValues = array('foo' => 'bar', 'foo_0' => 'baz');
$this->assertEquals($expectedValues, $parsedQuery->getValues());
}

public function testDoubleQuotedIdentifier()
Expand All @@ -124,13 +124,13 @@ public function testDoubleQuotedIdentifier()
SELECT ":foo"
SQL;
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($sql, $parsedQuery->getString());
$this->assertEquals($sql, $parsedQuery->getStatement());

$sql = <<<SQL
SELECT "to use double quotes, just double them "" :foo "
SQL;
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($sql, $parsedQuery->getString());
$this->assertEquals($sql, $parsedQuery->getStatement());
}

public function testStringConstants()
Expand All @@ -140,14 +140,14 @@ public function testStringConstants()
SELECT ':foo'
SQL;
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($sql, $parsedQuery->getString());
$this->assertEquals($sql, $parsedQuery->getStatement());


$sql = <<<SQL
SELECT 'single quote''s :foo'
SQL;
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($sql, $parsedQuery->getString());
$this->assertEquals($sql, $parsedQuery->getStatement());
}

public function testIdentifierString()
Expand All @@ -157,14 +157,14 @@ public function testIdentifierString()
SELECT `:foo`
SQL;
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($sql, $parsedQuery->getString());
$this->assertEquals($sql, $parsedQuery->getStatement());


$sql = <<<SQL
SELECT `single quote``s :foo`
SQL;
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($sql, $parsedQuery->getString());
$this->assertEquals($sql, $parsedQuery->getStatement());
}

public function testEscapedCharactersInStringConstants()
Expand All @@ -174,42 +174,42 @@ public function testEscapedCharactersInStringConstants()
SELECT 'Escaping \' :foo \''
SQL;
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($sql, $parsedQuery->getString());
$this->assertEquals($sql, $parsedQuery->getStatement());

$sql = <<<SQL
SELECT "Escaping \" :foo \""
SQL;
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($sql, $parsedQuery->getString());
$this->assertEquals($sql, $parsedQuery->getStatement());

$sql = <<<SQL
SELECT "Escaping \\\\" :foo ""
SQL;
$expectedSql = <<<SQL
$expectedStatement = <<<SQL
SELECT "Escaping \\\\" :foo, :foo_0 ""
SQL;
$expectedParameters = array('foo' => 'bar', 'foo_0' => 'baz');
$expectedValues = array('foo' => 'bar', 'foo_0' => 'baz');
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($expectedSql, $parsedQuery->getString());
$this->assertEquals($expectedParameters, $parsedQuery->getParameters());
$this->assertEquals($expectedStatement, $parsedQuery->getStatement());
$this->assertEquals($expectedValues, $parsedQuery->getValues());

$sql = <<<SQL
SELECT "Escaping \" :foo \"
SQL;
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($sql, $parsedQuery->getString());
$this->assertEquals($sql, $parsedQuery->getStatement());

$sql = <<<SQL
SELECT "Escaping "" :foo """
SQL;
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($sql, $parsedQuery->getString());
$this->assertEquals($sql, $parsedQuery->getStatement());

$sql = <<<SQL
SELECT 'Escaping '' :foo '''
SQL;
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($sql, $parsedQuery->getString());
$this->assertEquals($sql, $parsedQuery->getStatement());
}

public function testMultiQueries()
Expand Down Expand Up @@ -242,12 +242,12 @@ public function testMultiQueries()
SQL;
$queries = $this->parseMultipleQueries($sql, $parameters);
$this->assertTrue(count($queries) == 2);
$expectedSql = "SELECT :foo, :foo_0";
$expectedParameters = array('foo' => 'bar', 'foo_0' => 'baz');
$this->assertEquals($expectedSql, $queries[0]->getString());
$this->assertEquals($expectedSql, $queries[1]->getString());
$this->assertEquals($expectedParameters, $queries[0]->getParameters());
$this->assertEquals($expectedParameters, $queries[1]->getParameters());
$expectedStatement = "SELECT :foo, :foo_0";
$expectedValues = array('foo' => 'bar', 'foo_0' => 'baz');
$this->assertEquals($expectedStatement, $queries[0]->getStatement());
$this->assertEquals($expectedStatement, $queries[1]->getStatement());
$this->assertEquals($expectedValues, $queries[0]->getValues());
$this->assertEquals($expectedValues, $queries[1]->getValues());

$parameters = array('foo' => array('bar', 'baz'), 'bar' => array('foo', 'qux'));
$sql = <<<SQL
Expand All @@ -256,22 +256,22 @@ public function testMultiQueries()
SQL;
$queries = $this->parseMultipleQueries($sql, $parameters);
$this->assertTrue(count($queries) == 2);
$expectedSql = "SELECT :bar, :bar_0";
$expectedParameters = array('bar' => 'foo', 'bar_0' => 'qux');
$this->assertEquals($expectedSql, $queries[0]->getString());
$this->assertEquals($expectedParameters, $queries[0]->getParameters());
$expectedSql = "SELECT :foo, :foo_0";
$expectedParameters = array('foo' => 'bar', 'foo_0' => 'baz');
$this->assertEquals($expectedSql, $queries[1]->getString());
$this->assertEquals($expectedParameters, $queries[1]->getParameters());
$expectedStatement = "SELECT :bar, :bar_0";
$expectedValues = array('bar' => 'foo', 'bar_0' => 'qux');
$this->assertEquals($expectedStatement, $queries[0]->getStatement());
$this->assertEquals($expectedValues, $queries[0]->getValues());
$expectedStatement = "SELECT :foo, :foo_0";
$expectedValues = array('foo' => 'bar', 'foo_0' => 'baz');
$this->assertEquals($expectedStatement, $queries[1]->getStatement());
$this->assertEquals($expectedValues, $queries[1]->getValues());
}

public function testIssue107()
{
$sql = "UPDATE table SET `value`=:value, `blank`='', `value2` = :value2, `blank2` = '', `value3`=:value3 WHERE id = :id";
$parameters = ['value'=> 'string', 'id'=> 1, 'value2'=> 'string', 'value3'=>'string'];
$parsedQuery = $this->parseSingleQuery($sql, $parameters);
$this->assertEquals($parsedQuery->getString(), $sql);
$this->assertEquals($parsedQuery->getParameters(), $parameters);
$this->assertEquals($parsedQuery->getStatement(), $sql);
$this->assertEquals($parsedQuery->getValues(), $parameters);
}
}
4 changes: 2 additions & 2 deletions tests/Parser/NullParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function test()
$parser = new NullParser();
$parsedQueries = $parser->rebuild('foo');
$this->assertTrue(count($parsedQueries) == 1);
$this->assertSame('foo', $parsedQueries[0]->getString());
$this->assertSame([], $parsedQueries[0]->getParameters());
$this->assertSame('foo', $parsedQueries[0]->getStatement());
$this->assertSame([], $parsedQueries[0]->getValues());
}
}
Loading

0 comments on commit 7e2ed50

Please sign in to comment.