Completed
Push — work-fleets ( 29b14b...6b7253 )
by SuperNova.WS
05:46
created

DBStaticRecord::prepareFetchValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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