Completed
Push — master ( 3abb57...f20526 )
by Sébastien
02:03 queued 36s
created

MysqliMetadataReader::readFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 14
cts 14
cp 1
rs 9.2
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Soluble\Metadata\Reader;
4
5
use Soluble\Metadata\ColumnsMetadata;
6
use Soluble\Metadata\Exception;
7
use Soluble\Metadata\Reader\Mapping\MysqliMapping;
8
use Soluble\Datatype\Column;
9
10
class MysqliMetadataReader extends AbstractMetadataReader
11
{
12
    /**
13
     * @var \Mysqli
14
     */
15
    protected $mysqli;
16
17
    /**
18
     * @var array
19
     */
20
    protected static $metadata_cache = [];
21
22
    /**
23
     * @param \Mysqli $mysqli
24
     */
25 13
    public function __construct(\Mysqli $mysqli)
26 1
    {
27 13
        $this->mysqli = $mysqli;
28 13
    }
29
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * @throws \Soluble\Metadata\Exception\ConnectionException
34
     * @throws \Soluble\Metadata\Exception\EmptyQueryException
35
     * @throws \Soluble\Metadata\Exception\InvalidQueryException
36
     */
37 7
    protected function readColumnsMetadata($sql)
38
    {
39 7
        $metadata = new ColumnsMetadata();
40 7
        $fields = $this->readFields($sql);
41 5
        $type_map = MysqliMapping::getDatatypeMapping();
42
43 5
        foreach ($fields as $idx => $field) {
44 5
            $name = $field->orgname === '' ? $field->name : $field->orgname;
45 5
            $tableName = $field->orgtable;
46 5
            $schemaName = $field->db;
47
48 5
            $type = $field->type;
49
50 5
            if (!$type_map->offsetExists($type)) {
51
                $msg = "Cannot get type for field '$name'. Mapping for native type [$type] cannot be resolved into a valid type for driver: " . __CLASS__;
52
                throw new Exception\UnsupportedTypeException($msg);
53
            }
54
55 5
            $datatype = $type_map->offsetGet($type);
56
57 5
            $column = Column\Type::createColumnDefinition($datatype['type'], $name, $tableName, $schemaName);
58
59 5
            $column->setAlias($field->name);
60 5
            $column->setTableAlias($field->table);
61 5
            $column->setCatalog($field->catalog);
62 5
            $column->setOrdinalPosition($idx + 1);
63 5
            $column->setDataType($datatype['type']);
64 5
            $column->setIsNullable(!($field->flags & MYSQLI_NOT_NULL_FLAG) > 0 && ($field->orgtable !== '' || $field->orgtable !== null));
65 5
            $column->setIsPrimary(($field->flags & MYSQLI_PRI_KEY_FLAG) > 0);
66
67 5
            $column->setColumnDefault($field->def);
68
69 5
            if (($field->flags & MYSQLI_SET_FLAG) > 0) {
70 1
                $column->setNativeDataType('SET');
71 5
            } elseif (($field->flags & MYSQLI_ENUM_FLAG) > 0) {
72 2
                $column->setNativeDataType('ENUM');
73 2
            } else {
74 5
                $column->setNativeDataType($datatype['native']);
75
            }
76
77 5
            if ($field->table === '' || $field->table === null) {
78 1
                $column->setIsGroup(($field->flags & MYSQLI_GROUP_FLAG) > 0);
79 1
            }
80
81 5
            if ($column instanceof Column\Definition\NumericColumnInterface) {
82 5
                $column->setNumericUnsigned(($field->flags & MYSQLI_UNSIGNED_FLAG) > 0);
83 5
            }
84
85 5
            if ($column instanceof Column\Definition\IntegerColumn) {
86 5
                $column->setIsAutoIncrement(($field->flags & MYSQLI_AUTO_INCREMENT_FLAG) > 0);
87 5
            } elseif ($column instanceof Column\Definition\DecimalColumn) {
88
                // salary DECIMAL(5,2)
89
                // In this example, 5 is the precision and 2 is the scale.
90
                // Standard SQL requires that DECIMAL(5,2) be able to store any value
91
                // with five digits and two decimals, so values that can be stored in
92
                // the salary column range from -999.99 to 999.99.
93 2
                $column->setNumericScale($field->length - $field->decimals + 1);
94 2
                $column->setNumericPrecision($field->decimals);
95 2
            }
96
97 5
            if ($column instanceof Column\Definition\StringColumn) {
98 4
                $column->setCharacterMaximumLength($field->length);
99 4
            }
100
101 5
            if ($column instanceof Column\Definition\BlobColumn) {
102 1
                $column->setCharacterOctetLength($field->length);
103 1
            }
104
105 5
            $alias = $column->getAlias();
106
107 5
            if ($metadata->offsetExists($alias)) {
108 1
                $prev_column = $metadata->offsetGet($alias);
109 1
                $prev_def = $prev_column->toArray();
110 1
                $curr_def = $column->toArray();
111 1
                if ($prev_def['dataType'] !== $curr_def['dataType'] || $prev_def['nativeDataType'] !== $curr_def['nativeDataType']) {
112 1
                    throw new Exception\AmbiguousColumnException("Cannot get column metadata, non unique column found '$alias' in query with different definitions.");
113
                }
114
115
                // If the the previous definition, was a prev_def
116
                if ($prev_def['isPrimary']) {
117
                    $column = $prev_column;
118
                }
119
            }
120
121 5
            $metadata->offsetSet($alias, $column);
122 5
        }
123
124 4
        return $metadata;
125
    }
126
127
    /**
128
     * @param string $sql
129
     *
130
     * @throws Exception\ConnectionException
131
     *
132
     * @return array
133
     *
134
     * @throws \Soluble\Metadata\Exception\EmptyQueryException
135
     * @throws \Soluble\Metadata\Exception\InvalidQueryException
136
     */
137 7
    protected function readFields($sql)
138
    {
139 7
        if (trim($sql) === '') {
140 1
            throw new Exception\EmptyQueryException(__METHOD__ . ': Error cannot handle empty queries');
141
        }
142
143 6
        $sql = $this->getEmptiedQuery($sql);
144 6
        $stmt = $this->mysqli->prepare($sql);
145
146 6
        if (!$stmt) {
147 1
            $message = $this->mysqli->error;
148 1
            throw new Exception\InvalidQueryException(__METHOD__ . ": Error sql is not correct : $message");
149
        }
150 5
        $stmt->execute();
151
152 5
        $result = $stmt->result_metadata();
153 5
        $metaFields = $result->fetch_fields();
154 5
        $result->close();
155 5
        $stmt->close();
156
157 5
        return $metaFields;
158
    }
159
}
160