Completed
Push — master ( ca5c4a...e71512 )
by Dmytro
03:37
created

DBQuery::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Asymptix\db;
4
5
use Asymptix\core\OutputStream;
6
7
/**
8
 * DB SQL query object.
9
 *
10
 * @category Asymptix PHP Framework
11
 * @author Dmytro Zarezenko <[email protected]>
12
 * @copyright (c) 2015 - 2016, Dmytro Zarezenko
13
 *
14
 * @git https://github.com/Asymptix/Framework
15
 * @license http://opensource.org/licenses/MIT
16
 */
17
class DBQuery {
18
19
    /* Service variables */
20
21
    /**
22
     * Type of the SQL query.
23
     *
24
     * @var string
25
     */
26
    protected $type = DBQueryType::SELECT;
27
28
    /**
29
     * SQL conditions list.
30
     *
31
     * @var array
32
     */
33
    public $conditions = [];
34
35
    /**
36
     * SQL fields list for INSERT/UPDATE queries.
37
     *
38
     * @var array
39
     */
40
    public $fields = [];
41
42
    /**
43
     * SQL order list.
44
     *
45
     * @var array
46
     */
47
    public $order = null;
48
49
    /**
50
     * SQL limit value (may be pair array or integer value).
51
     *
52
     * @var mixed
53
     */
54
    public $limit = null;
55
56
    /**
57
     * Creates new DBQuery object with SQL type initialized.
58
     *
59
     * @param string $type SQL query type.
60
     */
61
    public function __construct($type = DBQueryType::SELECT) {
62
        $this->setType($type);
63
    }
64
65
    /**
66
     * Sets SQL query type with additional query type validation.
67
     *
68
     * @param string $type SQL query type.
69
     * @throws DBCoreException If invalid query type provided.
70
     */
71
    public function setType($type = DBQueryType::SELECT) {
72
        if (DBQueryType::isValidQueryType($type)) {
73
            $this->type = $type;
74
        } else {
75
            throw new DBCoreException("Invalid SQL query type '" . $type . "'");
76
        }
77
    }
78
79
    /**
80
     * Returns SQL query type.
81
     *
82
     * @return string
83
     */
84
    public function getType() {
85
        return $this->type;
86
    }
87
88
    /**
89
     * Detects type of the SQL query.
90
     *
91
     * @return string Type of the SQL query.
92
     * @throws DBCoreException If SQL query is invalid.
93
     */
94
    protected function detectType() {
95
        return DBQueryType::detectQueryType($this->query);
0 ignored issues
show
Bug introduced by
The property query does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
96
    }
97
98
    /**
99
     * Detects if DB query is selector query.
100
     *
101
     * @return bool
102
     */
103
    public function isSelector() {
104
        return DBQueryType::isSelector($this->getType());
105
    }
106
107
    /**
108
     * Detects if DB query is modifier query.
109
     *
110
     * @return bool
111
     */
112
    public function isModifier() {
113
        return DBQueryType::isModifier($this->getType());
114
    }
115
116
    /**
117
     * Outputs DB query debug information to the stream.
118
     *
119
     * @param string $query SQL query.
120
     * @param string $types SQL types string.
121
     * @param array $params List of SQL query parameters.
122
     */
123
    public static function showQueryDebugInfo($query = "", $types = "", array $params = []) {
124
        OutputStream::start();
125
        if (!empty($query)) {
126
            if (empty($types) && empty($params)) {
127
                OutputStream::message(OutputStream::MSG_INFO, "Q: " . $query);
128
            } else {
129
                if (strlen($types) === count($params)) {
130
                    $query = preg_replace('/\s+/', ' ', $query);
131
                    $preparedQuery = $query;
132
133
                    $paramsStr = [];
134
                    for ($i = 0; $i < strlen($types); $i++) {
135
                        $query = preg_replace("/\?/", DBField::sqlValue($types[$i], $params[$i]), $query, 1);
136
137
                        $paramsStr[] = $types[$i] . ": " . DBField::sqlValue($types[$i], $params[$i]);
138
                    }
139
140
                    OutputStream::message(OutputStream::MSG_INFO, "Q: " . $query);
141
                    OutputStream::message(OutputStream::MSG_INFO, "P: " . $preparedQuery);
142
143
                    OutputStream::message(OutputStream::MSG_INFO, "A: [" . implode(", ", $paramsStr) . "]");
144
                } else {
145
                    OutputStream::message(OutputStream::MSG_ERROR, "Number of types is not equal parameters number.");
146
                    OutputStream::message(OutputStream::MSG_INFO, "T: " . $types);
147
                    OutputStream::message(OutputStream::MSG_INFO, "A: [" . implode(", ", $params) . "]");
148
                }
149
            }
150
        } else {
151
            OutputStream::message(OutputStream::MSG_WARNING, "DB query is empty.");
152
        }
153
        OutputStream::close();
154
    }
155
156
}
157