/** * DQL FROM PARSER * parses the from part of the query string * @param string $str * @return void */ public function parse($str) { $str = trim($str); $parts = Doctrine_Tokenizer::bracketExplode($str, 'JOIN'); $operator = false; switch (trim($parts[0])) { case 'INNER': $operator = ':'; case 'LEFT': array_shift($parts); break; } $last = ''; foreach ($parts as $k => $part) { $part = trim($part); if (empty($part)) { continue; } $e = explode(' ', $part); if (end($e) == 'INNER' || end($e) == 'LEFT') { $last = array_pop($e); } $part = implode(' ', $e); foreach (Doctrine_Tokenizer::bracketExplode($part, ',') as $reference) { $reference = trim($reference); $e = explode('.', $reference); if ($operator) { $reference = array_shift($e) . $operator . implode('.', $e); } $table = $this->query->load($reference); } $operator = $last == 'INNER' ? ':' : '.'; } return null; }
/** * DQL CONDITION PARSER * parses the join condition/where/having part of the query string * * @param string $str * @return string */ public function parse($str) { $tmp = trim($str); $parts = Doctrine_Tokenizer::bracketExplode($str, array(' AND '), '(', ')'); if (count($parts) > 1) { $ret = array(); foreach ($parts as $part) { $part = Doctrine_Tokenizer::bracketTrim($part, '(', ')'); $ret[] = $this->parse($part); } $r = implode(' AND ', $ret); } else { $parts = Doctrine_Tokenizer::bracketExplode($str, array(' \\|\\| ', ' OR '), '(', ')'); if (count($parts) > 1) { $ret = array(); foreach ($parts as $part) { $part = Doctrine_Tokenizer::bracketTrim($part, '(', ')'); $ret[] = $this->parse($part); } $r = implode(' OR ', $ret); } else { if (substr($parts[0], 0, 1) == '(' && substr($parts[0], -1) == ')') { return $this->parse(substr($parts[0], 1, -1)); } else { return $this->load($parts[0]); } } } return '(' . $r . ')'; }
public function load($condition) { $condition = trim($condition); $e = Doctrine_Tokenizer::sqlExplode($condition); if (count($e) > 2) { $a = explode('.', $e[0]); $field = array_pop($a); $reference = implode('.', $a); $operator = $e[1]; $value = $e[2]; $alias = $this->query->getTableAlias($reference); $map = $this->query->getAliasDeclaration($reference); $table = $map['table']; // check if value is enumerated value $enumIndex = $table->enumIndex($field, trim($value, "'")); if (substr($value, 0, 1) == '(') { // trim brackets $trimmed = Doctrine_Tokenizer::bracketTrim($value); if (substr($trimmed, 0, 4) == 'FROM' || substr($trimmed, 0, 6) == 'SELECT') { // subquery found $q = new Doctrine_Query(); $value = '(' . $q->parseQuery($trimmed)->getQuery() . ')'; } elseif (substr($trimmed, 0, 4) == 'SQL:') { $value = '(' . substr($trimmed, 4) . ')'; } else { // simple in expression found $e = Doctrine_Tokenizer::sqlExplode($trimmed, ','); $value = array(); foreach ($e as $part) { $index = $table->enumIndex($field, trim($part, "'")); if ($index !== false) { $value[] = $index; } else { $value[] = $this->parseLiteralValue($part); } } $value = '(' . implode(', ', $value) . ')'; } } else { if ($enumIndex !== false) { $value = $enumIndex; } else { $value = $this->parseLiteralValue($value); } } switch ($operator) { case '<': case '>': case '=': case '!=': if ($enumIndex !== false) { $value = $enumIndex; } default: $condition = $alias . '.' . $field . ' ' . $operator . ' ' . $value; } } return $condition; }
public function parseClause($clause) { $e = Doctrine_Tokenizer::bracketExplode($clause, ' '); foreach ($e as $k => $expr) { $e[$k] = $this->parseExpression($expr); } return implode(' ', $e); }
/** * load * returns the parsed query part * * @param string $having * @return string */ public final function load($having) { $e = Doctrine_Tokenizer::bracketExplode($having, ' ', '(', ')'); $r = array_shift($e); $t = explode('(', $r); $count = count($t); $r = $this->parseAggregateFunction($r); $operator = array_shift($e); $value = implode(' ', $e); $r .= ' ' . $operator . ' ' . $value; return $r; }
public function parse($dql) { $parts = Doctrine_Tokenizer::sqlExplode($dql, ','); $result = array(); foreach ($parts as $part) { $set = Doctrine_Tokenizer::sqlExplode($part, '='); $e = explode('.', trim($set[0])); $field = array_pop($e); $reference = implode('.', $e); $alias = $this->query->getTableAlias($reference); $map = $this->query->getAliasDeclaration($reference); $result[] = $map['table']->getColumnName($field) . ' = ' . $set[1]; } return implode(', ', $result); }
/** * parse * Parses given field and field value to DQL condition * and parameters. This method should always return * prepared statement conditions (conditions that use * placeholders instead of literal values). * * @param string $alias component alias * @param string $field the field name * @param mixed $value the value of the field * @return void */ public function parseSingle($alias, $field, $value) { if (strpos($value, "'") !== false) { $value = Doctrine_Tokenizer::bracketTrim($value, "'", "'"); $a[] = $alias . '.' . $field . ' LIKE ?'; $this->params[] = $value . '%'; } else { $e2 = explode(' ', $value); foreach ($e2 as $v) { $v = trim($v); $a[] = $alias . '.' . $field . ' LIKE ?'; $this->params[] = $v . '%'; } } return implode(' OR ', $a); }
/** * parseQuery * parses an sql query and adds the parts to internal array * * @param string $query query to be parsed * @return Doctrine_RawSql this object */ public function parseQuery($query) { preg_match_all('/{([^}{]*)}/U', $query, $m); $this->fields = $m[1]; $this->clear(); $e = Doctrine_Tokenizer::sqlExplode($query, ' '); foreach ($e as $k => $part) { $low = strtolower($part); switch (strtolower($part)) { case 'select': case 'from': case 'where': case 'limit': case 'offset': case 'having': $p = $low; if (!isset($parts[$low])) { $parts[$low] = array(); } break; case 'order': case 'group': $i = $k + 1; if (isset($e[$i]) && strtolower($e[$i]) === 'by') { $p = $low; $p .= 'by'; $parts[$low . 'by'] = array(); } else { $parts[$p][] = $part; } break; case 'by': continue; default: if (!isset($parts[$p][0])) { $parts[$p][0] = $part; } else { $parts[$p][0] .= ' ' . $part; } } } $this->parts = $parts; $this->parts['select'] = array(); return $this; }
/** * parseClause * * @param string $alias component alias * @param string $field the field name * @param mixed $value the value of the field * @return void */ public function parseClause($alias, $field, $value) { $parts = Doctrine_Tokenizer::quoteExplode($value, ' AND '); if (count($parts) > 1) { $ret = array(); foreach ($parts as $part) { $ret[] = $this->parseSingle($alias, $field, $part); } $r = implode(' AND ', $ret); } else { $parts = Doctrine_Tokenizer::quoteExplode($value, ' OR '); if (count($parts) > 1) { $ret = array(); foreach ($parts as $part) { $ret[] = $this->parseClause($alias, $field, $part); } $r = implode(' OR ', $ret); } else { $ret = $this->parseSingle($alias, $field, $parts[0]); return $ret; } } return '(' . $r . ')'; }
/** * parses an EXISTS expression * * @param string $where query where part to be parsed * @param boolean $negation whether or not to use the NOT keyword * @return string */ public function parseExists($where, $negation) { $operator = $negation ? 'EXISTS' : 'NOT EXISTS'; $pos = strpos($where, '('); if ($pos == false) { throw new Doctrine_Query_Exception("Unknown expression, expected '('"); } $sub = Doctrine_Tokenizer::bracketTrim(substr($where, $pos)); return $operator . ' (' . $this->query->createSubquery()->parseQuery($sub, false)->getQuery() . ')'; }
/** * tokenizeQuery * 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', '?')) * * @param string $query DQL query * @throws Doctrine_Query_Exception if some generic parsing error occurs * @return array an array containing the query string parts */ public function tokenizeQuery($query) { $e = Doctrine_Tokenizer::sqlExplode($query, ' '); foreach ($e as $k => $part) { $part = trim($part); switch (strtolower($part)) { case 'delete': case 'update': case 'select': case 'set': case 'from': case 'where': case 'limit': case 'offset': case 'having': $p = $part; $parts[$part] = array(); break; case 'order': case 'group': $i = $k + 1; if (isset($e[$i]) && strtolower($e[$i]) === "by") { $p = $part; $parts[$part] = array(); } else { $parts[$p][] = $part; } break; case "by": continue; default: if (!isset($p)) { throw new Doctrine_Query_Exception("Couldn't parse query."); } $parts[$p][] = $part; } } return $parts; }