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_Base_Column
 25: {
 26:     protected $_name;
 27:     protected $_type;
 28:     protected $_null;
 29:     protected $_limit;
 30:     protected $_precision;
 31:     protected $_scale;
 32:     protected $_unsigned;
 33:     protected $_default;
 34:     protected $_sqlType;
 35:     protected $_isText;
 36:     protected $_isNumber;
 37: 
 38: 
 39:     /*##########################################################################
 40:     # Construct/Destruct
 41:     ##########################################################################*/
 42: 
 43:     /**
 44:      * Constructor.
 45:      *
 46:      * @param string $name     The column's name, such as "supplier_id" in
 47:      *                         "supplier_id int(11)".
 48:      * @param string $default  The type-casted default value, such as "new" in
 49:      *                         "sales_stage varchar(20) default 'new'".
 50:      * @param string $sqlType  Used to extract the column's type, length and
 51:      *                         signed status, if necessary. For example
 52:      *                         "varchar" and "60" in "company_name varchar(60)"
 53:      *                         or "unsigned => true" in "int(10) UNSIGNED".
 54:      * @param boolean $null    Whether this column allows NULL values.
 55:      */
 56:     public function __construct($name, $default, $sqlType = null, $null = true)
 57:     {
 58:         $this->_name      = $name;
 59:         $this->_sqlType   = $sqlType;
 60:         $this->_null      = $null;
 61: 
 62:         $this->_limit     = $this->_extractLimit($sqlType);
 63:         $this->_precision = $this->_extractPrecision($sqlType);
 64:         $this->_scale     = $this->_extractScale($sqlType);
 65:         $this->_unsigned  = $this->_extractUnsigned($sqlType);
 66: 
 67:         $this->_type      = $this->_simplifiedType($sqlType);
 68:         $this->_isText    = $this->_type == 'text'  || $this->_type == 'string';
 69:         $this->_isNumber  = $this->_type == 'float' || $this->_type == 'integer' || $this->_type == 'decimal';
 70: 
 71:         $this->_default   = $this->extractDefault($default);
 72:     }
 73: 
 74:     /**
 75:      * @return  boolean
 76:      */
 77:     public function isText()
 78:     {
 79:         return $this->_isText;
 80:     }
 81: 
 82:     /**
 83:      * @return  boolean
 84:      */
 85:     public function isNumber()
 86:     {
 87:         return $this->_isNumber;
 88:     }
 89: 
 90:     /**
 91:      * Casts value (which is a String) to an appropriate instance.
 92:      */
 93:     public function typeCast($value)
 94:     {
 95:         if ($value === null) return null;
 96: 
 97:         switch ($this->_type) {
 98:         case 'string':
 99:         case 'text':
100:             return $value;
101:         case 'integer':
102:             return strlen($value) ? (int)$value : null;
103:         case 'float':
104:             return strlen($value) ? (float)$value : null;
105:         case 'decimal':
106:             return $this->valueToDecimal($value);
107:         case 'datetime':
108:         case 'timestamp':
109:             return $this->stringToTime($value);
110:         case 'time':
111:             return $this->stringToDummyTime($value);
112:         case 'date':
113:             return $this->stringToDate($value);
114:         case 'binary':
115:             return $this->binaryToString($value);
116:         case 'boolean':
117:             return $this->valueToBoolean($value);
118:         default:
119:             return $value;
120:         }
121:     }
122: 
123:     public function extractDefault($default)
124:     {
125:         return $this->typeCast($default);
126:     }
127: 
128: 
129:     /*##########################################################################
130:     # Accessor
131:     ##########################################################################*/
132: 
133:     /**
134:      * @return  string
135:      */
136:     public function getName()
137:     {
138:         return $this->_name;
139:     }
140: 
141:     /**
142:      * @return  string
143:      */
144:     public function getDefault()
145:     {
146:         return $this->_default;
147:     }
148: 
149:     /**
150:      * @return  string
151:      */
152:     public function getType()
153:     {
154:         return $this->_type;
155:     }
156: 
157:     /**
158:      * @return  int
159:      */
160:     public function getLimit()
161:     {
162:         return $this->_limit;
163:     }
164: 
165:     /**
166:      * @return  int
167:      */
168:     public function precision()
169:     {
170:         return $this->_precision;
171:     }
172: 
173:     /**
174:      * @return  int
175:      */
176:     public function scale()
177:     {
178:         return $this->_scale;
179:     }
180: 
181:     /**
182:      * @return  boolean
183:      */
184:     public function isUnsigned()
185:     {
186:         return $this->_unsigned;
187:     }
188: 
189:     /**
190:      * @return  boolean
191:      */
192:     public function isNull()
193:     {
194:         return $this->_null;
195:     }
196: 
197:     /**
198:      * @return  string
199:      */
200:     public function getSqlType()
201:     {
202:         return $this->_sqlType;
203:     }
204: 
205: 
206:     /*##########################################################################
207:     # Type Juggling
208:     ##########################################################################*/
209: 
210:     /**
211:      * Used to convert from BLOBs to Strings
212:      *
213:      * @return  string
214:      */
215:     public function binaryToString($value)
216:     {
217:         return (string)$value;
218:     }
219: 
220:     /**
221:      * @param   string  $string
222:      * @return  Horde_Date
223:      */
224:     public function stringToDate($string)
225:     {
226:         if (empty($string) ||
227:             // preserve '0000-00-00' (http://bugs.php.net/bug.php?id=45647)
228:             preg_replace('/[^\d]/', '', $string) == 0) {
229:             return null;
230:         }
231: 
232:         $d = new Horde_Date($string);
233:         $d->setDefaultFormat('Y-m-d');
234: 
235:         return $d;
236:     }
237: 
238:     /**
239:      * @param   string  $string
240:      * @return  Horde_Date
241:      */
242:     public function stringToTime($string)
243:     {
244:         if (empty($string) ||
245:             // preserve '0000-00-00 00:00:00' (http://bugs.php.net/bug.php?id=45647)
246:             preg_replace('/[^\d]/', '', $string) == 0) {
247:             return null;
248:         }
249: 
250:         return new Horde_Date($string);
251:     }
252: 
253:     /**
254:      * @TODO Return a Horde_Date object instead?
255:      *
256:      * @param   string  $string
257:      * @return  Horde_Date
258:      */
259:     public function stringToDummyTime($value)
260:     {
261:         if (empty($string)) {
262:             return null;
263:         }
264:         return $this->stringToTime('2000-01-01 ' . $string);
265:     }
266: 
267:     /**
268:      * @param   mixed  $value
269:      * @return  boolean
270:      */
271:     public function valueToBoolean($value)
272:     {
273:         if ($value === true || $value === false) {
274:             return $value;
275:         }
276: 
277:         $value = strtolower($value);
278:         return $value == 'true' || $value == 't' || $value == '1';
279:     }
280: 
281:     /**
282:      * @param   mixed  $value
283:      * @return  decimal
284:      */
285:     public function valueToDecimal($value)
286:     {
287:         return (float)$value;
288:     }
289: 
290: 
291:     /*##########################################################################
292:     # Protected
293:     ##########################################################################*/
294: 
295:     /**
296:      * @param   string  $sqlType
297:      * @return  int
298:      */
299:     protected function _extractLimit($sqlType)
300:     {
301:         if (preg_match("/\((.*)\)/", $sqlType, $matches)) {
302:             return (int)$matches[1];
303:         }
304:         return null;
305:     }
306: 
307:     /**
308:      * @param   string  $sqlType
309:      * @return  int
310:      */
311:     protected function _extractPrecision($sqlType)
312:     {
313:         if (preg_match("/^(numeric|decimal|number)\((\d+)(,\d+)?\)/i", $sqlType, $matches)) {
314:             return (int)$matches[2];
315:         }
316:         return null;
317:     }
318: 
319:     /**
320:      * @param   string  $sqlType
321:      * @return  int
322:      */
323:     protected function _extractScale($sqlType)
324:     {
325:         switch (true) {
326:             case preg_match("/^(numeric|decimal|number)\((\d+)\)/i", $sqlType):
327:                 return 0;
328:             case preg_match("/^(numeric|decimal|number)\((\d+)(,(\d+))\)/i",
329:                             $sqlType, $match):
330:                 return (int)$match[4];
331:         }
332:     }
333: 
334:     /**
335:      * @param   string  $sqlType
336:      * @return  int
337:      */
338:     protected function _extractUnsigned($sqlType)
339:     {
340:         return (boolean)preg_match('/^int.*unsigned/i', $sqlType);
341:     }
342: 
343:     /**
344:      * @param   string  $fieldType
345:      * @return  string
346:      */
347:     protected function _simplifiedType($fieldType)
348:     {
349:         switch (true) {
350:         case preg_match('/int/i', $fieldType):
351:             return 'integer';
352:         case preg_match('/float|double/i', $fieldType):
353:             return 'float';
354:         case preg_match('/decimal|numeric|number/i', $fieldType):
355:             return $this->_scale == 0 ? 'integer' : 'decimal';
356:         case preg_match('/datetime/i', $fieldType):
357:             return 'datetime';
358:         case preg_match('/timestamp/i', $fieldType):
359:             return 'timestamp';
360:         case preg_match('/time/i', $fieldType):
361:             return 'time';
362:         case preg_match('/date/i', $fieldType):
363:             return 'date';
364:         case preg_match('/clob|text/i', $fieldType):
365:             return 'text';
366:         case preg_match('/blob|binary/i', $fieldType):
367:             return 'binary';
368:         case preg_match('/char|string/i', $fieldType):
369:             return 'string';
370:         case preg_match('/boolean/i', $fieldType):
371:             return 'boolean';
372:         }
373:     }
374: }
375: 
API documentation generated by ApiGen