Completed
Push — work-fleets ( 1c4183...d64c53 )
by SuperNova.WS
05:36
created

DBStaticRecord::getRecordById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 3
b 0
f 0
nc 1
nop 3
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
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::$dbStatic->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(), new DbSqlLiteral('MAX(id) AS 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);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
82
    return static::getRecord(array(static::$_idField . '=' . $recordId), $fieldList, $forUpdate);
83
  }
84
85
86
  /**
87
   * @param array $idList
88
   *
89
   * @return mysqli_result|null
90
   */
91
  /**
92
   * @param array $idList
93
   *
94
   * @return mysqli_result|null
95
   */
96
  public static function queryExistsIdInList($idList) {
97
    $query = null;
98
    if (!empty($idList) && is_array($idList)) {
99
      $query = static::$dbStatic->execute(
100
        static::buildSelect()
101
          ->fields(static::$_idField)
102
          ->where(array("`" . static::$_idField . "` IN (" . implode(',', $idList) . ")"))
103
      );
104
    }
105
106
    return !empty($query) ? $query : null;
107
  }
108
109
110
  /**
111
   * @param string $idList
112
   *
113
   * @return string
114
   */
115
  public static function filterIdListStringRepack($idList) {
116
    // TODO - remove HelperArray caller
117
    $idList = HelperArray::stringToArrayFilterEmpty($idList);
118
119
    $result = array();
120
    if (!empty($idList)) {
121
      $query = static::queryExistsIdInList($idList);
122
      while ($row = db_fetch($query)) {
123
        $result[] = $row[static::$_idField];
124
      }
125
    }
126
127
    // TODO - remove implode
128
    return implode(',', $result);
129
  }
130
131
  /**
132
   *
133
   */
134
  public static function lockAllRecords() {
135
    static::$dbStatic->execute(
136
      static::buildSelectLock()
137
    );
138
  }
139
140
}
141
142
DBStaticRecord::_init();
143