Passed
Push — 3.0 ( 8d2bb4...5e1ed3 )
by Rubén
05:10
created

QueryData::getMapClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Storage\Database;
26
27
use SP\DataModel\DataModelBase;
28
29
/**
30
 * Class QueryData
31
 *
32
 * @package SP\Storage
33
 */
34
final class QueryData
35
{
36
    /**
37
     * @var array
38
     */
39
    protected $params = [];
40
    /**
41
     * @var string
42
     */
43
    protected $query;
44
    /**
45
     * @var string
46
     */
47
    protected $mapClassName = '';
48
    /**
49
     * @var DataModelBase
50
     */
51
    protected $mapClass;
52
    /**
53
     * @var bool
54
     */
55
    protected $useKeyPair = false;
56
    /**
57
     * @var string
58
     */
59
    protected $select = '';
60
    /**
61
     * @var string
62
     */
63
    protected $from = '';
64
    /**
65
     * @var string
66
     */
67
    protected $where = '';
68
    /**
69
     * @var string
70
     */
71
    protected $groupBy = '';
72
    /**
73
     * @var string
74
     */
75
    protected $order = '';
76
    /**
77
     * @var string
78
     */
79
    protected $limit = '';
80
    /**
81
     * @var string
82
     */
83
    protected $queryCount = '';
84
    /**
85
     * @var int
86
     */
87
    protected $queryNumRows = 0;
88
    /**
89
     * @var int Código de estado tras realizar la consulta
90
     */
91
    protected $queryStatus = 0;
92
    /**
93
     * @var string
94
     */
95
    protected $onErrorMessage;
96
97
    /**
98
     * Añadir un parámetro a la consulta
99
     *
100
     * @param $value
101
     * @param $name
102
     */
103
    public function addParam($value, $name = null)
104
    {
105
        if (null !== $name) {
106
            $this->params[$name] = $value;
107
        } else {
108
            $this->params[] = $value;
109
        }
110
    }
111
112
    /**
113
     * @return array
114
     */
115
    public function getParams()
116
    {
117
        return $this->params;
118
    }
119
120
    /**
121
     * Establecer los parámetros de la consulta
122
     *
123
     * @param array $data
124
     */
125
    public function setParams(array $data)
126
    {
127
        $this->params = $data;
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    public function getQuery()
134
    {
135
        if (empty($this->query)) {
136
            return $this->select . ' ' . $this->from . ' ' . $this->where . ' ' . $this->groupBy . ' ' . $this->order . ' ' . $this->limit;
137
        }
138
139
        return $this->query;
140
    }
141
142
    /**
143
     * @param $query
144
     */
145
    public function setQuery($query)
146
    {
147
        $this->query = $query;
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getMapClassName()
154
    {
155
        return $this->mapClassName;
156
    }
157
158
    /**
159
     * @param string $mapClassName
160
     */
161
    public function setMapClassName($mapClassName)
162
    {
163
        $this->mapClassName = $mapClassName;
164
    }
165
166
    /**
167
     * @return bool
168
     */
169
    public function isUseKeyPair()
170
    {
171
        return $this->useKeyPair;
172
    }
173
174
    /**
175
     * @param boolean $useKeyPair
176
     */
177
    public function setUseKeyPair($useKeyPair)
178
    {
179
        $this->useKeyPair = (bool)$useKeyPair;
180
    }
181
182
    /**
183
     * @return string
184
     */
185
    public function getSelect()
186
    {
187
        return $this->select;
188
    }
189
190
    /**
191
     * @param string $select
192
     */
193
    public function setSelect($select)
194
    {
195
        $this->select = 'SELECT ' . $select;
196
    }
197
198
    /**
199
     * @return string
200
     */
201
    public function getOrder()
202
    {
203
        return $this->order;
204
    }
205
206
    /**
207
     * @param string $order
208
     */
209
    public function setOrder($order)
210
    {
211
        if (!empty($order)) {
212
            $this->order = 'ORDER BY ' . $order;
213
        }
214
    }
215
216
    /**
217
     * @return string
218
     */
219
    public function getLimit()
220
    {
221
        return $this->limit;
222
    }
223
224
    /**
225
     * @param string     $limit
226
     * @param array|null $params
227
     */
228
    public function setLimit($limit, array $params = null)
229
    {
230
        if (!empty($limit)) {
231
            $this->limit = 'LIMIT ' . $limit;
232
233
            if ($params !== null) {
234
                $this->addParams($params);
235
            }
236
        }
237
    }
238
239
    /**
240
     * Añadir parámetros a la consulta
241
     *
242
     * @param array $params
243
     */
244
    public function addParams(array $params)
245
    {
246
        $this->params = array_merge($this->params, $params);
247
    }
248
249
    /**
250
     * @return string
251
     */
252
    public function getQueryCount()
253
    {
254
        if (empty($this->queryCount)) {
255
            return 'SELECT COUNT(*) ' . $this->from . ' ' . $this->where;
256
        }
257
258
        return $this->queryCount;
259
    }
260
261
    /**
262
     * @return string
263
     */
264
    public function getFrom()
265
    {
266
        return $this->from;
267
    }
268
269
    /**
270
     * @param string $from
271
     */
272
    public function setFrom($from)
273
    {
274
        if (!empty($from)) {
275
            $this->from = 'FROM ' . $from;
276
        }
277
    }
278
279
    /**
280
     * @return string
281
     */
282
    public function getWhere()
283
    {
284
        return $this->where;
285
    }
286
287
    /**
288
     * @param array|string $where
289
     */
290
    public function setWhere($where)
291
    {
292
        if (!empty($where)) {
293
            if (is_array($where)) {
294
                $this->where = 'WHERE ' . implode(' AND ', $where);
295
            } else {
296
                $this->where = 'WHERE ' . $where;
297
            }
298
        }
299
    }
300
301
    /**
302
     * @return int
303
     */
304
    public function getQueryNumRows()
305
    {
306
        return (int)$this->queryNumRows;
307
    }
308
309
    /**
310
     * @param int $queryNumRows
311
     */
312
    public function setQueryNumRows($queryNumRows)
313
    {
314
        $this->queryNumRows = (int)$queryNumRows;
315
    }
316
317
    /**
318
     * @return int
319
     */
320
    public function getQueryStatus()
321
    {
322
        return $this->queryStatus;
323
    }
324
325
    /**
326
     * @param int $queryStatus
327
     */
328
    public function setQueryStatus($queryStatus)
329
    {
330
        $this->queryStatus = $queryStatus;
331
    }
332
333
    /**
334
     * @return string
335
     */
336
    public function getOnErrorMessage()
337
    {
338
        return $this->onErrorMessage ?: __u('Error while querying');
339
    }
340
341
    /**
342
     * @param string $onErrorMessage
343
     */
344
    public function setOnErrorMessage($onErrorMessage)
345
    {
346
        $this->onErrorMessage = $onErrorMessage;
347
    }
348
349
    /**
350
     * @return string
351
     */
352
    public function getGroupBy()
353
    {
354
        return $this->groupBy;
355
    }
356
357
    /**
358
     * @param string $groupBy
359
     */
360
    public function setGroupBy($groupBy)
361
    {
362
        if (!empty($groupBy)) {
363
            $this->groupBy = 'GROUP BY ' . $groupBy;
364
        }
365
    }
366
}