Issues (1369)

classes/DBAL/StorageSqlV2.php (2 issues)

1
<?php
2
/**
3
 * Created by Gorlum 13.11.2018 14:08
4
 */
5
6
namespace DBAL;
7
8
9
use Core\GlobalContainer;
10
use SN;
11
12
class StorageSqlV2 {
13
  /**
14
   * @var null|db_mysql $db
15
   */
16
  protected static $db = null;
17
18
  protected $forUpdate = false;
19
20
  public function __construct(GlobalContainer $services = null) {
21
    static::$db = !empty($services) ? $services->db : SN::$gc->db;
22
  }
23
24
  /**
25
   * @return $this
26
   */
27
  public function forUpdate() {
28
    $this->forUpdate = true;
29
30
    return $this;
31
  }
32
33
//  /**
34
//   * @param $tableName
35
//   * @param $conditions
36
//   *
37
//   * @return array|null
38
//   */
39
//  public function findFirstDbq($tableName, $conditions) {
40
//    $dbq = new DbQuery(static::$db);
41
//    $dbq
42
//      ->setTable($tableName)
43
//      ->setOneRow()
44
//      ->setWhereArray($conditions);
45
//
46
//    $array = static::$db->dbqSelectAndFetch($dbq);
47
//
48
//    return $array;
49
//  }
50
51
  /**
52
   * @param $tableName
53
   * @param $conditions
54
   *
55
   * @return DbMysqliResultIterator
56
   */
57
  public function findIterator($tableName, $conditions) {
58
    $dbq = new DbQuery(static::$db);
59
    $dbq
60
      ->setTable($tableName)
61
      ->setWhereArray($conditions);
62
63
    if ($this->forUpdate) {
64
      $dbq->setForUpdate();
65
    }
66
67
    return static::$db->selectIterator($dbq->select());
0 ignored issues
show
The method selectIterator() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
    return static::$db->/** @scrutinizer ignore-call */ selectIterator($dbq->select());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
  }
69
70
  /**
71
   * @param $tableName
72
   * @param $conditions
73
   *
74
   * @return array|null
75
   */
76
  public function findFirst($tableName, $conditions) {
77
    $iterator = $this->findIterator($tableName, $conditions);
78
79
    $iterator->rewind();
80
81
    return $iterator->valid() ? $iterator->current() : [];
82
  }
83
84
  /**
85
   * @param $tableName
86
   * @param $conditions
87
   *
88
   * @return array
89
   */
90
  public function findAll($tableName, $conditions) {
91
    $result = [];
92
93
    foreach ($this->findIterator($tableName, $conditions) as $record) {
94
      $result[] = $record;
95
    }
96
97
    return $result;
98
  }
99
100
//  /**
101
//   * @param IRecordIndexed $record
102
//   * @param                $index
103
//   *
104
//   * @return IRecordIndexed
105
//   */
106
//  public function findById(IRecordIndexed $record, $index, $tableName, $conditions) {
107
//    // TODO: Remake it for Storage not to know how record implemented
108
//
109
//    $dbq = new DbQuery(static::$db);
110
//    $dbq
111
//      ->setTable($tableName)
112
//      ->setOneRow()
113
//      ->setWhereArray([$record::indexField() => $index]);
114
//
115
//    $array = static::$db->dbqSelectAndFetch($dbq);
116
//    if (!empty($array)) {
117
//
118
//    }
119
//
120
//    return $record;
121
//  }
122
123
  /**
124
   * @param string $tableName
125
   *
126
   * @return DbFieldDescription[]
127
   */
128
  public function fields($tableName) {
129
    return static::$db->schema()->getTableSchema($tableName)->fields;
130
  }
131
132
133
  /**
134
   * @param string $tableName
135
   * @param array  $fieldList
136
   *
137
   * @return int|null|string
138
   */
139
  public function insert($tableName, array $fieldList) {
140
    $dbq = new DbQuery(static::$db);
141
    $dbq
142
      ->setTable($tableName)
143
      ->setValues($fieldList);
144
145
    $result = static::$db->doquery($dbq->insert(DbQuery::DB_INSERT_PLAIN, true));
146
147
    return $result ? static::$db->db_insert_id() : null;
148
  }
149
150
  /**
151
   * @param string $tableName
152
   * @param array  $conditions
153
   *
154
   * @return bool|null
155
   */
156
  public function delete($tableName, $conditions) {
157
    $dbq = new DbQuery(static::$db);
158
    $dbq
159
      ->setTable($tableName)
160
      ->setWhereArray($conditions);
161
162
    return $dbq->doDeleteDb();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $dbq->doDeleteDb() also could return the type mysqli_result which is incompatible with the documented return type boolean|null.
Loading history...
163
  }
164
165
  public function update($tableName, $conditions, $changes, $deltas) {
166
    $dbq = new DbQuery(static::$db);
167
    $dbq
168
      ->setTable($tableName)
169
      ->setValues($changes)
170
      ->setAdjust($deltas)
171
      ->setWhereArray($conditions);
172
173
    return $dbq->doUpdate();
174
  }
175
176
}
177