Completed
Push — work-fleets ( eaef65...a4e57a )
by SuperNova.WS
05:17
created

DBStaticRecord::getRecord()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 4
b 0
f 0
nc 1
nop 3
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 2
rs 9.6666
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 buildSelectLock() {
36
    return
37
      static::buildSelect()
38
        ->fields(1)
39
        ->forUpdate();
40
  }
41
42
  /**
43
   * @param array       $where
44
   * @param mixed|array $fieldList
45
   *     Field list can be scalar - it would be converted to array and used as field name
46
   * @param bool        $for_update
47
   *
48
   * @return array|null
49
   *
50
   * @see static::getRecordList
51
   */
52
  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...
53
    $result = static::fetchOne(
54
      static::buildSelect()
55
        ->fields($fieldList)
56
        ->where($where)
57
    );
58
59
    return $result;
60
  }
61
62
  /**
63
   * Get maximum ID from table
64
   *
65
   * @return int
66
   */
67
  public static function getMaxId() {
68
    $maxId = static::getRecord(array(), DbSqlLiteral::build()->max(static::$_idField, 'maxId'));
69
70
    return !empty($maxId['maxId']) ? $maxId['maxId'] : 0;
71
  }
72
73
  /**
74
   * @param int|string  $recordId
75
   * @param mixed|array $fieldList
76
   * @param bool        $forUpdate
77
   *
78
   * @return array|null
79
   */
80
  public static function getRecordById($recordId, $fieldList = '*', $forUpdate = false) {
81
    return static::getRecord(array(static::$_idField . '=' . $recordId), $fieldList, $forUpdate);
82
  }
83
84
  /**
85
   * @param DbSqlStatement $statement
86
   *
87
   * @return array|bool|mysqli_result|null
88
   */
89
  protected static function execute($statement) {
90
    return static::$dbStatic->execute($statement);
91
  }
92
93
  /**
94
   * @param DbSqlStatement $statement
95
   *
96
   * @return array|null
97
   */
98
  protected static function fetchOne($statement) {
99
    return static::$dbStatic->fetchOne($statement);
100
  }
101
102
  /**
103
   * @param array $idList
104
   *
105
   * @return mysqli_result|null
106
   */
107
  /**
108
   * @param array $idList
109
   *
110
   * @return mysqli_result|null
111
   */
112
  public static function queryExistsIdInList($idList) {
113
    $query = null;
114
    if (!empty($idList) && is_array($idList)) {
115
      $query = static::execute(
116
        static::buildSelect()
117
          ->fields(static::$_idField)
118
          ->where(array("`" . static::$_idField . "` IN (" . implode(',', $idList) . ")"))
119
      );
120
    }
121
122
    return !empty($query) ? $query : null;
123
  }
124
125
126
  /**
127
   * @param string $idList
128
   *
129
   * @return string
130
   */
131
  public static function filterIdListStringRepack($idList) {
132
    // TODO - remove HelperArray caller
133
    $idList = HelperArray::stringToArrayFilterEmpty($idList);
134
135
    $result = array();
136
    if (!empty($idList)) {
137
      $query = static::queryExistsIdInList($idList);
138
      while ($row = db_fetch($query)) {
139
        $result[] = $row[static::$_idField];
140
      }
141
    }
142
143
    // TODO - remove implode
144
    return implode(',', $result);
145
  }
146
147
  /**
148
   *
149
   */
150
  public static function lockAllRecords() {
151
    static::execute(
152
      static::buildSelectLock()
153
    );
154
  }
155
156
}
157
158
DBStaticRecord::_init();
159