/var/www/(Del)gepgroup.hk/php-activerecord/lib/Column.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
 * @package ActiveRecord
 */
namespace ActiveRecord;

/**
 * Class for a table column.
 *
 * @package ActiveRecord
 */
class Column
{
    
// types for $type
    
const STRING    1;
    const 
INTEGER    2;
    const 
DECIMAL    3;
    const 
DATETIME    4;
    const 
DATE        5;
    const 
TIME        6;

    
/**
     * Map a type to an column type.
     * @static
     * @var array
     */
    
static $TYPE_MAPPING = array(
        
'datetime'    => self::DATETIME,
        
'timestamp'    => self::DATETIME,
        
'date'        => self::DATE,
        
'time'        => self::TIME,

        
'int'        => self::INTEGER,
        
'tinyint'    => self::INTEGER,
        
'smallint'    => self::INTEGER,
        
'mediumint'    => self::INTEGER,
        
'bigint'    => self::INTEGER,

        
'float'        => self::DECIMAL,
        
'double'    => self::DECIMAL,
        
'numeric'    => self::DECIMAL,
        
'decimal'    => self::DECIMAL,
        
'dec'        => self::DECIMAL);

    
/**
     * The true name of this column.
     * @var string
     */
    
public $name;

    
/**
     * The inflected name of this columns .. hyphens/spaces will be => _.
     * @var string
     */
    
public $inflected_name;

    
/**
     * The type of this column: STRING, INTEGER, ...
     * @var integer
     */
    
public $type;

    
/**
     * The raw database specific type.
     * @var string
     */
    
public $raw_type;

    
/**
     * The maximum length of this column.
     * @var int
     */
    
public $length;

    
/**
     * True if this column allows null.
     * @var boolean
     */
    
public $nullable;

    
/**
     * True if this column is a primary key.
     * @var boolean
     */
    
public $pk;

    
/**
     * The default value of the column.
     * @var mixed
     */
    
public $default;

    
/**
     * True if this column is set to auto_increment.
     * @var boolean
     */
    
public $auto_increment;

    
/**
     * Name of the sequence to use for this column if any.
     * @var boolean
     */
    
public $sequence;

    
/**
     * Casts a value to the column's type.
     *
     * @param mixed $value The value to cast
     * @param Connection $connection The Connection this column belongs to
     * @return mixed type-casted value
     */
    
public function cast($value$connection)
    {
        if (
$value === null)
            return 
null;

        switch (
$this->type)
        {
            case 
self::STRING:    return (string)$value;
            case 
self::INTEGER:    return (int)$value;
            case 
self::DECIMAL:    return (double)$value;
            case 
self::DATETIME:
            case 
self::DATE:
                if (!
$value)
                    return 
null;

                if (
$value instanceof DateTime)
                    return 
$value;

                if (
$value instanceof \DateTime)
                    return new 
DateTime($value->format('Y-m-d H:i:s T'));

                return 
$connection->string_to_datetime($value);
        }
        return 
$value;
    }

    
/**
     * Sets the $type member variable.
     * @return mixed
     */
    
public function map_raw_type()
    {
        if (
$this->raw_type == 'integer')
            
$this->raw_type 'int';

        if (
array_key_exists($this->raw_type,self::$TYPE_MAPPING))
            
$this->type self::$TYPE_MAPPING[$this->raw_type];
        else
            
$this->type self::STRING;

        return 
$this->type;
    }
}
?>