-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRoomsLibDataSourceExecute.php
More file actions
308 lines (266 loc) · 7.97 KB
/
RoomsLibDataSourceExecute.php
File metadata and controls
308 lines (266 loc) · 7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/**
* ルーム削除時の関連テーブル削除処理に関するライブラリ
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('RoomsLibLog', 'Rooms.Lib');
/**
* ルーム削除時の関連テーブル削除処理に関するライブラリ
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Rooms\Lib
*/
class RoomsLibDataSourceExecute {
/**
* データソースオブジェクト
*
* @var DataSource
*/
private $__DataSource;
/**
* 使用するモデル
*
* @var Model
*/
private $__Model;
/**
* 実行シェル
*
* @var Shell
*/
private $__Shell;
/**
* コンストラクタ
*
* @param Model $Model モデル(当モデルは、MySQLのModelであれば何でも良い)
* @param Shell|null $Shell 実行シェル
* @return void
*/
public function __construct(Model $Model, $Shell = null) {
$this->__Model = $Model;
$this->__Shell = $Shell;
$this->__DataSource = $Model->getDataSource();
$this->__RoomsLibCache = new RoomsLibCache($Model);
}
/**
* テーブルリストをキャッシュから取得する。なければ、最新情報から取得し、キャッシュに保存する
*
* @return array
*/
public function showTables() {
//キャッシュから取得
$cacheTables = $this->__RoomsLibCache->readCache('tables', null);
if ($cacheTables) {
return $cacheTables;
}
$schemaTables = $this->__findAllSchemeFileTables();
$tablePrefix = $this->__Model->tablePrefix;
//LIKEで多少の絞り込みを行う。ただし、tablePrefixがない場合は、全テーブルが対象となってしまう。
$tables = $this->__Model->query("SHOW TABLES LIKE '{$tablePrefix}%s'");
$retTables = [];
foreach ($tables as $table) {
$realTableName = null;
if (array_key_exists('TABLE_NAMES', $table)) {
$realTableName = array_shift($table['TABLE_NAMES']);
} elseif (array_key_exists('TABLES', $table)) {
$realTableName = array_shift($table['TABLES']);
}
$realPrefix = substr($realTableName, 0, strlen($tablePrefix));
$tableName = substr($realTableName, strlen($tablePrefix));
if ($tablePrefix !== $realPrefix ||
! in_array($tableName, $schemaTables)) {
continue;
}
$retTables[$tableName] = $this->showTableColumns($realTableName);
}
//キャッシュに登録
$this->__RoomsLibCache->saveCache('tables', null, $retTables);
return $retTables;
}
/**
* schemaファイルからテーブルリストを取得する
*
* @return array
*/
private function __findAllSchemeFileTables() {
App::uses('CakeSchema', 'Model');
$plugins = App::objects('plugins');
$allTables = [];
foreach ($plugins as $plugin) {
$tables = $this->__getSchemeFileTablesByPlugin($plugin);
if ($tables) {
$allTables = array_merge($allTables, $tables);
}
}
return $allTables;
}
/**
* プラグイン名に対してschemaファイルのテーブルリストを取得する
*
* @param string $plugin プラグイン
* @return array|false
*/
private function __getSchemeFileTablesByPlugin($plugin) {
$class = $plugin . 'Schema';
if (! CakePlugin::loaded($plugin)) {
return false;
}
$filePath = CakePlugin::path($plugin) . 'Config' . DS . 'Schema' . DS . 'schema.php';
if (! file_exists($filePath)) {
return false;
}
include_once $filePath;
if (! class_exists($class)) {
return false;
}
$classVars = get_class_vars($class);
$tables = [];
foreach ($classVars as $key => $value) {
if (is_array($value) && !empty($value)) {
$tables[] = $key;
}
}
return $tables;
}
/**
* DBからカラムリストを取得する
*
* @param string $realTableName テーブルPrefix付きの実際のテーブル名
* @return array
*/
public function showTableColumns($realTableName) {
$columns = $this->__Model->query('SHOW COLUMNS FROM `' . $realTableName . '`');
$retColumns = [];
foreach ($columns as $column) {
$name = $column['COLUMNS']['Field'];
$retColumns[$name] = $column['COLUMNS'];
}
return $retColumns;
}
/**
* SELECTのSQLを実行する
*
* @param string $tableName テーブル名
* @param array $selectFieldNames SELECTのカラム名リスト
* @param array $wheres WHERE条件リスト
* @return array
*/
public function selectQuery($tableName, $selectFieldNames, $wheres) {
$tablePrefix = $this->__Model->tablePrefix;
$realTableName = $tablePrefix . $tableName;
$tableAlias = Inflector::classify($tableName);
$fields = array_map(function ($fieldName) use ($tableAlias) {
return $this->__Model->escapeField($fieldName, $tableAlias);
}, $selectFieldNames);
$sql = sprintf(
'SELECT %s FROM `%s` AS `%s` WHERE %s',
implode(', ', $fields),
$realTableName,
$tableAlias,
$this->__makeWhereSql($tableAlias, $wheres)
);
$queryResults = $this->__Model->query($sql);
return $this->__flattenForDeleteTargetValues($tableName, $tableAlias, $queryResults);
}
/**
* SELECT COUNT(*)のSQLを実行する
*
* @param string $tableName テーブル名
* @param array $wheres WHERE条件リスト
* @return array
*/
public function countQuery($tableName, $wheres) {
$tablePrefix = $this->__Model->tablePrefix;
$realTableName = $tablePrefix . $tableName;
$tableAlias = Inflector::classify($tableName);
$sql = sprintf(
'SELECT COUNT(*) AS count_num FROM `%s` AS `%s` WHERE %s',
$realTableName,
$tableAlias,
$this->__makeWhereSql($tableAlias, $wheres)
);
$queryResults = $this->__Model->query($sql);
if (isset($queryResults[0][0]['count_num'])) {
$count = (int)$queryResults[0][0]['count_num'];
} else {
$count = 0;
}
return $count;
}
/**
* DELETEのSQLを実行する
*
* @param string $tableName テーブル名
* @param array $wheres WHERE条件リスト
* @return array
*/
public function deleteQuery($tableName, $wheres) {
$tablePrefix = $this->__Model->tablePrefix;
$realTableName = $tablePrefix . $tableName;
$sql = sprintf(
'DELETE FROM `%s` WHERE %s',
$realTableName,
$this->__makeWhereSql($realTableName, $wheres)
);
RoomsLibLog::infoLog($this->__Shell, $sql, 2);
$queryResults = $this->__Model->query($sql);
RoomsLibLog::successLog(
$this->__Shell,
'--> AffectedRows = ' . $this->__Model->getAffectedRows(),
2
);
return $queryResults;
}
/**
* 外部フィードキーに対して、削除対象データを取得する
*
* @param string $tableAlias テーブルのAlias名
* @param array $wheres WHERE条件リスト
* @return array
*/
private function __makeWhereSql($tableAlias, $wheres) {
$whereConditions = [];
foreach ($wheres as $fieldName => $values) {
$whereField = $this->__Model->escapeField($fieldName, $tableAlias);
if (is_array($values)) {
$escapeValuesArr = array_map(function ($value) {
return $this->__DataSource->value($value, 'string');
}, $values);
$escapeValues = implode(', ', $escapeValuesArr);
$whereConditions[] = $whereField . ' IN (' . $escapeValues . ')';
} else {
$escapeValues = $this->__DataSource->value($values, 'string');
$whereConditions[] = $whereField . '=' . $escapeValues;
}
}
return implode(' AND ', $whereConditions);
}
/**
* クエリの結果をフラットの配列(一次配列、「.」で連結)にする
*
* @param string $tableName テーブル名
* @param string $tableAlias テーブルのAlias名
* @param array $queryResults クエリの結果
* @return array
*/
private function __flattenForDeleteTargetValues($tableName, $tableAlias, $queryResults) {
$retResults = [];
if (empty($queryResults)) {
return $retResults;
}
foreach ($queryResults as $result) {
foreach ($result[$tableAlias] as $field => $value) {
if (! isset($retResults[$tableName . '.' . $field])) {
$retResults[$tableName . '.' . $field] = [];
}
$retResults[$tableName . '.' . $field][] = $value;
}
}
return $retResults;
}
}