Passed
Push — master ( 8edad9...f8fafa )
by Fabrice
05:07
created

PdoExtractorTrait::getLimitOffsetBit()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.2
cc 4
eloc 5
nc 1
nop 0
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
     * @var \PDO
19
     */
20
    protected $pdo;
21
22
    /**
23
     * @var bool
24
     */
25
    protected $driverBufferedQuery;
26
27
    /**
28
     * @var array
29
     */
30
    protected $supportedDrivers = [
31
        'mysql'  => 'mysql',
32
        'sqlite' => 'sqlite',
33
        'pgsql'  => 'pgsql',
34
    ];
35
36
    /**
37
     * @var string
38
     */
39
    protected $dbDriverName;
40
41
    /**
42
     * @var array
43
     */
44
    protected $queryBindings;
45
46
    /**
47
     * leave no trace
48
     */
49
    public function __destruct()
50
    {
51
        if ($this->dbDriverName === 'mysql' && $this->driverBufferedQuery) {
52
            // set driver state back to where we met
53
            $this->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
54
        }
55
    }
56
57
    /**
58
     * @param \PDO $pdo
59
     */
60
    public function configurePdo(\PDO $pdo)
61
    {
62
        $this->pdo          = $pdo;
63
        $this->dbDriverName = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
64
65
        if (!isset($this->supportedDrivers[$this->dbDriverName])) {
66
            throw new \Exception('[YaEtl] Pdo driver not supported, must be one of: ' . \implode(', ', \array_keys($this->supportedDrivers)));
67
        }
68
69
        if ($this->dbDriverName === 'mysql') {
70
            // buffered wueries can have great performance impact
71
            // with large data sets
72
            $this->driverBufferedQuery = $this->pdo->getAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY);
73
74
            if ($this->driverBufferedQuery) {
75
                // disable buffered queries as we should be querying by a lot
76
                $this->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
77
            }
78
        }
79
    }
80
}
81