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

Problems creating query with some operators/functions #89

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
I'm trying to parse, modify and recreate a query.

1. Query:

$sql = "select ut.id, ut.numero_cartella, ut.nome, ut.cognome, 
floor(DATEDIFF(de.`data`,ut.data_di_nascita)/365) as eta,
sx.valore as sesso, cd.valore as diagnosi_prevalente, co.valore as 
consapevolezza,
DATEDIFF(de.`data`,az.data_inizio_assistenza) as durata_assistenza_giorni, 
ld.valore as luogo_decesso,
ca.valore as carico_assistenza, if(sa.id is null, 
null,if(sa.fkey_cod_care_giver_interno__con_chi_vive=1,'si','no')) as vive_solo,
sn.valore as oltre_70
from gen_cms_utenti ut";

2. Parse

$parser = new PHPSQLParser();
$parser->parse($sql);

3. Create query from the parsed query

$creator = new PHPSQLCreator();
$creator->create($parser->parsed); 
$save = $creator->created;

What is the expected output? What do you see instead?
Expected output: the original query.

What I see instead:
Uncaught exception 'UnableToCreateSQLException' with message 'unknown expr_type 
in function subtree[1] operator' in C:\xampp\htdocs\php-sql-creator.php:416
Stack trace:
#0 C:\xampp\htdocs\php-sql-creator.php(120): 
PHPSQLCreator->processFunction(Array)
#1 C:\xampp\htdocs\php-sql-creator.php(72): 
PHPSQLCreator->processSELECT(Array)
#2 C:\xampp\htdocs\php-sql-creator.php(53): 
PHPSQLCreator->processSelectStatement(Array)
#3 C:\xampp\htdocs\test.php(53): PHPSQLCreator->create(Array)
#4 {main}
  thrown in <b>C:\xampp\htdocs\php-sql-creator.php on line 416

What version of the product are you using? On what operating system?
php-sql-parser-20120822 on Windows 7 64bit

Please provide any additional information below.
I tried to comment the throw instruction in the php-sql-creator.php file(line 
416) and this is the output (if it can help):

SELECT 
ut.id,ut.numero_cartella,ut.nome,ut.cognome,floor(DATEDIFF(de.`data`,ut.data_di_
nascita),,365),sx.valore as sesso,cd.valore as diagnosi_prevalente,co.valore as 
consapevolezza,DATEDIFF(de.`data`,az.data_inizio_assistenza),ld.valore as 
luogo_decesso,ca.valore as 
carico_assistenza,if(sa.id,,null,null,if(sa.fkey_cod_care_giver_interno__con_chi
_vive,,1,'si','no')),sn.valore as oltre_70 FROM gen_cms_utenti ut 

Original Query                     Recreated Query
     "/"                 ->              ",,"
   "as eta"              ->            nothing
"[...]vive=1,'si','no'"  ->      "[...]vive,,1,'si','no'"

Original issue reported on code.google.com by davide.i...@gmail.com on 15 Feb 2013 at 10:41

GoogleCodeExporter commented 9 years ago
Partial Solution:
In php-sql-creator.php:
    1) Added the function:

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

    2) Modified processFunction($parsed) adding:
        if($this->isOperator($v)) {
                $sql = substr($sql, 0, -1) . " " . $this->processOperator($v);
        }

Original comment by davide.i...@gmail.com on 16 Feb 2013 at 1:23

GoogleCodeExporter commented 9 years ago
The problem with the aliases was solved in the issue 77:
http://code.google.com/p/php-sql-parser/issues/detail?id=77

Original comment by davide.i...@gmail.com on 16 Feb 2013 at 10:33

GoogleCodeExporter commented 9 years ago
I think, this issue has been fixed. Check r380.

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