Completed
Push — master ( 4d672a...e7b3ee )
by joanhey
02:42
created

SqlsrvMetadata::queryFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 3
eloc 17
nc 3
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  Copyright (c) 2005-2014  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
 * Adaptador de Metadata para Sqlsrv
27
 *
28
 */
29
class SqlsrvMetadata extends Metadata
30
{
31
    /**
32
     * Consultar los campos de la tabla en la base de datos
33
     *
34
     * @param  string $database base de datos
35
     * @param  string $table    tabla
36
     * @param  string $schema   squema
37
     * @return array
38
     */
39
    protected function queryFields($database, $table, $schema=null)
40
    {
41
        $sql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='$table'";
42
        $describe = Db::get($database)->query($sql);
43
        $fields = array();
44
        $pk = Db::get($database)->query("exec sp_pkeys @table_name='$table'");
45
        $pk = $pk->fetch(PDO::FETCH_OBJ);
46
        $pk = $pk->COLUMN_NAME;
47
        while( ( $value = $describe->fetch(PDO::FETCH_OBJ) ) ) :
48
            $fields[$value->COLUMN_NAME] = array(
49
                'Type' => $value->DATA_TYPE,
50
                'Null' => $value->IS_NULLABLE,
51
                'Key' => ($value->COLUMN_NAME == $pk) ? 'PRI' : '',
52
                'Default' => $value->COLUMN_DEFAULT,
53
                'Auto' => ''
54
            );
55
            $this->filterCol($fields[$value->COLUMN_NAME], $value->COLUMN_NAME);
56
        endwhile;
57
        #die( '<pre>'. print_r( $fields, 1 ) );
58
        return $fields;
59
    }
60
}
61