-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathUpdate.class.php
More file actions
127 lines (122 loc) · 4.39 KB
/
Update.class.php
File metadata and controls
127 lines (122 loc) · 4.39 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
<?php
/**
* モジュールアップデートクラス
*
* @package NetCommons.components
* @author Noriko Arai,Ryuji Masukawa
* @copyright 2006-2007 NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @project NetCommons Project, supported by National Institute of Informatics
* @access public
*/
class Chat_Update extends Action
{
//使用コンポーネントを受け取るため
var $db = null;
function execute()
{
// chatにindexを追加
$sql = "SHOW INDEX FROM `".$this->db->getPrefix()."chat` ;";
$results = $this->db->execute($sql);
if($results === false) return false;
$alter_table_room_id_flag = true;
foreach($results as $result) {
if(isset($result['Key_name']) && $result['Key_name'] == "room_id") {
$alter_table_room_id_flag = false;
}
}
if($alter_table_room_id_flag) {
$sql = "ALTER TABLE `".$this->db->getPrefix()."chat` ADD INDEX ( `room_id` ) ;";
$result = $this->db->execute($sql);
if($result === false) return false;
}
// chat_contentsにindexを追加
$sql = "SHOW INDEX FROM `".$this->db->getPrefix()."chat_contents` ;";
$results = $this->db->execute($sql);
if($results === false) return false;
$alter_table_chat_id_flag = true;
$alter_table_room_id_flag = true;
foreach($results as $result) {
if(isset($result['Key_name']) && $result['Key_name'] == "PRIMARY" &&
$result['Seq_in_index'] == 1 && $result['Column_name'] == "block_id") {
$alter_table_chat_id_flag = false;
}
if(isset($result['Key_name']) && $result['Key_name'] == "room_id") {
$alter_table_room_id_flag = false;
}
}
if($alter_table_chat_id_flag) {
$sql = "ALTER TABLE `".$this->db->getPrefix()."chat_contents` DROP PRIMARY KEY , ADD PRIMARY KEY ( `block_id` , `chat_id` ) ;";
$result = $this->db->execute($sql);
if($result === false) return false;
}
if($alter_table_room_id_flag) {
$sql = "ALTER TABLE `".$this->db->getPrefix()."chat_contents` ADD INDEX ( `room_id` ) ;";
$result = $this->db->execute($sql);
if($result === false) return false;
}
// chat_loginにindexを追加
$sql = "SHOW INDEX FROM `".$this->db->getPrefix()."chat_login` ;";
$results = $this->db->execute($sql);
if($results === false) return false;
$alter_table_block_id_flag = true;
$alter_table_block_id_2_flag = true;
$alter_table_room_id_flag = true;
foreach($results as $result) {
if(isset($result['Key_name']) && $result['Key_name'] == "block_id") {
$alter_table_block_id_flag = false;
}
if(isset($result['Key_name']) && $result['Key_name'] == "block_id_2") {
$alter_table_block_id_2_flag = false;
}
if(isset($result['Key_name']) && $result['Key_name'] == "room_id") {
$alter_table_room_id_flag = false;
}
}
if($alter_table_block_id_flag) {
$sql = "ALTER TABLE `".$this->db->getPrefix()."chat_login` ADD INDEX ( `block_id`, `update_user_id` ) ;";
$result = $this->db->execute($sql);
if($result === false) return false;
}
if($alter_table_block_id_2_flag) {
$sql = "ALTER TABLE `".$this->db->getPrefix()."chat_login`
ADD INDEX `block_id_2` ( `block_id`, `update_time` ) ;";
$result = $this->db->execute($sql);
if($result === false) return false;
}
if($alter_table_room_id_flag) {
$sql = "ALTER TABLE `".$this->db->getPrefix()."chat_login` ADD INDEX ( `room_id` ) ;";
$result = $this->db->execute($sql);
if($result === false) return false;
}
$sql = "SELECT block_id "
. "FROM " . $this->db->getPrefix() . "chat_contents "
. "GROUP BY block_id "
. "HAVING COUNT(chat_id) != MAX(chat_id)";
$chatBlocks = $this->db->execute($sql);
foreach ($chatBlocks as $chatBlock) {
$chatId = 1;
$sql = "SELECT chat_id "
. "FROM " . $this->db->getPrefix() . "chat_contents "
. "WHERE block_id = ? "
. "ORDER BY chat_id";
$chatContents = $this->db->execute($sql, $chatBlock['block_id']);
foreach ($chatContents as $chatContent) {
$updateColumns = array(
'chat_id' => $chatId
);
$whereColumns = array(
'block_id' => $chatBlock['block_id'],
'chat_id' => $chatContent['chat_id']
);
$result = $this->db->updateExecute('chat_contents', $updateColumns, $whereColumns, false);
if ($result === false) {
return false;
}
$chatId++;
}
}
return true;
}
}
?>