Completed
Push — master ( 9bd8a0...c20901 )
by joanhey
01:43
created

PgsqlMetadata::queryFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
/**
3
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://wiki.kumbiaphp.com/Licencia
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to [email protected] so we can send you a copy immediately.
14
 *
15
 * @category   Kumbia
16
 * @package    ActiveRecord
17
 * @subpackage Metadata
18
 * @copyright  Copyright (c) 2005-2014  Kumbia Team (http://www.kumbiaphp.com)
19
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
20
 */
21
22
namespace Kumbia\ActiveRecord\Metadata;
23
24
use Kumbia\ActiveRecord\Db;
25
26
/**
27
 * Adaptador de Metadata para Pgsql
28
 *
29
 */
30
class PgsqlMetadata extends Metadata
31
{
32
    /**
33
     * Consultar los campos de la tabla en la base de datos
34
     *
35
     * @param  string $database base de datos
36
     * @param  string $table    tabla
37
     * @param  string $schema   squema
38
     * @return array
39
     */
40
    protected function queryFields($database, $table, $schema = 'public')
41
    {
42
43
        // Nota: Se excluyen claves compuestas
44
        $describe = Db::get($database)->query("
45
            SELECT DISTINCT
46
                c.column_name AS field,
47
                c.udt_name AS type,
48
                tc.constraint_type AS key,
49
                c.column_default AS default,
50
                c.is_nullable AS null
51
            FROM information_schema.columns c
52
            LEFT OUTER JOIN information_schema.key_column_usage cu ON (
53
                cu.column_name = c.column_name AND cu.table_name = c.table_name AND (
54
                    SELECT COUNT(*) FROM information_schema.key_column_usage
55
                    WHERE constraint_name = cu.constraint_name
56
                ) = 1)
57
            LEFT OUTER JOIN information_schema.table_constraints tc 
58
            ON (cu.constraint_name = tc.constraint_name AND tc.constraint_type IN ('PRIMARY KEY', 'UNIQUE'))
59
            WHERE c.table_name = '$table' AND c.table_schema = '$schema';
60
        ");
61
62
        return self::describe($describe);
63
    }
64
    
65
    /**
66
     * Genera la metadata
67
     *
68
     * @param  \PDOStatement $describe
69
     * @return array
70
     */
71
    private static function describe(\PDOStatement $describe)
72
    {
73
        $fields = array();
74
        foreach ($describe as $value) {
75
76
            $fields[$value['field']] = array(
77
                'Type' => $value['type'],
78
                'Null' => $value['null'] != 'NO',
79
                'Default' => $value['default'] != '',
80
                'Key' => \substr($value['key'], 0, 3),
81
                'Auto' => \preg_match('/^nextval\(/', $value['default'])
82
            );
83
        }
84
85
        return $fields;
86
    }
87
}
88