Completed
Push — master ( 11bab9...013119 )
by joanhey
03:10 queued 03:06
created

SqlsrvMetadata::describe()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 8.8571
cc 5
eloc 12
nc 9
nop 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
 * @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
     * @return string
66
     */
67
    private static function pk($database, $table)
68
    {
69
        $pk = Db::get($database)->query("exec sp_pkeys @table_name='$table'");
70
        $pk = $pk->fetch(PDO::FETCH_OBJ);
71
        $pk = $pk->COLUMN_NAME;
72
        return $pk;
73
    }
74
    
75
    /**
76
     * Genera la metadata
77
     *
78
     * @param  \PDOStatement $describe
79
     * @return array
80
     */
81
    protected function describe(\PDOStatement $describe, $pk)
82
    {
83
        $fields = array();
84
        while( ( $value = $describe->fetch(PDO::FETCH_OBJ) ) ) :
85
            $fields[$value->field_name] = array(
86
                'Type' => $value->type_field,
87
                'Null' => $value->is_nullable ? 1 : '',
88
                'Key' => ($value->field_name == $pk) ? 'PRI' : '',
89
                'Default' => str_replace("''", "'", trim($value->default_value, "(')") ),
90
                'Auto' => ($value->is_auto_increment) ? 'auto_increment' : ''
91
            );
92
            $this->filterCol($fields[$value->field_name], $value->field_name);
93
        endwhile;
94
95
        return $fields;
96
    }
97
}
98