Completed
Push — dev ( ec1bdf...fa5005 )
by joanhey
03:23 queued 01:32
created

SqlsrvMetadata::queryFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
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  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 = null)
42
    {
43
        $sql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='$table'";
44
        $describe = Db::get($database)->query($sql);
45
        $fields = array();
46
        $pk = Db::get($database)->query("exec sp_pkeys @table_name='$table'");
47
        $pk = $pk->fetch(PDO::FETCH_OBJ);
48
        $pk = $pk->COLUMN_NAME;
49
        // TODO mejorar este código, la consulta SQL no usa el $schema
50
        while (( $value = $describe->fetch(PDO::FETCH_OBJ))) :
51
            $fields[$value->COLUMN_NAME] = array(
52
                'Type' => $value->DATA_TYPE,
53
                'Null' => $value->IS_NULLABLE,
54
                'Key' => ($value->COLUMN_NAME == $pk) ? 'PRI' : '',
55
                'Default' => $value->COLUMN_DEFAULT,
56
                'Auto' => ''
57
            );
58
            $this->filterCol($fields[$value->COLUMN_NAME], $value->COLUMN_NAME);
59
        endwhile;
60
        return $fields;
61
    }
62
}
63