Doctrine 1.2.4
Doctrine_Query_Tokenizer Class Reference

Public Member Functions

 bracketExplode ($str, $d= ' ', $e1= '(', $e2= ')')
 
 bracketTrim ($str, $e1= '(', $e2= ')')
 
 clauseExplode ($str, array $d, $e1= '(', $e2= ')')
 
 quotedStringExplode ($str)
 
 quoteExplode ($str, $d= ' ')
 
 sqlExplode ($str, $d= ' ', $e1= '(', $e2= ')')
 
 tokenizeQuery ($query)
 

Detailed Description

Definition at line 35 of file Tokenizer.php.

Member Function Documentation

Doctrine_Query_Tokenizer::bracketExplode (   $str,
  $d = ' ',
  $e1 = '(',
  $e2 = ')' 
)

Explodes a sql expression respecting bracket placement.

This method transform a sql expression in an array of simple clauses, while observing the parentheses precedence.

Note: bracketExplode always trims the returned pieces

$str = (age < 20 AND age > 18) AND email LIKE 'John@.nosp@m.exam.nosp@m.ple.c.nosp@m.om' $clauses = $tokenizer->bracketExplode($str, ' AND ', '(', ')'); // array("(age < 20 AND age > 18)", "email LIKE 'John@example.com'")

Parameters
string$strString to be bracket exploded
string$dDelimeter which explodes the string
string$e1First bracket, usually '('
string$e2Second bracket, usually ')'
Returns
array

Definition at line 152 of file Tokenizer.php.

{
if (is_string($d)) {
$d = array($d);
}
// Bracket explode has to be case insensitive
$regexp = $this->getSplitRegExpFromArray($d) . 'i';
$terms = $this->clauseExplodeRegExp($str, $regexp, $e1, $e2);
$res = array();
// Trim is here for historical reasons
foreach ($terms as $value) {
$res[] = trim($value[0]);
}
return $res;
}
Doctrine_Query_Tokenizer::bracketTrim (   $str,
  $e1 = '(',
  $e2 = ')' 
)

Trims brackets from string

Parameters
string$strString to remove the brackets
string$e1First bracket, usually '('
string$e2Second bracket, usually ')'
Returns
string

Definition at line 122 of file Tokenizer.php.

{
if (substr($str, 0, 1) === $e1 && substr($str, -1) === $e2) {
return substr($str, 1, -1);
} else {
return $str;
}
}
Doctrine_Query_Tokenizer::clauseExplode (   $str,
array  $d,
  $e1 = '(',
  $e2 = ')' 
)

Explodes a string into array using custom brackets and quote delimeters Each array element is a array of length 2 where the first entry contains the term, and the second entry contains the corresponding delimiter

example:

parameters: $str = "(age < 20 AND age > 18) AND name LIKE 'John'+' Doe'" $d = array(' ', '+') $e1 = '(' $e2 = ')'

would return an array: array( array('(age < 20 AND age > 18)', ' '), array('AND', ' '), array('name', ' '), array('LIKE', ' '), array('John', '+'), array(' Doe', '') );

Parameters
string$strString to be clause exploded
string$dDelimeter which explodes the string
string$e1First bracket, usually '('
string$e2Second bracket, usually ')'
Returns
array

Definition at line 285 of file Tokenizer.php.

{
$regexp = $this->getSplitRegExpFromArray($d);
return $this->clauseExplodeRegExp($str, $regexp, $e1, $e2);
}
Doctrine_Query_Tokenizer::quotedStringExplode (   $str)

Explodes the given string by <quoted words>="">

example:

paramters: $str ="'a' AND name = 'John O\'Connor'"

returns array("", "'a'", " AND name = ", "'John O\'Connor'")

Note the trailing empty string. In the result, all even elements are quoted strings.

Parameters
$strthe string to split
Returns
array

Definition at line 513 of file Tokenizer.php.

{
// Split by all possible incarnations of a quote
$split = array_map('preg_quote', array("\\'","''","'", "\\\"", "\"\"", "\""));
$split = '#(' . implode('|', $split) . ')#';
$str = preg_split($split, $str, -1, PREG_SPLIT_DELIM_CAPTURE);
$parts = array();
$mode = false; // Mode is either ' or " if the loop is inside a string quoted with ' or "
$i = 0;
foreach ($str as $key => $val) {
// This is some kind of quote
if ($key & 1) {
if ( ! $mode) {
if ($val == "'" || $val == "\"") {
$mode = $val;
$i++;
}
} else if ($mode == $val) {
if ( ! isset($parts[$i])) {
$parts[$i] = $val;
} else {
$parts[$i] .= $val;
}
$mode = false;
$i++;
continue;
}
}
if ( ! isset($parts[$i])) {
$parts[$i] = $val;
} else {
$parts[$i] .= $val;
}
}
return $parts;
}
Doctrine_Query_Tokenizer::quoteExplode (   $str,
  $d = ' ' 
)

Explode quotes from string

Note: quoteExplode always trims the returned pieces

example:

parameters: $str = email LIKE 'John@.nosp@m.exam.nosp@m.ple.c.nosp@m.om' $d = ' LIKE '

would return an array: array("email", "LIKE", "'John@example.com'")

Parameters
string$strString to be quote exploded
string$dDelimeter which explodes the string
Returns
array

Definition at line 191 of file Tokenizer.php.

{
if (is_string($d)) {
$d = array($d);
}
// According to the testcases quoteExplode is case insensitive
$regexp = $this->getSplitRegExpFromArray($d) . 'i';
$terms = $this->clauseExplodeCountBrackets($str, $regexp);
$res = array();
foreach ($terms as $val) {
$res[] = trim($val[0]);
}
return $res;
}
Doctrine_Query_Tokenizer::sqlExplode (   $str,
  $d = ' ',
  $e1 = '(',
  $e2 = ')' 
)

Explodes a string into array using custom brackets and quote delimeters

Note: sqlExplode trims all returned parts

example:

parameters: $str = "(age < 20 AND age > 18) AND name LIKE 'John Doe'" $d = ' ' $e1 = '(' $e2 = ')'

would return an array: array( '(age < 20 AND age > 18)', 'name', 'LIKE', 'John Doe' );

Parameters
string$strString to be SQL exploded
string$dDelimeter which explodes the string
string$e1First bracket, usually '('
string$e2Second bracket, usually ')'
Returns
array

Definition at line 239 of file Tokenizer.php.

{
if (is_string($d)) {
$d = array($d);
}
$terms = $this->clauseExplode($str, $d, $e1, $e2);
$res = array();
foreach ($terms as $value) {
$res[] = trim($value[0]);
}
return $res;
}
Doctrine_Query_Tokenizer::tokenizeQuery (   $query)

Splits the given dql query into an array where keys represent different query part names and values are arrays splitted using sqlExplode method

example:

parameter: $query = "SELECT u.* FROM User u WHERE u.name LIKE ?" returns: array( 'select' => array('u.*'), 'from' => array('User', 'u'), 'where' => array('u.name', 'LIKE', '?') );

Parameters
string$queryDQL query
Exceptions
Doctrine_Query_ExceptionIf some generic parsing error occurs
Returns
array An array containing the query string parts

Definition at line 59 of file Tokenizer.php.

{
$tokens = $this->sqlExplode($query, ' ');
$parts = array();
foreach ($tokens as $index => $token) {
$token = trim($token);
switch (strtolower($token)) {
case 'delete':
case 'update':
case 'select':
case 'set':
case 'from':
case 'where':
case 'limit':
case 'offset':
case 'having':
$p = $token;
//$parts[$token] = array();
$parts[$token] = '';
break;
case 'order':
case 'group':
$i = ($index + 1);
if (isset($tokens[$i]) && strtolower($tokens[$i]) === 'by') {
$p = $token;
$parts[$token] = '';
//$parts[$token] = array();
} else {
$parts[$p] .= "$token ";
//$parts[$p][] = $token;
}
break;
case 'by':
continue;
default:
if ( ! isset($p)) {
"Couldn't tokenize query. Encountered invalid token: '$token'."
);
}
$parts[$p] .= "$token ";
//$parts[$p][] = $token;
}
}
return $parts;
}

The documentation for this class was generated from the following file: