MySQL::lock()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
crap 2
1
<?php
2
namespace Nkey\Caribu\Type;
3
4
/**
5
 * Concrete mysql implementation of database type
6
 *
7
 * This class is part of Caribu package
8
 *
9
 * @author Maik Greubel <[email protected]>
10
 */
11
class MySQL extends AbstractType
12
{
13
14
    /**
15
     * (non-PHPdoc)
16
     *
17
     * @see \Nkey\Caribu\Type\IType::getDsn()
18
     */
19 6
    public function getDsn(): string
20
    {
21 6
        return "mysql:host={host};port={port};dbname={schema}";
22
    }
23
24
    /**
25
     * (non-PHPdoc)
26
     *
27
     * @see \Nkey\Caribu\Type\IType::getDefaultPort()
28
     */
29 6
    public function getDefaultPort(): int
30
    {
31 6
        return 3306;
32
    }
33
34
    /**
35
     * (non-PHPdoc)
36
     *
37
     * @see \Nkey\Caribu\Type\IType::getPrimaryKeyColumn()
38
     */
39 1
    public function getPrimaryKeyColumn(string $table, \Nkey\Caribu\Orm\Orm $orm): string
40
    {
41
        $query = "SELECT `COLUMN_NAME` as column_name FROM `information_schema`.`columns` " . //
42 1
"WHERE `TABLE_NAME` = '{table}' AND `TABLE_SCHEMA` = '{schema}' AND `COLUMN_KEY` = 'PRI'";
43
        
44 1
        return $this->getPrimaryColumnViaSql($orm, $table, $query);
45
    }
46
47
    /**
48
     * (non-PHPdoc)
49
     *
50
     * @see \Nkey\Caribu\Type\IType::lock()
51
     */
52 5
    public function lock(string $table, int $lockType, \Nkey\Caribu\Orm\Orm $orm)
53
    {
54 5
        $lock = "READ";
55 5
        if ($lockType == IType::LOCK_TYPE_WRITE) {
56 5
            $lock = "WRITE";
57
        }
58
        
59 5
        $this->changeLockViaSql($orm, $table, sprintf("LOCK TABLES `%s` %s", $table, $lock));
60 5
    }
61
62
    /**
63
     * (non-PHPdoc)
64
     *
65
     * @see \Nkey\Caribu\Type\IType::unlock()
66
     */
67 5
    public function unlock(string $table, \Nkey\Caribu\Orm\Orm $orm)
68
    {
69 5
        $this->changeLockViaSql($orm, $table, "UNLOCK TABLES");
70 5
    }
71
72
    /**
73
     * (non-PHPdoc)
74
     *
75
     * @see \Nkey\Caribu\Type\IType::getEscapeSign()
76
     */
77 6
    public function getEscapeSign(): string
78
    {
79 6
        return "`";
80
    }
81
82
    /**
83
     * (non-PHPdoc)
84
     *
85
     * @see \Nkey\Caribu\Type\IType::getSequenceNameForColumn()
86
     */
87 4
    public function getSequenceNameForColumn(string $table, string $columnName, \Nkey\Caribu\Orm\Orm $orm): string
88
    {
89 4
        return "";
90
    }
91
92
    /**
93
     * (non-PHPdoc)
94
     *
95
     * @see \Nkey\Caribu\Type\AbstractType::getTypeQuery()
96
     */
97 4
    protected function getTypeQuery(): string
98
    {
99 4
        $query = "SELECT `DATA_TYPE` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = '{schema}' " . "AND `TABLE_NAME` = '{table}' AND `COLUMN_NAME` = '{column}'";
100
        
101 4
        return $query;
102
    }
103
104
    /**
105
     * Checks whether given type is a string type
106
     *
107
     * @param string $type            
108
     *
109
     * @return bool true in case of type is string type, false otherwise
110
     */
111 4
    private function isStringType(string $type): bool
112
    {
113 4
        return $type === 'CHAR' || $type === 'VARCHAR' || $type === 'TEXT' || $type === 'TINYTEXT' || $type === 'MEDIUMTEXT' || $type === 'LONGTEXT' || $type === 'ENUM' || $type === 'SET';
114
    }
115
116
    /**
117
     * Checks whether given type is a binary type
118
     *
119
     * @param string $type            
120
     *
121
     * @return bool true in case of type is binary type, false otherwise
122
     */
123 3
    private function isBinaryType(string $type): bool
124
    {
125 3
        return $type === 'BINARY' || $type === 'VABINARY' || $type === 'TINYBLOB' || $type === 'BLOB' || $type === 'MEDIUMBLOB' || $type === 'LONGBLOB';
126
    }
127
128
    /**
129
     * Checks whether given type is a integer type
130
     *
131
     * @param string $type            
132
     *
133
     * @return bool true in case of type is integer type, false otherwise
134
     */
135 3
    private function isIntegerType(string $type): bool
136
    {
137 3
        return $type === 'INTEGER' || $type === 'INT' || $type === 'SMALLINT' || $type === 'TINYINT' || $type === 'MEDIUMINT' || $type === 'BIGINT';
138
    }
139
140
    /**
141
     * Checks whether given type is a decimal type
142
     *
143
     * @param string $type            
144
     *
145
     * @return bool true in case of type is decimal type, false otherwise
146
     */
147 1
    private function isDecimalType(string $type): bool
148
    {
149 1
        return $type === 'DECIMAL' || $type === 'NUMERIC' || $type === 'FLOAT' || $type === 'REAL' || $type === 'FIXED' || $type === 'DEC' || $type === 'DOUBLE PRECISION';
150
    }
151
152
    /**
153
     * Checks whether given type is a datetime type
154
     *
155
     * @param string $type            
156
     *
157
     * @return bool true in case of type is datetime type, false otherwise
158
     */
159 1
    private function isDateTimeType(string $type): bool
160
    {
161 1
        return $type === 'DATE' || $type === 'DATETIME' || $type === 'TIMESTAMP';
162
    }
163
164
    /**
165
     * (non-PHPdoc)
166
     *
167
     * @see \Nkey\Caribu\Type\AbstractType::mapType()
168
     */
169 4
    protected function mapType(array $result): int
170
    {
171 4
        $type = strtoupper($result['DATA_TYPE']);
172
        
173 4
        if ($this->isStringType($type)) {
174 4
            return \Nkey\Caribu\Orm\OrmDataType::STRING;
175
        }
176
        
177 3
        if ($this->isBinaryType($type)) {
178
            return \Nkey\Caribu\Orm\OrmDataType::BLOB;
179
        }
180
        
181 3
        if ($this->isIntegerType($type)) {
182 3
            return \Nkey\Caribu\Orm\OrmDataType::INTEGER;
183
        }
184
        
185 1
        if ($this->isDecimalType($type)) {
186
            return \Nkey\Caribu\Orm\OrmDataType::DECIMAL;
187
        }
188
        
189 1
        if ($this->isDateTimeType($type)) {
190 1
            return \Nkey\Caribu\Orm\OrmDataType::DATETIME;
191
        }
192
        
193
        return \Nkey\Caribu\Orm\OrmDataType::STRING;
194
    }
195
}
196