Passed
Pull Request — master (#41)
by Thomas
03:49
created

Pgsql::describe()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 17
cts 17
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 4
nop 1
crap 4
1
<?php
2
3
namespace ORM\Dbal;
4
5
use ORM\Exception;
6
use ORM\QueryBuilder\QueryBuilder;
7
use PDO;
8
9
/**
10
 * Database abstraction for PostgreSQL databases
11
 *
12
 * @package ORM\Dbal
13
 * @author  Thomas Flori <[email protected]>
14
 */
15
class Pgsql extends Dbal
16
{
17
    protected static $typeMapping = [
18
        'integer' => Type\Number::class,
19
        'smallint' => Type\Number::class,
20
        'bigint' => Type\Number::class,
21
        'numeric' => Type\Number::class,
22
        'real' => Type\Number::class,
23
        'double precision' => Type\Number::class,
24
        'money' => Type\Number::class,
25
26
        'character varying' => Type\VarChar::class,
27
        'character' => Type\VarChar::class,
28
29
        'text' => Type\Text::class,
30
31
        'date' => Type\DateTime::class,
32
        'timestamp without time zone' => Type\DateTime::class,
33
        'timestamp with time zone' => Type\DateTime::class,
34
        'time without time zone' => Type\Time::class,
35
        'time with time zone' => Type\Time::class,
36
37
        'json' => Type\Json::class,
38
        'boolean' => Type\Boolean::class,
39
    ];
40
41
    protected $booleanTrue = 'true';
42
    protected $booleanFalse = 'false';
43
44 2
    public function insert($entity, $useAutoIncrement = true)
45
    {
46 2
        $statement = $this->buildInsertStatement($entity);
47 2
        $pdo = $this->entityManager->getConnection();
48
49 2
        if ($useAutoIncrement && $entity::isAutoIncremented()) {
50 1
            $statement .= ' RETURNING ' . $entity::getColumnName($entity::getPrimaryKeyVars()[0]);
51 1
            $result = $pdo->query($statement);
52 1
            return $result->fetchColumn();
53
        }
54
55 1
        $pdo->query($statement);
56 1
        $this->entityManager->sync($entity, true);
57 1
        return true;
58
    }
59
60 26
    public function describe($schemaTable)
61
    {
62 26
        $table = explode($this->identifierDivider, $schemaTable);
63 26
        list($schema, $table) = count($table) === 2 ? $table : ['public', $table[0]];
64
65 26
        $query = new QueryBuilder('INFORMATION_SCHEMA.COLUMNS', '', $this->entityManager);
66 26
        $query->where('table_name', $table)->andWhere('table_schema', $schema);
67 26
        $query->columns([
68 26
            'column_name', 'column_default', 'data_type', 'is_nullable', 'character_maximum_length',
69
            'datetime_precision'
70
        ]);
71
72 26
        $result = $this->entityManager->getConnection()->query($query->getQuery());
73 26
        $rawColumns = $result->fetchAll(PDO::FETCH_ASSOC);
74 26
        if (count($rawColumns) === 0) {
75 2
            throw new Exception('Unknown table '  . $schemaTable);
76
        }
77
78 24
        $cols = array_map(function ($columnDefinition) {
79 24
            if (isset(static::$typeMapping[$columnDefinition['data_type']])) {
80 17
                $columnDefinition['type'] = static::$typeMapping[$columnDefinition['data_type']];
81
            }
82 24
            return new Column($this, $columnDefinition);
83 24
        }, $rawColumns);
84
85 24
        return new Table($cols);
86
    }
87
}
88