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_Mysql_Column extends Horde_Db_Adapter_Base_Column
25: {
26: /**
27: * @var array
28: */
29: protected static $_hasEmptyStringDefault = array('binary', 'string', 'text');
30:
31: /**
32: * @var string
33: */
34: protected $_originalDefault = null;
35:
36: /**
37: * Construct
38: * @param string $name
39: * @param string $default
40: * @param string $sqlType
41: * @param boolean $null
42: */
43: public function __construct($name, $default, $sqlType=null, $null=true)
44: {
45: $this->_originalDefault = $default;
46: parent::__construct($name, $default, $sqlType, $null);
47:
48: if ($this->_isMissingDefaultForgedAsEmptyString()) {
49: $this->_default = null;
50: }
51: }
52:
53: /**
54: * @param string $fieldType
55: * @return string
56: */
57: protected function _simplifiedType($fieldType)
58: {
59: if (strpos(strtolower($fieldType), 'tinyint(1)') !== false) {
60: return 'boolean';
61: } elseif (preg_match('/enum/i', $fieldType)) {
62: return 'string';
63: }
64: return parent::_simplifiedType($fieldType);
65: }
66:
67: /**
68: * MySQL misreports NOT NULL column default when none is given.
69: * We can't detect this for columns which may have a legitimate ''
70: * default (string, text, binary) but we can for others (integer,
71: * datetime, boolean, and the rest).
72: *
73: * Test whether the column has default '', is not null, and is not
74: * a type allowing default ''.
75: *
76: * @return boolean
77: */
78: protected function _isMissingDefaultForgedAsEmptyString()
79: {
80: return !$this->_null && $this->_originalDefault == '' &&
81: !in_array($this->_type, self::$_hasEmptyStringDefault);
82: }
83:
84: }
85: