Passed
Push — 2.x ( 9569a0...05c456 )
by Fabrice
02:19
created

PdoExtractorTrait::fetchRecords()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 23
rs 9.9
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
use fab2s\NodalFlow\YaEtlException;
13
14
/**
15
 * trait PdoExtractorTrait
16
 */
17
trait PdoExtractorTrait
18
{
19
    /**
20
     * The PDO connection
21
     *
22
     * @var \PDO
23
     */
24
    protected $pdo;
25
26
    /**
27
     * Keep track of Buffered queries status in case we need / can to tweak it
28
     *
29
     * @var bool
30
     */
31
    protected $driverBufferedQuery;
32
33
    /**
34
     * List of supported drivers
35
     *
36
     * @var array
37
     */
38
    protected $supportedDrivers = [
39
        'mysql'  => 'mysql',
40
        'sqlite' => 'sqlite',
41
        'pgsql'  => 'pgsql',
42
    ];
43
44
    /**
45
     * Current driver name
46
     *
47
     * @var string
48
     */
49
    protected $dbDriverName;
50
51
    /**
52
     * Query bindings
53
     *
54
     * @var array
55
     */
56
    protected $queryBindings;
57
58
    /**
59
     * Leave no trace
60
     */
61
    public function __destruct()
62
    {
63
        if ($this->dbDriverName === 'mysql' && $this->driverBufferedQuery) {
64
            // set driver state back to where we met
65
            $this->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
66
        }
67
    }
68
69
    /**
70
     * Properly set up PDO connection
71
     *
72
     * @param \PDO $pdo
73
     *
74
     * @throws YaEtlException
75
     */
76
    public function configurePdo(\PDO $pdo)
77
    {
78
        $this->pdo          = $pdo;
79
        $this->dbDriverName = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
80
81
        if (!isset($this->supportedDrivers[$this->dbDriverName])) {
82
            throw new YaEtlException('Pdo driver not supported, must be one of: ' . \implode(', ', \array_keys($this->supportedDrivers)));
83
        }
84
85
        if ($this->dbDriverName === 'mysql') {
86
            // buffered queries can have great performance impact
87
            // with large data sets
88
            $this->driverBufferedQuery = $this->pdo->getAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY);
89
90
            if ($this->driverBufferedQuery) {
91
                // disable buffered queries as we should be querying by a lot
92
                $this->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
93
            }
94
        }
95
    }
96
97
    /**
98
     * Fetch records
99
     *
100
     * @return bool
101
     */
102
    public function fetchRecords(): bool
103
    {
104
        $extractQuery = $this->getPaginatedQuery();
0 ignored issues
show
Bug introduced by
It seems like getPaginatedQuery() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
        /** @scrutinizer ignore-call */ 
105
        $extractQuery = $this->getPaginatedQuery();
Loading history...
105
        $statement    = $this->pdo->prepare($extractQuery);
106
        if (!$statement->execute(!empty($this->queryBindings) ? $this->queryBindings : null)) {
107
            return false;
108
        }
109
110
        // It is most likely better to proxy all records
111
        // as it also makes sure that we read from db as
112
        // fast as possible and release pressure asap
113
        $collection = new \SplDoublyLinkedList;
114
        while ($record = $statement->fetch(\PDO::FETCH_ASSOC)) {
115
            $collection->push($record);
116
        }
117
118
        $statement->closeCursor();
119
        unset($statement);
120
121
        /* @var $this DbExtractorAbstract */
122
        $this->setExtracted($collection);
123
124
        return !$collection->isEmpty();
125
    }
126
}
127