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: alias not added to function columns #77

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Any alias that is connected to a function or aggregate in SELECT is not added 
to the SQL string in php-sql-creator->processFunction.

Solution:

Add processAlias at a convenient location.

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

GoogleCodeExporter commented 9 years ago
In case anyone else is looking, this will fix it right up:

--- PHP-SQL-Parser/php-sql-creator.php
+++ PHP-SQL-Parser/php-sql-creator.php
@@ -417,7 +417,13 @@ if (!defined('HAVE_PHP_SQL_CREATOR')) {

                 $sql .= ($this->isReserved($v) ? " " : ",");
             }
-            return $parsed['base_expr'] . "(" . substr($sql, 0, -1) . ")";
+
+            $sql = $parsed['base_expr'] . "(" . substr($sql, 0, -1) . ")";
+
+            if (array_key_exists('alias', $parsed))
+                 $sql .= $this->processAlias($parsed['alias']);
+
+            return $sql;
         }

         protected function processSelectExpression($parsed) {

Original comment by cag...@gmail.com on 13 Dec 2012 at 1:11

GoogleCodeExporter commented 9 years ago
I also had problems with functions and aliases.
Solved the problem with the following code:

--- PHP-SQL-Parser/php-sql-creator.php --- Line: 394...

        protected function processFunction($parsed) {
            if (($parsed['expr_type'] !== 'aggregate_function') && ($parsed['expr_type'] !== 'function')) {
                return "";
            }

            if ($parsed['sub_tree'] === false) {
                return $parsed['base_expr'] . "()";
            }

            $sql = "";
            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 ($len == strlen($sql)) {
                    throw new UnableToCreateSQLException('function subtree', $k, $v, 'expr_type');
                }

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

            /** $alias === ISSO FOI ACRESCENTADO POR Guilherme Ethur <gethur@hotmail.com> 
             * PARA CORRIGIR PROBLEMA DE CLASSIFICAÇÃO DO ORDER BY QUANDO USAVA FUNÇÕES 
             * COM ÁLIAS DE CAMPO **/////
            if (isset($parsed['alias'])) {
                $alias = $this->processAlias($parsed['alias']);
            }else{
                $alias = "";
            }            

            return $parsed['base_expr'] . "(" . substr($sql, 0, -1) . ") $alias"; //a variável $alias é código de guilherme ethur

        }

Original comment by Guilherm...@gmail.com on 31 Jul 2013 at 2:52

GoogleCodeExporter commented 9 years ago
I think, this issue has been solved. Can you check that with r377 ?

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

GoogleCodeExporter commented 9 years ago

Original comment by pho...@gmx.de on 1 Nov 2013 at 3:47