benpjohnson / php-sql-parser

Automatically exported from code.google.com/p/php-sql-parser
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

php-sql-creator: operators in functions throw error #76

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Having operators in functions and aggregates throws an

UnableToCreateSQLException

in PHPSQLCreator->processFunction.

Solution:

Add 

                $sql .= $this->processOperator($v);

at line 410 (r346). 

Original issue reported on code.google.com by adrian.p...@googlemail.com on 18 Oct 2012 at 9:47

GoogleCodeExporter commented 9 years ago
It should actually be something like this (in order to get the separation of 
the variable list with the commas correct)

add:

        protected function isOperator($parsed) {
            return ($parsed['expr_type'] === ExpressionType::OPERATOR);
        }

and change from line 405 (r346) onwards to:
            foreach ($parsed['sub_tree'] as $k => $v) {
                $len = strlen($sql);
                $sql .= $this->processFunction($v);
                $sql .= $this->processConstant($v);
                $sql .= $this->processColRef($v);
                $sql .= $this->processReserved($v);

                if($this->isOperator($v)) {
                    $sql = substr($sql, 0, -1) . " " . $this->processOperator($v);
                }

                if ($len == strlen($sql)) {
                   throw new UnableToCreateSQLException('function subtree', $k, $v, 'expr_type');
                }

                $sql .= ($this->isReserved($v) || $this->isOperator($v) ? " " : ",");
            }

                $sql .= ($this->isReserved($v) ? " " : ",");

to

                if($this->isOperator($v)) {
                    $sql = substr($sql, 0, -1) . " ";
                } else {
                    $sql .= ($this->isReserved($v) ? " " : ",");
                }

Original comment by adrian.p...@googlemail.com on 18 Oct 2012 at 11:12

GoogleCodeExporter commented 9 years ago
Can you specify a short example? I could use it as test case.

Original comment by pho...@gmx.de on 23 Oct 2013 at 8:37

GoogleCodeExporter commented 9 years ago
Sure. The following SQL query parsed and then recreated throws the error:

SELECT AVG(2.0 * foo) FROM bar;

Error:

PHP Fatal error:  Uncaught exception 'UnableToCreateSQLException' with message 
'unknown expr_type in function subtree[1] operator' in 
/home/adrpar/tmp/php-sql-parser-read-only/php-sql-creator.php:419
Stack trace:
#0 /home/adrpar/tmp/php-sql-parser-read-only/php-sql-creator.php(125): 
PHPSQLCreator->processFunction(Array)
#1 /home/adrpar/tmp/php-sql-parser-read-only/php-sql-creator.php(73): 
PHPSQLCreator->processSELECT(Array)
#2 /home/adrpar/tmp/php-sql-parser-read-only/php-sql-creator.php(54): 
PHPSQLCreator->processSelectStatement(Array)
#3 /home/adrpar/tmp/php-sql-parser-read-only/example.php(22): 
PHPSQLCreator->create(Array)
#4 {main}
  thrown in /home/adrpar/tmp/php-sql-parser-read-only/php-sql-creator.php on line 419

Original comment by adrian.p...@googlemail.com on 23 Oct 2013 at 12:10

GoogleCodeExporter commented 9 years ago
I try to handle that with an additional expression part. The parser doesn't 
generate a good output for the case 

SELECT ABC(2.0 * foo, x) FROM bar

So, it seems to be better to look into the parameters.

Original comment by pho...@gmx.de on 23 Oct 2013 at 4:00

GoogleCodeExporter commented 9 years ago
rev 374 should fix that issue.

Original comment by pho...@gmx.de on 24 Oct 2013 at 9:17