1<?php
2/**
3 * Copyright 2007 Maintainable Software, LLC
4 * Copyright 2008-2017 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 */
24class Horde_Db_Adapter_Mysql_Column extends Horde_Db_Adapter_Base_Column
25{
26    /**
27     * @var array
28     */
29    protected $_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     */
55    protected function _setSimplifiedType()
56    {
57        if (strpos(Horde_String::lower($this->_sqlType), 'tinyint(1)') !== false) {
58            $this->_type = 'boolean';
59            return;
60        } elseif (preg_match('/enum/i', $this->_sqlType)) {
61            $this->_type = 'string';
62            return;
63        }
64        parent::_setSimplifiedType();
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, $this->_hasEmptyStringDefault);
82    }
83
84}
85