Completed
Push — work-fleets ( fb09a7...a5ca82 )
by SuperNova.WS
05:51
created

DbSqlStatement::processField()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
rs 9.2
ccs 10
cts 10
cp 1
crap 4
1
<?php
2
3
//pdump(DBStaticUser::getMaxId());
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
4
//pdump(DBStaticUser::getRecordById(67));
5
6
7
class DbSqlStatement {
8
9
  const SELECT = 'SELECT';
10
11
  protected static $allowedOperations = array(
12
    self::SELECT,
13
  );
14
15
  /**
16
   * @var db_mysql $db
17
   */
18
  protected $db;
19
20
  public $operation = '';
21
22
  public $table = '';
23
  public $alias = '';
24
25
  public $idField = '';
26
27
  /**
28
   * @var array
29
   */
30
  public $fields = array();
31
32
  public $where = array();
33
  public $group = array();
34
  public $order = array();
35
  public $limit = array();
36
37
  public $fetchOne = false;
38
39
  /**
40
   * @param db_mysql|null $db
41
   * @param string        $className
42
   *
43
   * @return DbSqlStatement
44
   */
45 1
  public static function build($db = null, $className = '') {
46 1
    $result = new self($db);
47 1
    if (!empty($className) && is_string($className)) {
48 1
      $result->getParamsFromStaticClass($className);
49 1
    }
50
51 1
    return $result;
52
  }
53
54
  /**
55
   * DbSqlStatement constructor.
56
   *
57
   * @param db_mysql|null $db
58
   */
59 3
  public function __construct($db = null) {
60 3
    $this->db = (!empty($db) && $db instanceof db_mysql) || !class_exists('classSupernova', false) ? $db : classSupernova::$db;
61 3
  }
62
63
  /**
64
   * Resets statement
65
   *
66
   * @param bool $full
67
   *
68
   * @return $this
69
   */
70
  // TODO - UNITTEST
71 1
  protected function _reset($full = true) {
72 1
    if ($full) {
73 1
      $this->operation = '';
74 1
      $this->table = '';
75 1
      $this->alias = '';
76 1
      $this->idField = '';
77 1
    }
78
79 1
    $this->fields = array();
80 1
    $this->where = array();
81 1
    $this->group = array();
82 1
    $this->order = array();
83 1
    $this->limit = array();
84 1
    $this->fetchOne = false;
85
86 1
    return $this;
87
  }
88
89
  /**
90
   * @param string $fieldName
91
   *
92
   * @return $this
93
   */
94 1
  public function setIdField($fieldName) {
95 1
    $this->idField = $fieldName;
96
97 1
    return $this;
98
  }
99
100
  /**
101
   * @param string $alias
102
   *
103
   * @return $this
104
   */
105 1
  public function fromAlias($alias) {
106 1
    $this->alias = $alias;
107
108 1
    return $this;
109
  }
110
111
  /**
112
   * @param string $tableName
113
   * @param string $alias
114
   *
115
   * @return $this
116
   */
117 1
  public function from($tableName, $alias = '') {
118 1
    $this->table = $tableName;
119 1
    $this->fromAlias($alias);
120
121 1
    return $this;
122
  }
123
124
  /**
125
   * @param string $params
126
   *
127
   * @return $this
128
   */
129 2
  public function getParamsFromStaticClass($params) {
130 2
    if (is_string($params) && $params && class_exists($params)) {
131 2
      $this->from($params::$_table);
132 2
      $this->setIdField($params::$_idField);
133 2
    }
134
135 2
    return $this;
136
  }
137
138
139
  /**
140
   * @return $this
141
   */
142 2
  public function select() {
143 2
    $this->_reset(false);
144 2
    $this->operation = DbSqlStatement::SELECT;
145 2
    $this->fields = array('*');
146
147 2
    return $this;
148
  }
149
150
  /**
151
   * @param array $fields
152
   *
153
   * @return $this
154
   */
155 1
  public function fields($fields = array()) {
156 1
    $this->fields = $fields;
157
158 1
    return $this;
159
  }
160
161
  /**
162
   * @param array $where
163
   *
164
   * @return $this
165
   */
166
  // TODO - fields should be escaped !!
167
  // TODO - $where should be validated and checked!
168 1
  public function where($where = array()) {
169 1
    if(!is_array($where)) {
170
      throw new ExceptionDbSqlWhereNotAnArray();
171
    }
172 1
    $this->where = $where;
173
174 1
    return $this;
175
  }
176
177
  public function fetchOne() {
178
    $this->fetchOne = true;
179
    $this->limit = array(1);
180
181
    return $this;
182
  }
183
184
  /**
185
   * @return string
186
   * @throws ExceptionDbOperationEmpty
187
   * @throws ExceptionDbOperationRestricted
188
   */
189 2
  public function __toString() {
190 2
    if (empty($this->operation)) {
191 1
      throw new ExceptionDbOperationEmpty();
192
    }
193
194 1
    if (!in_array($this->operation, self::$allowedOperations)) {
195 1
      throw new ExceptionDbOperationRestricted();
196
    }
197
198
    $result = '';
199
    $result .= $this->stringEscape($this->operation);
200
201
    $result .= ' ' . $this->selectFieldsToString($this->fields);
202
203
    $result .= ' FROM';
204
    $result .= ' `{{' . $this->stringEscape($this->table) . '}}`';
205
    $result .= !empty($this->alias) ? ' AS `' . $this->stringEscape($this->alias) . '`' : '';
206
207
    // TODO - fields should be escaped !!
208
    $result .= !empty($this->where) ? ' WHERE ' . implode(' AND ', $this->where) : '';
209
210
    // TODO - fields should be escaped !!
211
    $result .= !empty($this->group) ? ' GROUP BY ' . implode(',', $this->group) : '';
212
213
    // TODO - fields should be escaped !!
214
    $result .= !empty($this->order) ? ' ORDER BY ' . implode(',', $this->order) : '';
215
216
    // TODO - fields should be escaped !!
217
    $result .= !empty($this->limit) ? ' LIMIT ' . implode(',', $this->limit) : '';
218
219
    // TODO - protect from double escape!
220
221
    return $result;
222
  }
223
224
  /**
225
   * @param array|mixed $fields
226
   *
227
   * @return string
228
   * @throws ExceptionDBFieldEmpty
229
   */
230 16
  protected function selectFieldsToString($fields) {
231 16
    $fields = HelperArray::makeArray($fields);
232
233 16
    $result = array();
234 16
    foreach ($fields as $fieldName) {
235 15
      $string = $this->processField($fieldName);
236 15
      if ($string !== '') {
237 13
        $result[] = $string;
238 13
      }
239 16
    }
240
241 16
    if (empty($result)) {
242 3
      throw new ExceptionDBFieldEmpty();
243
    }
244
245 13
    return implode(',', $result);
246
  }
247
248
  /**
249
   * @param mixed $fieldName
250
   *
251
   * @return string
252
   */
253 13
  protected function processField($fieldName) {
254 13
    if (is_bool($fieldName)) {
255 4
      $result = (string)intval($fieldName);
256 13
    } elseif (is_null($fieldName)) {
257 2
      $result = 'NULL';
258 11
    } elseif ($fieldName === '*') {
259 2
      $result = '*';
260 2
    } else {
261 9
      $result = $this->processFieldDefault($fieldName);
262
    }
263
264 13
    return $result;
265
  }
266
267
  /**
268
   * @param mixed $fieldName
269
   *
270
   * @return string
271
   */
272 9
  protected function processFieldDefault($fieldName) {
273 9
    $result = (string)$fieldName;
274
    if (
275
      $result != ''
276 9
      &&
277
//      // Wildcard goes as is
278
//      $fieldName !== '*'
279
//      &&
280
      // Literals plays as they are - they do properly format by itself
281 8
      !($fieldName instanceof DbSqlLiteral)
282 9
      &&
283
      // Numeric need no escaping
284 7
      !is_numeric($fieldName)
285 9
    ) {
286
      // Other should be formatted
287 3
      $result = '`' . $this->stringEscape($result) . '`';
288 3
    }
289
290 9
    return $result;
291
  }
292
293
  /**
294
   * @param $string
295
   *
296
   * @return mixed|string
297
   */
298
  protected function stringEscape($string) {
299
    return
300
      method_exists($this->db, 'db_escape')
301
        ? $this->db->db_escape($string)
302
        : str_replace('`', '\`', addslashes($string));
303
  }
304
305
}
306