Passed
Push — work-fleets ( 4e14e1...ed0220 )
by SuperNova.WS
05:21
created

DBStaticRecord::prepareFetchOne()   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
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 0
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, get_called_class())->select();
30
  }
31
32
  /**
33
   * @return DbSqlStatement
34
   */
35
  public static function buildSelectNoFields() {
36
    return DbSqlStatement::build(null, get_called_class())->select(false);
37
  }
38
39
  /**
40
   * @return DbSqlStatement
41
   */
42
  public static function buildSelectLock() {
43
    return
44
      static::buildSelect()
45
        ->fields(1)
46
        ->forUpdate();
47
  }
48
49
  /**
50
   * @param array       $where
51
   * @param mixed|array $fieldList
52
   *     Field list can be scalar - it would be converted to array and used as field name
53
   * @param bool        $for_update
54
   *
55
   * @return array|null
56
   *
57
   * @see static::getRecordList
58
   */
59
  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...
60
    $result = static::fetchOne(
61
      static::buildSelect()
62
        ->fields($fieldList)
63
        ->where($where)
64
    );
65
66
    return $result;
67
  }
68
69
  /**
70
   * Get maximum ID from table
71
   *
72
   * @return int
73
   */
74
  public static function getMaxId() {
75
    $maxId = static::getRecord(array(), DbSqlLiteral::build()->max(static::$_idField, 'maxId'));
76
77
    return !empty($maxId['maxId']) ? $maxId['maxId'] : 0;
78
  }
79
80
  /**
81
   * @param int|string  $recordId
82
   * @param mixed|array $fieldList
83
   * @param bool        $forUpdate
84
   *
85
   * @return array|null
86
   */
87
  public static function getRecordById($recordId, $fieldList = '*', $forUpdate = false) {
88
    return static::getRecord(array(static::$_idField . '=' . $recordId), $fieldList, $forUpdate);
89
  }
90
91
  /**
92
   * @param DbSqlStatement $statement
93
   *
94
   * @return array|bool|mysqli_result|null
95
   */
96
  protected static function execute($statement) {
97
    return static::$dbStatic->execute($statement);
98
  }
99
100
  /**
101
   * @param DbSqlStatement $statement
102
   *
103
   * @return array|null
104
   */
105
  protected static function fetchOne($statement) {
106
    return static::$dbStatic->fetchOne($statement);
107
  }
108
109
  /**
110
   * @param array $idList
111
   *
112
   * @return mysqli_result|null
113
   */
114
  /**
115
   * @param array $idList
116
   *
117
   * @return mysqli_result|null
118
   */
119
  public static function queryExistsIdInList($idList) {
120
    $query = null;
121
    if (!empty($idList) && is_array($idList)) {
122
      $query = static::execute(
123
        static::buildSelect()
124
          ->fields(static::$_idField)
125
          ->where(array("`" . static::$_idField . "` IN (" . implode(',', $idList) . ")"))
126
      );
127
    }
128
129
    return !empty($query) ? $query : null;
130
  }
131
132
133
  /**
134
   * @param string $idList
135
   *
136
   * @return string
137
   */
138
  public static function filterIdListStringRepack($idList) {
139
    // TODO - remove HelperArray caller
140
    $idList = HelperArray::stringToArrayFilterEmpty($idList);
141
142
    $result = array();
143
    if (!empty($idList)) {
144
      $query = static::queryExistsIdInList($idList);
145
      while ($row = db_fetch($query)) {
146
        $result[] = $row[static::$_idField];
147
      }
148
    }
149
150
    // TODO - remove implode
151
    return implode(',', $result);
152
  }
153
154
  /**
155
   *
156
   */
157
  public static function lockAllRecords() {
158
    static::execute(
159
      static::buildSelectLock()
160
    );
161
  }
162
163
  /**
164
   * Builds and Executes prepared statement
165
   *
166
   * @param string $sqlQuery
167
   * @param array  $values
168
   *
169
   * @return array|bool|mysqli_result|null
170
   */
171
  protected static function prepareExecute($sqlQuery, $values = array()) {
172
    return static::$dbStatic->doquery(DbSqlPrepare::build($sqlQuery, $values));
173
  }
174
175
  /**
176
   * Builds and executes prepared statement then return first record from sets
177
   *
178
   * @param string $sqlQuery
179
   * @param array $values
180
   *
181
   * @return array|null
182
   */
183
  protected static function prepareFetchOne($sqlQuery, $values = array()) {
184
    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...
185
  }
186
187
  /**
188
   * Builds and executes prepared statement then fetches first record from sets and returns value from first field
189
   *
190
   * @param string $sqlQuery
191
   * @param array $values
192
   *
193
   * @return mixed|null
194
   */
195
  protected static function prepareFetchValue($sqlQuery, $values = array()) {
196
    $result = static::prepareFetchOne($sqlQuery, $values);
197
    if(empty($result) || !array($result)) {
198
      $result = null;
199
    } else {
200
      $result = array_pop($result);
201
    }
202
    return $result;
203
  }
204
205
}
206
207
DBStaticRecord::_init();
208