laminas-db icon indicating copy to clipboard operation
laminas-db copied to clipboard

Quoting behaves not as expected

Open modir opened this issue 2 years ago • 4 comments

Bug Report

| Version(s) | 2.13.4

Current behavior

We have here an MSSQL database were we run these statements against:

	$select = new Select();
        $select->from($this->tableGateway->getTable());

        $select->columns(['Code', 'Description'])
            ->where([$platform->quoteIdentifier('Promotional Order Active') => '1'])
            ->order('Description');

        // for debugging
        $sql = new Sql($this->tableGateway->getAdapter());

        $statement = $sql->prepareStatementForSqlObject($select);

        print_r($statement->getSql());

the output is then this SQL statement:

SELECT [Company CH$Brand Product Line].[Code] AS [Code], [Company CH$Brand Product Line].[Description] AS [Description] FROM [Company CH$Brand Product Line] WHERE [\][Company] [CH$Brand] [Product] [Line].[Promotional] [Order] [Active][]] = :where1 ORDER BY [Description] ASC

As you can see there are wrong quotings after the WHERE. Once the backslash, then each word with a quote instead of all together and then at the end empty brackets.

Expected behavior

I would have expected to be like this:

SELECT [Company CH$Brand Product Line].[Code] AS [Code], [Company CH$Brand Product Line].[Description] AS [Description] FROM [Company CH$Brand Product Line] WHERE [Company CH$Brand Product Line].[Promotional Order Active] = :where1 ORDER BY [Description] ASC

modir avatar Oct 14 '21 11:10 modir

Try disable quote identifiers in parameters of the PDO connection. For Oracle it is looks like

use Laminas\Db\Adapter\Adapter;

        $this->hostname = getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_OCI8_HOSTNAME');
        $this->database = getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_OCI8_DATABASE');
        $this->username = getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_OCI8_USERNAME');
        $this->password = getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_OCI8_PASSWORD');
        $this->dsn = sprintf('oci:dbname=//%s/%s',
            $this->hostname,
            $this->database
        );
        $driverOptions = [
            'driver' => 'pdo_oci',
            'dsn' => $this->dsn,
            'username' => $this->username,
            'password' => $this->password,
            'platform_options' => [
                'quote_identifiers' => false  // <<< !!! HERE !!!
            ],
        ];
        $adapter = new Adapter($driverOptions);

ZVanoZ avatar Nov 04 '21 10:11 ZVanoZ

@ZVanoZ I already tried this. The problem is then that other things are not quoted that should be.

modir avatar Nov 05 '21 12:11 modir

Identifier quoting issues are often related to https://github.com/laminas/laminas-db/issues/77 (original discussion at https://github.com/zendframework/zend-db/pull/233) and maybe https://github.com/zendframework/zend-db/pull/232. Maybe something in there could give a useful hint to fix in own fork fix. quoteIdentifierChain might give better results, but those PRs need to be revisted for proper solution.

alextech avatar Nov 09 '21 20:11 alextech

I have the same issue and also with brackets as table name (like Amount (LCY) which will be quoted as [Amount] [(][LCY][)]).

Is there any solution for brackets? I had made an own fork and fixed the whitespace issue based on the closed issue 84. But i can't find a good solution for brackets.

When this issue with whitespaces will be fixed? The solution was given 2 years ago.

dmetzler1988 avatar May 03 '22 12:05 dmetzler1988