Completed
Pull Request — master (#68)
by
unknown
02:06
created

SqlsrvMetadata   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
c 3
b 0
f 0
lcom 0
cbo 2
dl 0
loc 40
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B queryFields() 0 28 5
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
        $sql = "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
        $describe = Db::get($database)->query($sql);
53
        $fields = array();
54
        $pk = Db::get($database)->query("exec sp_pkeys @table_name='$table'");
55
        $pk = $pk->fetch(PDO::FETCH_OBJ);
56
        $pk = $pk->COLUMN_NAME;
57
        while( ( $value = $describe->fetch(PDO::FETCH_OBJ) ) ) :
58
            $fields[$value->field_name] = array(
59
                'Type' => $value->type_field,
60
                'Null' => $value->is_nullable ? 1 : '',
61
                'Key' => ($value->field_name == $pk) ? 'PRI' : '',
62
                'Default' => str_replace("''", "'", trim($value->default_value, "(')") ),
63
                'Auto' => ($value->is_auto_increment) ? 'auto_increment' : ''
64
            );
65
            $this->filterCol($fields[$value->field_name], $value->field_name);
66
        endwhile;
67
        return $fields;
68
    }
69
}
70