Issues

ZF2-570: Pgsql adapater has codes related to MySQL

Description

While trying to connect using:


public function getServiceConfig()
{
    return array(
        'factories' => array(
            'db_adapter' =>  function($sm) {
                $config = $sm->get('Configuration');
                $dbAdapter = new \Zend\Db\Adapter\Adapter($config['db']);
                return $dbAdapter;
            },
        ),
    );
}

return array(
    'db' => array(
        'driver'    => 'Pgsql',
        'database'  => 'db_name',
        'username'  => 'user',
        'password'  => 'pass',
        'hostname'  => '127.0.0.1',
    ),
);

I got the following error: {quote} Fatal error: Uncaught exception 'Zend\Db\Adapter\Exception\RuntimeException' with message 'The Mysqli extension is required for this adapter but the extension is not loaded' in /srv/httpd/brainpop/dev/web/vendor/ZendFramework/library/Zend/ServiceManager/ServiceManager.php on line 697

Zend\Db\Adapter\Exception\RuntimeException: The Mysqli extension is required for this adapter but the extension is not loaded in /srv/httpd/brainpop/dev/web/vendor/ZendFramework/library/Zend/Db/Adapter/Driver/Pgsql/Pgsql.php on line 114 {quote}

After further check in the code I found out that MySQL code is embedded within the Pgsql adapter in the following files: * library/Zend/Db/Adapter/Driver/Pgsql/Connection.php * library/Zend/Db/Adapter/Driver/Pgsql/Pgsql.php

Tried to hack it a bit but failed as other errors popped updb

Comments


public function checkEnvironment()
{
    if (!extension_loaded('mysqli')) {
        throw new Exception\RuntimeException('The Mysqli extension is required for this adapter but the extension is not loaded');
    }
}

…

public function createStatement($sqlOrResource = null)
{
    /*
    if (is_resource($sqlOrResource) && !in_array($sqlOrResource, $this->resources, true)) {
        $this->resources[] = $sqlOrResource;
    }
    */

    $statement = clone $this->statementPrototype;
    if (is_string($sqlOrResource)) {
        $statement->setSql($sqlOrResource);
    }

    /* elseif ($sqlOrResource instanceof \mysqli_stmt) {
        $statement->setResource($sqlOrResource);
    }
    */

    $statement->initialize($this->connection->getResource());
    return $statement;
}

use mysqli;

…

/**
 * Constructor
 *
 * @param mysqli|array|null $connectionInfo
 */
public function __construct($connectionInfo = null)
{
    if (is_array($connectionInfo)) {
        $this->setConnectionParameters($connectionInfo);
    } elseif ($connectionInfo instanceof mysqli) {
        $this->setResource($connectionInfo);
    }
}

…

/**
 * @param  string $sql
 * @return resource|\Zend\Db\ResultSet\ResultSetInterface
 */
public function execute($sql)
{
    if (!$this->isConnected()) {
        $this->connect();
    }

    $resultResource = pg_query($this->resource, $sql);

    //var_dump(pg_result_status($resultResource));

    // if the returnValue is something other than a mysqli_result, bypass wrapping it
    if ($resultResource === false) {
        throw new Exception\InvalidQueryException(pg_errormessage());
    }

    $resultPrototype = $this->driver->createResult(($resultResource === true) ? $this->resource : $resultResource);
    return $resultPrototype;
}

This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit: https://github.com/zendframework/zf2/issues/2595