Passed
Push — master ( 050c29...4fa43a )
by Thomas
58s
created

Mysql   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 105
ccs 42
cts 42
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A insert() 0 14 3
A describe() 0 15 3
C normalizeColumnDefinition() 0 34 11
1
<?php
2
3
namespace ORM\Dbal;
4
5
use ORM\Entity;
6
use ORM\Exception;
7
8
/**
9
 * Database abstraction for MySQL databases
10
 *
11
 * @package ORM\Dbal
12
 * @author  Thomas Flori <[email protected]>
13
 */
14
class Mysql extends Dbal
15
{
16
    protected static $typeMapping = [
17
        'tinyint'   => Type\Number::class,
18
        'smallint'  => Type\Number::class,
19
        'mediumint' => Type\Number::class,
20
        'int'       => Type\Number::class,
21
        'bigint'    => Type\Number::class,
22
        'decimal'   => Type\Number::class,
23
        'float'     => Type\Number::class,
24
        'double'    => Type\Number::class,
25
26
        'varchar' => Type\VarChar::class,
27
        'char'    => Type\VarChar::class,
28
29
        'text'       => Type\Text::class,
30
        'tinytext'   => Type\Text::class,
31
        'mediumtext' => Type\Text::class,
32
        'longtext'   => Type\Text::class,
33
34
        'datetime'  => Type\DateTime::class,
35
        'date'      => Type\DateTime::class,
36
        'timestamp' => Type\DateTime::class,
37
38
        'time' => Type\Time::class,
39
        'enum' => Type\Enum::class,
40
        'set'  => Type\Set::class,
41
        'json' => Type\Json::class,
42
    ];
43
44 6
    public function insert(Entity $entity, $useAutoIncrement = true)
45
    {
46 6
        $statement = $this->buildInsertStatement($entity);
47 6
        $pdo       = $this->entityManager->getConnection();
48
49 6
        if ($useAutoIncrement && $entity::isAutoIncremented()) {
50 4
            $pdo->query($statement);
51 1
            $this->updateAutoincrement($entity, $pdo->query("SELECT LAST_INSERT_ID()")->fetchColumn());
52
        } else {
53 2
            $pdo->query($statement);
54
        }
55
56 3
        return $this->entityManager->sync($entity, true);
57
    }
58
59 51
    public function describe($table)
60
    {
61
        try {
62 51
            $result = $this->entityManager->getConnection()->query('DESCRIBE ' . $this->escapeIdentifier($table));
63 2
        } catch (\PDOException $exception) {
64 1
            throw new Exception('Unknown table ' . $table, 0, $exception);
65
        }
66
67 49
        $cols = [];
68 49
        while ($rawColumn = $result->fetch(\PDO::FETCH_ASSOC)) {
69 49
            $cols[] = new Column($this, $this->normalizeColumnDefinition($rawColumn));
70
        }
71
72 49
        return new Table($cols);
73
    }
74
75
    /**
76
     * Normalize a column definition
77
     *
78
     * The column definition from "DESCRIBE <table>" is to special as useful. Here we normalize it to a more
79
     * ANSI-SQL style.
80
     *
81
     * @param array $rawColumn
82
     * @return array
83
     */
84 49
    protected function normalizeColumnDefinition($rawColumn)
85
    {
86 49
        $definition = [];
87
88 49
        $definition['data_type'] = $this->normalizeType($rawColumn['Type']);
89 49
        if (isset(static::$typeMapping[$definition['data_type']])) {
90 44
            $definition['type'] = static::$typeMapping[$definition['data_type']];
91
        }
92
93 49
        $definition['column_name']              = $rawColumn['Field'];
94 49
        $definition['is_nullable']              = $rawColumn['Null'] === 'YES';
95 49
        $definition['column_default']           = $rawColumn['Default'] !== null ? $rawColumn['Default'] :
96 46
            ($rawColumn['Extra'] === 'auto_increment' ? 'sequence(AUTO_INCREMENT)' : null);
97 49
        $definition['character_maximum_length'] = null;
98 49
        $definition['datetime_precision']       = null;
99
100 49
        switch ($definition['data_type']) {
101 49
            case 'varchar':
102 47
            case 'char':
103 4
                $definition['character_maximum_length'] = $this->extractParenthesis($rawColumn['Type']);
104 4
                break;
105 45
            case 'datetime':
106 41
            case 'timestamp':
107 37
            case 'time':
108 12
                $definition['datetime_precision'] = $this->extractParenthesis($rawColumn['Type']);
109 12
                break;
110 33
            case 'set':
111 31
            case 'enum':
112 4
                $definition['enumeration_values'] = $this->extractParenthesis($rawColumn['Type']);
113 4
                break;
114
        }
115
116 49
        return $definition;
117
    }
118
}
119