[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/dbal/src/drivers/pgsql/ -> lmbPgsqlTableInfo.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/dbal/src/drivers/lmbDbTableInfo.class.php');
  10  lmb_require('limb/dbal/src/drivers/pgsql/lmbPgsqlColumnInfo.class.php');
  11  
  12  /**

  13   * class lmbPgsqlTableInfo.

  14   *

  15   * @package dbal

  16   * @version $Id: lmbPgsqlTableInfo.class.php 5945 2007-06-06 08:31:43Z pachanga $

  17   */
  18  class lmbPgsqlTableInfo extends lmbDbTableInfo
  19  {
  20    protected $database;
  21  
  22    protected $isExisting = false;
  23    protected $isColumnsLoaded = false;
  24  
  25    protected $oid;
  26  
  27    function __construct($database, $name, $isExisting = false, $oid = 1)
  28    {
  29      parent::__construct($name);
  30      $this->database = $database;
  31      $this->isExisting = $isExisting;
  32      $this->oid = $oid;
  33    }
  34  
  35    //Based on code from Creole
  36    function loadColumns()
  37    {
  38  
  39      if($this->isExisting && !$this->isColumnsLoaded)
  40      {
  41        $connection = $this->database->getConnection();
  42  
  43        $result = $connection->execute(sprintf("SELECT
  44                                                  att.attname,
  45                                                  att.atttypmod,
  46                                                  att.atthasdef,
  47                                                  att.attnotnull,
  48                                                  def.adsrc,
  49                                                  CASE WHEN att.attndims > 0 THEN 1 ELSE 0 END AS isarray,
  50                                                  CASE
  51                                                    WHEN ty.typname = 'bpchar'
  52                                                      THEN 'char'
  53                                                    WHEN ty.typname = '_bpchar'
  54                                                      THEN '_char'
  55                                                    ELSE
  56                                                      ty.typname
  57                                                  END AS typname,
  58                                                  ty.typtype
  59                                                  FROM pg_attribute att
  60                                                  JOIN pg_type ty ON ty.oid=att.atttypid
  61                                                  LEFT OUTER JOIN pg_attrdef def ON adrelid=att.attrelid AND adnum=att.attnum
  62                                                  WHERE att.attrelid = %d AND att.attnum > 0
  63                                                  AND att.attisdropped IS false
  64                                                  ORDER BY att.attnum", $this->oid));
  65  
  66        while($row = pg_fetch_assoc($result))
  67        {
  68          if(((int) $row['isarray']) === 1)
  69          {
  70            $connection->_raiseError(sprintf("Array datatypes are not currently supported [%s.%s]", $this->name, $row['attname']));
  71          }
  72  
  73          $name = $row['attname'];
  74          if(strtolower($row['typtype']) == 'd')
  75          {
  76            //not supported yet
  77  
  78          }
  79          else
  80          {
  81            $type = $row['typname'];
  82            $arrLengthPrecision = $this->_processLengthPrecision($row['atttypmod'], $type);
  83            $size = $arrLengthPrecision['length'];
  84            $scale = $arrLengthPrecision['precision'];
  85            $boolHasDefault = $row['atthasdef'];
  86            $default = $row['adsrc'];
  87            $isNullable =(($row['attnotnull'] == 't') ?  false : true);
  88          }
  89  
  90          $isAutoIncrement = null;
  91  
  92          if(($boolHasDefault == 't') &&(strlen(trim($default)) > 0))
  93          {
  94            if(!preg_match('/^nextval\(/', $default))
  95            {
  96              $strDefault= preg_replace('/::[\W\D]*/', '', $default);
  97              $default = str_replace("'", '', $strDefault);
  98            }
  99            else
 100            {
 101              $isAutoIncrement = true;
 102              $default = null;
 103            }
 104          }
 105          else
 106          {
 107            $default = null;
 108          }
 109  
 110          $this->columns[$name] = new lmbPgsqlColumnInfo($this,
 111                 $name, $type, $size, $scale, $isNullable, $default, $isAutoIncrement);
 112        }
 113  
 114        $this->isColumnsLoaded = true;
 115      }
 116    }
 117  
 118    function getDatabase()
 119    {
 120      return $this->database;
 121    }
 122  
 123    private function _processLengthPrecision($intTypmod, $strName)
 124    {
 125      $arrRetVal = array('length' => null, 'precision' => null);
 126  
 127      // Some datatypes don't have a Typmod
 128      if($intTypmod == -1)
 129      {
 130        return $arrRetVal;
 131      }
 132  
 133      // Numeric Datatype?
 134      if($strName == "numeric")
 135      {
 136        $intLen =($intTypmod - 4) >> 16;
 137        $intPrec =($intTypmod - 4) & 0xffff;
 138        $intLen = sprintf("%ld", $intLen);
 139        if($intPrec)
 140        {
 141          $intPrec = sprintf("%ld", $intPrec);
 142        }
 143        $arrRetVal['length'] = $intLen;
 144        $arrRetVal['precision'] = $intPrec;
 145      }
 146      elseif($strName == "time" || $strName == 'timetz'
 147          || $strName == "timestamp" || $strName == 'timestamptz'
 148          || $strName == 'interval' || $strName == 'bit')
 149      {
 150        $arrRetVal['length'] = sprintf("%ld", $intTypmod);
 151      }
 152      else
 153      {
 154        $arrRetVal['length'] = sprintf("%ld",($intTypmod - 4));
 155      }
 156      return $arrRetVal;
 157    }
 158  }
 159  
 160  ?>


Generated: Sat Nov 22 03:48:54 2008 Cross-referenced by PHPXref 0.7