• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List

E:/E/GEAMP/www/openbiz/openbiz/others/Zend/Db/Adapter/Pdo/Pgsql.php

00001 <?php
00027 require_once 'Zend/Db/Adapter/Pdo/Abstract.php';
00028 
00029 
00039 class Zend_Db_Adapter_Pdo_Pgsql extends Zend_Db_Adapter_Pdo_Abstract
00040 {
00041 
00047     protected $_pdoType = 'pgsql';
00048 
00060     protected $_numericDataTypes = array(
00061         Zend_Db::INT_TYPE    => Zend_Db::INT_TYPE,
00062         Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
00063         Zend_Db::FLOAT_TYPE  => Zend_Db::FLOAT_TYPE,
00064         'INTEGER'            => Zend_Db::INT_TYPE,
00065         'SERIAL'             => Zend_Db::INT_TYPE,
00066         'SMALLINT'           => Zend_Db::INT_TYPE,
00067         'BIGINT'             => Zend_Db::BIGINT_TYPE,
00068         'BIGSERIAL'          => Zend_Db::BIGINT_TYPE,
00069         'DECIMAL'            => Zend_Db::FLOAT_TYPE,
00070         'DOUBLE PRECISION'   => Zend_Db::FLOAT_TYPE,
00071         'NUMERIC'            => Zend_Db::FLOAT_TYPE,
00072         'REAL'               => Zend_Db::FLOAT_TYPE
00073     );
00074 
00081     protected function _connect()
00082     {
00083         if ($this->_connection) {
00084             return;
00085         }
00086 
00087         parent::_connect();
00088 
00089         if (!empty($this->_config['charset'])) {
00090             $sql = "SET NAMES '" . $this->_config['charset'] . "'";
00091             $this->_connection->exec($sql);
00092         }
00093     }
00094 
00100     public function listTables()
00101     {
00102         // @todo use a better query with joins instead of subqueries
00103         $sql = "SELECT c.relname AS table_name "
00104              . "FROM pg_class c, pg_user u "
00105              . "WHERE c.relowner = u.usesysid AND c.relkind = 'r' "
00106              . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
00107              . "AND c.relname !~ '^(pg_|sql_)' "
00108              . "UNION "
00109              . "SELECT c.relname AS table_name "
00110              . "FROM pg_class c "
00111              . "WHERE c.relkind = 'r' "
00112              . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
00113              . "AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) "
00114              . "AND c.relname !~ '^pg_'";
00115 
00116         return $this->fetchCol($sql);
00117     }
00118 
00149     public function describeTable($tableName, $schemaName = null)
00150     {
00151         $sql = "SELECT
00152                 a.attnum,
00153                 n.nspname,
00154                 c.relname,
00155                 a.attname AS colname,
00156                 t.typname AS type,
00157                 a.atttypmod,
00158                 FORMAT_TYPE(a.atttypid, a.atttypmod) AS complete_type,
00159                 d.adsrc AS default_value,
00160                 a.attnotnull AS notnull,
00161                 a.attlen AS length,
00162                 co.contype,
00163                 ARRAY_TO_STRING(co.conkey, ',') AS conkey
00164             FROM pg_attribute AS a
00165                 JOIN pg_class AS c ON a.attrelid = c.oid
00166                 JOIN pg_namespace AS n ON c.relnamespace = n.oid
00167                 JOIN pg_type AS t ON a.atttypid = t.oid
00168                 LEFT OUTER JOIN pg_constraint AS co ON (co.conrelid = c.oid
00169                     AND a.attnum = ANY(co.conkey) AND co.contype = 'p')
00170                 LEFT OUTER JOIN pg_attrdef AS d ON d.adrelid = c.oid AND d.adnum = a.attnum
00171             WHERE a.attnum > 0 AND c.relname = ".$this->quote($tableName);
00172         if ($schemaName) {
00173             $sql .= " AND n.nspname = ".$this->quote($schemaName);
00174         }
00175         $sql .= ' ORDER BY a.attnum';
00176 
00177         $stmt = $this->query($sql);
00178 
00179         // Use FETCH_NUM so we are not dependent on the CASE attribute of the PDO connection
00180         $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
00181 
00182         $attnum        = 0;
00183         $nspname       = 1;
00184         $relname       = 2;
00185         $colname       = 3;
00186         $type          = 4;
00187         $atttypemod    = 5;
00188         $complete_type = 6;
00189         $default_value = 7;
00190         $notnull       = 8;
00191         $length        = 9;
00192         $contype       = 10;
00193         $conkey        = 11;
00194 
00195         $desc = array();
00196         foreach ($result as $key => $row) {
00197             $defaultValue = $row[$default_value];
00198             if ($row[$type] == 'varchar') {
00199                 if (preg_match('/character varying(?:\((\d+)\))?/', $row[$complete_type], $matches)) {
00200                     if (isset($matches[1])) {
00201                         $row[$length] = $matches[1];
00202                     } else {
00203                         $row[$length] = null; // unlimited
00204                     }
00205                 }
00206                 if (preg_match("/^'(.*?)'::character varying$/", $defaultValue, $matches)) {
00207                     $defaultValue = $matches[1];
00208                 }
00209             }
00210             list($primary, $primaryPosition, $identity) = array(false, null, false);
00211             if ($row[$contype] == 'p') {
00212                 $primary = true;
00213                 $primaryPosition = array_search($row[$attnum], explode(',', $row[$conkey])) + 1;
00214                 $identity = (bool) (preg_match('/^nextval/', $row[$default_value]));
00215             }
00216             $desc[$this->foldCase($row[$colname])] = array(
00217                 'SCHEMA_NAME'      => $this->foldCase($row[$nspname]),
00218                 'TABLE_NAME'       => $this->foldCase($row[$relname]),
00219                 'COLUMN_NAME'      => $this->foldCase($row[$colname]),
00220                 'COLUMN_POSITION'  => $row[$attnum],
00221                 'DATA_TYPE'        => $row[$type],
00222                 'DEFAULT'          => $defaultValue,
00223                 'NULLABLE'         => (bool) ($row[$notnull] != 't'),
00224                 'LENGTH'           => $row[$length],
00225                 'SCALE'            => null, // @todo
00226                 'PRECISION'        => null, // @todo
00227                 'UNSIGNED'         => null, // @todo
00228                 'PRIMARY'          => $primary,
00229                 'PRIMARY_POSITION' => $primaryPosition,
00230                 'IDENTITY'         => $identity
00231             );
00232         }
00233         return $desc;
00234     }
00235 
00236 
00245     public function limit($sql, $count, $offset = 0)
00246     {
00247         $count = intval($count);
00248         if ($count <= 0) {
00252             require_once 'Zend/Db/Adapter/Exception.php';
00253             throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid");
00254         }
00255 
00256         $offset = intval($offset);
00257         if ($offset < 0) {
00261             require_once 'Zend/Db/Adapter/Exception.php';
00262             throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
00263         }
00264 
00265         $sql .= " LIMIT $count";
00266         if ($offset > 0) {
00267             $sql .= " OFFSET $offset";
00268         }
00269 
00270         return $sql;
00271     }
00272 
00281     public function lastSequenceId($sequenceName)
00282     {
00283         $this->_connect();
00284         $value = $this->fetchOne("SELECT CURRVAL(".$this->quote($sequenceName).")");
00285         return $value;
00286     }
00287 
00296     public function nextSequenceId($sequenceName)
00297     {
00298         $this->_connect();
00299         $value = $this->fetchOne("SELECT NEXTVAL(".$this->quote($sequenceName).")");
00300         return $value;
00301     }
00302 
00317     public function lastInsertId($tableName = null, $primaryKey = null)
00318     {
00319         if ($tableName !== null) {
00320             $sequenceName = $tableName;
00321             if ($primaryKey) {
00322                 $sequenceName .= "_$primaryKey";
00323             }
00324             $sequenceName .= '_seq';
00325             return $this->lastSequenceId($sequenceName);
00326         }
00327         return $this->_connection->lastInsertId($tableName);
00328     }
00329 
00330 }

Generated on Thu Apr 19 2012 17:01:17 for openbiz by  doxygen 1.7.2