Passed
Push — 2.x ( 18bce1...b13a23 )
by Maxim
17:24
created

MySQLCompiler::limit()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 5
nop 4
dl 0
loc 22
ccs 11
cts 11
cp 1
crap 5
rs 9.5555
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Cycle ORM package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Database\Driver\MySQL;
13
14
use Cycle\Database\Driver\CachingCompilerInterface;
15
use Cycle\Database\Driver\Compiler;
16
use Cycle\Database\Driver\MySQL\Injection\CompileJson;
17
use Cycle\Database\Driver\Quoter;
18
use Cycle\Database\Injection\FragmentInterface;
19
use Cycle\Database\Injection\Parameter;
20
use Cycle\Database\Query\QueryParameters;
21
22
/**
23
 * MySQL syntax specific compiler.
24
 */
25
class MySQLCompiler extends Compiler implements CachingCompilerInterface
26
{
27
    /**
28
     * @param QueryParameters $params
29
     * @param Quoter          $q
30
     * @param array           $tokens
31
     *
32 46
     * @return string
33
     */
34 46
    protected function insertQuery(QueryParameters $params, Quoter $q, array $tokens): string
35 4
    {
36 4
        if ($tokens['columns'] === []) {
37 4
            return sprintf(
38
                'INSERT INTO %s () VALUES ()',
39
                $this->name($params, $q, $tokens['table'], true)
40
            );
41 42
        }
42
43
        return parent::insertQuery($params, $q, $tokens);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     *
49 326
     * @link http://dev.mysql.com/doc/refman/5.0/en/select.html#id4651990
50
     */
51 326
    protected function limit(QueryParameters $params, Quoter $q, int $limit = null, int $offset = null): string
52 310
    {
53
        if ($limit === null && $offset === null) {
54
            return '';
55 16
        }
56 16
57
        $statement = '';
58
        if ($limit === null) {
59 4
            // When limit is not provided (or 0) but offset does we can replace
60
            // limit value with PHP_INT_MAX
61 12
            $statement .= 'LIMIT 18446744073709551615 ';
62 12
        } else {
63
            $statement .= 'LIMIT ? ';
64
            $params->push(new Parameter($limit));
65 16
        }
66 10
67 10
        if ($offset !== null) {
68
            $statement .= 'OFFSET ?';
69
            $params->push(new Parameter($offset));
70 16
        }
71
72
        return trim($statement);
73
    }
74
75
    protected function compileJsonOrderBy(string $path): FragmentInterface
76
    {
77
        return new CompileJson($path);
78
    }
79
}
80