~desarrollokumbia/kumbia/ActiveRecord

« back to all changes in this revision

Viewing changes to db_pool/adapthers/pgsql_db.php

  • Committer: Deivinson Tejeda
  • Date: 2009-12-28 16:13:28 UTC
  • Revision ID: deivinsontejeda@gmail.com-20091228161328-w3mbndugqdgeck08
->Añadiendo implementacion del describe en pgsql
->Validando que este cargada la extension PDO
->Utilizando bloque try - catch

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 * @copyright  Copyright (c) 2005-2009 Kumbia Team (http://www.kumbiaphp.com)
20
20
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
21
21
 */
22
 
 
23
22
class PgsqlDb extends DbAdapther
24
23
{
25
 
 
 
24
    /**
 
25
     * Obtiene la metadata de una Tabla
 
26
     * 
 
27
     * @param string $schema
 
28
     * @param string $table
 
29
     * @return Rows
 
30
     * 
 
31
     */
 
32
    public function describe($table, $schema=null)
 
33
    {
 
34
        $sql = "SELECT c.column_name as name,
 
35
                CASE
 
36
                WHEN ct.constraint_type='PRIMARY KEY' THEN 'PRI'
 
37
                WHEN ct.constraint_type='UNIQUE' THEN 'UNI'
 
38
                WHEN ct.constraint_type='FOREIGN KEY' THEN 'FK'
 
39
                WHEN ct.constraint_type='CHECK' THEN 'CHK'
 
40
                ELSE '' END AS Index,
 
41
                c.column_default as Default, 
 
42
                c.is_nullable as Null, 
 
43
                c.udt_name as Type,
 
44
                CASE 
 
45
                WHEN c.character_maximum_length is null THEN (c.numeric_precision) ELSE c.character_maximum_length END as length
 
46
                FROM information_schema.columns c
 
47
                LEFT JOIN information_schema.constraint_column_usage cu ON
 
48
                cu.table_catalog = c.table_catalog AND cu.table_schema = c.table_schema AND cu.table_name = c.table_name
 
49
                AND cu.column_name = c.column_name
 
50
                LEFT JOIN information_schema.table_constraints ct ON
 
51
                ct.constraint_name = cu.constraint_name
 
52
                WHERE c.table_catalog = :database AND c.table_schema = :schema AND c.table_name = :table
 
53
                ORDER BY c.ordinal_position";
 
54
        try {
 
55
            $prepare = $this->pdo()->prepare($sql);
 
56
            //ejecutando la consulta preparada
 
57
            $results = $prepare->execute(array('database'=>'test', 'schema'=>'public', 'table'=>'prueba'));
 
58
            if ($results) {
 
59
                require_once CORE_PATH . 'libs/ActiveRecord/db_pool/rows.php';
 
60
                $row = new Rows();
 
61
                while ($field = $prepare->fetchObject()) {
 
62
                    //Nombre del Campo
 
63
                    $column = $row->column($field->name);
 
64
                    $column->setAlias($field->name);
 
65
                    //valor por defecto
 
66
                    if (! is_null($field->default)) {
 
67
                        if (strpos($field->default, 'nextval(') !== FALSE) {
 
68
                            $column->_isAutoIncrement = TRUE;
 
69
                        } elseif ($field->type == 'serial' || $field->type == 'bigserial') {
 
70
                            $column->_isAutoIncrement = TRUE;
 
71
                        } else {
 
72
                            $column->_default = $field->default;
 
73
                        }
 
74
                    }
 
75
                    //puede ser null?
 
76
                    if($field->null == 'NO'){
 
77
                        $column->_isNull = FALSE;
 
78
                    }
 
79
                    //Relaciones
 
80
                    if(substr($field->name, strlen($field->name) -3, 3) == '_id'){
 
81
                        $column->_relation = substr($field->name, 0, -3);
 
82
                        $row->setRelation($field->name, $column->_relation);
 
83
                    }
 
84
                    //tipo de dato
 
85
                    $column->_dbType = $field->type;
 
86
                    //longitud
 
87
                    $column->_length = $field->length;
 
88
                    //indices
 
89
                    switch ($field->index){
 
90
                        case 'PRI':
 
91
                            $row->setPK($field->name);
 
92
                            break;
 
93
                        case 'FK':
 
94
                            $row->setFK($field->name);
 
95
                            break;
 
96
                        case 'UNI':
 
97
                            $column->_isUni = TRUE;
 
98
                            break;
 
99
                    }
 
100
                }
 
101
            }
 
102
        } catch (PDOException $e) {
 
103
            throw new KumbiaException($e->getMessage());
 
104
        }
 
105
        return $row;
 
106
    }
26
107
}