Completed
Pull Request — master (#3)
by Rémy
01:26
created

Helper::isSQLite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @author Rémy M. Böhler <[email protected]>
4
 */
5
declare(strict_types=1);
6
7
namespace Rorm;
8
9
class Helper
10
{
11 4
    public function isMySQL(\PDO $dbh): bool
12
    {
13 4
        return $dbh->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'mysql';
14
    }
15
16 4
    public function isSQLite(\PDO $dbh): bool
17
    {
18 4
        return $dbh->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite';
19
    }
20
21 6
    public function quote(\PDO $dbh, $value)
22
    {
23
        switch (true) {
24 6
            case is_bool($value):
25 4
                if ($this->isMySQL($dbh)) {
26 2
                    return $value ? 'TRUE' : 'FALSE';
27
                }
28 2
                return $value ? 1 : 0;
29 2
            case $value === null:
30 2
                return 'NULL';
31 2
            case is_int($value):
32 2
                return (int)$value;
33 2
            case is_float($value):
34 2
                return (float)$value;
35
            default:
36 2
                return $dbh->quote($value);
37
        }
38
    }
39
40
    /**
41
     * Method to quote identifiers
42
     * Please make sure you keep the quoter as long you are needing it.
43
     */
44 16
    public function getIdentifierQuoter(\PDO $dbh): callable
45
    {
46 16
        if ($this->isMySQL($dbh)) {
47
            // mysql mode
48 8
            return function ($identifier) {
49 8
                return '`' . str_replace('`', '``', $identifier) . '`';
50 8
            };
51
        }
52
53
        // standard sql mode
54 8
        return function ($identifier) {
55 8
            return '"' . str_replace('"', '""', $identifier) . '"';
56 8
        };
57
    }
58
}
59