Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Postgres: Error when querying with json operator #6233

Closed
robbestad opened this issue May 7, 2014 · 2 comments
Closed

Postgres: Error when querying with json operator #6233

robbestad opened this issue May 7, 2014 · 2 comments

Comments

@robbestad
Copy link
Contributor

Querying json fields with the ->> operator gives an error using Zend\Db\Sql\Sql

Sample query:

array('d' => 'products_description'), 'p.id = d.id', array('id',"data->>'description'")

error:

"detail": "SQLSTATE[42703]: Undefined column: 7 ERROR:  column d.data\"-\">\">\"'\"description\"' does not exist\nLINE 1: SELECT \"p\".*, \"d\".\"data\"\"-\"\">\"\">\"\"'\"\"description\"\"'\" AS \"dat...\n                      ^"
@mkroeders
Copy link

Not tested, but got it for now working

Zend\Db\Adapter\Platform\Postgresql

Replace quoteIdentifierInFragment with the following code

`public function quoteIdentifierInFragment($identifier, array $safeWords = array())
{
$jsonParts = preg_split('#(->{1,2}|#>{1,2})#', $identifier, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$identifier = array_shift($jsonParts);

    $parts = preg_split('#([\.\s\W])#', $identifier, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
    if ($safeWords) {
        $safeWords = array_flip($safeWords);
        $safeWords = array_change_key_case($safeWords, CASE_LOWER);
    }

    foreach ($parts as $i => $part) {
        if ($safeWords && isset($safeWords[strtolower($part)])) {
            continue;
        }
        switch ($part) {
            case ' ':
            case '.':
            case '*':
            case 'AS':
            case 'As':
            case 'aS':
            case 'as':
                break;
            default:
                $parts[$i] = '"' . str_replace('"', '\\' . '"', $part) . '"';
        }
    }

    if (count($jsonParts) > 1) {
        $last = array_pop($jsonParts);
        $lastParts = preg_split('#([\.\s\W])#', $last, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        array_push($jsonParts, array_shift($lastParts));

        foreach($jsonParts as $part) {
            switch ($part) {
                case '#>' :
                case '#>>' :
                case '->' :
                case '->>' :
                    array_push($parts, $part);
                    continue;

                default :
                    array_push($parts, $this->quoteValue($part));
            }
        }
        array_push($parts, $this->quoteIdentifierInFragment(implode(' ', $lastParts), $safeWords));
    }

    return implode('', $parts);
}`

Again not tested

@GeeH
Copy link

GeeH commented Jun 27, 2016

This issue has been closed as part of the bug migration program as outlined here - http://framework.zend.com/blog/2016-04-11-issue-closures.html

@GeeH GeeH closed this as completed Jun 27, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants