QueryWrapper::select()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
namespace Smoren\QueryRelationManager\Pdo;
4
5
use PDO;
6
use Smoren\QueryRelationManager\Base\QueryRelationManagerException;
7
use Smoren\QueryRelationManager\Base\QueryWrapperInterface;
8
9
/**
10
 * QueryWrapper implementation for PDO
11
 * @author Smoren <[email protected]>
12
 */
13
class QueryWrapper implements QueryWrapperInterface
14
{
15
    /**
16
     * @var PDO|null PDO connection object
17
     */
18
    protected static ?PDO $pdo = null;
19
20
    /**
21
     * @var string SQL query string
22
     */
23
    protected string $query;
24
25
    /**
26
     * @var array<string, scalar> values of dynamic query params
27
     */
28
    protected array $mapParams;
29
30
    /**
31
     * Creates and sets default connection to DB
32
     * @param string $dsn DSN
33
     * @param string $username username
34
     * @param string|null $password password
35
     * @return PDO PDO connection object
36
     */
37
    public static function setDbConfig(string $dsn, string $username, ?string $password = null): PDO
38
    {
39
        static::$pdo = new PDO(
40
            $dsn,
41
            $username,
42
            $password,
43
            [
44
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
45
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
46
                PDO::ATTR_EMULATE_PREPARES => false,
47
            ]
48
        );
49
50
        return static::$pdo;
51
    }
52
53
    /**
54
     * QueryWrapper constructor.
55
     */
56
    public function __construct()
57
    {
58
        $this->query = '';
59
        $this->mapParams = [];
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function select(array $arSelect): QueryWrapperInterface
66
    {
67
        $this->query .= 'SELECT ';
68
69
        $buf = [];
70
        foreach($arSelect as $alias => $field) {
71
            $buf[] = addslashes($field).' AS '.addslashes($alias);
72
        }
73
74
        $this->query .= implode(', ', $buf).' ';
75
76
        return $this;
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82
    public function from(array $mapFrom): QueryWrapperInterface
83
    {
84
        $this->query .= ' FROM ';
85
86
        foreach($mapFrom as $alias => $tableName) {
87
            $this->query .= ' '.addslashes($tableName).' '.addslashes($alias).' ';
88
            break;
89
        }
90
91
        return $this;
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97
    public function join(
98
        string $type,
99
        array $mapTable,
100
        string $condition,
101
        array $extraJoinParams = []
102
    ): QueryWrapperInterface {
103
        $this->query .= " ".addslashes($type)." JOIN ";
104
105
        foreach($mapTable as $alias => $tableName) {
106
            $this->query .= addslashes($tableName).' '.addslashes($alias).' ';
107
            break;
108
        }
109
110
        $this->query .= " ON {$condition} ";
111
112
        foreach($extraJoinParams as $key => $val) {
113
            $this->mapParams[$key] = $val;
114
        }
115
116
        return $this;
117
    }
118
119
    /**
120
     * @inheritDoc
121
     * @param PDO|null $db DB connection object
122
     * @return array<array<mixed>>
123
     * @throws QueryRelationManagerException
124
     */
125
    public function all($db = null): array
126
    {
127
        $db = $db ?? static::$pdo;
128
129
        if(!$db) {
130
            throw new QueryRelationManagerException('no pdo connection opened');
131
        }
132
133
        $q = $db->prepare($this->query);
134
135
        foreach($this->mapParams as $key => $val) {
136
            $q->bindValue($key, $val);
137
        }
138
139
        $q->execute();
140
141
        return $q->fetchAll() ?: [];
142
    }
143
144
    /**
145
     * @inheritDoc
146
     */
147
    public function getRawSql(): string
148
    {
149
        $from = array_keys($this->mapParams);
150
        $to = array_values($this->mapParams);
151
        foreach($to as &$param) {
152
            $param = "'{$param}'";
153
        }
154
        unset($param);
155
156
        return str_replace($from, $to, $this->query);
157
    }
158
159
    /**
160
     * Sets raw SQL to query
161
     * @param string $sql raw SQL string
162
     * @return $this
163
     */
164
    public function setRawSql(string $sql): self
165
    {
166
        $this->query = $sql;
167
168
        return $this;
169
    }
170
171
    /**
172
     * @inheritDoc
173
     */
174
    public function &getQuery()
175
    {
176
        return $this->query;
177
    }
178
}
179