Completed
Pull Request — 2.0 (#75)
by Julien
02:03
created

SimpleQueryManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 94
c 4
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B query() 0 29 1
A doQuery() 0 11 1
A prepareArguments() 0 16 3
1
<?php
2
/*
3
 * This file is part of the Pomm's Foundation package.
4
 *
5
 * (c) 2014 - 2015 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Foundation\QueryManager;
11
12
use PommProject\Foundation\ConvertedResultIterator;
13
use PommProject\Foundation\Listener\SendNotificationTrait;
14
use PommProject\Foundation\Session\ResultHandler;
15
16
/**
17
 * SimpleQueryManager
18
 *
19
 * Query system as a client.
20
 *
21
 * @package   Foundation
22
 * @copyright 2014 - 2015 Grégoire HUBERT
23
 * @author    Grégoire HUBERT
24
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
25
 */
26
class SimpleQueryManager extends QueryManagerClient
27
{
28
    use SendNotificationTrait;
29
    use QueryParameterParserTrait;
30
31
    /**
32
     * query
33
     *
34
     * Perform a simple escaped query and return converted result iterator.
35
     *
36
     * @access public
37
     * @param  string $sql
38
     * @param  array $parameters
39
     * @return ConvertedResultIterator
40
     */
41
    public function query($sql, array $parameters = [])
42
    {
43
        $parameters = $this->prepareArguments($sql, $parameters);
44
        $this->sendNotification(
45
            'query:pre',
46
            [
47
                'sql'           => $sql,
48
                'parameters'    => $parameters,
49
                'session_stamp' => $this->getSession()->getStamp(),
50
            ]
51
        );
52
        $start    = microtime(true);
53
        $resource = $this->doQuery($sql, $parameters);
54
        $end      = microtime(true);
55
56
        $iterator = new ConvertedResultIterator(
57
            $resource,
58
            $this->getSession()
59
        );
60
        $this->sendNotification(
61
            'query:post',
62
            [
63
                'result_count' => $iterator->count(),
64
                'time_ms'      => sprintf("%03.1f", ($end - $start) * 1000),
65
            ]
66
        );
67
68
        return $iterator;
69
    }
70
71
    /**
72
     * doQuery
73
     *
74
     * Perform the query
75
     *
76
     * @access protected
77
     * @param  string $sql
78
     * @param  array $parameters
79
     * @return ResultHandler
80
     */
81
    protected function doQuery($sql, array $parameters)
82
    {
83
        return $this
84
            ->getSession()
85
            ->getConnection()
86
            ->sendQueryWithParameters(
87
                $this->orderParameters($sql),
88
                $parameters
89
            )
90
            ;
91
    }
92
93
    /**
94
     * prepareArguments
95
     *
96
     * Prepare and convert $parameters if needed.
97
     *
98
     * @access protected
99
     * @param  string   $sql
100
     * @param  array    $parameters
101
     * @return array    $parameters
102
     */
103
    protected function prepareArguments($sql, array $parameters)
104
    {
105
        $types = $this->getParametersType($sql);
106
107
        foreach ($parameters as $index => $value) {
108
            if ($types[$index] !== '') {
109
                $parameters[$index] = $this
110
                    ->getSession()
111
                    ->getClientUsingPooler('converter', $types[$index])
112
                    ->toPgStandardFormat($value, $types[$index])
113
                    ;
114
            }
115
        }
116
117
        return $parameters;
118
    }
119
}
120