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

DbSqlStatement::_reset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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