|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPCR\Shell\Query; |
| 4 | + |
| 5 | +use PHPCR\Util\ValueConverter; |
| 6 | +use PHPCR\Util\QOM\Sql2Scanner; |
| 7 | +use PHPCR\Util\QOM\Sql2ToQomQueryConverter; |
| 8 | +use PHPCR\Query\InvalidQueryException; |
| 9 | +use PHPCR\Query\QOM\SourceInterface; |
| 10 | + |
| 11 | +/** |
| 12 | + * Parse "UPDATE" queries. |
| 13 | + * |
| 14 | + * This class extends the Sql2ToQomQueryConverter class and adapts it |
| 15 | + * to parse UPDATE queries. |
| 16 | + * |
| 17 | + * @author Daniel Leech <daniel@dantleech.com> |
| 18 | + */ |
| 19 | +class UpdateParser extends Sql2ToQomQueryConverter |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Parse an "SQL2" UPDATE statement and construct a query builder |
| 23 | + * for selecting the rows and build a field => value mapping for the |
| 24 | + * update. |
| 25 | + * |
| 26 | + * @param string $sql2 |
| 27 | + * |
| 28 | + * @return array($query, $updates) |
| 29 | + */ |
| 30 | + public function parse($sql2) |
| 31 | + { |
| 32 | + $this->implicitSelectorName = null; |
| 33 | + $this->sql2 = $sql2; |
| 34 | + $this->scanner = new Sql2Scanner($sql2); |
| 35 | + $source = null; |
| 36 | + $constraint = null; |
| 37 | + |
| 38 | + while ($this->scanner->lookupNextToken() !== '') { |
| 39 | + switch (strtoupper($this->scanner->lookupNextToken())) { |
| 40 | + case 'UPDATE': |
| 41 | + $this->scanner->expectToken('UPDATE'); |
| 42 | + $source = $this->parseSource(); |
| 43 | + break; |
| 44 | + case 'SET': |
| 45 | + $this->scanner->expectToken('SET'); |
| 46 | + $updates = $this->parseUpdates(); |
| 47 | + break; |
| 48 | + case 'WHERE': |
| 49 | + $this->scanner->expectToken('WHERE'); |
| 50 | + $constraint = $this->parseConstraint(); |
| 51 | + break; |
| 52 | + default: |
| 53 | + throw new InvalidQueryException('Expected end of query, got "' . $this->scanner->lookupNextToken() . '" in ' . $this->sql2); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (!$source instanceof SourceInterface) { |
| 58 | + throw new InvalidQueryException('Invalid query, source could not be determined: '.$sql2); |
| 59 | + } |
| 60 | + |
| 61 | + $query = $this->factory->createQuery($source, $constraint); |
| 62 | + |
| 63 | + $res = new \ArrayObject(array($query, $updates)); |
| 64 | + |
| 65 | + return $res; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Parse the SET section of the query, returning |
| 70 | + * an array containing the property names (<selectorName.propertyName) |
| 71 | + * as keys and an array |
| 72 | + * |
| 73 | + * array( |
| 74 | + * 'selector' => <selector>, |
| 75 | + * 'name' => <name>, |
| 76 | + * '<value>' => <property value>, |
| 77 | + * ) |
| 78 | + * |
| 79 | + * @return array |
| 80 | + */ |
| 81 | + protected function parseUpdates() |
| 82 | + { |
| 83 | + $updates = array(); |
| 84 | + |
| 85 | + while (true) { |
| 86 | + $selectorName = $this->scanner->fetchNextToken(); |
| 87 | + $delimiter = $this->scanner->fetchNextToken(); |
| 88 | + |
| 89 | + if ($delimiter !== '.') { |
| 90 | + $property = array( |
| 91 | + 'selector' => null, |
| 92 | + 'name' => $selectorName |
| 93 | + ); |
| 94 | + $equals = $delimiter; |
| 95 | + } else { |
| 96 | + $property = array( |
| 97 | + 'selector' => $selectorName, |
| 98 | + 'name' => $this->scanner->fetchNextToken() |
| 99 | + ); |
| 100 | + $equals = $this->scanner->fetchNextToken(); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + if ($equals !== '=') { |
| 105 | + throw new InvalidQueryException(sprintf( |
| 106 | + 'Expected "=" after property name in UPDATE query, got "%s"', |
| 107 | + $equals, |
| 108 | + $this->sql2 |
| 109 | + )); |
| 110 | + } |
| 111 | + |
| 112 | + $value = $this->parseLiteralValue(); |
| 113 | + $property['value'] = $value; |
| 114 | + |
| 115 | + $updates[$property['selector'] . '.' . $property['name']] = $property; |
| 116 | + |
| 117 | + $next = $this->scanner->lookupNextToken(); |
| 118 | + |
| 119 | + if ($next == ',') { |
| 120 | + $next = $this->scanner->fetchNextToken(); |
| 121 | + } elseif (strtolower($next) == 'where' || !$next) { |
| 122 | + break; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return $updates; |
| 127 | + } |
| 128 | +} |
0 commit comments