sanjosh / 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

processOrderBy exception when Order By Clause is a function #105

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. This sql:
SELECT users0.user_name AS 'CIS UserName'
    ,calls.description AS 'Description'
    ,contacts2.first_name AS 'Contacts First Name'
    ,contacts2.last_name AS 'Contacts Last Name'
    ,calls_cstm.date_logged_c AS 'Date'
    ,calls_cstm.contact_type_c AS 'Contact Type'
    ,dbo.fn_GetAccountName(calls.parent_id) AS 'Account Name'
FROM calls
LEFT JOIN calls_cstm ON calls.id = calls_cstm.id_c
LEFT JOIN users users0 ON calls.assigned_user_id = users0.id
LEFT JOIN contacts contacts2 ON calls.contact_id = contacts2.id
WHERE calls.deleted = 0
    AND (
        DATEADD(SECOND, 0, calls_cstm.date_logged_c) BETWEEN '2013-01-01'
            AND '2013-12-31'
        )
ORDER BY dbo.fn_GetAccountName(calls.parent_id) ASC LIMIT 0
    ,15

What is the expected output? What do you see instead?
Expect successful parse, but instead get:
Fatal error: Uncaught exception 'UnableToCreateSQLException' with message 
'unknown expr_type in ORDER[0] function' in 
C:\php-sql-parser-20131130\PHP-SQL-Parser\php-sql-creator.php:250
Stack trace:
#0 C:\php-sql-parser-20131130\PHP-SQL-Parser\php-sql-creator.php(166): 
PHPSQLCreator->processORDER(Array)
#1 C:\php-sql-parser-20131130\PHP-SQL-Parser\php-sql-creator.php(53): 
PHPSQLCreator->processSelectStatement(Array)
#2 C:\php-sql-parser-20131130\PHP-SQL-Parser\php-sql-creator.php(40): 
PHPSQLCreator->create(Array)
#3 C:\php-sql-parser-20131130\PHP-SQL-Parser\php-limit-test.php(41): 
PHPSQLCreator->__construct(Array)
#4 {main}
  thrown in C:\php-sql-parser-20131130\PHP-SQL-Parser\php-sql-creator.php on line 250

What version of the product are you using? On what operating system?
Using version php-sql-parser-20131130 on Windows 7.

Please provide any additional information below.
Adding one line (after line 246) in php-sql-creator.php fixes it for me:

        protected function processORDER($parsed) {
            $sql = "";
            foreach ($parsed as $k => $v) {
                $len = strlen($sql);
                $sql .= $this->processOrderByAlias($v);
                $sql .= $this->processColRef($v);
                $sql .= $this->processFunction($v); //NEW LINE

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

                $sql .= ",";
            }
            $sql = substr($sql, 0, -1);
            return "ORDER BY " . $sql;
        }

Original issue reported on code.google.com by ljwilson...@gmail.com on 17 Dec 2013 at 6:12

GoogleCodeExporter commented 8 years ago
Yes, this is the correct fix for the issue, I will include it into the /trunk 
asap.

Original comment by pho...@gmx.de on 17 Dec 2013 at 7:53

GoogleCodeExporter commented 8 years ago
You should also enhance the processFunction method. It must call the 
processDirection() too. If you don't do that, you won't get the direction 
keyword (ASC, DESC) after a function.

Original comment by pho...@gmx.de on 17 Dec 2013 at 8:58

GoogleCodeExporter commented 8 years ago
Fixed in r827.

Original comment by pho...@gmx.de on 17 Dec 2013 at 9:11