SqlsrvMetadata::pk()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
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
 *
19
 * @copyright  2005 - 2020  Kumbia Team (http://www.kumbiaphp.com)
20
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
21
 */
22
namespace Kumbia\ActiveRecord\Metadata;
23
24
use \PDO;
25
26
/**
27
 * Adaptador de Metadata para Sqlsrv
28
 *
29
 */
30
class SqlsrvMetadata extends Metadata
31
{
32
    /**
33
     * Consultar los campos de la tabla en la base de datos
34
     *
35
     * @param  \PDO    $pdo base de datos
36
     * @param  string  $table    tabla
37
     * @param  string  $schema   squema
38
     * @return array
39
     */
40
    protected function queryFields(\PDO $pdo, string $table, string $schema = 'dbo'): array
41
    {
42
        $describe = $pdo->query(
43
            "SELECT
44
                c.name AS field_name,
45
                c.is_identity AS is_auto_increment,
46
                c.is_nullable,
47
                object_definition(c.default_object_id) AS default_value,
48
                t.name AS type_field
49
            FROM sys.columns c join sys.types t
50
            ON c.system_type_id = t.user_type_id
51
            WHERE object_id = object_id('$schema.$table')"
52
        );
53
54
        $pk = self::pk($pdo, $table);
55
56
        return self::describe($describe, $pk);
57
    }
58
59
    /**
60
     * Optiene el PK
61
     *
62
     * @param  \PDO     $pdo      base de datos
63
     * @param  string   $table    tabla
64
     * @return string
65
     */
66
    private static function pk(\PDO $pdo, string $table): string
67
    {
68
        $pk = $pdo->query("exec sp_pkeys @table_name='$table'");
69
        $pk = $pk->fetch(\PDO::FETCH_OBJ);
70
71
        return $pk->COLUMN_NAME;
72
    }
73
74
    /**
75
     * Genera la metadata
76
     *
77
     * @param  \PDOStatement $describe SQL result
78
     * @param  string        $pk       Primary key
79
     * @return array
80
     */
81
    protected function describe(\PDOStatement $describe, string $pk): array
82
    {
83
        // TODO Mejorar
84
        $fields = [];
85
        while ($value = $describe->fetch()) {
86
            $fields[$value->field_name] = [
87
                'Type'    => $value->type_field,
88
                'Null'    => ($value->is_nullable),
89
                'Key'     => ($value->field_name === $pk) ? 'PRI' : '',
90
                'Default' => \str_replace("''", "'", \trim($value->default_value, "(')")),
91
                'Auto'    => ($value->is_auto_increment)
92
            ];
93
            $this->filterColumn($fields[$value->field_name], $value->field_name);
94
        }
95
96
        return $fields;
97
    }
98
}
99