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

Pgsql   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 8
dl 0
loc 73
ccs 26
cts 26
cp 1
rs 10

2 Methods

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