/**
     * Populates the types list from pg_catalog.pg_type table
     *
     * @param bool $force Force loading from database even if cached list is available
     * @throws exceptions\InvalidQueryException
     */
    private function _loadTypes($force = false)
    {
        $cacheKey = $this->_connection->getConnectionId() . '-types';
        if (!($cache = $this->_connection->getMetadataCache()) || $force || null === ($this->_dbTypes = $cache->getItem($cacheKey))) {
            $this->_dbTypes = array('composite' => array(), 'array' => array(), 'range' => array(), 'names' => array());
            $sql = <<<SQL
    select t.oid, nspname, typname, typarray, typrelid
    from pg_catalog.pg_type as t, pg_catalog.pg_namespace as s
    where t.typnamespace = s.oid and
          typtype != 'd'
    order by 4 desc
SQL;
            if (!($res = @pg_query($this->_connection->getResource(), $sql))) {
                throw new exceptions\InvalidQueryException(pg_last_error($this->_connection->getResource()));
            }
            while ($row = pg_fetch_assoc($res)) {
                if (!isset($this->_dbTypes['names'][$row['typname']])) {
                    $this->_dbTypes['names'][$row['typname']] = array($row['nspname'] => $row['oid']);
                } else {
                    $this->_dbTypes['names'][$row['typname']][$row['nspname']] = $row['oid'];
                }
                if ('0' !== $row['typarray']) {
                    $this->_dbTypes['array'][$row['typarray']] = $row['oid'];
                }
                if ('0' !== $row['typrelid']) {
                    $this->_dbTypes['composite'][$row['oid']] = $row['typrelid'];
                }
            }
            pg_free_result($res);
            if (version_compare(pg_parameter_status($this->_connection->getResource(), 'server_version'), '9.2.0', '>=')) {
                if (!($res = @pg_query($this->_connection->getResource(), "select rngtypid, rngsubtype from pg_range"))) {
                    throw new exceptions\InvalidQueryException(pg_last_error($this->_connection->getResource()));
                }
                while ($row = pg_fetch_assoc($res)) {
                    $this->_dbTypes['range'][$row['rngtypid']] = $row['rngsubtype'];
                }
            }
            if ($cache) {
                $cache->setItem($cacheKey, $this->_dbTypes);
            }
        }
        $this->_oidMap = array();
        foreach ($this->_dbTypes['names'] as $typeName => $schemas) {
            foreach ($schemas as $schemaName => $oid) {
                $this->_oidMap[$oid] = array($schemaName, $typeName);
            }
        }
    }
Пример #2
0
 /**
  * @dataProvider getExplicitTypes
  */
 public function testQuoteExplicitTypes($value, $type, $expected)
 {
     if (!TESTS_SAD_SPIRIT_PG_WRAPPER_CONNECTION_STRING) {
         $this->markTestSkipped('Connection string is not configured');
     }
     $connection = new Connection(TESTS_SAD_SPIRIT_PG_WRAPPER_CONNECTION_STRING);
     $this->assertSame($expected, $connection->quote($value, $type));
 }
 public function testEnumTypeConverterFromMetadata()
 {
     if (!TESTS_SAD_SPIRIT_PG_WRAPPER_CONNECTION_STRING) {
         $this->markTestSkipped('Connection string is not configured');
     }
     $connection = new Connection(TESTS_SAD_SPIRIT_PG_WRAPPER_CONNECTION_STRING, false);
     $connection->setTypeConverterFactory($this->factory);
     $connection->execute("drop type if exists testenum");
     $connection->execute("create type testenum as enum('yes', 'no', 'maybe', 'test')");
     $result = $connection->execute("select 'maybe'::testenum as value");
     $this->assertSame('maybe', $result[0]['value']);
 }