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

Mysql::normalizeColumnDefinition()   C

Complexity

Conditions 11
Paths 64

Size

Total Lines 34
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 11

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 26
cts 26
cp 1
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 26
nc 64
nop 1
crap 11

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 5
    public function insert(Entity $entity, $useAutoIncrement = true)
45
    {
46 5
        $statement = $this->buildInsertStatement($entity);
47 5
        $pdo = $this->entityManager->getConnection();
48
49 5
        if ($useAutoIncrement && $entity::isAutoIncremented()) {
50 3
            $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 50
    public function describe($table)
60
    {
61
        try {
62 50
            $result = $this->entityManager->getConnection()->query('DESCRIBE ' . $this->escapeIdentifier($table));
63 1
        } 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