Completed
Push — work-fleets ( 15d822...d7c9ad )
by SuperNova.WS
06:04
created

DBStaticRecord::fetchOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 0
cts 2
cp 0
crap 2
1
<?php
2
3
/**
4
 * Class DBStaticRecord
5
 */
6
class DBStaticRecord {
7
8
  public static $_table = '_table';
9
  public static $_idField = 'id';
10
11
  /**
12
   * @var db_mysql $dbStatic
13
   */
14
  protected static $dbStatic;
15
16
  /**
17
   * DBStaticRecord constructor.
18
   *
19
   * @param db_mysql|null $db
20
   */
21
  public static function _init($db = null) {
22
    static::$dbStatic = (!empty($db) && $db instanceof db_mysql) || !class_exists('classSupernova', false) ? $db : classSupernova::$db;
23
  }
24
25
  /**
26
   * @return DbSqlStatement
27
   */
28
  public static function buildSelect() {
29
    return DbSqlStatement::build(null)->getParamsFromStaticClass(get_called_class())->select(false);
0 ignored issues
show
Unused Code introduced by
The call to DbSqlStatement::select() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
30
  }
31
32
  /**
33
   * @return DbSqlStatement
34
   */
35
  public static function buildSelectAll() {
36
    return static::buildSelect()->field('*');
37
  }
38
39
  /**
40
   * @return DbSqlStatement
41
   */
42
  public static function buildSelectLock() {
43
    return
44
      static::buildSelect()->field(1)
45
        ->forUpdate();
46
  }
47
48
  /**
49
   * @param array       $where
50
   * @param mixed|array $fieldList
51
   *     Field list can be scalar - it would be converted to array and used as field name
52
   * @param bool        $for_update
53
   *
54
   * @return array|null
55
   *
56
   * @see static::getRecordList
57
   */
58
  protected static function getRecord($where = array(), $fieldList = '*', $for_update = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $for_update is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
    $result = static::fetchOne(
60
      static::buildSelect()->fields($fieldList)
61
        ->where($where)
62
    );
63
64
    return $result;
65
  }
66
67
  /**
68
   * Get maximum ID from table
69
   *
70
   * @return int
71
   */
72
  public static function getMaxId() {
73
    $maxId = static::getRecord(array(), DbSqlLiteral::build()->max(static::$_idField, 'maxId'));
74
75
    return !empty($maxId['maxId']) ? $maxId['maxId'] : 0;
76
  }
77
78
  /**
79
   * @param int|string  $recordId
80
   * @param mixed|array $fieldList
81
   * @param bool        $forUpdate
82
   *
83
   * @return array|null
84
   */
85
  public static function getRecordById($recordId, $fieldList = '*', $forUpdate = false) {
86
    return static::getRecord(array(static::$_idField . '=' . $recordId), $fieldList, $forUpdate);
87
  }
88
89
  /**
90
   * @param DbSqlStatement $statement
91
   *
92
   * @return array|bool|mysqli_result|null
93
   */
94
  protected static function execute($statement) {
95
    return static::$dbStatic->execute((string)$statement);
96
  }
97
98
  /**
99
   * @param DbSqlStatement $statement
100
   *
101
   * @return array|null
102
   */
103
  protected static function fetchOne($statement) {
104
    return static::$dbStatic->fetchOne($statement);
105
  }
106
107
  /**
108
   * @param array $idList
109
   *
110
   * @return mysqli_result|null
111
   */
112
  /**
113
   * @param array $idList
114
   *
115
   * @return mysqli_result|null
116
   */
117
  public static function queryExistsIdInList($idList) {
118
    $query = null;
119
    if (!empty($idList) && is_array($idList)) {
120
      $query = static::execute(
121
        static::buildSelectAll()
122
          ->fields(static::$_idField)
123
          ->where(array("`" . static::$_idField . "` IN (" . implode(',', $idList) . ")"))
124
      );
125
    }
126
127
    return !empty($query) ? $query : null;
128
  }
129
130
131
  /**
132
   * @param string $idList
133
   *
134
   * @return string
135
   */
136
  public static function filterIdListStringRepack($idList) {
137
    // TODO - remove HelperArray caller
138
    $idList = HelperArray::stringToArrayFilterEmpty($idList);
139
140
    $result = array();
141
    if (!empty($idList)) {
142
      $query = static::queryExistsIdInList($idList);
143
      while($row = db_fetch($query)) {
144
        $result[] = $row[static::$_idField];
145
      }
146
    }
147
148
    // TODO - remove implode
149
    return implode(',', $result);
150
  }
151
152
  /**
153
   *
154
   */
155
  public static function lockAllRecords() {
156
    static::execute(
157
      static::buildSelectLock()
158
    );
159
  }
160
161
  /**
162
   * Builds and Executes prepared statement
163
   *
164
   * @param string $sqlQuery
165
   * @param array  $values
166
   *
167
   * @return array|bool|mysqli_result|null
168
   */
169
  protected static function prepareExecute($sqlQuery, $values = array()) {
170
    return static::$dbStatic->sqlPrepareAndExecute($sqlQuery, $values);
171
  }
172
173
  /**
174
   * Builds and executes prepared statement then return first record from sets
175
   *
176
   * @param string $sqlQuery
177
   * @param array  $values
178
   *
179
   * @return array|null
180
   */
181
  protected static function prepareFetchOne($sqlQuery, $values = array()) {
182
    return static::$dbStatic->db_fetch(static::prepareExecute($sqlQuery, $values));
0 ignored issues
show
Bug introduced by
static::prepareExecute($sqlQuery, $values) cannot be passed to db_fetch() as the parameter $query expects a reference.
Loading history...
183
  }
184
185
  /**
186
   * Builds and executes prepared statement then fetches first record from sets and returns value from first field
187
   *
188
   * @param string $sqlQuery
189
   * @param array  $values
190
   *
191
   * @return mixed|null
192
   */
193
  protected static function prepareFetchValue($sqlQuery, $values = array()) {
194
    $result = static::prepareFetchOne($sqlQuery, $values);
195
    if (empty($result) || !array($result)) {
196
      $result = null;
197
    } else {
198
      $result = array_pop($result);
199
    }
200
201
    return $result;
202
  }
203
204
}
205
206
DBStaticRecord::_init();
207