Overview

Packages

  • Db
    • Adapter
    • Migration

Classes

  • Horde_Db_Adapter_Base
  • Horde_Db_Adapter_Base_Column
  • Horde_Db_Adapter_Base_ColumnDefinition
  • Horde_Db_Adapter_Base_Index
  • Horde_Db_Adapter_Base_Schema
  • Horde_Db_Adapter_Base_Table
  • Horde_Db_Adapter_Base_TableDefinition
  • Horde_Db_Adapter_Mysql
  • Horde_Db_Adapter_Mysql_Column
  • Horde_Db_Adapter_Mysql_Result
  • Horde_Db_Adapter_Mysql_Schema
  • Horde_Db_Adapter_Mysqli
  • Horde_Db_Adapter_Mysqli_Result
  • Horde_Db_Adapter_Pdo_Base
  • Horde_Db_Adapter_Pdo_Mysql
  • Horde_Db_Adapter_Pdo_Pgsql
  • Horde_Db_Adapter_Pdo_Sqlite
  • Horde_Db_Adapter_Postgresql_Column
  • Horde_Db_Adapter_Postgresql_Schema
  • Horde_Db_Adapter_SplitRead
  • Horde_Db_Adapter_Sqlite_Column
  • Horde_Db_Adapter_Sqlite_Schema

Interfaces

  • Horde_Db_Adapter
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Copyright 2007 Maintainable Software, LLC
  4:  * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
  5:  *
  6:  * @author     Mike Naberezny <mike@maintainable.com>
  7:  * @author     Derek DeVries <derek@maintainable.com>
  8:  * @author     Chuck Hagenbuch <chuck@horde.org>
  9:  * @license    http://www.horde.org/licenses/bsd
 10:  * @category   Horde
 11:  * @package    Db
 12:  * @subpackage Adapter
 13:  */
 14: 
 15: /**
 16:  * @author     Mike Naberezny <mike@maintainable.com>
 17:  * @author     Derek DeVries <derek@maintainable.com>
 18:  * @author     Chuck Hagenbuch <chuck@horde.org>
 19:  * @license    http://www.horde.org/licenses/bsd
 20:  * @category   Horde
 21:  * @package    Db
 22:  * @subpackage Adapter
 23:  */
 24: class Horde_Db_Adapter_Postgresql_Column extends Horde_Db_Adapter_Base_Column
 25: {
 26:     /*##########################################################################
 27:     # Constants
 28:     ##########################################################################*/
 29: 
 30:     /**
 31:      * The internal PostgreSQL identifier of the money data type.
 32:      * @const integer
 33:      */
 34:     const MONEY_COLUMN_TYPE_OID = 790;
 35: 
 36: 
 37:     /**
 38:      * @var integer
 39:      */
 40:     public static $moneyPrecision = 19;
 41: 
 42:     /**
 43:      * @var array
 44:      */
 45:     protected static $_hasEmptyStringDefault = array('binary', 'string', 'text');
 46: 
 47: 
 48:     /**
 49:      * Construct
 50:      * @param   string  $name
 51:      * @param   string  $default
 52:      * @param   string  $sqlType
 53:      * @param   boolean $null
 54:      */
 55:     public function __construct($name, $default, $sqlType=null, $null=true)
 56:     {
 57:         parent::__construct($name, $this->_extractValueFromDefault($default), $sqlType, $null);
 58:     }
 59: 
 60:     /**
 61:      * @param   string  $fieldType
 62:      * @return  string
 63:      */
 64:     protected function _simplifiedType($fieldType)
 65:     {
 66:         switch (true) {
 67:             // Numeric and monetary types
 68:             case preg_match('/^(?:real|double precision)$/', $fieldType):
 69:                 return 'float';
 70:             // Monetary types
 71:             case preg_match('/^money$/', $fieldType):
 72:                 return 'decimal';
 73:             // Character types
 74:             case preg_match('/^(?:character varying|bpchar)(?:\(\d+\))?$/', $fieldType):
 75:                 return 'string';
 76:             // Binary data types
 77:             case preg_match('/^bytea$/', $fieldType):
 78:                 return 'binary';
 79:             // Date/time types
 80:             case preg_match('/^timestamp with(?:out)? time zone$/', $fieldType):
 81:                 return 'datetime';
 82:             case preg_match('/^interval$/', $fieldType):
 83:                 return 'string';
 84:             // Geometric types
 85:             case preg_match('/^(?:point|line|lseg|box|"?path"?|polygon|circle)$/', $fieldType):
 86:                 return 'string';
 87:             // Network address types
 88:             case preg_match('/^(?:cidr|inet|macaddr)$/', $fieldType):
 89:                 return 'string';
 90:             // Bit strings
 91:             case preg_match('/^bit(?: varying)?(?:\(\d+\))?$/', $fieldType):
 92:                 return 'string';
 93:             // XML type
 94:             case preg_match('/^xml$/', $fieldType):
 95:                 return 'string';
 96:             // Arrays
 97:             case preg_match('/^\D+\[\]$/', $fieldType):
 98:                 return 'string';
 99:             // Object identifier types
100:             case preg_match('/^oid$/', $fieldType):
101:                 return 'integer';
102:         }
103: 
104:         // Pass through all types that are not specific to PostgreSQL.
105:         return parent::_simplifiedType($fieldType);
106:     }
107: 
108:     /**
109:      * Extracts the value from a PostgreSQL column default definition.
110:      */
111:     protected function _extractValueFromDefault($default)
112:     {
113:         switch (true) {
114:             // Numeric types
115:             case preg_match('/\A-?\d+(\.\d*)?\z/', $default):
116:                 return $default;
117:             // Character types
118:             case preg_match('/\A\'(.*)\'::(?:character varying|bpchar|text)\z/m', $default, $matches):
119:                 return $matches[1];
120:             // Character types (8.1 formatting)
121:             case preg_match('/\AE\'(.*)\'::(?:character varying|bpchar|text)\z/m', $default, $matches):
122:                 /*@TODO fix preg callback*/
123:                 return preg_replace('/\\(\d\d\d)/', '$1.oct.chr', $matches[1]);
124:             // Binary data types
125:             case preg_match('/\A\'(.*)\'::bytea\z/m', $default, $matches):
126:                 return $matches[1];
127:             // Date/time types
128:             case preg_match('/\A\'(.+)\'::(?:time(?:stamp)? with(?:out)? time zone|date)\z/', $default, $matches):
129:                 return $matches[1];
130:             case preg_match('/\A\'(.*)\'::interval\z/', $default, $matches):
131:                 return $matches[1];
132:             // Boolean type
133:             case $default == 'true':
134:                 return true;
135:             case $default == 'false':
136:                 return false;
137:             // Geometric types
138:             case preg_match('/\A\'(.*)\'::(?:point|line|lseg|box|"?path"?|polygon|circle)\z/', $default, $matches):
139:                 return $matches[1];
140:             // Network address types
141:             case preg_match('/\A\'(.*)\'::(?:cidr|inet|macaddr)\z/', $default, $matches):
142:                 return $matches[1];
143:             // Bit string types
144:             case preg_match('/\AB\'(.*)\'::"?bit(?: varying)?"?\z/', $default, $matches):
145:                 return $matches[1];
146:             // XML type
147:             case preg_match('/\A\'(.*)\'::xml\z/m', $default, $matches):
148:                 return $matches[1];
149:             // Arrays
150:             case preg_match('/\A\'(.*)\'::"?\D+"?\[\]\z/', $default, $matches):
151:                 return $matches[1];
152:             // Object identifier types
153:             case preg_match('/\A-?\d+\z/', $default, $matches):
154:                 return $matches[1];
155:             default:
156:                 // Anything else is blank, some user type, or some function
157:                 // and we can't know the value of that, so return nil.
158:                 return null;
159:         }
160:     }
161: 
162:     /**
163:      * Used to convert from BLOBs (BYTEAs) to Strings.
164:      *
165:      * @return  string
166:      */
167:     public function binaryToString($value)
168:     {
169:         if (is_resource($value)) {
170:             $string = stream_get_contents($value);
171:             fclose($value);
172:             return $string;
173:         }
174: 
175:         return preg_replace_callback("/(?:\\\'|\\\\\\\\|\\\\\d{3})/", array($this, 'binaryToStringCallback'), $value);
176:     }
177: 
178:     /**
179:      * Callback function for binaryToString().
180:      */
181:     public function binaryToStringCallback($matches)
182:     {
183:         if ($matches[0] == '\\\'') {
184:             return "'";
185:         } elseif ($matches[0] == '\\\\\\\\') {
186:             return '\\';
187:         }
188: 
189:         return chr(octdec(substr($matches[0], -3)));
190:     }
191: 
192: 
193:     /*##########################################################################
194:     # Protected
195:     ##########################################################################*/
196: 
197:     /**
198:      * @param   string  $sqlType
199:      * @return  int
200:      */
201:     protected function _extractLimit($sqlType)
202:     {
203:         if (preg_match('/^bigint/i', $sqlType)) {
204:             return 8;
205:         }
206:         if (preg_match('/^smallint/i', $sqlType)) {
207:             return 2;
208:         }
209:         return parent::_extractLimit($sqlType);
210:     }
211: 
212:     /**
213:      * @param   string  $sqlType
214:      * @return  int
215:      */
216:     protected function _extractPrecision($sqlType)
217:     {
218:         if (preg_match('/^money/', $sqlType)) {
219:             return self::$moneyPrecision;
220:         }
221:         return parent::_extractPrecision($sqlType);
222:     }
223: 
224:     /**
225:      * @param   string  $sqlType
226:      * @return  int
227:      */
228:     protected function _extractScale($sqlType)
229:     {
230:         if (preg_match('/^money/', $sqlType)) {
231:             return 2;
232:         }
233:         return parent::_extractScale($sqlType);
234:     }
235: 
236: }
237: 
API documentation generated by ApiGen