PgsqlMetadata   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A queryFields() 0 28 2
A describe() 0 17 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 - 2020  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 \PDO;
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  \PDO    $pdo      base de datos
33
     * @param  string  $table    tabla
34
     * @param  string  $schema   esquema, por defecto 'public'
35
     * 
36
     * @return array
37
     */
38
    protected function queryFields(\PDO $pdo, string $table, string $schema = ''): array
39
    {
40
        $schema = $schema ?: 'public'; // default to public
41
        // Nota: Se excluyen claves compuestas
42
        $describe = $pdo->query(
43
            "SELECT
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
            \PDO::FETCH_OBJ
62
        );
63
64
        return $this->describe($describe->fetchAll());
65
    }
66
67
    /**
68
     * Genera la metadata.
69
     *
70
     * @param  array $describe
71
     * 
72
     * @return array
73
     */
74
    private function describe(array $describe): array
75
    {
76
        $fields = [];
77
        // TODO mejorar este código
78
        foreach ($describe as $value) {
79
            $fields[$value->field] = [
80
                'Type'    => $value->type,
81
                'Null'    => $value->null !== 'NO',
82
                'Default' => $value->default != '',
83
                'Key'     => \substr($value->key, 0, 3),
84
                'Auto'    => (bool) \preg_match('/^nextval\(/', $value->default)
85
            ];
86
            $this->filterColumn($fields[$value->field], $value->field);
87
        }
88
89
        return $fields;
90
    }
91
}
92