Skip to content

Commit

Permalink
make charset configurable in parser
Browse files Browse the repository at this point in the history
also, standardize terminology
  • Loading branch information
Paul M. Jones committed Jan 23, 2017
1 parent 7e2ed50 commit b7f3dbf
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 78 deletions.
20 changes: 13 additions & 7 deletions src/Parser/AbstractParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class AbstractParser implements ParserInterface
* @var array
*
*/
protected $handlers = array();
protected $handlers = [];

/**
*
Expand All @@ -40,26 +40,32 @@ abstract class AbstractParser implements ParserInterface
*/
protected $numberedPlaceHolderCharacter = "?";

protected $charset = 'UTF-8';

public function __construct($charset = 'UTF-8')
{
$this->charset = $charset;
}

/**
*
* Given a query string and parameters, rebuilds it so that parameters all
* match up, and replaces array-based placeholders.
*
* @param string $string The query statement string.
* @param string $statement The query statement string.
*
* @param array $parameters Bind these values into the query.
* @param array $values Bind these values into the query.
*
* @return Query[]
*
*/
public function rebuild($string, array $parameters = [])
public function rebuild($statement, array $values = [])
{
$query = new Query($string, $parameters);
$query = new Query($statement, $values);
$queries = array();
$charset = 'UTF-8';

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

$last_check_index = -1;

Expand Down
26 changes: 10 additions & 16 deletions src/Parser/MysqlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,16 @@
*/
class MysqlParser extends AbstractParser
{
/**
* Constructor. Sets up the array of callbacks.
*/
public function __construct()
{
$this->handlers = array(
'-' => 'handleSingleLineComment',
'/' => 'handleMultiLineComment',
'"' => 'handleMySQLQuotedString',
"'" => 'handleMySQLQuotedString',
"`" => 'handleQuotedString',
':' => 'handleColon',
'?' => 'handleNumberedParameter',
';' => 'handleSemiColon',
);
}
protected $handlers = [
'-' => 'handleSingleLineComment',
'/' => 'handleMultiLineComment',
'"' => 'handleMySQLQuotedString',
"'" => 'handleMySQLQuotedString',
"`" => 'handleQuotedString',
':' => 'handleColon',
'?' => 'handleNumberedParameter',
';' => 'handleSemiColon',
];

/**
*
Expand Down
8 changes: 4 additions & 4 deletions src/Parser/NullParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class NullParser implements ParserInterface
*
* Leaves the query and parameters alone.
*
* @param string $string The query statement string.
* @param string $statement The query statement string.
*
* @param array $parameters Bind these values into the query.
* @param array $values Bind these values into the query.
*
* @return Query[]
*
*/
public function rebuild($string, array $parameters = [])
public function rebuild($statement, array $values = [])
{
return [new Query($string, $parameters)];
return [new Query($statement, $values)];
}
}
32 changes: 13 additions & 19 deletions src/Parser/PgsqlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,19 @@
*/
class PgsqlParser extends AbstractParser
{
/**
* Constructor. Sets up the array of callbacks.
*/
public function __construct()
{
$this->handlers = array(
'-' => 'handleSingleLineComment',
'/' => 'handleMultiLineComment',
'"' => 'handleQuotedString',
"'" => 'handleQuotedString',
'E' => 'handlePossibleCStyleString',
'e' => 'handlePossibleCStyleString',
':' => 'handleColon',
'?' => 'handleNumberedParameter',
';' => 'handleSemiColon',
'$' => 'handleDollar',
'[' => 'handleArray',
);
}
protected $handlers = [
'-' => 'handleSingleLineComment',
'/' => 'handleMultiLineComment',
'"' => 'handleQuotedString',
"'" => 'handleQuotedString',
'E' => 'handlePossibleCStyleString',
'e' => 'handlePossibleCStyleString',
':' => 'handleColon',
'?' => 'handleNumberedParameter',
';' => 'handleSemiColon',
'$' => 'handleDollar',
'[' => 'handleArray',
];

/**
*
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class Query
{
/**
* @var string A query statement
* @var string A query statement string
*/
private $statement = '';

Expand Down
24 changes: 9 additions & 15 deletions src/Parser/SqliteParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,15 @@
*/
class SqliteParser extends AbstractParser
{
/**
* Constructor. Sets up the array of callbacks.
*/
public function __construct()
{
$this->handlers = array(
'-' => 'handleSingleLineComment',
'/' => 'handleMultiLineComment',
'"' => 'handleQuotedString',
"'" => 'handleSqliteQuotedString',
':' => 'handleColon',
'?' => 'handleNumberedParameter',
';' => 'handleSemiColon',
);
}
protected $handlers = [
'-' => 'handleSingleLineComment',
'/' => 'handleMultiLineComment',
'"' => 'handleQuotedString',
"'" => 'handleSqliteQuotedString',
':' => 'handleColon',
'?' => 'handleNumberedParameter',
';' => 'handleSemiColon',
];

/**
*
Expand Down
26 changes: 10 additions & 16 deletions src/Parser/SqlsrvParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,16 @@
*/
class SqlsrvParser extends AbstractParser
{
/**
* Constructor. Sets up the array of callbacks.
*/
public function __construct()
{
$this->handlers = array(
'-' => 'handleSingleLineComment',
'/' => 'handleMultiLineComment',
'"' => 'handleQuotedString',
"'" => 'handleQuotedString',
"[" => 'handleIdentifier',
':' => 'handleColon',
'?' => 'handleNumberedParameter',
';' => 'handleSemiColon',
);
}
protected $handlers = [
'-' => 'handleSingleLineComment',
'/' => 'handleMultiLineComment',
'"' => 'handleQuotedString',
"'" => 'handleQuotedString',
"[" => 'handleIdentifier',
':' => 'handleColon',
'?' => 'handleNumberedParameter',
';' => 'handleSemiColon',
];

/**
*
Expand Down

0 comments on commit b7f3dbf

Please sign in to comment.