Completed
Push — work-fleets ( b8fd67...a5a64a )
by SuperNova.WS
05:59
created

DbSqlStatement::processFieldDefault()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

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