1
|
|
|
<?php namespace XoopsModules\Smartfaq; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* SmartobjectDbupdater class |
5
|
|
|
* |
6
|
|
|
* Class performing the database update for the module |
7
|
|
|
* |
8
|
|
|
* @package SmartObject |
9
|
|
|
* @author marcan <[email protected]> |
10
|
|
|
* @link http://www.smartfactory.ca The SmartFactory |
11
|
|
|
*/ |
12
|
|
|
class SmartobjectDbupdater |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* SmartobjectDbupdater constructor. |
16
|
|
|
*/ |
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Use to execute a general query |
23
|
|
|
* |
24
|
|
|
* @param string $query query that will be executed |
25
|
|
|
* @param string $goodmsg message displayed on success |
26
|
|
|
* @param string $badmsg message displayed on error |
27
|
|
|
* |
28
|
|
|
* @return bool true if success, false if an error occured |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
public function runQuery($query, $goodmsg, $badmsg) |
32
|
|
|
{ |
33
|
|
|
global $xoopsDB; |
|
|
|
|
34
|
|
|
$ret = $xoopsDB->queryF($query); |
35
|
|
|
if (!$ret) { |
36
|
|
|
echo " $badmsg<br>"; |
37
|
|
|
|
38
|
|
|
return false; |
39
|
|
|
} else { |
40
|
|
|
echo " $goodmsg<br>"; |
41
|
|
|
|
42
|
|
|
return true; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Use to rename a table |
48
|
|
|
* |
49
|
|
|
* @param string $from name of the table to rename |
50
|
|
|
* @param string $to new name of the renamed table |
51
|
|
|
* |
52
|
|
|
* @return bool true if success, false if an error occured |
53
|
|
|
*/ |
54
|
|
|
public function renameTable($from, $to) |
55
|
|
|
{ |
56
|
|
|
global $xoopsDB; |
|
|
|
|
57
|
|
|
|
58
|
|
|
$from = $xoopsDB->prefix($from); |
59
|
|
|
$to = $xoopsDB->prefix($to); |
60
|
|
|
|
61
|
|
|
$query = sprintf('ALTER TABLE %s RENAME %s', $from, $to); |
62
|
|
|
$ret = $xoopsDB->queryF($query); |
63
|
|
|
if (!$ret) { |
64
|
|
|
echo ' ' . sprintf(_SDU_MSG_RENAME_TABLE_ERR, $from) . '<br>'; |
65
|
|
|
|
66
|
|
|
return false; |
67
|
|
|
} else { |
68
|
|
|
echo ' ' . sprintf(_SDU_MSG_RENAME_TABLE, $from, $to) . '<br>'; |
69
|
|
|
|
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Use to update a table |
76
|
|
|
* |
77
|
|
|
* @param object $table {@link SmartDbTable} that will be updated |
78
|
|
|
* |
79
|
|
|
* @see SmartDbTable |
80
|
|
|
* |
81
|
|
|
* @return bool true if success, false if an error occured |
82
|
|
|
*/ |
83
|
|
|
public function updateTable($table) |
84
|
|
|
{ |
85
|
|
|
global $xoopsDB; |
|
|
|
|
86
|
|
|
|
87
|
|
|
$ret = true; |
88
|
|
|
|
89
|
|
|
// If table has a structure, create the table |
90
|
|
|
if ($table->getStructure()) { |
91
|
|
|
$ret = $table->createTable() && $ret; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// If table is flag for drop, drop it |
95
|
|
|
if ($table->getFlagForDrop) { |
96
|
|
|
$ret = $table->dropTable() && $ret; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// If table has data, insert it |
100
|
|
|
if ($table->getData()) { |
101
|
|
|
$ret = $table->addData() && $ret; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// If table has new fields to be added, add them |
105
|
|
|
if ($table->getNewFields()) { |
106
|
|
|
$ret = $table->addNewFields() && $ret; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// If table has altered field, alter the table |
110
|
|
|
if ($table->getAlteredFields()) { |
111
|
|
|
$ret = $table->alterTable() && $ret; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// If table has updated field values, update the table |
115
|
|
|
if ($table->getUpdatedFields()) { |
116
|
|
|
$ret = $table->updateFieldsValues($table) && $ret; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// If table has dropped field, alter the table |
120
|
|
|
if ($table->getDroppedFields()) { |
121
|
|
|
$ret = $table->dropFields($table) && $ret; |
122
|
|
|
} |
123
|
|
|
//felix |
124
|
|
|
// If table has updated field values, update the table |
125
|
|
|
if ($table->getUpdatedWhere()) { |
126
|
|
|
$ret = $table->UpdateWhereValues($table) && $ret; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $ret; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state