PgsqlMetadata   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A queryFields() 0 25 1
A describe() 0 16 2
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
 *
17
 * @copyright  2005 - 2016  Kumbia Team (http://www.kumbiaphp.com)
18
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
19
 */
20
namespace Kumbia\ActiveRecord\Metadata;
21
22
use Kumbia\ActiveRecord\Db;
23
24
/**
25
 * Adaptador de Metadata para Pgsql.
26
 */
27
class PgsqlMetadata extends Metadata
28
{
29
    /**
30
     * Consultar los campos de la tabla en la base de datos.
31
     *
32
     * @param string $database base de datos
33
     * @param string $table    tabla
34
     * @param string $schema   squema
35
     *
36
     * @return array
37
     */
38
    protected function queryFields($database, $table, $schema = 'public')
39
    {
40
41
        // Nota: Se excluyen claves compuestas
42
        $describe = Db::get($database)->query(
43
            "SELECT DISTINCT
44
                c.column_name AS field,
45
                c.udt_name AS type,
46
                tc.constraint_type AS key,
47
                c.column_default AS default,
48
                c.is_nullable AS null
49
            FROM information_schema.columns c
50
            LEFT OUTER JOIN information_schema.key_column_usage cu ON (
51
                cu.column_name = c.column_name AND cu.table_name = c.table_name AND (
52
                    SELECT COUNT(*) FROM information_schema.key_column_usage
53
                    WHERE constraint_name = cu.constraint_name
54
                ) = 1)
55
            LEFT OUTER JOIN information_schema.table_constraints tc
56
            ON (cu.constraint_name = tc.constraint_name AND tc.constraint_type
57
            IN ('PRIMARY KEY', 'UNIQUE'))
58
            WHERE c.table_name = '$table' AND c.table_schema = '$schema';"
59
        );
60
61
        return self::describe($describe);
62
    }
63
64
    /**
65
     * Genera la metadata.
66
     *
67
     * @param \PDOStatement $describe
68
     *
69
     * @return array
70
     */
71
    private static function describe(\PDOStatement $describe)
72
    {
73
        $fields = [];
74
        // TODO mejorar este código
75
        foreach ($describe as $value) {
76
            $fields[$value['field']] = [
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