Completed
Push — master ( dd3854...7344d8 )
by Fabrice
05:37
created

PdoExtractor::fetchRecords()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 13
nc 3
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
use fab2s\NodalFlow\NodalFlowException;
13
use fab2s\NodalFlow\YaEtlException;
14
15
/**
16
 * Class PdoExtractor
17
 */
18
class PdoExtractor extends DbExtractorAbstract
19
{
20
    use PdoExtractorTrait;
21
22
    /**
23
     * Instantiate PdoExtractor
24
     *
25
     * @param \PDO        $pdo
26
     * @param string|null $extractQuery
27
     *
28
     * @throws NodalFlowException
29
     * @throws YaEtlException
30
     */
31
    public function __construct(\PDO $pdo, $extractQuery = null)
32
    {
33
        $this->configurePdo($pdo);
34
35
        parent::__construct($extractQuery);
36
    }
37
38
    /**
39
     * Leave no trace
40
     * implement here to allow easier overriding
41
     */
42
    public function __destruct()
43
    {
44
        if ($this->driverBufferedQuery) {
45
            // set driver state back to where we met
46
            $this->pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
47
        }
48
    }
49
50
    /**
51
     * Fetch records
52
     *
53
     * @return bool
54
     */
55
    public function fetchRecords()
56
    {
57
        $extractQuery = $this->getPaginatedQuery();
58
59
        $query = $this->pdo->prepare($extractQuery);
60
        if (!$query->execute(!empty($this->queryBindings) ? $this->queryBindings : null)) {
61
            return false;
62
        }
63
64
        $this->extracted = new \SplDoublyLinkedList;
65
        $hasRecord       = false;
66
        while ($record = $query->fetch(\PDO::FETCH_ASSOC)) {
67
            $this->extracted->push($record);
68
            $hasRecord = true;
69
        }
70
71
        $query->closeCursor();
72
        unset($query);
73
        $this->extracted->rewind();
74
75
        return $hasRecord;
76
    }
77
78
    /**
79
     * This method sets offset and limit in the query
80
     * WARNING : if you set an offset without limit,
81
     * the limit will be set to  $this->maxdefaultLimit
82
     *
83
     * @return string the paginated query with current offset and limit
84
     */
85
    protected function getPaginatedQuery()
86
    {
87
        return $this->extractQuery . $this->getLimitOffsetBit();
88
    }
89
}
90