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

Helper::isMySQL()   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 2
    public function isMySQL(\PDO $dbh): bool
12
    {
13 2
        return $dbh->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'mysql';
14
    }
15
16 2
    public function isSQLite(\PDO $dbh): bool
17
    {
18 2
        return $dbh->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite';
19
    }
20
21 3
    public function quote(\PDO $dbh, $value)
22
    {
23 3
        if (is_bool($value)) {
24
            /**
25
             * MySQL has true and false literals
26
             * SQLite does not support boolean type nor literals
27
             */
28 2
            if ($this->isMySQL($dbh)) {
29 1
                return $value ? 'TRUE' : 'FALSE';
30
            }
31
32 1
            return $value ? 1 : 0;
33
        }
34 1
        if ($value === null) {
35 1
            return 'NULL';
36
        }
37 1
        if (is_int($value)) {
38 1
            return (int)$value;
39
        }
40 1
        if (is_float($value)) {
41 1
            return (float)$value;
42
        }
43
44 1
        return $dbh->quote($value);
45
    }
46
47
    /**
48
     * Method to quote identifiers
49
     * Please make sure you keep the quoter as long you are needing it.
50
     */
51 8
    public function getIdentifierQuoter(\PDO $dbh): callable
52
    {
53 8
        if ($this->isMySQL($dbh)) {
54
            // mysql mode
55 4
            return function ($identifier) {
56 4
                return '`' . str_replace('`', '``', $identifier) . '`';
57 4
            };
58
        }
59
60
        // standard sql mode
61 4
        return function ($identifier) {
62 4
            return '"' . str_replace('"', '""', $identifier) . '"';
63 4
        };
64
    }
65
}
66