SqlsrvMetadata   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A queryFields() 0 18 1
A pk() 0 6 1
A describe() 0 17 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  2005 - 2016  Kumbia Team (http://www.kumbiaphp.com)
19
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
20
 */
21
namespace Kumbia\ActiveRecord\Metadata;
22
23
use Kumbia\ActiveRecord\Db;
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 string $database base de datos
36
     * @param string $table    tabla
37
     * @param string $schema   squema
38
     *
39
     * @return array
40
     */
41
    protected function queryFields($database, $table, $schema = 'dbo')
42
    {
43
        $describe = Db::get($database)->query(
44
            "SELECT
45
                c.name AS field_name,
46
                c.is_identity AS is_auto_increment,
47
                c.is_nullable,
48
                object_definition(c.default_object_id) AS default_value,
49
                t.name AS type_field
50
            FROM sys.columns c join sys.types t
51
            ON c.system_type_id = t.user_type_id
52
            WHERE object_id = object_id('$schema.$table')"
53
        );
54
55
        $pk = self::pk($database, $table);
56
57
        return self::describe($describe, $pk);
58
    }
59
60
    /**
61
     * Optiene el PK
62
     *
63
     * @param string $database base de datos
64
     * @param string $table    tabla
65
     *
66
     * @return string
67
     */
68
    private static function pk($database, $table)
69
    {
70
        $pk = Db::get($database)->query("exec sp_pkeys @table_name='$table'");
71
        $pk = $pk->fetch(PDO::FETCH_OBJ);
72
        return $pk->COLUMN_NAME;
73
    }
74
75
    /**
76
     * Genera la metadata
77
     *
78
     * @param \PDOStatement $describe SQL result
79
     * @param string        $pk       Primary key
80
     *
81
     * @return array
82
     */
83
    protected function describe(\PDOStatement $describe, $pk)
84
    {
85
        // TODO Mejorar
86
        $fields = [];
87
        while (( $value = $describe->fetch(PDO::FETCH_OBJ))) :
88
            $fields[$value->field_name] = [
89
                'Type'    => $value->type_field,
90
                'Null'    => ($value->is_nullable),
91
                'Key'     => ($value->field_name == $pk) ? 'PRI' : '',
92
                'Default' => str_replace("''", "'", trim($value->default_value, "(')")),
93
                'Auto'    => ($value->is_auto_increment)
94
            ];
95
            $this->filterCol($fields[$value->field_name], $value->field_name);
96
        endwhile;
97
98
        return $fields;
99
    }
100
}
101