/var/www/(Del)gepgroup.hk/php-activerecord/lib/adapters/PgsqlAdapter.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
<?php
/**
 * @package ActiveRecord
 */
namespace ActiveRecord;

/**
 * Adapter for Postgres (not completed yet)
 * 
 * @package ActiveRecord
 */
class PgsqlAdapter extends Connection
{
    static 
$QUOTE_CHARACTER '"';
    static 
$DEFAULT_PORT 5432;

    public function 
supports_sequences()
    {
        return 
true;
    }

    public function 
get_sequence_name($table$column_name)
    {
        return 
"{$table}_{$column_name}_seq";
    }

    public function 
next_sequence_value($sequence_name)
    {
        return 
"nextval('" str_replace("'","\\'",$sequence_name) . "')";
    }

    public function 
limit($sql$offset$limit)
    {
        return 
$sql ' LIMIT ' intval($limit) . ' OFFSET ' intval($offset);
    }

    public function 
query_column_info($table)
    {
        
$sql = <<<SQL
SELECT
      a.attname AS field,
      a.attlen,
      REPLACE(pg_catalog.format_type(a.atttypid, a.atttypmod), 'character varying', 'varchar') AS type,
      a.attnotnull AS not_nullable,
      (SELECT 't'
        FROM pg_index
        WHERE c.oid = pg_index.indrelid
        AND a.attnum = ANY (pg_index.indkey)
        AND pg_index.indisprimary = 't'
      ) IS NOT NULL AS pk,      
      REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE((SELECT pg_attrdef.adsrc
        FROM pg_attrdef
        WHERE c.oid = pg_attrdef.adrelid
        AND pg_attrdef.adnum=a.attnum
      ),'::[a-z_ ]+',''),'''$',''),'^''','') AS default
FROM pg_attribute a, pg_class c, pg_type t
WHERE c.relname = ?
      AND a.attnum > 0
      AND a.attrelid = c.oid
      AND a.atttypid = t.oid
ORDER BY a.attnum
SQL;
        
$values = array($table);
        return 
$this->query($sql,$values);
    }

    public function 
query_for_tables()
    {
        return 
$this->query("SELECT tablename FROM pg_tables WHERE schemaname NOT IN('information_schema','pg_catalog')");
    }

    public function 
create_column(&$column)
    {
        
$c = new Column();
        
$c->inflected_name    Inflector::instance()->variablize($column['field']);
        
$c->name            $column['field'];
        
$c->nullable        = ($column['not_nullable'] ? false true);
        
$c->pk                = ($column['pk'] ? true false);
        
$c->auto_increment    false;

        if (
substr($column['type'],0,9) == 'timestamp')
        {
            
$c->raw_type 'datetime';
            
$c->length 19;
        }
        elseif (
$column['type'] == 'date')
        {
            
$c->raw_type 'date';
            
$c->length 10;
        }
        else
        {
            
preg_match('/^([A-Za-z0-9_]+)(\(([0-9]+(,[0-9]+)?)\))?/',$column['type'],$matches);

            
$c->raw_type = (count($matches) > $matches[1] : $column['type']);
            
$c->length count($matches) >= intval($matches[3]) : intval($column['attlen']);

            if (
$c->length 0)
                
$c->length null;
        }

        
$c->map_raw_type();

        if (
$column['default'])
        {
            
preg_match("/^nextval\('(.*)'\)$/",$column['default'],$matches);

            if (
count($matches) == 2)
                
$c->sequence $matches[1];
            else
                
$c->default $c->cast($column['default'],$this);
        }
        return 
$c;
    }

    public function 
set_encoding($charset)
    {
        
$this->query("SET NAMES '$charset'");
    }

    public function 
native_database_types()
    {
        return array(
            
'primary_key' => 'serial primary key',
            
'string' => array('name' => 'character varying''length' => 255),
            
'text' => array('name' => 'text'),
            
'integer' => array('name' => 'integer'),
            
'float' => array('name' => 'float'),
            
'datetime' => array('name' => 'datetime'),
            
'timestamp' => array('name' => 'timestamp'),
            
'time' => array('name' => 'time'),
            
'date' => array('name' => 'date'),
            
'binary' => array('name' => 'binary'),
            
'boolean' => array('name' => 'boolean')
        );
    }

}
?>