Completed
Push — master ( 6b6d8d...229d9d )
by joanhey
6s
created

SqlsrvMetadata::queryFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 1
eloc 6
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  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
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