Completed
Push — trunk ( 91a948...0e7a2e )
by SuperNova.WS
05:44
created

classes/DBAL/OldDbChangeSet.php (1 issue)

1
<?php
2
3
/**
4
 * Created by Gorlum 03.03.2017 22:32
5
 */
6
7
namespace DBAL;
8
9
use SN;
10
use Unit\DBStaticUnit;
11
12
/**
13
 * Class DBAL\OldDbChangeSet
14
 *
15
 * This class is DEPRECATED!
16
 * It should not be used in new code from now!
17
 *
18
 * @deprecated
19
 */
20
class OldDbChangeSet {
21
22
  /**
23
   * @param      $unit_id
24
   * @param      $unit_value
25
   * @param      $user
26
   * @param null $planet_id
27
   *
28
   * @return array
29
   * @deprecated
30
   */
31
  public static function db_changeset_prepare_unit($unit_id, $unit_value, $user, $planet_id = null) {
32
    if (!is_array($user)) {
33
      // TODO - remove later
34
      print('<h1>СООБЩИТЕ ЭТО АДМИНУ: DBAL\OldDbChangeSet::db_changeset_prepare_unit() - USER is not ARRAY</h1>');
35
      pdump(debug_backtrace());
36
      die('USER is not ARRAY');
37
    }
38
    if (!isset($user['id']) || !$user['id']) {
39
      // TODO - remove later
40
      print('<h1>СООБЩИТЕ ЭТО АДМИНУ: DBAL\OldDbChangeSet::db_changeset_prepare_unit() - USER[id] пустой</h1>');
41
      pdump($user);
42
      pdump(debug_backtrace());
43
      die('USER[id] пустой');
44
    }
45
    $planet_id = is_array($planet_id) && isset($planet_id['id']) ? $planet_id['id'] : $planet_id;
46
47
    $unit_location = sys_get_unit_location($user, array(), $unit_id);
48
    $location_id = $unit_location == LOC_USER ? $user['id'] : $planet_id;
49
    $location_id = $location_id ? $location_id : 'NULL';
50
51
    $temp = DBStaticUnit::db_unit_by_location($user['id'], $unit_location, $location_id, $unit_id);
52
    if (!empty($temp['unit_id'])) {
53
      $db_changeset = array(
54
        'action'  => SQL_OP_UPDATE,
55
        P_VERSION => 1,
56
        'where'   => array(
57
          "unit_id" => $temp['unit_id'],
58
        ),
59
        'fields'  => array(
60
          'unit_level' => array(
61
            'delta' => $unit_value
62
          ),
63
        ),
64
      );
65
    } else {
66
      $db_changeset = array(
67
        'action' => SQL_OP_INSERT,
68
        'fields' => array(
69
          'unit_player_id'     => array(
70
            'set' => $user['id'],
71
          ),
72
          'unit_location_type' => array(
73
            'set' => $unit_location,
74
          ),
75
          'unit_location_id'   => array(
76
            'set' => $unit_location == LOC_USER ? $user['id'] : $planet_id,
77
          ),
78
          'unit_type'          => array(
79
            'set' => get_unit_param($unit_id, P_UNIT_TYPE),
80
          ),
81
          'unit_snid'          => array(
82
            'set' => $unit_id,
83
          ),
84
          'unit_level'         => array(
85
            'set' => $unit_value,
86
          ),
87
        ),
88
      );
89
    }
90
91
    return $db_changeset;
92
  }
93
94
  public static function db_changeset_condition_compile(&$conditions, &$table_name = '') {
95
    if (!$conditions[P_LOCATION] || $conditions[P_LOCATION] == LOC_NONE) {
96
      $conditions[P_LOCATION] = LOC_NONE;
97
      switch ($table_name) {
98
        case 'users':
99
        case LOC_USER:
100
          $conditions[P_TABLE_NAME] = $table_name = 'users';
101
          $conditions[P_LOCATION] = LOC_USER;
102
        break;
103
104
        case 'planets':
105
        case LOC_PLANET:
106
          $conditions[P_TABLE_NAME] = $table_name = 'planets';
107
          $conditions[P_LOCATION] = LOC_PLANET;
108
        break;
109
110
        case 'unit':
111
        case LOC_UNIT:
112
          $conditions[P_TABLE_NAME] = $table_name = 'unit';
113
          $conditions[P_LOCATION] = LOC_UNIT;
114
        break;
115
      }
116
    }
117
118
    $conditions[P_FIELDS_STR] = '';
119
    if ($conditions['fields']) {
120
      $fields = array();
121
      foreach ($conditions['fields'] as $field_name => $field_data) {
122
        $condition = "`{$field_name}` = ";
123
        $value = '';
124
        if ($field_data['delta']) {
125
          $value = "`{$field_name}`" . ($field_data['delta'] >= 0 ? '+' : '') . $field_data['delta'];
126
        } elseif ($field_data['set']) {
127
          $value = (is_string($field_data['set']) ? "'{$field_data['set']}'" : $field_data['set']);
128
        }
129
130
        if ($value) {
131
          $fields[] = $condition . $value;
132
        }
133
      }
134
      $conditions[P_FIELDS_STR] = implode(',', $fields);
135
    }
136
137
    $conditions[P_WHERE_STR] = '';
138
    if (!empty($conditions['where'])) {
139
      if ($conditions[P_VERSION] == 1) {
140
        $the_conditions = array();
141
        foreach ($conditions['where'] as $field_id => $field_value) {
142
          // Простое условие - $field_id = $field_value
143
          if (is_string($field_id)) {
144
            $field_value =
145
              $field_value === null ? 'NULL' :
146
                (is_string($field_value) ? "'" . db_escape($field_value) . "'" :
147
                  (is_bool($field_value) ? intval($field_value) : $field_value));
148
            $the_conditions[] = "`{$field_id}` = {$field_value}";
149
          } else {
150
            die('Неподдерживаемый тип условия');
151
          }
152
        }
153
      } else {
154
        $the_conditions = &$conditions['where'];
155
      }
156
      $conditions[P_WHERE_STR] = implode(' AND ', $the_conditions);
157
    }
158
159
    switch ($conditions['action']) {
160
      case SQL_OP_DELETE:
161
        $conditions[P_ACTION_STR] = ("DELETE FROM {{{$table_name}}}");
162
      break;
163
      case SQL_OP_UPDATE:
164
        $conditions[P_ACTION_STR] = ("UPDATE {{{$table_name}}} SET");
165
      break;
166
      case SQL_OP_INSERT:
167
        $conditions[P_ACTION_STR] = ("INSERT INTO {{{$table_name}}} SET");
168
      break;
169
      // case SQL_OP_REPLACE: $result = doquery("REPLACE INTO {{{$table_name}}} SET {$fields}") && $result; break;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
170
      default:
171
        die('Неподдерживаемая операция в DBAL\OldDbChangeSet::db_changeset_condition_compile');
172
    }
173
174
    $conditions[P_QUERY_STR] = $conditions[P_ACTION_STR] . ' ' . $conditions[P_FIELDS_STR] . (' WHERE ' . $conditions[P_WHERE_STR]);
175
  }
176
177
  /**
178
   * @param $db_changeset
179
   *
180
   * @return bool
181
   * @deprecated
182
   */
183
  public static function db_changeset_apply($db_changeset) {
184
    $result = true;
185
    if (!is_array($db_changeset) || empty($db_changeset)) {
186
      return $result;
187
    }
188
189
    foreach ($db_changeset as $table_name => &$table_data) {
190
      foreach ($table_data as $record_id => &$conditions) {
191
        OldDbChangeSet::db_changeset_condition_compile($conditions, $table_name);
192
193
        if ($conditions['action'] != SQL_OP_DELETE && !$conditions[P_FIELDS_STR]) {
194
          continue;
195
        }
196
        if ($conditions['action'] == SQL_OP_DELETE && !$conditions[P_WHERE_STR]) {
197
          continue;
198
        } // Защита от случайного удаления всех данных в таблице
199
200
        if ($conditions[P_LOCATION] != LOC_NONE) {
201
          switch ($conditions['action']) {
202
            case SQL_OP_DELETE:
203
              $result = SN::db_del_record_list($conditions[P_LOCATION], $conditions[P_WHERE_STR]) && $result;
204
            break;
205
            case SQL_OP_UPDATE:
206
              $result = SN::db_upd_record_list($conditions[P_LOCATION], $conditions[P_WHERE_STR], $conditions[P_FIELDS_STR]) && $result;
207
            break;
208
            case SQL_OP_INSERT:
209
              $result = SN::db_ins_record($conditions[P_LOCATION], $conditions[P_FIELDS_STR]) && $result;
210
            break;
211
            default:
212
              die('Неподдерживаемая операция в SN::db_changeset_apply');
213
          }
214
        } else {
215
          $result = doquery($conditions[P_QUERY_STR]) && $result;
216
        }
217
      }
218
    }
219
220
    return $result;
221
  }
222
223
}
224