Completed
Push — master ( 044813...fc85b2 )
by Fabrice
02:37
created

PdoExtractorTrait::setExtractQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of YaEtl.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\YaEtl\Extractors;
11
12
/**
13
 * trait PdoExtractorTrait
14
 */
15
trait PdoExtractorTrait
16
{
17
    /**
18
     * This is hte default limit applied when no limit
19
     * is set and an offset is set.
20
     * It's 2^31 – 1 = 2147483647 = max 32bit
21
     * Use 2^63 − 1 = 9223372036854775807 = max 64bit
22
     * if your os and dbms are 64bit
23
     *
24
     * @var int
25
     */
26
    protected $maxdefaultLimit = 2147483647;
27
28
    /**
29
     * @var \PDO
30
     */
31
    protected $pdo;
32
33
    /**
34
     * @var bool
35
     */
36
    protected $driverBufferedQuery;
37
38
    /**
39
     * @var array
40
     */
41
    protected $supportedDrivers = [
42
        'mysql'  => 'mysql',
43
        'sqlite' => 'sqlite',
44
        'pgsql'  => 'pgsql',
45
    ];
46
47
    /**
48
     * @var string
49
     */
50
    protected $dbDriverName;
51
52
    /**
53
     * @var array
54
     */
55
    protected $queryBindings;
56
57
    /**
58
     * leave no trace
59
     */
60
    public function __destruct()
61
    {
62
        if ($this->dbDriverName === 'mysql' && $this->driverBufferedQuery) {
63
            // set driver state back to where we met
64
            $this->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
65
        }
66
    }
67
68
    /**
69
     * @param \PDO $pdo
70
     */
71
    public function configurePdo(\PDO $pdo)
72
    {
73
        $this->pdo          = $pdo;
74
        $this->dbDriverName = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
75
76
        if (!isset($this->supportedDrivers[$this->dbDriverName])) {
77
            throw new \Exception('[YaEtl] Pdo driver not supported, must be one of: ' . \implode(', ', \array_keys($this->supportedDrivers)));
78
        }
79
80
        if ($this->dbDriverName === 'mysql') {
81
            // buffered wueries can have great performance impact
82
            // with large data sets
83
            $this->driverBufferedQuery = $this->pdo->getAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY);
84
85
            if ($this->driverBufferedQuery) {
86
                // disable buffered queries as we should be querying by a lot
87
                $this->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
88
            }
89
        }
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    protected function getLimitOffsetBit()
96
    {
97
        return ' ' . \implode('', [
98
            $this->limit ? ' LIMIT ' . (int) $this->limit : ($this->offset ? ' LIMIT ' . $this->maxdefaultLimit : ''),
99
            $this->offset ? ' OFFSET ' . (int) $this->offset : '',
100
            ';',
101
        ]);
102
    }
103
}
104