|
indvdum@0
|
1 |
<?php
|
|
indvdum@0
|
2 |
/**
|
|
indvdum@0
|
3 |
*
|
|
indvdum@0
|
4 |
* @package acp
|
|
indvdum@0
|
5 |
* @version $Id$
|
|
indvdum@0
|
6 |
* @copyright (c) 2005 phpBB Group
|
|
indvdum@0
|
7 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
indvdum@0
|
8 |
*
|
|
indvdum@0
|
9 |
*/
|
|
indvdum@0
|
10 |
|
|
indvdum@0
|
11 |
/**
|
|
indvdum@0
|
12 |
* @ignore
|
|
indvdum@0
|
13 |
*/
|
|
indvdum@0
|
14 |
if (!defined('IN_PHPBB'))
|
|
indvdum@0
|
15 |
{
|
|
indvdum@0
|
16 |
exit;
|
|
indvdum@0
|
17 |
}
|
|
indvdum@0
|
18 |
|
|
indvdum@0
|
19 |
/**
|
|
indvdum@0
|
20 |
* @package acp
|
|
indvdum@0
|
21 |
*/
|
|
indvdum@0
|
22 |
class acp_database
|
|
indvdum@0
|
23 |
{
|
|
indvdum@0
|
24 |
var $u_action;
|
|
indvdum@0
|
25 |
|
|
indvdum@0
|
26 |
function main($id, $mode)
|
|
indvdum@0
|
27 |
{
|
|
indvdum@0
|
28 |
global $cache, $db, $user, $auth, $template, $table_prefix;
|
|
indvdum@0
|
29 |
global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
|
|
indvdum@0
|
30 |
|
|
indvdum@0
|
31 |
$user->add_lang('acp/database');
|
|
indvdum@0
|
32 |
|
|
indvdum@0
|
33 |
$this->tpl_name = 'acp_database';
|
|
indvdum@0
|
34 |
$this->page_title = 'ACP_DATABASE';
|
|
indvdum@0
|
35 |
|
|
indvdum@0
|
36 |
$action = request_var('action', '');
|
|
indvdum@0
|
37 |
$submit = (isset($_POST['submit'])) ? true : false;
|
|
indvdum@0
|
38 |
|
|
indvdum@0
|
39 |
$template->assign_vars(array(
|
|
indvdum@0
|
40 |
'MODE' => $mode
|
|
indvdum@0
|
41 |
));
|
|
indvdum@0
|
42 |
|
|
indvdum@0
|
43 |
switch ($mode)
|
|
indvdum@0
|
44 |
{
|
|
indvdum@0
|
45 |
case 'backup':
|
|
indvdum@0
|
46 |
|
|
indvdum@0
|
47 |
$this->page_title = 'ACP_BACKUP';
|
|
indvdum@0
|
48 |
|
|
indvdum@0
|
49 |
switch ($action)
|
|
indvdum@0
|
50 |
{
|
|
indvdum@0
|
51 |
case 'download':
|
|
indvdum@0
|
52 |
$type = request_var('type', '');
|
|
indvdum@0
|
53 |
$table = request_var('table', array(''));
|
|
indvdum@0
|
54 |
$format = request_var('method', '');
|
|
indvdum@0
|
55 |
$where = request_var('where', '');
|
|
indvdum@0
|
56 |
|
|
indvdum@0
|
57 |
if (!sizeof($table))
|
|
indvdum@0
|
58 |
{
|
|
indvdum@0
|
59 |
trigger_error($user->lang['TABLE_SELECT_ERROR'] . adm_back_link($this->u_action), E_USER_WARNING);
|
|
indvdum@0
|
60 |
}
|
|
indvdum@0
|
61 |
|
|
indvdum@0
|
62 |
$store = $download = $structure = $schema_data = false;
|
|
indvdum@0
|
63 |
|
|
indvdum@0
|
64 |
if ($where == 'store_and_download' || $where == 'store')
|
|
indvdum@0
|
65 |
{
|
|
indvdum@0
|
66 |
$store = true;
|
|
indvdum@0
|
67 |
}
|
|
indvdum@0
|
68 |
|
|
indvdum@0
|
69 |
if ($where == 'store_and_download' || $where == 'download')
|
|
indvdum@0
|
70 |
{
|
|
indvdum@0
|
71 |
$download = true;
|
|
indvdum@0
|
72 |
}
|
|
indvdum@0
|
73 |
|
|
indvdum@0
|
74 |
if ($type == 'full' || $type == 'structure')
|
|
indvdum@0
|
75 |
{
|
|
indvdum@0
|
76 |
$structure = true;
|
|
indvdum@0
|
77 |
}
|
|
indvdum@0
|
78 |
|
|
indvdum@0
|
79 |
if ($type == 'full' || $type == 'data')
|
|
indvdum@0
|
80 |
{
|
|
indvdum@0
|
81 |
$schema_data = true;
|
|
indvdum@0
|
82 |
}
|
|
indvdum@0
|
83 |
|
|
indvdum@0
|
84 |
@set_time_limit(1200);
|
|
indvdum@0
|
85 |
@set_time_limit(0);
|
|
indvdum@0
|
86 |
|
|
indvdum@0
|
87 |
$time = time();
|
|
indvdum@0
|
88 |
|
|
indvdum@0
|
89 |
$filename = 'backup_' . $time . '_' . unique_id();
|
|
indvdum@0
|
90 |
switch ($db->sql_layer)
|
|
indvdum@0
|
91 |
{
|
|
indvdum@0
|
92 |
case 'mysqli':
|
|
indvdum@0
|
93 |
case 'mysql4':
|
|
indvdum@0
|
94 |
case 'mysql':
|
|
indvdum@0
|
95 |
$extractor = new mysql_extractor($download, $store, $format, $filename, $time);
|
|
indvdum@0
|
96 |
break;
|
|
indvdum@0
|
97 |
|
|
indvdum@0
|
98 |
case 'sqlite':
|
|
indvdum@0
|
99 |
$extractor = new sqlite_extractor($download, $store, $format, $filename, $time);
|
|
indvdum@0
|
100 |
break;
|
|
indvdum@0
|
101 |
|
|
indvdum@0
|
102 |
case 'postgres':
|
|
indvdum@0
|
103 |
$extractor = new postgres_extractor($download, $store, $format, $filename, $time);
|
|
indvdum@0
|
104 |
break;
|
|
indvdum@0
|
105 |
|
|
indvdum@0
|
106 |
case 'oracle':
|
|
indvdum@0
|
107 |
$extractor = new oracle_extractor($download, $store, $format, $filename, $time);
|
|
indvdum@0
|
108 |
break;
|
|
indvdum@0
|
109 |
|
|
indvdum@0
|
110 |
case 'mssql':
|
|
indvdum@0
|
111 |
case 'mssql_odbc':
|
|
indvdum@0
|
112 |
case 'mssqlnative':
|
|
indvdum@0
|
113 |
$extractor = new mssql_extractor($download, $store, $format, $filename, $time);
|
|
indvdum@0
|
114 |
break;
|
|
indvdum@0
|
115 |
|
|
indvdum@0
|
116 |
case 'firebird':
|
|
indvdum@0
|
117 |
$extractor = new firebird_extractor($download, $store, $format, $filename, $time);
|
|
indvdum@0
|
118 |
break;
|
|
indvdum@0
|
119 |
}
|
|
indvdum@0
|
120 |
|
|
indvdum@0
|
121 |
$extractor->write_start($table_prefix);
|
|
indvdum@0
|
122 |
|
|
indvdum@0
|
123 |
foreach ($table as $table_name)
|
|
indvdum@0
|
124 |
{
|
|
indvdum@0
|
125 |
// Get the table structure
|
|
indvdum@0
|
126 |
if ($structure)
|
|
indvdum@0
|
127 |
{
|
|
indvdum@0
|
128 |
$extractor->write_table($table_name);
|
|
indvdum@0
|
129 |
}
|
|
indvdum@0
|
130 |
else
|
|
indvdum@0
|
131 |
{
|
|
indvdum@0
|
132 |
// We might wanna empty out all that junk :D
|
|
indvdum@0
|
133 |
switch ($db->sql_layer)
|
|
indvdum@0
|
134 |
{
|
|
indvdum@0
|
135 |
case 'sqlite':
|
|
indvdum@0
|
136 |
case 'firebird':
|
|
indvdum@0
|
137 |
$extractor->flush('DELETE FROM ' . $table_name . ";\n");
|
|
indvdum@0
|
138 |
break;
|
|
indvdum@0
|
139 |
|
|
indvdum@0
|
140 |
case 'mssql':
|
|
indvdum@0
|
141 |
case 'mssql_odbc':
|
|
indvdum@0
|
142 |
case 'mssqlnative':
|
|
indvdum@0
|
143 |
$extractor->flush('TRUNCATE TABLE ' . $table_name . "GO\n");
|
|
indvdum@0
|
144 |
break;
|
|
indvdum@0
|
145 |
|
|
indvdum@0
|
146 |
case 'oracle':
|
|
indvdum@0
|
147 |
$extractor->flush('TRUNCATE TABLE ' . $table_name . "/\n");
|
|
indvdum@0
|
148 |
break;
|
|
indvdum@0
|
149 |
|
|
indvdum@0
|
150 |
default:
|
|
indvdum@0
|
151 |
$extractor->flush('TRUNCATE TABLE ' . $table_name . ";\n");
|
|
indvdum@0
|
152 |
break;
|
|
indvdum@0
|
153 |
}
|
|
indvdum@0
|
154 |
}
|
|
indvdum@0
|
155 |
|
|
indvdum@0
|
156 |
// Data
|
|
indvdum@0
|
157 |
if ($schema_data)
|
|
indvdum@0
|
158 |
{
|
|
indvdum@0
|
159 |
$extractor->write_data($table_name);
|
|
indvdum@0
|
160 |
}
|
|
indvdum@0
|
161 |
}
|
|
indvdum@0
|
162 |
|
|
indvdum@0
|
163 |
$extractor->write_end();
|
|
indvdum@0
|
164 |
|
|
indvdum@0
|
165 |
add_log('admin', 'LOG_DB_BACKUP');
|
|
indvdum@0
|
166 |
|
|
indvdum@0
|
167 |
if ($download == true)
|
|
indvdum@0
|
168 |
{
|
|
indvdum@0
|
169 |
exit;
|
|
indvdum@0
|
170 |
}
|
|
indvdum@0
|
171 |
|
|
indvdum@0
|
172 |
trigger_error($user->lang['BACKUP_SUCCESS'] . adm_back_link($this->u_action));
|
|
indvdum@0
|
173 |
break;
|
|
indvdum@0
|
174 |
|
|
indvdum@0
|
175 |
default:
|
|
indvdum@0
|
176 |
include($phpbb_root_path . 'includes/functions_install.' . $phpEx);
|
|
indvdum@0
|
177 |
$tables = get_tables($db);
|
|
indvdum@0
|
178 |
asort($tables);
|
|
indvdum@0
|
179 |
foreach ($tables as $table_name)
|
|
indvdum@0
|
180 |
{
|
|
indvdum@0
|
181 |
if (strlen($table_prefix) === 0 || stripos($table_name, $table_prefix) === 0)
|
|
indvdum@0
|
182 |
{
|
|
indvdum@0
|
183 |
$template->assign_block_vars('tables', array(
|
|
indvdum@0
|
184 |
'TABLE' => $table_name
|
|
indvdum@0
|
185 |
));
|
|
indvdum@0
|
186 |
}
|
|
indvdum@0
|
187 |
}
|
|
indvdum@0
|
188 |
unset($tables);
|
|
indvdum@0
|
189 |
|
|
indvdum@0
|
190 |
$template->assign_vars(array(
|
|
indvdum@0
|
191 |
'U_ACTION' => $this->u_action . '&action=download'
|
|
indvdum@0
|
192 |
));
|
|
indvdum@0
|
193 |
|
|
indvdum@0
|
194 |
$available_methods = array('gzip' => 'zlib', 'bzip2' => 'bz2');
|
|
indvdum@0
|
195 |
|
|
indvdum@0
|
196 |
foreach ($available_methods as $type => $module)
|
|
indvdum@0
|
197 |
{
|
|
indvdum@0
|
198 |
if (!@extension_loaded($module))
|
|
indvdum@0
|
199 |
{
|
|
indvdum@0
|
200 |
continue;
|
|
indvdum@0
|
201 |
}
|
|
indvdum@0
|
202 |
|
|
indvdum@0
|
203 |
$template->assign_block_vars('methods', array(
|
|
indvdum@0
|
204 |
'TYPE' => $type
|
|
indvdum@0
|
205 |
));
|
|
indvdum@0
|
206 |
}
|
|
indvdum@0
|
207 |
|
|
indvdum@0
|
208 |
$template->assign_block_vars('methods', array(
|
|
indvdum@0
|
209 |
'TYPE' => 'text'
|
|
indvdum@0
|
210 |
));
|
|
indvdum@0
|
211 |
break;
|
|
indvdum@0
|
212 |
}
|
|
indvdum@0
|
213 |
break;
|
|
indvdum@0
|
214 |
|
|
indvdum@0
|
215 |
case 'restore':
|
|
indvdum@0
|
216 |
|
|
indvdum@0
|
217 |
$this->page_title = 'ACP_RESTORE';
|
|
indvdum@0
|
218 |
|
|
indvdum@0
|
219 |
switch ($action)
|
|
indvdum@0
|
220 |
{
|
|
indvdum@0
|
221 |
case 'submit':
|
|
indvdum@0
|
222 |
$delete = request_var('delete', '');
|
|
indvdum@0
|
223 |
$file = request_var('file', '');
|
|
indvdum@0
|
224 |
$download = request_var('download', '');
|
|
indvdum@0
|
225 |
|
|
indvdum@0
|
226 |
if (!preg_match('#^backup_\d{10,}_[a-z\d]{16}\.(sql(?:\.(?:gz|bz2))?)$#', $file, $matches))
|
|
indvdum@0
|
227 |
{
|
|
indvdum@0
|
228 |
trigger_error($user->lang['BACKUP_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
|
|
indvdum@0
|
229 |
}
|
|
indvdum@0
|
230 |
|
|
indvdum@0
|
231 |
$file_name = $phpbb_root_path . 'store/' . $matches[0];
|
|
indvdum@0
|
232 |
|
|
indvdum@0
|
233 |
if (!file_exists($file_name) || !is_readable($file_name))
|
|
indvdum@0
|
234 |
{
|
|
indvdum@0
|
235 |
trigger_error($user->lang['BACKUP_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
|
|
indvdum@0
|
236 |
}
|
|
indvdum@0
|
237 |
|
|
indvdum@0
|
238 |
if ($delete)
|
|
indvdum@0
|
239 |
{
|
|
indvdum@0
|
240 |
if (confirm_box(true))
|
|
indvdum@0
|
241 |
{
|
|
indvdum@0
|
242 |
unlink($file_name);
|
|
indvdum@0
|
243 |
add_log('admin', 'LOG_DB_DELETE');
|
|
indvdum@0
|
244 |
trigger_error($user->lang['BACKUP_DELETE'] . adm_back_link($this->u_action));
|
|
indvdum@0
|
245 |
}
|
|
indvdum@0
|
246 |
else
|
|
indvdum@0
|
247 |
{
|
|
indvdum@0
|
248 |
confirm_box(false, $user->lang['DELETE_SELECTED_BACKUP'], build_hidden_fields(array('delete' => $delete, 'file' => $file)));
|
|
indvdum@0
|
249 |
}
|
|
indvdum@0
|
250 |
}
|
|
indvdum@0
|
251 |
else if ($download || confirm_box(true))
|
|
indvdum@0
|
252 |
{
|
|
indvdum@0
|
253 |
if ($download)
|
|
indvdum@0
|
254 |
{
|
|
indvdum@0
|
255 |
$name = $matches[0];
|
|
indvdum@0
|
256 |
|
|
indvdum@0
|
257 |
switch ($matches[1])
|
|
indvdum@0
|
258 |
{
|
|
indvdum@0
|
259 |
case 'sql':
|
|
indvdum@0
|
260 |
$mimetype = 'text/x-sql';
|
|
indvdum@0
|
261 |
break;
|
|
indvdum@0
|
262 |
case 'sql.bz2':
|
|
indvdum@0
|
263 |
$mimetype = 'application/x-bzip2';
|
|
indvdum@0
|
264 |
break;
|
|
indvdum@0
|
265 |
case 'sql.gz':
|
|
indvdum@0
|
266 |
$mimetype = 'application/x-gzip';
|
|
indvdum@0
|
267 |
break;
|
|
indvdum@0
|
268 |
}
|
|
indvdum@0
|
269 |
|
|
indvdum@0
|
270 |
header('Pragma: no-cache');
|
|
indvdum@0
|
271 |
header("Content-Type: $mimetype; name=\"$name\"");
|
|
indvdum@0
|
272 |
header("Content-disposition: attachment; filename=$name");
|
|
indvdum@0
|
273 |
|
|
indvdum@0
|
274 |
@set_time_limit(0);
|
|
indvdum@0
|
275 |
|
|
indvdum@0
|
276 |
$fp = @fopen($file_name, 'rb');
|
|
indvdum@0
|
277 |
|
|
indvdum@0
|
278 |
if ($fp !== false)
|
|
indvdum@0
|
279 |
{
|
|
indvdum@0
|
280 |
while (!feof($fp))
|
|
indvdum@0
|
281 |
{
|
|
indvdum@0
|
282 |
echo fread($fp, 8192);
|
|
indvdum@0
|
283 |
}
|
|
indvdum@0
|
284 |
fclose($fp);
|
|
indvdum@0
|
285 |
}
|
|
indvdum@0
|
286 |
|
|
indvdum@0
|
287 |
flush();
|
|
indvdum@0
|
288 |
exit;
|
|
indvdum@0
|
289 |
}
|
|
indvdum@0
|
290 |
|
|
indvdum@0
|
291 |
switch ($matches[1])
|
|
indvdum@0
|
292 |
{
|
|
indvdum@0
|
293 |
case 'sql':
|
|
indvdum@0
|
294 |
$fp = fopen($file_name, 'rb');
|
|
indvdum@0
|
295 |
$read = 'fread';
|
|
indvdum@0
|
296 |
$seek = 'fseek';
|
|
indvdum@0
|
297 |
$eof = 'feof';
|
|
indvdum@0
|
298 |
$close = 'fclose';
|
|
indvdum@0
|
299 |
$fgetd = 'fgetd';
|
|
indvdum@0
|
300 |
break;
|
|
indvdum@0
|
301 |
|
|
indvdum@0
|
302 |
case 'sql.bz2':
|
|
indvdum@0
|
303 |
$fp = bzopen($file_name, 'r');
|
|
indvdum@0
|
304 |
$read = 'bzread';
|
|
indvdum@0
|
305 |
$seek = '';
|
|
indvdum@0
|
306 |
$eof = 'feof';
|
|
indvdum@0
|
307 |
$close = 'bzclose';
|
|
indvdum@0
|
308 |
$fgetd = 'fgetd_seekless';
|
|
indvdum@0
|
309 |
break;
|
|
indvdum@0
|
310 |
|
|
indvdum@0
|
311 |
case 'sql.gz':
|
|
indvdum@0
|
312 |
$fp = gzopen($file_name, 'rb');
|
|
indvdum@0
|
313 |
$read = 'gzread';
|
|
indvdum@0
|
314 |
$seek = 'gzseek';
|
|
indvdum@0
|
315 |
$eof = 'gzeof';
|
|
indvdum@0
|
316 |
$close = 'gzclose';
|
|
indvdum@0
|
317 |
$fgetd = 'fgetd';
|
|
indvdum@0
|
318 |
break;
|
|
indvdum@0
|
319 |
}
|
|
indvdum@0
|
320 |
|
|
indvdum@0
|
321 |
switch ($db->sql_layer)
|
|
indvdum@0
|
322 |
{
|
|
indvdum@0
|
323 |
case 'mysql':
|
|
indvdum@0
|
324 |
case 'mysql4':
|
|
indvdum@0
|
325 |
case 'mysqli':
|
|
indvdum@0
|
326 |
case 'sqlite':
|
|
indvdum@0
|
327 |
while (($sql = $fgetd($fp, ";\n", $read, $seek, $eof)) !== false)
|
|
indvdum@0
|
328 |
{
|
|
indvdum@0
|
329 |
$db->sql_query($sql);
|
|
indvdum@0
|
330 |
}
|
|
indvdum@0
|
331 |
break;
|
|
indvdum@0
|
332 |
|
|
indvdum@0
|
333 |
case 'firebird':
|
|
indvdum@0
|
334 |
$delim = ";\n";
|
|
indvdum@0
|
335 |
while (($sql = $fgetd($fp, $delim, $read, $seek, $eof)) !== false)
|
|
indvdum@0
|
336 |
{
|
|
indvdum@0
|
337 |
$query = trim($sql);
|
|
indvdum@0
|
338 |
if (substr($query, 0, 8) === 'SET TERM')
|
|
indvdum@0
|
339 |
{
|
|
indvdum@0
|
340 |
$delim = $query[9] . "\n";
|
|
indvdum@0
|
341 |
continue;
|
|
indvdum@0
|
342 |
}
|
|
indvdum@0
|
343 |
$db->sql_query($query);
|
|
indvdum@0
|
344 |
}
|
|
indvdum@0
|
345 |
break;
|
|
indvdum@0
|
346 |
|
|
indvdum@0
|
347 |
case 'postgres':
|
|
indvdum@0
|
348 |
$delim = ";\n";
|
|
indvdum@0
|
349 |
while (($sql = $fgetd($fp, $delim, $read, $seek, $eof)) !== false)
|
|
indvdum@0
|
350 |
{
|
|
indvdum@0
|
351 |
$query = trim($sql);
|
|
indvdum@0
|
352 |
|
|
indvdum@0
|
353 |
if (substr($query, 0, 13) == 'CREATE DOMAIN')
|
|
indvdum@0
|
354 |
{
|
|
indvdum@0
|
355 |
list(, , $domain) = explode(' ', $query);
|
|
indvdum@0
|
356 |
$sql = "SELECT domain_name
|
|
indvdum@0
|
357 |
FROM information_schema.domains
|
|
indvdum@0
|
358 |
WHERE domain_name = '$domain';";
|
|
indvdum@0
|
359 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
360 |
if (!$db->sql_fetchrow($result))
|
|
indvdum@0
|
361 |
{
|
|
indvdum@0
|
362 |
$db->sql_query($query);
|
|
indvdum@0
|
363 |
}
|
|
indvdum@0
|
364 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
365 |
}
|
|
indvdum@0
|
366 |
else
|
|
indvdum@0
|
367 |
{
|
|
indvdum@0
|
368 |
$db->sql_query($query);
|
|
indvdum@0
|
369 |
}
|
|
indvdum@0
|
370 |
|
|
indvdum@0
|
371 |
if (substr($query, 0, 4) == 'COPY')
|
|
indvdum@0
|
372 |
{
|
|
indvdum@0
|
373 |
while (($sub = $fgetd($fp, "\n", $read, $seek, $eof)) !== '\.')
|
|
indvdum@0
|
374 |
{
|
|
indvdum@0
|
375 |
if ($sub === false)
|
|
indvdum@0
|
376 |
{
|
|
indvdum@0
|
377 |
trigger_error($user->lang['RESTORE_FAILURE'] . adm_back_link($this->u_action), E_USER_WARNING);
|
|
indvdum@0
|
378 |
}
|
|
indvdum@0
|
379 |
pg_put_line($db->db_connect_id, $sub . "\n");
|
|
indvdum@0
|
380 |
}
|
|
indvdum@0
|
381 |
pg_put_line($db->db_connect_id, "\\.\n");
|
|
indvdum@0
|
382 |
pg_end_copy($db->db_connect_id);
|
|
indvdum@0
|
383 |
}
|
|
indvdum@0
|
384 |
}
|
|
indvdum@0
|
385 |
break;
|
|
indvdum@0
|
386 |
|
|
indvdum@0
|
387 |
case 'oracle':
|
|
indvdum@0
|
388 |
while (($sql = $fgetd($fp, "/\n", $read, $seek, $eof)) !== false)
|
|
indvdum@0
|
389 |
{
|
|
indvdum@0
|
390 |
$db->sql_query($sql);
|
|
indvdum@0
|
391 |
}
|
|
indvdum@0
|
392 |
break;
|
|
indvdum@0
|
393 |
|
|
indvdum@0
|
394 |
case 'mssql':
|
|
indvdum@0
|
395 |
case 'mssql_odbc':
|
|
indvdum@0
|
396 |
case 'mssqlnative':
|
|
indvdum@0
|
397 |
while (($sql = $fgetd($fp, "GO\n", $read, $seek, $eof)) !== false)
|
|
indvdum@0
|
398 |
{
|
|
indvdum@0
|
399 |
$db->sql_query($sql);
|
|
indvdum@0
|
400 |
}
|
|
indvdum@0
|
401 |
break;
|
|
indvdum@0
|
402 |
}
|
|
indvdum@0
|
403 |
|
|
indvdum@0
|
404 |
$close($fp);
|
|
indvdum@0
|
405 |
|
|
indvdum@0
|
406 |
// Purge the cache due to updated data
|
|
indvdum@0
|
407 |
$cache->purge();
|
|
indvdum@0
|
408 |
|
|
indvdum@0
|
409 |
add_log('admin', 'LOG_DB_RESTORE');
|
|
indvdum@0
|
410 |
trigger_error($user->lang['RESTORE_SUCCESS'] . adm_back_link($this->u_action));
|
|
indvdum@0
|
411 |
break;
|
|
indvdum@0
|
412 |
}
|
|
indvdum@0
|
413 |
else if (!$download)
|
|
indvdum@0
|
414 |
{
|
|
indvdum@0
|
415 |
confirm_box(false, $user->lang['RESTORE_SELECTED_BACKUP'], build_hidden_fields(array('file' => $file)));
|
|
indvdum@0
|
416 |
}
|
|
indvdum@0
|
417 |
|
|
indvdum@0
|
418 |
default:
|
|
indvdum@0
|
419 |
$methods = array('sql');
|
|
indvdum@0
|
420 |
$available_methods = array('sql.gz' => 'zlib', 'sql.bz2' => 'bz2');
|
|
indvdum@0
|
421 |
|
|
indvdum@0
|
422 |
foreach ($available_methods as $type => $module)
|
|
indvdum@0
|
423 |
{
|
|
indvdum@0
|
424 |
if (!@extension_loaded($module))
|
|
indvdum@0
|
425 |
{
|
|
indvdum@0
|
426 |
continue;
|
|
indvdum@0
|
427 |
}
|
|
indvdum@0
|
428 |
$methods[] = $type;
|
|
indvdum@0
|
429 |
}
|
|
indvdum@0
|
430 |
|
|
indvdum@0
|
431 |
$dir = $phpbb_root_path . 'store/';
|
|
indvdum@0
|
432 |
$dh = @opendir($dir);
|
|
indvdum@0
|
433 |
|
|
indvdum@0
|
434 |
$backup_files = array();
|
|
indvdum@0
|
435 |
|
|
indvdum@0
|
436 |
if ($dh)
|
|
indvdum@0
|
437 |
{
|
|
indvdum@0
|
438 |
while (($file = readdir($dh)) !== false)
|
|
indvdum@0
|
439 |
{
|
|
indvdum@0
|
440 |
if (preg_match('#^backup_(\d{10,})_[a-z\d]{16}\.(sql(?:\.(?:gz|bz2))?)$#', $file, $matches))
|
|
indvdum@0
|
441 |
{
|
|
indvdum@0
|
442 |
if (in_array($matches[2], $methods))
|
|
indvdum@0
|
443 |
{
|
|
indvdum@0
|
444 |
$backup_files[(int) $matches[1]] = $file;
|
|
indvdum@0
|
445 |
}
|
|
indvdum@0
|
446 |
}
|
|
indvdum@0
|
447 |
}
|
|
indvdum@0
|
448 |
closedir($dh);
|
|
indvdum@0
|
449 |
}
|
|
indvdum@0
|
450 |
|
|
indvdum@0
|
451 |
if (!empty($backup_files))
|
|
indvdum@0
|
452 |
{
|
|
indvdum@0
|
453 |
krsort($backup_files);
|
|
indvdum@0
|
454 |
|
|
indvdum@0
|
455 |
foreach ($backup_files as $name => $file)
|
|
indvdum@0
|
456 |
{
|
|
indvdum@0
|
457 |
$template->assign_block_vars('files', array(
|
|
indvdum@0
|
458 |
'FILE' => $file,
|
|
indvdum@0
|
459 |
'NAME' => $user->format_date($name, 'd-m-Y H:i:s', true),
|
|
indvdum@0
|
460 |
'SUPPORTED' => true,
|
|
indvdum@0
|
461 |
));
|
|
indvdum@0
|
462 |
}
|
|
indvdum@0
|
463 |
}
|
|
indvdum@0
|
464 |
|
|
indvdum@0
|
465 |
$template->assign_vars(array(
|
|
indvdum@0
|
466 |
'U_ACTION' => $this->u_action . '&action=submit'
|
|
indvdum@0
|
467 |
));
|
|
indvdum@0
|
468 |
break;
|
|
indvdum@0
|
469 |
}
|
|
indvdum@0
|
470 |
break;
|
|
indvdum@0
|
471 |
}
|
|
indvdum@0
|
472 |
}
|
|
indvdum@0
|
473 |
}
|
|
indvdum@0
|
474 |
|
|
indvdum@0
|
475 |
/**
|
|
indvdum@0
|
476 |
* @package acp
|
|
indvdum@0
|
477 |
*/
|
|
indvdum@0
|
478 |
class base_extractor
|
|
indvdum@0
|
479 |
{
|
|
indvdum@0
|
480 |
var $fh;
|
|
indvdum@0
|
481 |
var $fp;
|
|
indvdum@0
|
482 |
var $write;
|
|
indvdum@0
|
483 |
var $close;
|
|
indvdum@0
|
484 |
var $store;
|
|
indvdum@0
|
485 |
var $download;
|
|
indvdum@0
|
486 |
var $time;
|
|
indvdum@0
|
487 |
var $format;
|
|
indvdum@0
|
488 |
var $run_comp = false;
|
|
indvdum@0
|
489 |
|
|
indvdum@0
|
490 |
function base_extractor($download = false, $store = false, $format, $filename, $time)
|
|
indvdum@0
|
491 |
{
|
|
indvdum@0
|
492 |
$this->download = $download;
|
|
indvdum@0
|
493 |
$this->store = $store;
|
|
indvdum@0
|
494 |
$this->time = $time;
|
|
indvdum@0
|
495 |
$this->format = $format;
|
|
indvdum@0
|
496 |
|
|
indvdum@0
|
497 |
switch ($format)
|
|
indvdum@0
|
498 |
{
|
|
indvdum@0
|
499 |
case 'text':
|
|
indvdum@0
|
500 |
$ext = '.sql';
|
|
indvdum@0
|
501 |
$open = 'fopen';
|
|
indvdum@0
|
502 |
$this->write = 'fwrite';
|
|
indvdum@0
|
503 |
$this->close = 'fclose';
|
|
indvdum@0
|
504 |
$mimetype = 'text/x-sql';
|
|
indvdum@0
|
505 |
break;
|
|
indvdum@0
|
506 |
case 'bzip2':
|
|
indvdum@0
|
507 |
$ext = '.sql.bz2';
|
|
indvdum@0
|
508 |
$open = 'bzopen';
|
|
indvdum@0
|
509 |
$this->write = 'bzwrite';
|
|
indvdum@0
|
510 |
$this->close = 'bzclose';
|
|
indvdum@0
|
511 |
$mimetype = 'application/x-bzip2';
|
|
indvdum@0
|
512 |
break;
|
|
indvdum@0
|
513 |
case 'gzip':
|
|
indvdum@0
|
514 |
$ext = '.sql.gz';
|
|
indvdum@0
|
515 |
$open = 'gzopen';
|
|
indvdum@0
|
516 |
$this->write = 'gzwrite';
|
|
indvdum@0
|
517 |
$this->close = 'gzclose';
|
|
indvdum@0
|
518 |
$mimetype = 'application/x-gzip';
|
|
indvdum@0
|
519 |
break;
|
|
indvdum@0
|
520 |
}
|
|
indvdum@0
|
521 |
|
|
indvdum@0
|
522 |
if ($download == true)
|
|
indvdum@0
|
523 |
{
|
|
indvdum@0
|
524 |
$name = $filename . $ext;
|
|
indvdum@0
|
525 |
header('Pragma: no-cache');
|
|
indvdum@0
|
526 |
header("Content-Type: $mimetype; name=\"$name\"");
|
|
indvdum@0
|
527 |
header("Content-disposition: attachment; filename=$name");
|
|
indvdum@0
|
528 |
|
|
indvdum@0
|
529 |
switch ($format)
|
|
indvdum@0
|
530 |
{
|
|
indvdum@0
|
531 |
case 'bzip2':
|
|
indvdum@0
|
532 |
ob_start();
|
|
indvdum@0
|
533 |
break;
|
|
indvdum@0
|
534 |
|
|
indvdum@0
|
535 |
case 'gzip':
|
|
indvdum@0
|
536 |
if ((isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) && strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'msie') === false)
|
|
indvdum@0
|
537 |
{
|
|
indvdum@0
|
538 |
ob_start('ob_gzhandler');
|
|
indvdum@0
|
539 |
}
|
|
indvdum@0
|
540 |
else
|
|
indvdum@0
|
541 |
{
|
|
indvdum@0
|
542 |
$this->run_comp = true;
|
|
indvdum@0
|
543 |
}
|
|
indvdum@0
|
544 |
break;
|
|
indvdum@0
|
545 |
}
|
|
indvdum@0
|
546 |
}
|
|
indvdum@0
|
547 |
|
|
indvdum@0
|
548 |
if ($store == true)
|
|
indvdum@0
|
549 |
{
|
|
indvdum@0
|
550 |
global $phpbb_root_path;
|
|
indvdum@0
|
551 |
$file = $phpbb_root_path . 'store/' . $filename . $ext;
|
|
indvdum@0
|
552 |
|
|
indvdum@0
|
553 |
$this->fp = $open($file, 'w');
|
|
indvdum@0
|
554 |
|
|
indvdum@0
|
555 |
if (!$this->fp)
|
|
indvdum@0
|
556 |
{
|
|
indvdum@0
|
557 |
trigger_error('FILE_WRITE_FAIL', E_USER_ERROR);
|
|
indvdum@0
|
558 |
}
|
|
indvdum@0
|
559 |
}
|
|
indvdum@0
|
560 |
}
|
|
indvdum@0
|
561 |
|
|
indvdum@0
|
562 |
function write_end()
|
|
indvdum@0
|
563 |
{
|
|
indvdum@0
|
564 |
static $close;
|
|
indvdum@0
|
565 |
|
|
indvdum@0
|
566 |
if ($this->store)
|
|
indvdum@0
|
567 |
{
|
|
indvdum@0
|
568 |
if ($close === null)
|
|
indvdum@0
|
569 |
{
|
|
indvdum@0
|
570 |
$close = $this->close;
|
|
indvdum@0
|
571 |
}
|
|
indvdum@0
|
572 |
$close($this->fp);
|
|
indvdum@0
|
573 |
}
|
|
indvdum@0
|
574 |
|
|
indvdum@0
|
575 |
// bzip2 must be written all the way at the end
|
|
indvdum@0
|
576 |
if ($this->download && $this->format === 'bzip2')
|
|
indvdum@0
|
577 |
{
|
|
indvdum@0
|
578 |
$c = ob_get_clean();
|
|
indvdum@0
|
579 |
echo bzcompress($c);
|
|
indvdum@0
|
580 |
}
|
|
indvdum@0
|
581 |
}
|
|
indvdum@0
|
582 |
|
|
indvdum@0
|
583 |
function flush($data)
|
|
indvdum@0
|
584 |
{
|
|
indvdum@0
|
585 |
static $write;
|
|
indvdum@0
|
586 |
if ($this->store === true)
|
|
indvdum@0
|
587 |
{
|
|
indvdum@0
|
588 |
if ($write === null)
|
|
indvdum@0
|
589 |
{
|
|
indvdum@0
|
590 |
$write = $this->write;
|
|
indvdum@0
|
591 |
}
|
|
indvdum@0
|
592 |
$write($this->fp, $data);
|
|
indvdum@0
|
593 |
}
|
|
indvdum@0
|
594 |
|
|
indvdum@0
|
595 |
if ($this->download === true)
|
|
indvdum@0
|
596 |
{
|
|
indvdum@0
|
597 |
if ($this->format === 'bzip2' || $this->format === 'text' || ($this->format === 'gzip' && !$this->run_comp))
|
|
indvdum@0
|
598 |
{
|
|
indvdum@0
|
599 |
echo $data;
|
|
indvdum@0
|
600 |
}
|
|
indvdum@0
|
601 |
|
|
indvdum@0
|
602 |
// we can write the gzip data as soon as we get it
|
|
indvdum@0
|
603 |
if ($this->format === 'gzip')
|
|
indvdum@0
|
604 |
{
|
|
indvdum@0
|
605 |
if ($this->run_comp)
|
|
indvdum@0
|
606 |
{
|
|
indvdum@0
|
607 |
echo gzencode($data);
|
|
indvdum@0
|
608 |
}
|
|
indvdum@0
|
609 |
else
|
|
indvdum@0
|
610 |
{
|
|
indvdum@0
|
611 |
ob_flush();
|
|
indvdum@0
|
612 |
flush();
|
|
indvdum@0
|
613 |
}
|
|
indvdum@0
|
614 |
}
|
|
indvdum@0
|
615 |
}
|
|
indvdum@0
|
616 |
}
|
|
indvdum@0
|
617 |
}
|
|
indvdum@0
|
618 |
|
|
indvdum@0
|
619 |
/**
|
|
indvdum@0
|
620 |
* @package acp
|
|
indvdum@0
|
621 |
*/
|
|
indvdum@0
|
622 |
class mysql_extractor extends base_extractor
|
|
indvdum@0
|
623 |
{
|
|
indvdum@0
|
624 |
function write_start($table_prefix)
|
|
indvdum@0
|
625 |
{
|
|
indvdum@0
|
626 |
$sql_data = "#\n";
|
|
indvdum@0
|
627 |
$sql_data .= "# phpBB Backup Script\n";
|
|
indvdum@0
|
628 |
$sql_data .= "# Dump of tables for $table_prefix\n";
|
|
indvdum@0
|
629 |
$sql_data .= "# DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
|
|
indvdum@0
|
630 |
$sql_data .= "#\n";
|
|
indvdum@0
|
631 |
$this->flush($sql_data);
|
|
indvdum@0
|
632 |
}
|
|
indvdum@0
|
633 |
|
|
indvdum@0
|
634 |
function write_table($table_name)
|
|
indvdum@0
|
635 |
{
|
|
indvdum@0
|
636 |
global $db;
|
|
indvdum@0
|
637 |
static $new_extract;
|
|
indvdum@0
|
638 |
|
|
indvdum@0
|
639 |
if ($new_extract === null)
|
|
indvdum@0
|
640 |
{
|
|
indvdum@0
|
641 |
if ($db->sql_layer === 'mysqli' || version_compare($db->sql_server_info(true), '3.23.20', '>='))
|
|
indvdum@0
|
642 |
{
|
|
indvdum@0
|
643 |
$new_extract = true;
|
|
indvdum@0
|
644 |
}
|
|
indvdum@0
|
645 |
else
|
|
indvdum@0
|
646 |
{
|
|
indvdum@0
|
647 |
$new_extract = false;
|
|
indvdum@0
|
648 |
}
|
|
indvdum@0
|
649 |
}
|
|
indvdum@0
|
650 |
|
|
indvdum@0
|
651 |
if ($new_extract)
|
|
indvdum@0
|
652 |
{
|
|
indvdum@0
|
653 |
$this->new_write_table($table_name);
|
|
indvdum@0
|
654 |
}
|
|
indvdum@0
|
655 |
else
|
|
indvdum@0
|
656 |
{
|
|
indvdum@0
|
657 |
$this->old_write_table($table_name);
|
|
indvdum@0
|
658 |
}
|
|
indvdum@0
|
659 |
}
|
|
indvdum@0
|
660 |
|
|
indvdum@0
|
661 |
function write_data($table_name)
|
|
indvdum@0
|
662 |
{
|
|
indvdum@0
|
663 |
global $db;
|
|
indvdum@0
|
664 |
if ($db->sql_layer === 'mysqli')
|
|
indvdum@0
|
665 |
{
|
|
indvdum@0
|
666 |
$this->write_data_mysqli($table_name);
|
|
indvdum@0
|
667 |
}
|
|
indvdum@0
|
668 |
else
|
|
indvdum@0
|
669 |
{
|
|
indvdum@0
|
670 |
$this->write_data_mysql($table_name);
|
|
indvdum@0
|
671 |
}
|
|
indvdum@0
|
672 |
}
|
|
indvdum@0
|
673 |
|
|
indvdum@0
|
674 |
function write_data_mysqli($table_name)
|
|
indvdum@0
|
675 |
{
|
|
indvdum@0
|
676 |
global $db;
|
|
indvdum@0
|
677 |
$sql = "SELECT *
|
|
indvdum@0
|
678 |
FROM $table_name";
|
|
indvdum@0
|
679 |
$result = mysqli_query($db->db_connect_id, $sql, MYSQLI_USE_RESULT);
|
|
indvdum@0
|
680 |
if ($result != false)
|
|
indvdum@0
|
681 |
{
|
|
indvdum@0
|
682 |
$fields_cnt = mysqli_num_fields($result);
|
|
indvdum@0
|
683 |
|
|
indvdum@0
|
684 |
// Get field information
|
|
indvdum@0
|
685 |
$field = mysqli_fetch_fields($result);
|
|
indvdum@0
|
686 |
$field_set = array();
|
|
indvdum@0
|
687 |
|
|
indvdum@0
|
688 |
for ($j = 0; $j < $fields_cnt; $j++)
|
|
indvdum@0
|
689 |
{
|
|
indvdum@0
|
690 |
$field_set[] = $field[$j]->name;
|
|
indvdum@0
|
691 |
}
|
|
indvdum@0
|
692 |
|
|
indvdum@0
|
693 |
$search = array("\\", "'", "\x00", "\x0a", "\x0d", "\x1a", '"');
|
|
indvdum@0
|
694 |
$replace = array("\\\\", "\\'", '\0', '\n', '\r', '\Z', '\\"');
|
|
indvdum@0
|
695 |
$fields = implode(', ', $field_set);
|
|
indvdum@0
|
696 |
$sql_data = 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES ';
|
|
indvdum@0
|
697 |
$first_set = true;
|
|
indvdum@0
|
698 |
$query_len = 0;
|
|
indvdum@0
|
699 |
$max_len = get_usable_memory();
|
|
indvdum@0
|
700 |
|
|
indvdum@0
|
701 |
while ($row = mysqli_fetch_row($result))
|
|
indvdum@0
|
702 |
{
|
|
indvdum@0
|
703 |
$values = array();
|
|
indvdum@0
|
704 |
if ($first_set)
|
|
indvdum@0
|
705 |
{
|
|
indvdum@0
|
706 |
$query = $sql_data . '(';
|
|
indvdum@0
|
707 |
}
|
|
indvdum@0
|
708 |
else
|
|
indvdum@0
|
709 |
{
|
|
indvdum@0
|
710 |
$query .= ',(';
|
|
indvdum@0
|
711 |
}
|
|
indvdum@0
|
712 |
|
|
indvdum@0
|
713 |
for ($j = 0; $j < $fields_cnt; $j++)
|
|
indvdum@0
|
714 |
{
|
|
indvdum@0
|
715 |
if (!isset($row[$j]) || is_null($row[$j]))
|
|
indvdum@0
|
716 |
{
|
|
indvdum@0
|
717 |
$values[$j] = 'NULL';
|
|
indvdum@0
|
718 |
}
|
|
indvdum@0
|
719 |
else if (($field[$j]->flags & 32768) && !($field[$j]->flags & 1024))
|
|
indvdum@0
|
720 |
{
|
|
indvdum@0
|
721 |
$values[$j] = $row[$j];
|
|
indvdum@0
|
722 |
}
|
|
indvdum@0
|
723 |
else
|
|
indvdum@0
|
724 |
{
|
|
indvdum@0
|
725 |
$values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'";
|
|
indvdum@0
|
726 |
}
|
|
indvdum@0
|
727 |
}
|
|
indvdum@0
|
728 |
$query .= implode(', ', $values) . ')';
|
|
indvdum@0
|
729 |
|
|
indvdum@0
|
730 |
$query_len += strlen($query);
|
|
indvdum@0
|
731 |
if ($query_len > $max_len)
|
|
indvdum@0
|
732 |
{
|
|
indvdum@0
|
733 |
$this->flush($query . ";\n\n");
|
|
indvdum@0
|
734 |
$query = '';
|
|
indvdum@0
|
735 |
$query_len = 0;
|
|
indvdum@0
|
736 |
$first_set = true;
|
|
indvdum@0
|
737 |
}
|
|
indvdum@0
|
738 |
else
|
|
indvdum@0
|
739 |
{
|
|
indvdum@0
|
740 |
$first_set = false;
|
|
indvdum@0
|
741 |
}
|
|
indvdum@0
|
742 |
}
|
|
indvdum@0
|
743 |
mysqli_free_result($result);
|
|
indvdum@0
|
744 |
|
|
indvdum@0
|
745 |
// check to make sure we have nothing left to flush
|
|
indvdum@0
|
746 |
if (!$first_set && $query)
|
|
indvdum@0
|
747 |
{
|
|
indvdum@0
|
748 |
$this->flush($query . ";\n\n");
|
|
indvdum@0
|
749 |
}
|
|
indvdum@0
|
750 |
}
|
|
indvdum@0
|
751 |
}
|
|
indvdum@0
|
752 |
|
|
indvdum@0
|
753 |
function write_data_mysql($table_name)
|
|
indvdum@0
|
754 |
{
|
|
indvdum@0
|
755 |
global $db;
|
|
indvdum@0
|
756 |
$sql = "SELECT *
|
|
indvdum@0
|
757 |
FROM $table_name";
|
|
indvdum@0
|
758 |
$result = mysql_unbuffered_query($sql, $db->db_connect_id);
|
|
indvdum@0
|
759 |
|
|
indvdum@0
|
760 |
if ($result != false)
|
|
indvdum@0
|
761 |
{
|
|
indvdum@0
|
762 |
$fields_cnt = mysql_num_fields($result);
|
|
indvdum@0
|
763 |
|
|
indvdum@0
|
764 |
// Get field information
|
|
indvdum@0
|
765 |
$field = array();
|
|
indvdum@0
|
766 |
for ($i = 0; $i < $fields_cnt; $i++)
|
|
indvdum@0
|
767 |
{
|
|
indvdum@0
|
768 |
$field[] = mysql_fetch_field($result, $i);
|
|
indvdum@0
|
769 |
}
|
|
indvdum@0
|
770 |
$field_set = array();
|
|
indvdum@0
|
771 |
|
|
indvdum@0
|
772 |
for ($j = 0; $j < $fields_cnt; $j++)
|
|
indvdum@0
|
773 |
{
|
|
indvdum@0
|
774 |
$field_set[] = $field[$j]->name;
|
|
indvdum@0
|
775 |
}
|
|
indvdum@0
|
776 |
|
|
indvdum@0
|
777 |
$search = array("\\", "'", "\x00", "\x0a", "\x0d", "\x1a", '"');
|
|
indvdum@0
|
778 |
$replace = array("\\\\", "\\'", '\0', '\n', '\r', '\Z', '\\"');
|
|
indvdum@0
|
779 |
$fields = implode(', ', $field_set);
|
|
indvdum@0
|
780 |
$sql_data = 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES ';
|
|
indvdum@0
|
781 |
$first_set = true;
|
|
indvdum@0
|
782 |
$query_len = 0;
|
|
indvdum@0
|
783 |
$max_len = get_usable_memory();
|
|
indvdum@0
|
784 |
|
|
indvdum@0
|
785 |
while ($row = mysql_fetch_row($result))
|
|
indvdum@0
|
786 |
{
|
|
indvdum@0
|
787 |
$values = array();
|
|
indvdum@0
|
788 |
if ($first_set)
|
|
indvdum@0
|
789 |
{
|
|
indvdum@0
|
790 |
$query = $sql_data . '(';
|
|
indvdum@0
|
791 |
}
|
|
indvdum@0
|
792 |
else
|
|
indvdum@0
|
793 |
{
|
|
indvdum@0
|
794 |
$query .= ',(';
|
|
indvdum@0
|
795 |
}
|
|
indvdum@0
|
796 |
|
|
indvdum@0
|
797 |
for ($j = 0; $j < $fields_cnt; $j++)
|
|
indvdum@0
|
798 |
{
|
|
indvdum@0
|
799 |
if (!isset($row[$j]) || is_null($row[$j]))
|
|
indvdum@0
|
800 |
{
|
|
indvdum@0
|
801 |
$values[$j] = 'NULL';
|
|
indvdum@0
|
802 |
}
|
|
indvdum@0
|
803 |
else if ($field[$j]->numeric && ($field[$j]->type !== 'timestamp'))
|
|
indvdum@0
|
804 |
{
|
|
indvdum@0
|
805 |
$values[$j] = $row[$j];
|
|
indvdum@0
|
806 |
}
|
|
indvdum@0
|
807 |
else
|
|
indvdum@0
|
808 |
{
|
|
indvdum@0
|
809 |
$values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'";
|
|
indvdum@0
|
810 |
}
|
|
indvdum@0
|
811 |
}
|
|
indvdum@0
|
812 |
$query .= implode(', ', $values) . ')';
|
|
indvdum@0
|
813 |
|
|
indvdum@0
|
814 |
$query_len += strlen($query);
|
|
indvdum@0
|
815 |
if ($query_len > $max_len)
|
|
indvdum@0
|
816 |
{
|
|
indvdum@0
|
817 |
$this->flush($query . ";\n\n");
|
|
indvdum@0
|
818 |
$query = '';
|
|
indvdum@0
|
819 |
$query_len = 0;
|
|
indvdum@0
|
820 |
$first_set = true;
|
|
indvdum@0
|
821 |
}
|
|
indvdum@0
|
822 |
else
|
|
indvdum@0
|
823 |
{
|
|
indvdum@0
|
824 |
$first_set = false;
|
|
indvdum@0
|
825 |
}
|
|
indvdum@0
|
826 |
}
|
|
indvdum@0
|
827 |
mysql_free_result($result);
|
|
indvdum@0
|
828 |
|
|
indvdum@0
|
829 |
// check to make sure we have nothing left to flush
|
|
indvdum@0
|
830 |
if (!$first_set && $query)
|
|
indvdum@0
|
831 |
{
|
|
indvdum@0
|
832 |
$this->flush($query . ";\n\n");
|
|
indvdum@0
|
833 |
}
|
|
indvdum@0
|
834 |
}
|
|
indvdum@0
|
835 |
}
|
|
indvdum@0
|
836 |
|
|
indvdum@0
|
837 |
function new_write_table($table_name)
|
|
indvdum@0
|
838 |
{
|
|
indvdum@0
|
839 |
global $db;
|
|
indvdum@0
|
840 |
|
|
indvdum@0
|
841 |
$sql = 'SHOW CREATE TABLE ' . $table_name;
|
|
indvdum@0
|
842 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
843 |
$row = $db->sql_fetchrow($result);
|
|
indvdum@0
|
844 |
|
|
indvdum@0
|
845 |
$sql_data = '# Table: ' . $table_name . "\n";
|
|
indvdum@0
|
846 |
$sql_data .= "DROP TABLE IF EXISTS $table_name;\n";
|
|
indvdum@0
|
847 |
$this->flush($sql_data . $row['Create Table'] . ";\n\n");
|
|
indvdum@0
|
848 |
|
|
indvdum@0
|
849 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
850 |
}
|
|
indvdum@0
|
851 |
|
|
indvdum@0
|
852 |
function old_write_table($table_name)
|
|
indvdum@0
|
853 |
{
|
|
indvdum@0
|
854 |
global $db;
|
|
indvdum@0
|
855 |
|
|
indvdum@0
|
856 |
$sql_data = '# Table: ' . $table_name . "\n";
|
|
indvdum@0
|
857 |
$sql_data .= "DROP TABLE IF EXISTS $table_name;\n";
|
|
indvdum@0
|
858 |
$sql_data .= "CREATE TABLE $table_name(\n";
|
|
indvdum@0
|
859 |
$rows = array();
|
|
indvdum@0
|
860 |
|
|
indvdum@0
|
861 |
$sql = "SHOW FIELDS
|
|
indvdum@0
|
862 |
FROM $table_name";
|
|
indvdum@0
|
863 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
864 |
|
|
indvdum@0
|
865 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
866 |
{
|
|
indvdum@0
|
867 |
$line = ' ' . $row['Field'] . ' ' . $row['Type'];
|
|
indvdum@0
|
868 |
|
|
indvdum@0
|
869 |
if (!is_null($row['Default']))
|
|
indvdum@0
|
870 |
{
|
|
indvdum@0
|
871 |
$line .= " DEFAULT '{$row['Default']}'";
|
|
indvdum@0
|
872 |
}
|
|
indvdum@0
|
873 |
|
|
indvdum@0
|
874 |
if ($row['Null'] != 'YES')
|
|
indvdum@0
|
875 |
{
|
|
indvdum@0
|
876 |
$line .= ' NOT NULL';
|
|
indvdum@0
|
877 |
}
|
|
indvdum@0
|
878 |
|
|
indvdum@0
|
879 |
if ($row['Extra'] != '')
|
|
indvdum@0
|
880 |
{
|
|
indvdum@0
|
881 |
$line .= ' ' . $row['Extra'];
|
|
indvdum@0
|
882 |
}
|
|
indvdum@0
|
883 |
|
|
indvdum@0
|
884 |
$rows[] = $line;
|
|
indvdum@0
|
885 |
}
|
|
indvdum@0
|
886 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
887 |
|
|
indvdum@0
|
888 |
$sql = "SHOW KEYS
|
|
indvdum@0
|
889 |
FROM $table_name";
|
|
indvdum@0
|
890 |
|
|
indvdum@0
|
891 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
892 |
|
|
indvdum@0
|
893 |
$index = array();
|
|
indvdum@0
|
894 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
895 |
{
|
|
indvdum@0
|
896 |
$kname = $row['Key_name'];
|
|
indvdum@0
|
897 |
|
|
indvdum@0
|
898 |
if ($kname != 'PRIMARY')
|
|
indvdum@0
|
899 |
{
|
|
indvdum@0
|
900 |
if ($row['Non_unique'] == 0)
|
|
indvdum@0
|
901 |
{
|
|
indvdum@0
|
902 |
$kname = "UNIQUE|$kname";
|
|
indvdum@0
|
903 |
}
|
|
indvdum@0
|
904 |
}
|
|
indvdum@0
|
905 |
|
|
indvdum@0
|
906 |
if ($row['Sub_part'])
|
|
indvdum@0
|
907 |
{
|
|
indvdum@0
|
908 |
$row['Column_name'] .= '(' . $row['Sub_part'] . ')';
|
|
indvdum@0
|
909 |
}
|
|
indvdum@0
|
910 |
$index[$kname][] = $row['Column_name'];
|
|
indvdum@0
|
911 |
}
|
|
indvdum@0
|
912 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
913 |
|
|
indvdum@0
|
914 |
foreach ($index as $key => $columns)
|
|
indvdum@0
|
915 |
{
|
|
indvdum@0
|
916 |
$line = ' ';
|
|
indvdum@0
|
917 |
|
|
indvdum@0
|
918 |
if ($key == 'PRIMARY')
|
|
indvdum@0
|
919 |
{
|
|
indvdum@0
|
920 |
$line .= 'PRIMARY KEY (' . implode(', ', $columns) . ')';
|
|
indvdum@0
|
921 |
}
|
|
indvdum@0
|
922 |
else if (strpos($key, 'UNIQUE') === 0)
|
|
indvdum@0
|
923 |
{
|
|
indvdum@0
|
924 |
$line .= 'UNIQUE ' . substr($key, 7) . ' (' . implode(', ', $columns) . ')';
|
|
indvdum@0
|
925 |
}
|
|
indvdum@0
|
926 |
else if (strpos($key, 'FULLTEXT') === 0)
|
|
indvdum@0
|
927 |
{
|
|
indvdum@0
|
928 |
$line .= 'FULLTEXT ' . substr($key, 9) . ' (' . implode(', ', $columns) . ')';
|
|
indvdum@0
|
929 |
}
|
|
indvdum@0
|
930 |
else
|
|
indvdum@0
|
931 |
{
|
|
indvdum@0
|
932 |
$line .= "KEY $key (" . implode(', ', $columns) . ')';
|
|
indvdum@0
|
933 |
}
|
|
indvdum@0
|
934 |
|
|
indvdum@0
|
935 |
$rows[] = $line;
|
|
indvdum@0
|
936 |
}
|
|
indvdum@0
|
937 |
|
|
indvdum@0
|
938 |
$sql_data .= implode(",\n", $rows);
|
|
indvdum@0
|
939 |
$sql_data .= "\n);\n\n";
|
|
indvdum@0
|
940 |
|
|
indvdum@0
|
941 |
$this->flush($sql_data);
|
|
indvdum@0
|
942 |
}
|
|
indvdum@0
|
943 |
}
|
|
indvdum@0
|
944 |
|
|
indvdum@0
|
945 |
/**
|
|
indvdum@0
|
946 |
* @package acp
|
|
indvdum@0
|
947 |
*/
|
|
indvdum@0
|
948 |
class sqlite_extractor extends base_extractor
|
|
indvdum@0
|
949 |
{
|
|
indvdum@0
|
950 |
function write_start($prefix)
|
|
indvdum@0
|
951 |
{
|
|
indvdum@0
|
952 |
$sql_data = "--\n";
|
|
indvdum@0
|
953 |
$sql_data .= "-- phpBB Backup Script\n";
|
|
indvdum@0
|
954 |
$sql_data .= "-- Dump of tables for $prefix\n";
|
|
indvdum@0
|
955 |
$sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
|
|
indvdum@0
|
956 |
$sql_data .= "--\n";
|
|
indvdum@0
|
957 |
$sql_data .= "BEGIN TRANSACTION;\n";
|
|
indvdum@0
|
958 |
$this->flush($sql_data);
|
|
indvdum@0
|
959 |
}
|
|
indvdum@0
|
960 |
|
|
indvdum@0
|
961 |
function write_table($table_name)
|
|
indvdum@0
|
962 |
{
|
|
indvdum@0
|
963 |
global $db;
|
|
indvdum@0
|
964 |
$sql_data = '-- Table: ' . $table_name . "\n";
|
|
indvdum@0
|
965 |
$sql_data .= "DROP TABLE $table_name;\n";
|
|
indvdum@0
|
966 |
|
|
indvdum@0
|
967 |
$sql = "SELECT sql
|
|
indvdum@0
|
968 |
FROM sqlite_master
|
|
indvdum@0
|
969 |
WHERE type = 'table'
|
|
indvdum@0
|
970 |
AND name = '" . $db->sql_escape($table_name) . "'
|
|
indvdum@0
|
971 |
ORDER BY type DESC, name;";
|
|
indvdum@0
|
972 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
973 |
$row = $db->sql_fetchrow($result);
|
|
indvdum@0
|
974 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
975 |
|
|
indvdum@0
|
976 |
// Create Table
|
|
indvdum@0
|
977 |
$sql_data .= $row['sql'] . ";\n";
|
|
indvdum@0
|
978 |
|
|
indvdum@0
|
979 |
$result = $db->sql_query("PRAGMA index_list('" . $db->sql_escape($table_name) . "');");
|
|
indvdum@0
|
980 |
|
|
indvdum@0
|
981 |
$ar = array();
|
|
indvdum@0
|
982 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
983 |
{
|
|
indvdum@0
|
984 |
$ar[] = $row;
|
|
indvdum@0
|
985 |
}
|
|
indvdum@0
|
986 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
987 |
|
|
indvdum@0
|
988 |
foreach ($ar as $value)
|
|
indvdum@0
|
989 |
{
|
|
indvdum@0
|
990 |
if (strpos($value['name'], 'autoindex') !== false)
|
|
indvdum@0
|
991 |
{
|
|
indvdum@0
|
992 |
continue;
|
|
indvdum@0
|
993 |
}
|
|
indvdum@0
|
994 |
|
|
indvdum@0
|
995 |
$result = $db->sql_query("PRAGMA index_info('" . $db->sql_escape($value['name']) . "');");
|
|
indvdum@0
|
996 |
|
|
indvdum@0
|
997 |
$fields = array();
|
|
indvdum@0
|
998 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
999 |
{
|
|
indvdum@0
|
1000 |
$fields[] = $row['name'];
|
|
indvdum@0
|
1001 |
}
|
|
indvdum@0
|
1002 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1003 |
|
|
indvdum@0
|
1004 |
$sql_data .= 'CREATE ' . ($value['unique'] ? 'UNIQUE ' : '') . 'INDEX ' . $value['name'] . ' on ' . $table_name . ' (' . implode(', ', $fields) . ");\n";
|
|
indvdum@0
|
1005 |
}
|
|
indvdum@0
|
1006 |
|
|
indvdum@0
|
1007 |
$this->flush($sql_data . "\n");
|
|
indvdum@0
|
1008 |
}
|
|
indvdum@0
|
1009 |
|
|
indvdum@0
|
1010 |
function write_data($table_name)
|
|
indvdum@0
|
1011 |
{
|
|
indvdum@0
|
1012 |
global $db;
|
|
indvdum@0
|
1013 |
static $proper;
|
|
indvdum@0
|
1014 |
|
|
indvdum@0
|
1015 |
if (is_null($proper))
|
|
indvdum@0
|
1016 |
{
|
|
indvdum@0
|
1017 |
$proper = version_compare(PHP_VERSION, '5.1.3', '>=');
|
|
indvdum@0
|
1018 |
}
|
|
indvdum@0
|
1019 |
|
|
indvdum@0
|
1020 |
if ($proper)
|
|
indvdum@0
|
1021 |
{
|
|
indvdum@0
|
1022 |
$col_types = sqlite_fetch_column_types($db->db_connect_id, $table_name);
|
|
indvdum@0
|
1023 |
}
|
|
indvdum@0
|
1024 |
else
|
|
indvdum@0
|
1025 |
{
|
|
indvdum@0
|
1026 |
$sql = "SELECT sql
|
|
indvdum@0
|
1027 |
FROM sqlite_master
|
|
indvdum@0
|
1028 |
WHERE type = 'table'
|
|
indvdum@0
|
1029 |
AND name = '" . $table_name . "'";
|
|
indvdum@0
|
1030 |
$table_data = sqlite_single_query($db->db_connect_id, $sql);
|
|
indvdum@0
|
1031 |
$table_data = preg_replace('#CREATE\s+TABLE\s+"?' . $table_name . '"?#i', '', $table_data);
|
|
indvdum@0
|
1032 |
$table_data = trim($table_data);
|
|
indvdum@0
|
1033 |
|
|
indvdum@0
|
1034 |
preg_match('#\((.*)\)#s', $table_data, $matches);
|
|
indvdum@0
|
1035 |
|
|
indvdum@0
|
1036 |
$table_cols = explode(',', trim($matches[1]));
|
|
indvdum@0
|
1037 |
foreach ($table_cols as $declaration)
|
|
indvdum@0
|
1038 |
{
|
|
indvdum@0
|
1039 |
$entities = preg_split('#\s+#', trim($declaration));
|
|
indvdum@0
|
1040 |
$column_name = preg_replace('/"?([^"]+)"?/', '\1', $entities[0]);
|
|
indvdum@0
|
1041 |
|
|
indvdum@0
|
1042 |
// Hit a primary key, those are not what we need :D
|
|
indvdum@0
|
1043 |
if (empty($entities[1]) || (strtolower($entities[0]) === 'primary' && strtolower($entities[1]) === 'key'))
|
|
indvdum@0
|
1044 |
{
|
|
indvdum@0
|
1045 |
continue;
|
|
indvdum@0
|
1046 |
}
|
|
indvdum@0
|
1047 |
$col_types[$column_name] = $entities[1];
|
|
indvdum@0
|
1048 |
}
|
|
indvdum@0
|
1049 |
}
|
|
indvdum@0
|
1050 |
|
|
indvdum@0
|
1051 |
$sql = "SELECT *
|
|
indvdum@0
|
1052 |
FROM $table_name";
|
|
indvdum@0
|
1053 |
$result = sqlite_unbuffered_query($db->db_connect_id, $sql);
|
|
indvdum@0
|
1054 |
$rows = sqlite_fetch_all($result, SQLITE_ASSOC);
|
|
indvdum@0
|
1055 |
$sql_insert = 'INSERT INTO ' . $table_name . ' (' . implode(', ', array_keys($col_types)) . ') VALUES (';
|
|
indvdum@0
|
1056 |
foreach ($rows as $row)
|
|
indvdum@0
|
1057 |
{
|
|
indvdum@0
|
1058 |
foreach ($row as $column_name => $column_data)
|
|
indvdum@0
|
1059 |
{
|
|
indvdum@0
|
1060 |
if (is_null($column_data))
|
|
indvdum@0
|
1061 |
{
|
|
indvdum@0
|
1062 |
$row[$column_name] = 'NULL';
|
|
indvdum@0
|
1063 |
}
|
|
indvdum@0
|
1064 |
else if ($column_data == '')
|
|
indvdum@0
|
1065 |
{
|
|
indvdum@0
|
1066 |
$row[$column_name] = "''";
|
|
indvdum@0
|
1067 |
}
|
|
indvdum@0
|
1068 |
else if (strpos($col_types[$column_name], 'text') !== false || strpos($col_types[$column_name], 'char') !== false || strpos($col_types[$column_name], 'blob') !== false)
|
|
indvdum@0
|
1069 |
{
|
|
indvdum@0
|
1070 |
$row[$column_name] = sanitize_data_generic(str_replace("'", "''", $column_data));
|
|
indvdum@0
|
1071 |
}
|
|
indvdum@0
|
1072 |
}
|
|
indvdum@0
|
1073 |
$this->flush($sql_insert . implode(', ', $row) . ");\n");
|
|
indvdum@0
|
1074 |
}
|
|
indvdum@0
|
1075 |
}
|
|
indvdum@0
|
1076 |
|
|
indvdum@0
|
1077 |
function write_end()
|
|
indvdum@0
|
1078 |
{
|
|
indvdum@0
|
1079 |
$this->flush("COMMIT;\n");
|
|
indvdum@0
|
1080 |
parent::write_end();
|
|
indvdum@0
|
1081 |
}
|
|
indvdum@0
|
1082 |
}
|
|
indvdum@0
|
1083 |
|
|
indvdum@0
|
1084 |
/**
|
|
indvdum@0
|
1085 |
* @package acp
|
|
indvdum@0
|
1086 |
*/
|
|
indvdum@0
|
1087 |
class postgres_extractor extends base_extractor
|
|
indvdum@0
|
1088 |
{
|
|
indvdum@0
|
1089 |
function write_start($prefix)
|
|
indvdum@0
|
1090 |
{
|
|
indvdum@0
|
1091 |
$sql_data = "--\n";
|
|
indvdum@0
|
1092 |
$sql_data .= "-- phpBB Backup Script\n";
|
|
indvdum@0
|
1093 |
$sql_data .= "-- Dump of tables for $prefix\n";
|
|
indvdum@0
|
1094 |
$sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
|
|
indvdum@0
|
1095 |
$sql_data .= "--\n";
|
|
indvdum@0
|
1096 |
$sql_data .= "BEGIN TRANSACTION;\n";
|
|
indvdum@0
|
1097 |
$this->flush($sql_data);
|
|
indvdum@0
|
1098 |
}
|
|
indvdum@0
|
1099 |
|
|
indvdum@0
|
1100 |
function write_table($table_name)
|
|
indvdum@0
|
1101 |
{
|
|
indvdum@0
|
1102 |
global $db;
|
|
indvdum@0
|
1103 |
static $domains_created = array();
|
|
indvdum@0
|
1104 |
|
|
indvdum@0
|
1105 |
$sql = "SELECT a.domain_name, a.data_type, a.character_maximum_length, a.domain_default
|
|
indvdum@0
|
1106 |
FROM INFORMATION_SCHEMA.domains a, INFORMATION_SCHEMA.column_domain_usage b
|
|
indvdum@0
|
1107 |
WHERE a.domain_name = b.domain_name
|
|
indvdum@0
|
1108 |
AND b.table_name = '{$table_name}'";
|
|
indvdum@0
|
1109 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
1110 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1111 |
{
|
|
indvdum@0
|
1112 |
if (empty($domains_created[$row['domain_name']]))
|
|
indvdum@0
|
1113 |
{
|
|
indvdum@0
|
1114 |
$domains_created[$row['domain_name']] = true;
|
|
indvdum@0
|
1115 |
//$sql_data = "DROP DOMAIN {$row['domain_name']};\n";
|
|
indvdum@0
|
1116 |
$sql_data = "CREATE DOMAIN {$row['domain_name']} as {$row['data_type']}";
|
|
indvdum@0
|
1117 |
if (!empty($row['character_maximum_length']))
|
|
indvdum@0
|
1118 |
{
|
|
indvdum@0
|
1119 |
$sql_data .= '(' . $row['character_maximum_length'] . ')';
|
|
indvdum@0
|
1120 |
}
|
|
indvdum@0
|
1121 |
$sql_data .= ' NOT NULL';
|
|
indvdum@0
|
1122 |
if (!empty($row['domain_default']))
|
|
indvdum@0
|
1123 |
{
|
|
indvdum@0
|
1124 |
$sql_data .= ' DEFAULT ' . $row['domain_default'];
|
|
indvdum@0
|
1125 |
}
|
|
indvdum@0
|
1126 |
$this->flush($sql_data . ";\n");
|
|
indvdum@0
|
1127 |
}
|
|
indvdum@0
|
1128 |
}
|
|
indvdum@0
|
1129 |
|
|
indvdum@0
|
1130 |
$sql_data = '-- Table: ' . $table_name . "\n";
|
|
indvdum@0
|
1131 |
$sql_data .= "DROP TABLE $table_name;\n";
|
|
indvdum@0
|
1132 |
// PGSQL does not "tightly" bind sequences and tables, we must guess...
|
|
indvdum@0
|
1133 |
$sql = "SELECT relname
|
|
indvdum@0
|
1134 |
FROM pg_class
|
|
indvdum@0
|
1135 |
WHERE relkind = 'S'
|
|
indvdum@0
|
1136 |
AND relname = '{$table_name}_seq'";
|
|
indvdum@0
|
1137 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
1138 |
// We don't even care about storing the results. We already know the answer if we get rows back.
|
|
indvdum@0
|
1139 |
if ($db->sql_fetchrow($result))
|
|
indvdum@0
|
1140 |
{
|
|
indvdum@0
|
1141 |
$sql_data .= "DROP SEQUENCE {$table_name}_seq;\n";
|
|
indvdum@0
|
1142 |
$sql_data .= "CREATE SEQUENCE {$table_name}_seq;\n";
|
|
indvdum@0
|
1143 |
}
|
|
indvdum@0
|
1144 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1145 |
|
|
indvdum@0
|
1146 |
$field_query = "SELECT a.attnum, a.attname as field, t.typname as type, a.attlen as length, a.atttypmod as lengthvar, a.attnotnull as notnull
|
|
indvdum@0
|
1147 |
FROM pg_class c, pg_attribute a, pg_type t
|
|
indvdum@0
|
1148 |
WHERE c.relname = '" . $db->sql_escape($table_name) . "'
|
|
indvdum@0
|
1149 |
AND a.attnum > 0
|
|
indvdum@0
|
1150 |
AND a.attrelid = c.oid
|
|
indvdum@0
|
1151 |
AND a.atttypid = t.oid
|
|
indvdum@0
|
1152 |
ORDER BY a.attnum";
|
|
indvdum@0
|
1153 |
$result = $db->sql_query($field_query);
|
|
indvdum@0
|
1154 |
|
|
indvdum@0
|
1155 |
$sql_data .= "CREATE TABLE $table_name(\n";
|
|
indvdum@0
|
1156 |
$lines = array();
|
|
indvdum@0
|
1157 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1158 |
{
|
|
indvdum@0
|
1159 |
// Get the data from the table
|
|
indvdum@0
|
1160 |
$sql_get_default = "SELECT pg_get_expr(d.adbin, d.adrelid) as rowdefault
|
|
indvdum@0
|
1161 |
FROM pg_attrdef d, pg_class c
|
|
indvdum@0
|
1162 |
WHERE (c.relname = '" . $db->sql_escape($table_name) . "')
|
|
indvdum@0
|
1163 |
AND (c.oid = d.adrelid)
|
|
indvdum@0
|
1164 |
AND d.adnum = " . $row['attnum'];
|
|
indvdum@0
|
1165 |
$def_res = $db->sql_query($sql_get_default);
|
|
indvdum@0
|
1166 |
$def_row = $db->sql_fetchrow($def_res);
|
|
indvdum@0
|
1167 |
$db->sql_freeresult($def_res);
|
|
indvdum@0
|
1168 |
|
|
indvdum@0
|
1169 |
if (empty($def_row))
|
|
indvdum@0
|
1170 |
{
|
|
indvdum@0
|
1171 |
unset($row['rowdefault']);
|
|
indvdum@0
|
1172 |
}
|
|
indvdum@0
|
1173 |
else
|
|
indvdum@0
|
1174 |
{
|
|
indvdum@0
|
1175 |
$row['rowdefault'] = $def_row['rowdefault'];
|
|
indvdum@0
|
1176 |
}
|
|
indvdum@0
|
1177 |
|
|
indvdum@0
|
1178 |
if ($row['type'] == 'bpchar')
|
|
indvdum@0
|
1179 |
{
|
|
indvdum@0
|
1180 |
// Internally stored as bpchar, but isn't accepted in a CREATE TABLE statement.
|
|
indvdum@0
|
1181 |
$row['type'] = 'char';
|
|
indvdum@0
|
1182 |
}
|
|
indvdum@0
|
1183 |
|
|
indvdum@0
|
1184 |
$line = ' ' . $row['field'] . ' ' . $row['type'];
|
|
indvdum@0
|
1185 |
|
|
indvdum@0
|
1186 |
if (strpos($row['type'], 'char') !== false)
|
|
indvdum@0
|
1187 |
{
|
|
indvdum@0
|
1188 |
if ($row['lengthvar'] > 0)
|
|
indvdum@0
|
1189 |
{
|
|
indvdum@0
|
1190 |
$line .= '(' . ($row['lengthvar'] - 4) . ')';
|
|
indvdum@0
|
1191 |
}
|
|
indvdum@0
|
1192 |
}
|
|
indvdum@0
|
1193 |
|
|
indvdum@0
|
1194 |
if (strpos($row['type'], 'numeric') !== false)
|
|
indvdum@0
|
1195 |
{
|
|
indvdum@0
|
1196 |
$line .= '(';
|
|
indvdum@0
|
1197 |
$line .= sprintf("%s,%s", (($row['lengthvar'] >> 16) & 0xffff), (($row['lengthvar'] - 4) & 0xffff));
|
|
indvdum@0
|
1198 |
$line .= ')';
|
|
indvdum@0
|
1199 |
}
|
|
indvdum@0
|
1200 |
|
|
indvdum@0
|
1201 |
if (isset($row['rowdefault']))
|
|
indvdum@0
|
1202 |
{
|
|
indvdum@0
|
1203 |
$line .= ' DEFAULT ' . $row['rowdefault'];
|
|
indvdum@0
|
1204 |
}
|
|
indvdum@0
|
1205 |
|
|
indvdum@0
|
1206 |
if ($row['notnull'] == 't')
|
|
indvdum@0
|
1207 |
{
|
|
indvdum@0
|
1208 |
$line .= ' NOT NULL';
|
|
indvdum@0
|
1209 |
}
|
|
indvdum@0
|
1210 |
|
|
indvdum@0
|
1211 |
$lines[] = $line;
|
|
indvdum@0
|
1212 |
}
|
|
indvdum@0
|
1213 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1214 |
|
|
indvdum@0
|
1215 |
|
|
indvdum@0
|
1216 |
// Get the listing of primary keys.
|
|
indvdum@0
|
1217 |
$sql_pri_keys = "SELECT ic.relname as index_name, bc.relname as tab_name, ta.attname as column_name, i.indisunique as unique_key, i.indisprimary as primary_key
|
|
indvdum@0
|
1218 |
FROM pg_class bc, pg_class ic, pg_index i, pg_attribute ta, pg_attribute ia
|
|
indvdum@0
|
1219 |
WHERE (bc.oid = i.indrelid)
|
|
indvdum@0
|
1220 |
AND (ic.oid = i.indexrelid)
|
|
indvdum@0
|
1221 |
AND (ia.attrelid = i.indexrelid)
|
|
indvdum@0
|
1222 |
AND (ta.attrelid = bc.oid)
|
|
indvdum@0
|
1223 |
AND (bc.relname = '" . $db->sql_escape($table_name) . "')
|
|
indvdum@0
|
1224 |
AND (ta.attrelid = i.indrelid)
|
|
indvdum@0
|
1225 |
AND (ta.attnum = i.indkey[ia.attnum-1])
|
|
indvdum@0
|
1226 |
ORDER BY index_name, tab_name, column_name";
|
|
indvdum@0
|
1227 |
|
|
indvdum@0
|
1228 |
$result = $db->sql_query($sql_pri_keys);
|
|
indvdum@0
|
1229 |
|
|
indvdum@0
|
1230 |
$index_create = $index_rows = $primary_key = array();
|
|
indvdum@0
|
1231 |
|
|
indvdum@0
|
1232 |
// We do this in two steps. It makes placing the comma easier
|
|
indvdum@0
|
1233 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1234 |
{
|
|
indvdum@0
|
1235 |
if ($row['primary_key'] == 't')
|
|
indvdum@0
|
1236 |
{
|
|
indvdum@0
|
1237 |
$primary_key[] = $row['column_name'];
|
|
indvdum@0
|
1238 |
$primary_key_name = $row['index_name'];
|
|
indvdum@0
|
1239 |
}
|
|
indvdum@0
|
1240 |
else
|
|
indvdum@0
|
1241 |
{
|
|
indvdum@0
|
1242 |
// We have to store this all this info because it is possible to have a multi-column key...
|
|
indvdum@0
|
1243 |
// we can loop through it again and build the statement
|
|
indvdum@0
|
1244 |
$index_rows[$row['index_name']]['table'] = $table_name;
|
|
indvdum@0
|
1245 |
$index_rows[$row['index_name']]['unique'] = ($row['unique_key'] == 't') ? true : false;
|
|
indvdum@0
|
1246 |
$index_rows[$row['index_name']]['column_names'][] = $row['column_name'];
|
|
indvdum@0
|
1247 |
}
|
|
indvdum@0
|
1248 |
}
|
|
indvdum@0
|
1249 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1250 |
|
|
indvdum@0
|
1251 |
if (!empty($index_rows))
|
|
indvdum@0
|
1252 |
{
|
|
indvdum@0
|
1253 |
foreach ($index_rows as $idx_name => $props)
|
|
indvdum@0
|
1254 |
{
|
|
indvdum@0
|
1255 |
$index_create[] = 'CREATE ' . ($props['unique'] ? 'UNIQUE ' : '') . "INDEX $idx_name ON $table_name (" . implode(', ', $props['column_names']) . ");";
|
|
indvdum@0
|
1256 |
}
|
|
indvdum@0
|
1257 |
}
|
|
indvdum@0
|
1258 |
|
|
indvdum@0
|
1259 |
if (!empty($primary_key))
|
|
indvdum@0
|
1260 |
{
|
|
indvdum@0
|
1261 |
$lines[] = " CONSTRAINT $primary_key_name PRIMARY KEY (" . implode(', ', $primary_key) . ")";
|
|
indvdum@0
|
1262 |
}
|
|
indvdum@0
|
1263 |
|
|
indvdum@0
|
1264 |
// Generate constraint clauses for CHECK constraints
|
|
indvdum@0
|
1265 |
$sql_checks = "SELECT conname as index_name, consrc
|
|
indvdum@0
|
1266 |
FROM pg_constraint, pg_class bc
|
|
indvdum@0
|
1267 |
WHERE conrelid = bc.oid
|
|
indvdum@0
|
1268 |
AND bc.relname = '" . $db->sql_escape($table_name) . "'
|
|
indvdum@0
|
1269 |
AND NOT EXISTS (
|
|
indvdum@0
|
1270 |
SELECT *
|
|
indvdum@0
|
1271 |
FROM pg_constraint as c, pg_inherits as i
|
|
indvdum@0
|
1272 |
WHERE i.inhrelid = pg_constraint.conrelid
|
|
indvdum@0
|
1273 |
AND c.conname = pg_constraint.conname
|
|
indvdum@0
|
1274 |
AND c.consrc = pg_constraint.consrc
|
|
indvdum@0
|
1275 |
AND c.conrelid = i.inhparent
|
|
indvdum@0
|
1276 |
)";
|
|
indvdum@0
|
1277 |
$result = $db->sql_query($sql_checks);
|
|
indvdum@0
|
1278 |
|
|
indvdum@0
|
1279 |
// Add the constraints to the sql file.
|
|
indvdum@0
|
1280 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1281 |
{
|
|
indvdum@0
|
1282 |
if (!is_null($row['consrc']))
|
|
indvdum@0
|
1283 |
{
|
|
indvdum@0
|
1284 |
$lines[] = ' CONSTRAINT ' . $row['index_name'] . ' CHECK ' . $row['consrc'];
|
|
indvdum@0
|
1285 |
}
|
|
indvdum@0
|
1286 |
}
|
|
indvdum@0
|
1287 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1288 |
|
|
indvdum@0
|
1289 |
$sql_data .= implode(", \n", $lines);
|
|
indvdum@0
|
1290 |
$sql_data .= "\n);\n";
|
|
indvdum@0
|
1291 |
|
|
indvdum@0
|
1292 |
if (!empty($index_create))
|
|
indvdum@0
|
1293 |
{
|
|
indvdum@0
|
1294 |
$sql_data .= implode("\n", $index_create) . "\n\n";
|
|
indvdum@0
|
1295 |
}
|
|
indvdum@0
|
1296 |
$this->flush($sql_data);
|
|
indvdum@0
|
1297 |
}
|
|
indvdum@0
|
1298 |
|
|
indvdum@0
|
1299 |
function write_data($table_name)
|
|
indvdum@0
|
1300 |
{
|
|
indvdum@0
|
1301 |
global $db;
|
|
indvdum@0
|
1302 |
// Grab all of the data from current table.
|
|
indvdum@0
|
1303 |
$sql = "SELECT *
|
|
indvdum@0
|
1304 |
FROM $table_name";
|
|
indvdum@0
|
1305 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
1306 |
|
|
indvdum@0
|
1307 |
$i_num_fields = pg_num_fields($result);
|
|
indvdum@0
|
1308 |
$seq = '';
|
|
indvdum@0
|
1309 |
|
|
indvdum@0
|
1310 |
for ($i = 0; $i < $i_num_fields; $i++)
|
|
indvdum@0
|
1311 |
{
|
|
indvdum@0
|
1312 |
$ary_type[] = pg_field_type($result, $i);
|
|
indvdum@0
|
1313 |
$ary_name[] = pg_field_name($result, $i);
|
|
indvdum@0
|
1314 |
|
|
indvdum@0
|
1315 |
|
|
indvdum@0
|
1316 |
$sql = "SELECT pg_get_expr(d.adbin, d.adrelid) as rowdefault
|
|
indvdum@0
|
1317 |
FROM pg_attrdef d, pg_class c
|
|
indvdum@0
|
1318 |
WHERE (c.relname = '{$table_name}')
|
|
indvdum@0
|
1319 |
AND (c.oid = d.adrelid)
|
|
indvdum@0
|
1320 |
AND d.adnum = " . strval($i + 1);
|
|
indvdum@0
|
1321 |
$result2 = $db->sql_query($sql);
|
|
indvdum@0
|
1322 |
if ($row = $db->sql_fetchrow($result2))
|
|
indvdum@0
|
1323 |
{
|
|
indvdum@0
|
1324 |
// Determine if we must reset the sequences
|
|
indvdum@0
|
1325 |
if (strpos($row['rowdefault'], "nextval('") === 0)
|
|
indvdum@0
|
1326 |
{
|
|
indvdum@0
|
1327 |
$seq .= "SELECT SETVAL('{$table_name}_seq',(select case when max({$ary_name[$i]})>0 then max({$ary_name[$i]})+1 else 1 end FROM {$table_name}));\n";
|
|
indvdum@0
|
1328 |
}
|
|
indvdum@0
|
1329 |
}
|
|
indvdum@0
|
1330 |
}
|
|
indvdum@0
|
1331 |
|
|
indvdum@0
|
1332 |
$this->flush("COPY $table_name (" . implode(', ', $ary_name) . ') FROM stdin;' . "\n");
|
|
indvdum@0
|
1333 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1334 |
{
|
|
indvdum@0
|
1335 |
$schema_vals = array();
|
|
indvdum@0
|
1336 |
|
|
indvdum@0
|
1337 |
// Build the SQL statement to recreate the data.
|
|
indvdum@0
|
1338 |
for ($i = 0; $i < $i_num_fields; $i++)
|
|
indvdum@0
|
1339 |
{
|
|
indvdum@0
|
1340 |
$str_val = $row[$ary_name[$i]];
|
|
indvdum@0
|
1341 |
|
|
indvdum@0
|
1342 |
if (preg_match('#char|text|bool|bytea#i', $ary_type[$i]))
|
|
indvdum@0
|
1343 |
{
|
|
indvdum@0
|
1344 |
$str_val = str_replace(array("\n", "\t", "\r", "\b", "\f", "\v"), array('\n', '\t', '\r', '\b', '\f', '\v'), addslashes($str_val));
|
|
indvdum@0
|
1345 |
$str_empty = '';
|
|
indvdum@0
|
1346 |
}
|
|
indvdum@0
|
1347 |
else
|
|
indvdum@0
|
1348 |
{
|
|
indvdum@0
|
1349 |
$str_empty = '\N';
|
|
indvdum@0
|
1350 |
}
|
|
indvdum@0
|
1351 |
|
|
indvdum@0
|
1352 |
if (empty($str_val) && $str_val !== '0')
|
|
indvdum@0
|
1353 |
{
|
|
indvdum@0
|
1354 |
$str_val = $str_empty;
|
|
indvdum@0
|
1355 |
}
|
|
indvdum@0
|
1356 |
|
|
indvdum@0
|
1357 |
$schema_vals[] = $str_val;
|
|
indvdum@0
|
1358 |
}
|
|
indvdum@0
|
1359 |
|
|
indvdum@0
|
1360 |
// Take the ordered fields and their associated data and build it
|
|
indvdum@0
|
1361 |
// into a valid sql statement to recreate that field in the data.
|
|
indvdum@0
|
1362 |
$this->flush(implode("\t", $schema_vals) . "\n");
|
|
indvdum@0
|
1363 |
}
|
|
indvdum@0
|
1364 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1365 |
$this->flush("\\.\n");
|
|
indvdum@0
|
1366 |
|
|
indvdum@0
|
1367 |
// Write out the sequence statements
|
|
indvdum@0
|
1368 |
$this->flush($seq);
|
|
indvdum@0
|
1369 |
}
|
|
indvdum@0
|
1370 |
|
|
indvdum@0
|
1371 |
function write_end()
|
|
indvdum@0
|
1372 |
{
|
|
indvdum@0
|
1373 |
$this->flush("COMMIT;\n");
|
|
indvdum@0
|
1374 |
parent::write_end();
|
|
indvdum@0
|
1375 |
}
|
|
indvdum@0
|
1376 |
}
|
|
indvdum@0
|
1377 |
|
|
indvdum@0
|
1378 |
/**
|
|
indvdum@0
|
1379 |
* @package acp
|
|
indvdum@0
|
1380 |
*/
|
|
indvdum@0
|
1381 |
class mssql_extractor extends base_extractor
|
|
indvdum@0
|
1382 |
{
|
|
indvdum@0
|
1383 |
function write_end()
|
|
indvdum@0
|
1384 |
{
|
|
indvdum@0
|
1385 |
$this->flush("COMMIT\nGO\n");
|
|
indvdum@0
|
1386 |
parent::write_end();
|
|
indvdum@0
|
1387 |
}
|
|
indvdum@0
|
1388 |
|
|
indvdum@0
|
1389 |
function write_start($prefix)
|
|
indvdum@0
|
1390 |
{
|
|
indvdum@0
|
1391 |
$sql_data = "--\n";
|
|
indvdum@0
|
1392 |
$sql_data .= "-- phpBB Backup Script\n";
|
|
indvdum@0
|
1393 |
$sql_data .= "-- Dump of tables for $prefix\n";
|
|
indvdum@0
|
1394 |
$sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
|
|
indvdum@0
|
1395 |
$sql_data .= "--\n";
|
|
indvdum@0
|
1396 |
$sql_data .= "BEGIN TRANSACTION\n";
|
|
indvdum@0
|
1397 |
$sql_data .= "GO\n";
|
|
indvdum@0
|
1398 |
$this->flush($sql_data);
|
|
indvdum@0
|
1399 |
}
|
|
indvdum@0
|
1400 |
|
|
indvdum@0
|
1401 |
function write_table($table_name)
|
|
indvdum@0
|
1402 |
{
|
|
indvdum@0
|
1403 |
global $db;
|
|
indvdum@0
|
1404 |
$sql_data = '-- Table: ' . $table_name . "\n";
|
|
indvdum@0
|
1405 |
$sql_data .= "IF OBJECT_ID(N'$table_name', N'U') IS NOT NULL\n";
|
|
indvdum@0
|
1406 |
$sql_data .= "DROP TABLE $table_name;\n";
|
|
indvdum@0
|
1407 |
$sql_data .= "GO\n";
|
|
indvdum@0
|
1408 |
$sql_data .= "\nCREATE TABLE [$table_name] (\n";
|
|
indvdum@0
|
1409 |
$rows = array();
|
|
indvdum@0
|
1410 |
|
|
indvdum@0
|
1411 |
$text_flag = false;
|
|
indvdum@0
|
1412 |
|
|
indvdum@0
|
1413 |
$sql = "SELECT COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') as IS_IDENTITY
|
|
indvdum@0
|
1414 |
FROM INFORMATION_SCHEMA.COLUMNS
|
|
indvdum@0
|
1415 |
WHERE TABLE_NAME = '$table_name'";
|
|
indvdum@0
|
1416 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
1417 |
|
|
indvdum@0
|
1418 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1419 |
{
|
|
indvdum@0
|
1420 |
$line = "\t[{$row['COLUMN_NAME']}] [{$row['DATA_TYPE']}]";
|
|
indvdum@0
|
1421 |
|
|
indvdum@0
|
1422 |
if ($row['DATA_TYPE'] == 'text')
|
|
indvdum@0
|
1423 |
{
|
|
indvdum@0
|
1424 |
$text_flag = true;
|
|
indvdum@0
|
1425 |
}
|
|
indvdum@0
|
1426 |
|
|
indvdum@0
|
1427 |
if ($row['IS_IDENTITY'])
|
|
indvdum@0
|
1428 |
{
|
|
indvdum@0
|
1429 |
$line .= ' IDENTITY (1 , 1)';
|
|
indvdum@0
|
1430 |
}
|
|
indvdum@0
|
1431 |
|
|
indvdum@0
|
1432 |
if ($row['CHARACTER_MAXIMUM_LENGTH'] && $row['DATA_TYPE'] !== 'text')
|
|
indvdum@0
|
1433 |
{
|
|
indvdum@0
|
1434 |
$line .= ' (' . $row['CHARACTER_MAXIMUM_LENGTH'] . ')';
|
|
indvdum@0
|
1435 |
}
|
|
indvdum@0
|
1436 |
|
|
indvdum@0
|
1437 |
if ($row['IS_NULLABLE'] == 'YES')
|
|
indvdum@0
|
1438 |
{
|
|
indvdum@0
|
1439 |
$line .= ' NULL';
|
|
indvdum@0
|
1440 |
}
|
|
indvdum@0
|
1441 |
else
|
|
indvdum@0
|
1442 |
{
|
|
indvdum@0
|
1443 |
$line .= ' NOT NULL';
|
|
indvdum@0
|
1444 |
}
|
|
indvdum@0
|
1445 |
|
|
indvdum@0
|
1446 |
if ($row['COLUMN_DEFAULT'])
|
|
indvdum@0
|
1447 |
{
|
|
indvdum@0
|
1448 |
$line .= ' DEFAULT ' . $row['COLUMN_DEFAULT'];
|
|
indvdum@0
|
1449 |
}
|
|
indvdum@0
|
1450 |
|
|
indvdum@0
|
1451 |
$rows[] = $line;
|
|
indvdum@0
|
1452 |
}
|
|
indvdum@0
|
1453 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1454 |
|
|
indvdum@0
|
1455 |
$sql_data .= implode(",\n", $rows);
|
|
indvdum@0
|
1456 |
$sql_data .= "\n) ON [PRIMARY]";
|
|
indvdum@0
|
1457 |
|
|
indvdum@0
|
1458 |
if ($text_flag)
|
|
indvdum@0
|
1459 |
{
|
|
indvdum@0
|
1460 |
$sql_data .= " TEXTIMAGE_ON [PRIMARY]";
|
|
indvdum@0
|
1461 |
}
|
|
indvdum@0
|
1462 |
|
|
indvdum@0
|
1463 |
$sql_data .= "\nGO\n\n";
|
|
indvdum@0
|
1464 |
$rows = array();
|
|
indvdum@0
|
1465 |
|
|
indvdum@0
|
1466 |
$sql = "SELECT CONSTRAINT_NAME, COLUMN_NAME
|
|
indvdum@0
|
1467 |
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
|
indvdum@0
|
1468 |
WHERE TABLE_NAME = '$table_name'";
|
|
indvdum@0
|
1469 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
1470 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1471 |
{
|
|
indvdum@0
|
1472 |
if (!sizeof($rows))
|
|
indvdum@0
|
1473 |
{
|
|
indvdum@0
|
1474 |
$sql_data .= "ALTER TABLE [$table_name] WITH NOCHECK ADD\n";
|
|
indvdum@0
|
1475 |
$sql_data .= "\tCONSTRAINT [{$row['CONSTRAINT_NAME']}] PRIMARY KEY CLUSTERED \n\t(\n";
|
|
indvdum@0
|
1476 |
}
|
|
indvdum@0
|
1477 |
$rows[] = "\t\t[{$row['COLUMN_NAME']}]";
|
|
indvdum@0
|
1478 |
}
|
|
indvdum@0
|
1479 |
if (sizeof($rows))
|
|
indvdum@0
|
1480 |
{
|
|
indvdum@0
|
1481 |
$sql_data .= implode(",\n", $rows);
|
|
indvdum@0
|
1482 |
$sql_data .= "\n\t) ON [PRIMARY] \nGO\n";
|
|
indvdum@0
|
1483 |
}
|
|
indvdum@0
|
1484 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1485 |
|
|
indvdum@0
|
1486 |
$index = array();
|
|
indvdum@0
|
1487 |
$sql = "EXEC sp_statistics '$table_name'";
|
|
indvdum@0
|
1488 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
1489 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1490 |
{
|
|
indvdum@0
|
1491 |
if ($row['TYPE'] == 3)
|
|
indvdum@0
|
1492 |
{
|
|
indvdum@0
|
1493 |
$index[$row['INDEX_NAME']][] = '[' . $row['COLUMN_NAME'] . ']';
|
|
indvdum@0
|
1494 |
}
|
|
indvdum@0
|
1495 |
}
|
|
indvdum@0
|
1496 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1497 |
|
|
indvdum@0
|
1498 |
foreach ($index as $index_name => $column_name)
|
|
indvdum@0
|
1499 |
{
|
|
indvdum@0
|
1500 |
$index[$index_name] = implode(', ', $column_name);
|
|
indvdum@0
|
1501 |
}
|
|
indvdum@0
|
1502 |
|
|
indvdum@0
|
1503 |
foreach ($index as $index_name => $columns)
|
|
indvdum@0
|
1504 |
{
|
|
indvdum@0
|
1505 |
$sql_data .= "\nCREATE INDEX [$index_name] ON [$table_name]($columns) ON [PRIMARY]\nGO\n";
|
|
indvdum@0
|
1506 |
}
|
|
indvdum@0
|
1507 |
$this->flush($sql_data);
|
|
indvdum@0
|
1508 |
}
|
|
indvdum@0
|
1509 |
|
|
indvdum@0
|
1510 |
function write_data($table_name)
|
|
indvdum@0
|
1511 |
{
|
|
indvdum@0
|
1512 |
global $db;
|
|
indvdum@0
|
1513 |
|
|
indvdum@0
|
1514 |
if ($db->sql_layer === 'mssql')
|
|
indvdum@0
|
1515 |
{
|
|
indvdum@0
|
1516 |
$this->write_data_mssql($table_name);
|
|
indvdum@0
|
1517 |
}
|
|
indvdum@0
|
1518 |
else if($db->sql_layer === 'mssqlnative')
|
|
indvdum@0
|
1519 |
{
|
|
indvdum@0
|
1520 |
$this->write_data_mssqlnative($table_name);
|
|
indvdum@0
|
1521 |
}
|
|
indvdum@0
|
1522 |
else
|
|
indvdum@0
|
1523 |
{
|
|
indvdum@0
|
1524 |
$this->write_data_odbc($table_name);
|
|
indvdum@0
|
1525 |
}
|
|
indvdum@0
|
1526 |
}
|
|
indvdum@0
|
1527 |
|
|
indvdum@0
|
1528 |
function write_data_mssql($table_name)
|
|
indvdum@0
|
1529 |
{
|
|
indvdum@0
|
1530 |
global $db;
|
|
indvdum@0
|
1531 |
$ary_type = $ary_name = array();
|
|
indvdum@0
|
1532 |
$ident_set = false;
|
|
indvdum@0
|
1533 |
$sql_data = '';
|
|
indvdum@0
|
1534 |
|
|
indvdum@0
|
1535 |
// Grab all of the data from current table.
|
|
indvdum@0
|
1536 |
$sql = "SELECT *
|
|
indvdum@0
|
1537 |
FROM $table_name";
|
|
indvdum@0
|
1538 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
1539 |
|
|
indvdum@0
|
1540 |
$retrieved_data = mssql_num_rows($result);
|
|
indvdum@0
|
1541 |
|
|
indvdum@0
|
1542 |
$i_num_fields = mssql_num_fields($result);
|
|
indvdum@0
|
1543 |
|
|
indvdum@0
|
1544 |
for ($i = 0; $i < $i_num_fields; $i++)
|
|
indvdum@0
|
1545 |
{
|
|
indvdum@0
|
1546 |
$ary_type[$i] = mssql_field_type($result, $i);
|
|
indvdum@0
|
1547 |
$ary_name[$i] = mssql_field_name($result, $i);
|
|
indvdum@0
|
1548 |
}
|
|
indvdum@0
|
1549 |
|
|
indvdum@0
|
1550 |
if ($retrieved_data)
|
|
indvdum@0
|
1551 |
{
|
|
indvdum@0
|
1552 |
$sql = "SELECT 1 as has_identity
|
|
indvdum@0
|
1553 |
FROM INFORMATION_SCHEMA.COLUMNS
|
|
indvdum@0
|
1554 |
WHERE COLUMNPROPERTY(object_id('$table_name'), COLUMN_NAME, 'IsIdentity') = 1";
|
|
indvdum@0
|
1555 |
$result2 = $db->sql_query($sql);
|
|
indvdum@0
|
1556 |
$row2 = $db->sql_fetchrow($result2);
|
|
indvdum@0
|
1557 |
if (!empty($row2['has_identity']))
|
|
indvdum@0
|
1558 |
{
|
|
indvdum@0
|
1559 |
$sql_data .= "\nSET IDENTITY_INSERT $table_name ON\nGO\n";
|
|
indvdum@0
|
1560 |
$ident_set = true;
|
|
indvdum@0
|
1561 |
}
|
|
indvdum@0
|
1562 |
$db->sql_freeresult($result2);
|
|
indvdum@0
|
1563 |
}
|
|
indvdum@0
|
1564 |
|
|
indvdum@0
|
1565 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1566 |
{
|
|
indvdum@0
|
1567 |
$schema_vals = $schema_fields = array();
|
|
indvdum@0
|
1568 |
|
|
indvdum@0
|
1569 |
// Build the SQL statement to recreate the data.
|
|
indvdum@0
|
1570 |
for ($i = 0; $i < $i_num_fields; $i++)
|
|
indvdum@0
|
1571 |
{
|
|
indvdum@0
|
1572 |
$str_val = $row[$ary_name[$i]];
|
|
indvdum@0
|
1573 |
|
|
indvdum@0
|
1574 |
if (preg_match('#char|text|bool|varbinary#i', $ary_type[$i]))
|
|
indvdum@0
|
1575 |
{
|
|
indvdum@0
|
1576 |
$str_quote = '';
|
|
indvdum@0
|
1577 |
$str_empty = "''";
|
|
indvdum@0
|
1578 |
$str_val = sanitize_data_mssql(str_replace("'", "''", $str_val));
|
|
indvdum@0
|
1579 |
}
|
|
indvdum@0
|
1580 |
else if (preg_match('#date|timestamp#i', $ary_type[$i]))
|
|
indvdum@0
|
1581 |
{
|
|
indvdum@0
|
1582 |
if (empty($str_val))
|
|
indvdum@0
|
1583 |
{
|
|
indvdum@0
|
1584 |
$str_quote = '';
|
|
indvdum@0
|
1585 |
}
|
|
indvdum@0
|
1586 |
else
|
|
indvdum@0
|
1587 |
{
|
|
indvdum@0
|
1588 |
$str_quote = "'";
|
|
indvdum@0
|
1589 |
}
|
|
indvdum@0
|
1590 |
}
|
|
indvdum@0
|
1591 |
else
|
|
indvdum@0
|
1592 |
{
|
|
indvdum@0
|
1593 |
$str_quote = '';
|
|
indvdum@0
|
1594 |
$str_empty = 'NULL';
|
|
indvdum@0
|
1595 |
}
|
|
indvdum@0
|
1596 |
|
|
indvdum@0
|
1597 |
if (empty($str_val) && $str_val !== '0' && !(is_int($str_val) || is_float($str_val)))
|
|
indvdum@0
|
1598 |
{
|
|
indvdum@0
|
1599 |
$str_val = $str_empty;
|
|
indvdum@0
|
1600 |
}
|
|
indvdum@0
|
1601 |
|
|
indvdum@0
|
1602 |
$schema_vals[$i] = $str_quote . $str_val . $str_quote;
|
|
indvdum@0
|
1603 |
$schema_fields[$i] = $ary_name[$i];
|
|
indvdum@0
|
1604 |
}
|
|
indvdum@0
|
1605 |
|
|
indvdum@0
|
1606 |
// Take the ordered fields and their associated data and build it
|
|
indvdum@0
|
1607 |
// into a valid sql statement to recreate that field in the data.
|
|
indvdum@0
|
1608 |
$sql_data .= "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ");\nGO\n";
|
|
indvdum@0
|
1609 |
|
|
indvdum@0
|
1610 |
$this->flush($sql_data);
|
|
indvdum@0
|
1611 |
$sql_data = '';
|
|
indvdum@0
|
1612 |
}
|
|
indvdum@0
|
1613 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1614 |
|
|
indvdum@0
|
1615 |
if ($retrieved_data && $ident_set)
|
|
indvdum@0
|
1616 |
{
|
|
indvdum@0
|
1617 |
$sql_data .= "\nSET IDENTITY_INSERT $table_name OFF\nGO\n";
|
|
indvdum@0
|
1618 |
}
|
|
indvdum@0
|
1619 |
$this->flush($sql_data);
|
|
indvdum@0
|
1620 |
}
|
|
indvdum@0
|
1621 |
|
|
indvdum@0
|
1622 |
function write_data_mssqlnative($table_name)
|
|
indvdum@0
|
1623 |
{
|
|
indvdum@0
|
1624 |
global $db;
|
|
indvdum@0
|
1625 |
$ary_type = $ary_name = array();
|
|
indvdum@0
|
1626 |
$ident_set = false;
|
|
indvdum@0
|
1627 |
$sql_data = '';
|
|
indvdum@0
|
1628 |
|
|
indvdum@0
|
1629 |
// Grab all of the data from current table.
|
|
indvdum@0
|
1630 |
$sql = "SELECT * FROM $table_name";
|
|
indvdum@0
|
1631 |
$db->mssqlnative_set_query_options(array('Scrollable' => SQLSRV_CURSOR_STATIC));
|
|
indvdum@0
|
1632 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
1633 |
|
|
indvdum@0
|
1634 |
$retrieved_data = $db->mssqlnative_num_rows($result);
|
|
indvdum@0
|
1635 |
|
|
indvdum@0
|
1636 |
if (!$retrieved_data)
|
|
indvdum@0
|
1637 |
{
|
|
indvdum@0
|
1638 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1639 |
return;
|
|
indvdum@0
|
1640 |
}
|
|
indvdum@0
|
1641 |
|
|
indvdum@0
|
1642 |
$sql = "SELECT * FROM $table_name";
|
|
indvdum@0
|
1643 |
$result_fields = $db->sql_query_limit($sql, 1);
|
|
indvdum@0
|
1644 |
|
|
indvdum@0
|
1645 |
$row = new result_mssqlnative($result_fields);
|
|
indvdum@0
|
1646 |
$i_num_fields = $row->num_fields();
|
|
indvdum@0
|
1647 |
|
|
indvdum@0
|
1648 |
for ($i = 0; $i < $i_num_fields; $i++)
|
|
indvdum@0
|
1649 |
{
|
|
indvdum@0
|
1650 |
$ary_type[$i] = $row->field_type($i);
|
|
indvdum@0
|
1651 |
$ary_name[$i] = $row->field_name($i);
|
|
indvdum@0
|
1652 |
}
|
|
indvdum@0
|
1653 |
$db->sql_freeresult($result_fields);
|
|
indvdum@0
|
1654 |
|
|
indvdum@0
|
1655 |
$sql = "SELECT 1 as has_identity
|
|
indvdum@0
|
1656 |
FROM INFORMATION_SCHEMA.COLUMNS
|
|
indvdum@0
|
1657 |
WHERE COLUMNPROPERTY(object_id('$table_name'), COLUMN_NAME, 'IsIdentity') = 1";
|
|
indvdum@0
|
1658 |
$result2 = $db->sql_query($sql);
|
|
indvdum@0
|
1659 |
$row2 = $db->sql_fetchrow($result2);
|
|
indvdum@0
|
1660 |
|
|
indvdum@0
|
1661 |
if (!empty($row2['has_identity']))
|
|
indvdum@0
|
1662 |
{
|
|
indvdum@0
|
1663 |
$sql_data .= "\nSET IDENTITY_INSERT $table_name ON\nGO\n";
|
|
indvdum@0
|
1664 |
$ident_set = true;
|
|
indvdum@0
|
1665 |
}
|
|
indvdum@0
|
1666 |
$db->sql_freeresult($result2);
|
|
indvdum@0
|
1667 |
|
|
indvdum@0
|
1668 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1669 |
{
|
|
indvdum@0
|
1670 |
$schema_vals = $schema_fields = array();
|
|
indvdum@0
|
1671 |
|
|
indvdum@0
|
1672 |
// Build the SQL statement to recreate the data.
|
|
indvdum@0
|
1673 |
for ($i = 0; $i < $i_num_fields; $i++)
|
|
indvdum@0
|
1674 |
{
|
|
indvdum@0
|
1675 |
$str_val = $row[$ary_name[$i]];
|
|
indvdum@0
|
1676 |
|
|
indvdum@0
|
1677 |
// defaults to type number - better quote just to be safe, so check for is_int too
|
|
indvdum@0
|
1678 |
if (is_int($ary_type[$i]) || preg_match('#char|text|bool|varbinary#i', $ary_type[$i]))
|
|
indvdum@0
|
1679 |
{
|
|
indvdum@0
|
1680 |
$str_quote = '';
|
|
indvdum@0
|
1681 |
$str_empty = "''";
|
|
indvdum@0
|
1682 |
$str_val = sanitize_data_mssql(str_replace("'", "''", $str_val));
|
|
indvdum@0
|
1683 |
}
|
|
indvdum@0
|
1684 |
else if (preg_match('#date|timestamp#i', $ary_type[$i]))
|
|
indvdum@0
|
1685 |
{
|
|
indvdum@0
|
1686 |
if (empty($str_val))
|
|
indvdum@0
|
1687 |
{
|
|
indvdum@0
|
1688 |
$str_quote = '';
|
|
indvdum@0
|
1689 |
}
|
|
indvdum@0
|
1690 |
else
|
|
indvdum@0
|
1691 |
{
|
|
indvdum@0
|
1692 |
$str_quote = "'";
|
|
indvdum@0
|
1693 |
}
|
|
indvdum@0
|
1694 |
}
|
|
indvdum@0
|
1695 |
else
|
|
indvdum@0
|
1696 |
{
|
|
indvdum@0
|
1697 |
$str_quote = '';
|
|
indvdum@0
|
1698 |
$str_empty = 'NULL';
|
|
indvdum@0
|
1699 |
}
|
|
indvdum@0
|
1700 |
|
|
indvdum@0
|
1701 |
if (empty($str_val) && $str_val !== '0' && !(is_int($str_val) || is_float($str_val)))
|
|
indvdum@0
|
1702 |
{
|
|
indvdum@0
|
1703 |
$str_val = $str_empty;
|
|
indvdum@0
|
1704 |
}
|
|
indvdum@0
|
1705 |
|
|
indvdum@0
|
1706 |
$schema_vals[$i] = $str_quote . $str_val . $str_quote;
|
|
indvdum@0
|
1707 |
$schema_fields[$i] = $ary_name[$i];
|
|
indvdum@0
|
1708 |
}
|
|
indvdum@0
|
1709 |
|
|
indvdum@0
|
1710 |
// Take the ordered fields and their associated data and build it
|
|
indvdum@0
|
1711 |
// into a valid sql statement to recreate that field in the data.
|
|
indvdum@0
|
1712 |
$sql_data .= "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ");\nGO\n";
|
|
indvdum@0
|
1713 |
|
|
indvdum@0
|
1714 |
$this->flush($sql_data);
|
|
indvdum@0
|
1715 |
$sql_data = '';
|
|
indvdum@0
|
1716 |
}
|
|
indvdum@0
|
1717 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1718 |
|
|
indvdum@0
|
1719 |
if ($ident_set)
|
|
indvdum@0
|
1720 |
{
|
|
indvdum@0
|
1721 |
$sql_data .= "\nSET IDENTITY_INSERT $table_name OFF\nGO\n";
|
|
indvdum@0
|
1722 |
}
|
|
indvdum@0
|
1723 |
$this->flush($sql_data);
|
|
indvdum@0
|
1724 |
}
|
|
indvdum@0
|
1725 |
|
|
indvdum@0
|
1726 |
function write_data_odbc($table_name)
|
|
indvdum@0
|
1727 |
{
|
|
indvdum@0
|
1728 |
global $db;
|
|
indvdum@0
|
1729 |
$ary_type = $ary_name = array();
|
|
indvdum@0
|
1730 |
$ident_set = false;
|
|
indvdum@0
|
1731 |
$sql_data = '';
|
|
indvdum@0
|
1732 |
|
|
indvdum@0
|
1733 |
// Grab all of the data from current table.
|
|
indvdum@0
|
1734 |
$sql = "SELECT *
|
|
indvdum@0
|
1735 |
FROM $table_name";
|
|
indvdum@0
|
1736 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
1737 |
|
|
indvdum@0
|
1738 |
$retrieved_data = odbc_num_rows($result);
|
|
indvdum@0
|
1739 |
|
|
indvdum@0
|
1740 |
if ($retrieved_data)
|
|
indvdum@0
|
1741 |
{
|
|
indvdum@0
|
1742 |
$sql = "SELECT 1 as has_identity
|
|
indvdum@0
|
1743 |
FROM INFORMATION_SCHEMA.COLUMNS
|
|
indvdum@0
|
1744 |
WHERE COLUMNPROPERTY(object_id('$table_name'), COLUMN_NAME, 'IsIdentity') = 1";
|
|
indvdum@0
|
1745 |
$result2 = $db->sql_query($sql);
|
|
indvdum@0
|
1746 |
$row2 = $db->sql_fetchrow($result2);
|
|
indvdum@0
|
1747 |
if (!empty($row2['has_identity']))
|
|
indvdum@0
|
1748 |
{
|
|
indvdum@0
|
1749 |
$sql_data .= "\nSET IDENTITY_INSERT $table_name ON\nGO\n";
|
|
indvdum@0
|
1750 |
$ident_set = true;
|
|
indvdum@0
|
1751 |
}
|
|
indvdum@0
|
1752 |
$db->sql_freeresult($result2);
|
|
indvdum@0
|
1753 |
}
|
|
indvdum@0
|
1754 |
|
|
indvdum@0
|
1755 |
$i_num_fields = odbc_num_fields($result);
|
|
indvdum@0
|
1756 |
|
|
indvdum@0
|
1757 |
for ($i = 0; $i < $i_num_fields; $i++)
|
|
indvdum@0
|
1758 |
{
|
|
indvdum@0
|
1759 |
$ary_type[$i] = odbc_field_type($result, $i + 1);
|
|
indvdum@0
|
1760 |
$ary_name[$i] = odbc_field_name($result, $i + 1);
|
|
indvdum@0
|
1761 |
}
|
|
indvdum@0
|
1762 |
|
|
indvdum@0
|
1763 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1764 |
{
|
|
indvdum@0
|
1765 |
$schema_vals = $schema_fields = array();
|
|
indvdum@0
|
1766 |
|
|
indvdum@0
|
1767 |
// Build the SQL statement to recreate the data.
|
|
indvdum@0
|
1768 |
for ($i = 0; $i < $i_num_fields; $i++)
|
|
indvdum@0
|
1769 |
{
|
|
indvdum@0
|
1770 |
$str_val = $row[$ary_name[$i]];
|
|
indvdum@0
|
1771 |
|
|
indvdum@0
|
1772 |
if (preg_match('#char|text|bool|varbinary#i', $ary_type[$i]))
|
|
indvdum@0
|
1773 |
{
|
|
indvdum@0
|
1774 |
$str_quote = '';
|
|
indvdum@0
|
1775 |
$str_empty = "''";
|
|
indvdum@0
|
1776 |
$str_val = sanitize_data_mssql(str_replace("'", "''", $str_val));
|
|
indvdum@0
|
1777 |
}
|
|
indvdum@0
|
1778 |
else if (preg_match('#date|timestamp#i', $ary_type[$i]))
|
|
indvdum@0
|
1779 |
{
|
|
indvdum@0
|
1780 |
if (empty($str_val))
|
|
indvdum@0
|
1781 |
{
|
|
indvdum@0
|
1782 |
$str_quote = '';
|
|
indvdum@0
|
1783 |
}
|
|
indvdum@0
|
1784 |
else
|
|
indvdum@0
|
1785 |
{
|
|
indvdum@0
|
1786 |
$str_quote = "'";
|
|
indvdum@0
|
1787 |
}
|
|
indvdum@0
|
1788 |
}
|
|
indvdum@0
|
1789 |
else
|
|
indvdum@0
|
1790 |
{
|
|
indvdum@0
|
1791 |
$str_quote = '';
|
|
indvdum@0
|
1792 |
$str_empty = 'NULL';
|
|
indvdum@0
|
1793 |
}
|
|
indvdum@0
|
1794 |
|
|
indvdum@0
|
1795 |
if (empty($str_val) && $str_val !== '0' && !(is_int($str_val) || is_float($str_val)))
|
|
indvdum@0
|
1796 |
{
|
|
indvdum@0
|
1797 |
$str_val = $str_empty;
|
|
indvdum@0
|
1798 |
}
|
|
indvdum@0
|
1799 |
|
|
indvdum@0
|
1800 |
$schema_vals[$i] = $str_quote . $str_val . $str_quote;
|
|
indvdum@0
|
1801 |
$schema_fields[$i] = $ary_name[$i];
|
|
indvdum@0
|
1802 |
}
|
|
indvdum@0
|
1803 |
|
|
indvdum@0
|
1804 |
// Take the ordered fields and their associated data and build it
|
|
indvdum@0
|
1805 |
// into a valid sql statement to recreate that field in the data.
|
|
indvdum@0
|
1806 |
$sql_data .= "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ");\nGO\n";
|
|
indvdum@0
|
1807 |
|
|
indvdum@0
|
1808 |
$this->flush($sql_data);
|
|
indvdum@0
|
1809 |
|
|
indvdum@0
|
1810 |
$sql_data = '';
|
|
indvdum@0
|
1811 |
|
|
indvdum@0
|
1812 |
}
|
|
indvdum@0
|
1813 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1814 |
|
|
indvdum@0
|
1815 |
if ($retrieved_data && $ident_set)
|
|
indvdum@0
|
1816 |
{
|
|
indvdum@0
|
1817 |
$sql_data .= "\nSET IDENTITY_INSERT $table_name OFF\nGO\n";
|
|
indvdum@0
|
1818 |
}
|
|
indvdum@0
|
1819 |
$this->flush($sql_data);
|
|
indvdum@0
|
1820 |
}
|
|
indvdum@0
|
1821 |
|
|
indvdum@0
|
1822 |
}
|
|
indvdum@0
|
1823 |
|
|
indvdum@0
|
1824 |
/**
|
|
indvdum@0
|
1825 |
* @package acp
|
|
indvdum@0
|
1826 |
*/
|
|
indvdum@0
|
1827 |
class oracle_extractor extends base_extractor
|
|
indvdum@0
|
1828 |
{
|
|
indvdum@0
|
1829 |
function write_table($table_name)
|
|
indvdum@0
|
1830 |
{
|
|
indvdum@0
|
1831 |
global $db;
|
|
indvdum@0
|
1832 |
$sql_data = '-- Table: ' . $table_name . "\n";
|
|
indvdum@0
|
1833 |
$sql_data .= "DROP TABLE $table_name\n/\n";
|
|
indvdum@0
|
1834 |
$sql_data .= "\nCREATE TABLE $table_name (\n";
|
|
indvdum@0
|
1835 |
|
|
indvdum@0
|
1836 |
$sql = "SELECT COLUMN_NAME, DATA_TYPE, DATA_PRECISION, DATA_LENGTH, NULLABLE, DATA_DEFAULT
|
|
indvdum@0
|
1837 |
FROM ALL_TAB_COLS
|
|
indvdum@0
|
1838 |
WHERE table_name = '{$table_name}'";
|
|
indvdum@0
|
1839 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
1840 |
|
|
indvdum@0
|
1841 |
$rows = array();
|
|
indvdum@0
|
1842 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1843 |
{
|
|
indvdum@0
|
1844 |
$line = ' "' . $row['column_name'] . '" ' . $row['data_type'];
|
|
indvdum@0
|
1845 |
|
|
indvdum@0
|
1846 |
if ($row['data_type'] !== 'CLOB')
|
|
indvdum@0
|
1847 |
{
|
|
indvdum@0
|
1848 |
if ($row['data_type'] !== 'VARCHAR2' && $row['data_type'] !== 'CHAR')
|
|
indvdum@0
|
1849 |
{
|
|
indvdum@0
|
1850 |
$line .= '(' . $row['data_precision'] . ')';
|
|
indvdum@0
|
1851 |
}
|
|
indvdum@0
|
1852 |
else
|
|
indvdum@0
|
1853 |
{
|
|
indvdum@0
|
1854 |
$line .= '(' . $row['data_length'] . ')';
|
|
indvdum@0
|
1855 |
}
|
|
indvdum@0
|
1856 |
}
|
|
indvdum@0
|
1857 |
|
|
indvdum@0
|
1858 |
if (!empty($row['data_default']))
|
|
indvdum@0
|
1859 |
{
|
|
indvdum@0
|
1860 |
$line .= ' DEFAULT ' . $row['data_default'];
|
|
indvdum@0
|
1861 |
}
|
|
indvdum@0
|
1862 |
|
|
indvdum@0
|
1863 |
if ($row['nullable'] == 'N')
|
|
indvdum@0
|
1864 |
{
|
|
indvdum@0
|
1865 |
$line .= ' NOT NULL';
|
|
indvdum@0
|
1866 |
}
|
|
indvdum@0
|
1867 |
$rows[] = $line;
|
|
indvdum@0
|
1868 |
}
|
|
indvdum@0
|
1869 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1870 |
|
|
indvdum@0
|
1871 |
$sql = "SELECT A.CONSTRAINT_NAME, A.COLUMN_NAME
|
|
indvdum@0
|
1872 |
FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B
|
|
indvdum@0
|
1873 |
WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
|
|
indvdum@0
|
1874 |
AND B.CONSTRAINT_TYPE = 'P'
|
|
indvdum@0
|
1875 |
AND A.TABLE_NAME = '{$table_name}'";
|
|
indvdum@0
|
1876 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
1877 |
|
|
indvdum@0
|
1878 |
$primary_key = array();
|
|
indvdum@0
|
1879 |
$contraint_name = '';
|
|
indvdum@0
|
1880 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1881 |
{
|
|
indvdum@0
|
1882 |
$constraint_name = '"' . $row['constraint_name'] . '"';
|
|
indvdum@0
|
1883 |
$primary_key[] = '"' . $row['column_name'] . '"';
|
|
indvdum@0
|
1884 |
}
|
|
indvdum@0
|
1885 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1886 |
|
|
indvdum@0
|
1887 |
if (sizeof($primary_key))
|
|
indvdum@0
|
1888 |
{
|
|
indvdum@0
|
1889 |
$rows[] = " CONSTRAINT {$constraint_name} PRIMARY KEY (" . implode(', ', $primary_key) . ')';
|
|
indvdum@0
|
1890 |
}
|
|
indvdum@0
|
1891 |
|
|
indvdum@0
|
1892 |
$sql = "SELECT A.CONSTRAINT_NAME, A.COLUMN_NAME
|
|
indvdum@0
|
1893 |
FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B
|
|
indvdum@0
|
1894 |
WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
|
|
indvdum@0
|
1895 |
AND B.CONSTRAINT_TYPE = 'U'
|
|
indvdum@0
|
1896 |
AND A.TABLE_NAME = '{$table_name}'";
|
|
indvdum@0
|
1897 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
1898 |
|
|
indvdum@0
|
1899 |
$unique = array();
|
|
indvdum@0
|
1900 |
$contraint_name = '';
|
|
indvdum@0
|
1901 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1902 |
{
|
|
indvdum@0
|
1903 |
$constraint_name = '"' . $row['constraint_name'] . '"';
|
|
indvdum@0
|
1904 |
$unique[] = '"' . $row['column_name'] . '"';
|
|
indvdum@0
|
1905 |
}
|
|
indvdum@0
|
1906 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1907 |
|
|
indvdum@0
|
1908 |
if (sizeof($unique))
|
|
indvdum@0
|
1909 |
{
|
|
indvdum@0
|
1910 |
$rows[] = " CONSTRAINT {$constraint_name} UNIQUE (" . implode(', ', $unique) . ')';
|
|
indvdum@0
|
1911 |
}
|
|
indvdum@0
|
1912 |
|
|
indvdum@0
|
1913 |
$sql_data .= implode(",\n", $rows);
|
|
indvdum@0
|
1914 |
$sql_data .= "\n)\n/\n";
|
|
indvdum@0
|
1915 |
|
|
indvdum@0
|
1916 |
$sql = "SELECT A.REFERENCED_NAME, C.*
|
|
indvdum@0
|
1917 |
FROM USER_DEPENDENCIES A, USER_TRIGGERS B, USER_SEQUENCES C
|
|
indvdum@0
|
1918 |
WHERE A.REFERENCED_TYPE = 'SEQUENCE'
|
|
indvdum@0
|
1919 |
AND A.NAME = B.TRIGGER_NAME
|
|
indvdum@0
|
1920 |
AND B.TABLE_NAME = '{$table_name}'
|
|
indvdum@0
|
1921 |
AND C.SEQUENCE_NAME = A.REFERENCED_NAME";
|
|
indvdum@0
|
1922 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
1923 |
|
|
indvdum@0
|
1924 |
$type = request_var('type', '');
|
|
indvdum@0
|
1925 |
|
|
indvdum@0
|
1926 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1927 |
{
|
|
indvdum@0
|
1928 |
$sql_data .= "\nDROP SEQUENCE \"{$row['referenced_name']}\"\n/\n";
|
|
indvdum@0
|
1929 |
$sql_data .= "\nCREATE SEQUENCE \"{$row['referenced_name']}\"";
|
|
indvdum@0
|
1930 |
|
|
indvdum@0
|
1931 |
if ($type == 'full')
|
|
indvdum@0
|
1932 |
{
|
|
indvdum@0
|
1933 |
$sql_data .= ' START WITH ' . $row['last_number'];
|
|
indvdum@0
|
1934 |
}
|
|
indvdum@0
|
1935 |
|
|
indvdum@0
|
1936 |
$sql_data .= "\n/\n";
|
|
indvdum@0
|
1937 |
}
|
|
indvdum@0
|
1938 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1939 |
|
|
indvdum@0
|
1940 |
$sql = "SELECT DESCRIPTION, WHEN_CLAUSE, TRIGGER_BODY
|
|
indvdum@0
|
1941 |
FROM USER_TRIGGERS
|
|
indvdum@0
|
1942 |
WHERE TABLE_NAME = '{$table_name}'";
|
|
indvdum@0
|
1943 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
1944 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1945 |
{
|
|
indvdum@0
|
1946 |
$sql_data .= "\nCREATE OR REPLACE TRIGGER {$row['description']}WHEN ({$row['when_clause']})\n{$row['trigger_body']}\n/\n";
|
|
indvdum@0
|
1947 |
}
|
|
indvdum@0
|
1948 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1949 |
|
|
indvdum@0
|
1950 |
$sql = "SELECT A.INDEX_NAME, B.COLUMN_NAME
|
|
indvdum@0
|
1951 |
FROM USER_INDEXES A, USER_IND_COLUMNS B
|
|
indvdum@0
|
1952 |
WHERE A.UNIQUENESS = 'NONUNIQUE'
|
|
indvdum@0
|
1953 |
AND A.INDEX_NAME = B.INDEX_NAME
|
|
indvdum@0
|
1954 |
AND B.TABLE_NAME = '{$table_name}'";
|
|
indvdum@0
|
1955 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
1956 |
|
|
indvdum@0
|
1957 |
$index = array();
|
|
indvdum@0
|
1958 |
|
|
indvdum@0
|
1959 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1960 |
{
|
|
indvdum@0
|
1961 |
$index[$row['index_name']][] = $row['column_name'];
|
|
indvdum@0
|
1962 |
}
|
|
indvdum@0
|
1963 |
|
|
indvdum@0
|
1964 |
foreach ($index as $index_name => $column_names)
|
|
indvdum@0
|
1965 |
{
|
|
indvdum@0
|
1966 |
$sql_data .= "\nCREATE INDEX $index_name ON $table_name(" . implode(', ', $column_names) . ")\n/\n";
|
|
indvdum@0
|
1967 |
}
|
|
indvdum@0
|
1968 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
1969 |
$this->flush($sql_data);
|
|
indvdum@0
|
1970 |
}
|
|
indvdum@0
|
1971 |
|
|
indvdum@0
|
1972 |
function write_data($table_name)
|
|
indvdum@0
|
1973 |
{
|
|
indvdum@0
|
1974 |
global $db;
|
|
indvdum@0
|
1975 |
$ary_type = $ary_name = array();
|
|
indvdum@0
|
1976 |
|
|
indvdum@0
|
1977 |
// Grab all of the data from current table.
|
|
indvdum@0
|
1978 |
$sql = "SELECT *
|
|
indvdum@0
|
1979 |
FROM $table_name";
|
|
indvdum@0
|
1980 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
1981 |
|
|
indvdum@0
|
1982 |
$i_num_fields = ocinumcols($result);
|
|
indvdum@0
|
1983 |
|
|
indvdum@0
|
1984 |
for ($i = 0; $i < $i_num_fields; $i++)
|
|
indvdum@0
|
1985 |
{
|
|
indvdum@0
|
1986 |
$ary_type[$i] = ocicolumntype($result, $i + 1);
|
|
indvdum@0
|
1987 |
$ary_name[$i] = ocicolumnname($result, $i + 1);
|
|
indvdum@0
|
1988 |
}
|
|
indvdum@0
|
1989 |
|
|
indvdum@0
|
1990 |
$sql_data = '';
|
|
indvdum@0
|
1991 |
|
|
indvdum@0
|
1992 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
1993 |
{
|
|
indvdum@0
|
1994 |
$schema_vals = $schema_fields = array();
|
|
indvdum@0
|
1995 |
|
|
indvdum@0
|
1996 |
// Build the SQL statement to recreate the data.
|
|
indvdum@0
|
1997 |
for ($i = 0; $i < $i_num_fields; $i++)
|
|
indvdum@0
|
1998 |
{
|
|
indvdum@0
|
1999 |
// Oracle uses uppercase - we use lowercase
|
|
indvdum@0
|
2000 |
$str_val = $row[strtolower($ary_name[$i])];
|
|
indvdum@0
|
2001 |
|
|
indvdum@0
|
2002 |
if (preg_match('#char|text|bool|raw|clob#i', $ary_type[$i]))
|
|
indvdum@0
|
2003 |
{
|
|
indvdum@0
|
2004 |
$str_quote = '';
|
|
indvdum@0
|
2005 |
$str_empty = "''";
|
|
indvdum@0
|
2006 |
$str_val = sanitize_data_oracle($str_val);
|
|
indvdum@0
|
2007 |
}
|
|
indvdum@0
|
2008 |
else if (preg_match('#date|timestamp#i', $ary_type[$i]))
|
|
indvdum@0
|
2009 |
{
|
|
indvdum@0
|
2010 |
if (empty($str_val))
|
|
indvdum@0
|
2011 |
{
|
|
indvdum@0
|
2012 |
$str_quote = '';
|
|
indvdum@0
|
2013 |
}
|
|
indvdum@0
|
2014 |
else
|
|
indvdum@0
|
2015 |
{
|
|
indvdum@0
|
2016 |
$str_quote = "'";
|
|
indvdum@0
|
2017 |
}
|
|
indvdum@0
|
2018 |
}
|
|
indvdum@0
|
2019 |
else
|
|
indvdum@0
|
2020 |
{
|
|
indvdum@0
|
2021 |
$str_quote = '';
|
|
indvdum@0
|
2022 |
$str_empty = 'NULL';
|
|
indvdum@0
|
2023 |
}
|
|
indvdum@0
|
2024 |
|
|
indvdum@0
|
2025 |
if (empty($str_val) && $str_val !== '0')
|
|
indvdum@0
|
2026 |
{
|
|
indvdum@0
|
2027 |
$str_val = $str_empty;
|
|
indvdum@0
|
2028 |
}
|
|
indvdum@0
|
2029 |
|
|
indvdum@0
|
2030 |
$schema_vals[$i] = $str_quote . $str_val . $str_quote;
|
|
indvdum@0
|
2031 |
$schema_fields[$i] = '"' . $ary_name[$i] . '"';
|
|
indvdum@0
|
2032 |
}
|
|
indvdum@0
|
2033 |
|
|
indvdum@0
|
2034 |
// Take the ordered fields and their associated data and build it
|
|
indvdum@0
|
2035 |
// into a valid sql statement to recreate that field in the data.
|
|
indvdum@0
|
2036 |
$sql_data = "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ")\n/\n";
|
|
indvdum@0
|
2037 |
|
|
indvdum@0
|
2038 |
$this->flush($sql_data);
|
|
indvdum@0
|
2039 |
}
|
|
indvdum@0
|
2040 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
2041 |
}
|
|
indvdum@0
|
2042 |
|
|
indvdum@0
|
2043 |
function write_start($prefix)
|
|
indvdum@0
|
2044 |
{
|
|
indvdum@0
|
2045 |
$sql_data = "--\n";
|
|
indvdum@0
|
2046 |
$sql_data .= "-- phpBB Backup Script\n";
|
|
indvdum@0
|
2047 |
$sql_data .= "-- Dump of tables for $prefix\n";
|
|
indvdum@0
|
2048 |
$sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
|
|
indvdum@0
|
2049 |
$sql_data .= "--\n";
|
|
indvdum@0
|
2050 |
$this->flush($sql_data);
|
|
indvdum@0
|
2051 |
}
|
|
indvdum@0
|
2052 |
}
|
|
indvdum@0
|
2053 |
|
|
indvdum@0
|
2054 |
/**
|
|
indvdum@0
|
2055 |
* @package acp
|
|
indvdum@0
|
2056 |
*/
|
|
indvdum@0
|
2057 |
class firebird_extractor extends base_extractor
|
|
indvdum@0
|
2058 |
{
|
|
indvdum@0
|
2059 |
function write_start($prefix)
|
|
indvdum@0
|
2060 |
{
|
|
indvdum@0
|
2061 |
$sql_data = "--\n";
|
|
indvdum@0
|
2062 |
$sql_data .= "-- phpBB Backup Script\n";
|
|
indvdum@0
|
2063 |
$sql_data .= "-- Dump of tables for $prefix\n";
|
|
indvdum@0
|
2064 |
$sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
|
|
indvdum@0
|
2065 |
$sql_data .= "--\n";
|
|
indvdum@0
|
2066 |
$this->flush($sql_data);
|
|
indvdum@0
|
2067 |
}
|
|
indvdum@0
|
2068 |
|
|
indvdum@0
|
2069 |
function write_data($table_name)
|
|
indvdum@0
|
2070 |
{
|
|
indvdum@0
|
2071 |
global $db;
|
|
indvdum@0
|
2072 |
$ary_type = $ary_name = array();
|
|
indvdum@0
|
2073 |
|
|
indvdum@0
|
2074 |
// Grab all of the data from current table.
|
|
indvdum@0
|
2075 |
$sql = "SELECT *
|
|
indvdum@0
|
2076 |
FROM $table_name";
|
|
indvdum@0
|
2077 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
2078 |
|
|
indvdum@0
|
2079 |
$i_num_fields = ibase_num_fields($result);
|
|
indvdum@0
|
2080 |
|
|
indvdum@0
|
2081 |
for ($i = 0; $i < $i_num_fields; $i++)
|
|
indvdum@0
|
2082 |
{
|
|
indvdum@0
|
2083 |
$info = ibase_field_info($result, $i);
|
|
indvdum@0
|
2084 |
$ary_type[$i] = $info['type'];
|
|
indvdum@0
|
2085 |
$ary_name[$i] = $info['name'];
|
|
indvdum@0
|
2086 |
}
|
|
indvdum@0
|
2087 |
|
|
indvdum@0
|
2088 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
2089 |
{
|
|
indvdum@0
|
2090 |
$schema_vals = $schema_fields = array();
|
|
indvdum@0
|
2091 |
|
|
indvdum@0
|
2092 |
// Build the SQL statement to recreate the data.
|
|
indvdum@0
|
2093 |
for ($i = 0; $i < $i_num_fields; $i++)
|
|
indvdum@0
|
2094 |
{
|
|
indvdum@0
|
2095 |
$str_val = $row[strtolower($ary_name[$i])];
|
|
indvdum@0
|
2096 |
|
|
indvdum@0
|
2097 |
if (preg_match('#char|text|bool|varbinary|blob#i', $ary_type[$i]))
|
|
indvdum@0
|
2098 |
{
|
|
indvdum@0
|
2099 |
$str_quote = '';
|
|
indvdum@0
|
2100 |
$str_empty = "''";
|
|
indvdum@0
|
2101 |
$str_val = sanitize_data_generic(str_replace("'", "''", $str_val));
|
|
indvdum@0
|
2102 |
}
|
|
indvdum@0
|
2103 |
else if (preg_match('#date|timestamp#i', $ary_type[$i]))
|
|
indvdum@0
|
2104 |
{
|
|
indvdum@0
|
2105 |
if (empty($str_val))
|
|
indvdum@0
|
2106 |
{
|
|
indvdum@0
|
2107 |
$str_quote = '';
|
|
indvdum@0
|
2108 |
}
|
|
indvdum@0
|
2109 |
else
|
|
indvdum@0
|
2110 |
{
|
|
indvdum@0
|
2111 |
$str_quote = "'";
|
|
indvdum@0
|
2112 |
}
|
|
indvdum@0
|
2113 |
}
|
|
indvdum@0
|
2114 |
else
|
|
indvdum@0
|
2115 |
{
|
|
indvdum@0
|
2116 |
$str_quote = '';
|
|
indvdum@0
|
2117 |
$str_empty = 'NULL';
|
|
indvdum@0
|
2118 |
}
|
|
indvdum@0
|
2119 |
|
|
indvdum@0
|
2120 |
if (empty($str_val) && $str_val !== '0')
|
|
indvdum@0
|
2121 |
{
|
|
indvdum@0
|
2122 |
$str_val = $str_empty;
|
|
indvdum@0
|
2123 |
}
|
|
indvdum@0
|
2124 |
|
|
indvdum@0
|
2125 |
$schema_vals[$i] = $str_quote . $str_val . $str_quote;
|
|
indvdum@0
|
2126 |
$schema_fields[$i] = '"' . $ary_name[$i] . '"';
|
|
indvdum@0
|
2127 |
}
|
|
indvdum@0
|
2128 |
|
|
indvdum@0
|
2129 |
// Take the ordered fields and their associated data and build it
|
|
indvdum@0
|
2130 |
// into a valid sql statement to recreate that field in the data.
|
|
indvdum@0
|
2131 |
$sql_data = "INSERT INTO $table_name (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ");\n";
|
|
indvdum@0
|
2132 |
|
|
indvdum@0
|
2133 |
$this->flush($sql_data);
|
|
indvdum@0
|
2134 |
}
|
|
indvdum@0
|
2135 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
2136 |
}
|
|
indvdum@0
|
2137 |
|
|
indvdum@0
|
2138 |
function write_table($table_name)
|
|
indvdum@0
|
2139 |
{
|
|
indvdum@0
|
2140 |
global $db;
|
|
indvdum@0
|
2141 |
|
|
indvdum@0
|
2142 |
$sql_data = '-- Table: ' . $table_name . "\n";
|
|
indvdum@0
|
2143 |
$sql_data .= "DROP TABLE $table_name;\n";
|
|
indvdum@0
|
2144 |
|
|
indvdum@0
|
2145 |
$data_types = array(7 => 'SMALLINT', 8 => 'INTEGER', 10 => 'FLOAT', 12 => 'DATE', 13 => 'TIME', 14 => 'CHARACTER', 27 => 'DOUBLE PRECISION', 35 => 'TIMESTAMP', 37 => 'VARCHAR', 40 => 'CSTRING', 261 => 'BLOB', 701 => 'DECIMAL', 702 => 'NUMERIC');
|
|
indvdum@0
|
2146 |
|
|
indvdum@0
|
2147 |
$sql_data .= "\nCREATE TABLE $table_name (\n";
|
|
indvdum@0
|
2148 |
|
|
indvdum@0
|
2149 |
$sql = 'SELECT DISTINCT R.RDB$FIELD_NAME as FNAME, R.RDB$NULL_FLAG as NFLAG, R.RDB$DEFAULT_SOURCE as DSOURCE, F.RDB$FIELD_TYPE as FTYPE, F.RDB$FIELD_SUB_TYPE as STYPE, F.RDB$FIELD_LENGTH as FLEN
|
|
indvdum@0
|
2150 |
FROM RDB$RELATION_FIELDS R
|
|
indvdum@0
|
2151 |
JOIN RDB$FIELDS F ON R.RDB$FIELD_SOURCE=F.RDB$FIELD_NAME
|
|
indvdum@0
|
2152 |
LEFT JOIN RDB$FIELD_DIMENSIONS D ON R.RDB$FIELD_SOURCE = D.RDB$FIELD_NAME
|
|
indvdum@0
|
2153 |
WHERE F.RDB$SYSTEM_FLAG = 0
|
|
indvdum@0
|
2154 |
AND R.RDB$RELATION_NAME = \''. $table_name . '\'
|
|
indvdum@0
|
2155 |
ORDER BY R.RDB$FIELD_POSITION';
|
|
indvdum@0
|
2156 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
2157 |
|
|
indvdum@0
|
2158 |
$rows = array();
|
|
indvdum@0
|
2159 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
2160 |
{
|
|
indvdum@0
|
2161 |
$line = "\t" . '"' . $row['fname'] . '" ' . $data_types[$row['ftype']];
|
|
indvdum@0
|
2162 |
|
|
indvdum@0
|
2163 |
if ($row['ftype'] == 261 && $row['stype'] == 1)
|
|
indvdum@0
|
2164 |
{
|
|
indvdum@0
|
2165 |
$line .= ' SUB_TYPE TEXT';
|
|
indvdum@0
|
2166 |
}
|
|
indvdum@0
|
2167 |
|
|
indvdum@0
|
2168 |
if ($row['ftype'] == 37 || $row['ftype'] == 14)
|
|
indvdum@0
|
2169 |
{
|
|
indvdum@0
|
2170 |
$line .= ' (' . $row['flen'] . ')';
|
|
indvdum@0
|
2171 |
}
|
|
indvdum@0
|
2172 |
|
|
indvdum@0
|
2173 |
if (!empty($row['dsource']))
|
|
indvdum@0
|
2174 |
{
|
|
indvdum@0
|
2175 |
$line .= ' ' . $row['dsource'];
|
|
indvdum@0
|
2176 |
}
|
|
indvdum@0
|
2177 |
|
|
indvdum@0
|
2178 |
if (!empty($row['nflag']))
|
|
indvdum@0
|
2179 |
{
|
|
indvdum@0
|
2180 |
$line .= ' NOT NULL';
|
|
indvdum@0
|
2181 |
}
|
|
indvdum@0
|
2182 |
$rows[] = $line;
|
|
indvdum@0
|
2183 |
}
|
|
indvdum@0
|
2184 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
2185 |
|
|
indvdum@0
|
2186 |
$sql_data .= implode(",\n", $rows);
|
|
indvdum@0
|
2187 |
$sql_data .= "\n);\n";
|
|
indvdum@0
|
2188 |
$keys = array();
|
|
indvdum@0
|
2189 |
|
|
indvdum@0
|
2190 |
$sql = 'SELECT I.RDB$FIELD_NAME as NAME
|
|
indvdum@0
|
2191 |
FROM RDB$RELATION_CONSTRAINTS RC, RDB$INDEX_SEGMENTS I, RDB$INDICES IDX
|
|
indvdum@0
|
2192 |
WHERE (I.RDB$INDEX_NAME = RC.RDB$INDEX_NAME)
|
|
indvdum@0
|
2193 |
AND (IDX.RDB$INDEX_NAME = RC.RDB$INDEX_NAME)
|
|
indvdum@0
|
2194 |
AND (RC.RDB$RELATION_NAME = \''. $table_name . '\')
|
|
indvdum@0
|
2195 |
ORDER BY I.RDB$FIELD_POSITION';
|
|
indvdum@0
|
2196 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
2197 |
|
|
indvdum@0
|
2198 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
2199 |
{
|
|
indvdum@0
|
2200 |
$keys[] = $row['name'];
|
|
indvdum@0
|
2201 |
}
|
|
indvdum@0
|
2202 |
|
|
indvdum@0
|
2203 |
if (sizeof($keys))
|
|
indvdum@0
|
2204 |
{
|
|
indvdum@0
|
2205 |
$sql_data .= "\nALTER TABLE $table_name ADD PRIMARY KEY (" . implode(', ', $keys) . ');';
|
|
indvdum@0
|
2206 |
}
|
|
indvdum@0
|
2207 |
|
|
indvdum@0
|
2208 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
2209 |
|
|
indvdum@0
|
2210 |
$sql = 'SELECT I.RDB$INDEX_NAME as INAME, I.RDB$UNIQUE_FLAG as UFLAG, S.RDB$FIELD_NAME as FNAME
|
|
indvdum@0
|
2211 |
FROM RDB$INDICES I JOIN RDB$INDEX_SEGMENTS S ON S.RDB$INDEX_NAME=I.RDB$INDEX_NAME
|
|
indvdum@0
|
2212 |
WHERE (I.RDB$SYSTEM_FLAG IS NULL OR I.RDB$SYSTEM_FLAG=0)
|
|
indvdum@0
|
2213 |
AND I.RDB$FOREIGN_KEY IS NULL
|
|
indvdum@0
|
2214 |
AND I.RDB$RELATION_NAME = \''. $table_name . '\'
|
|
indvdum@0
|
2215 |
AND I.RDB$INDEX_NAME NOT STARTING WITH \'RDB$\'
|
|
indvdum@0
|
2216 |
ORDER BY S.RDB$FIELD_POSITION';
|
|
indvdum@0
|
2217 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
2218 |
|
|
indvdum@0
|
2219 |
$index = array();
|
|
indvdum@0
|
2220 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
2221 |
{
|
|
indvdum@0
|
2222 |
$index[$row['iname']]['unique'] = !empty($row['uflag']);
|
|
indvdum@0
|
2223 |
$index[$row['iname']]['values'][] = $row['fname'];
|
|
indvdum@0
|
2224 |
}
|
|
indvdum@0
|
2225 |
|
|
indvdum@0
|
2226 |
foreach ($index as $index_name => $data)
|
|
indvdum@0
|
2227 |
{
|
|
indvdum@0
|
2228 |
$sql_data .= "\nCREATE ";
|
|
indvdum@0
|
2229 |
if ($data['unique'])
|
|
indvdum@0
|
2230 |
{
|
|
indvdum@0
|
2231 |
$sql_data .= 'UNIQUE ';
|
|
indvdum@0
|
2232 |
}
|
|
indvdum@0
|
2233 |
$sql_data .= "INDEX $index_name ON $table_name(" . implode(', ', $data['values']) . ");";
|
|
indvdum@0
|
2234 |
}
|
|
indvdum@0
|
2235 |
$sql_data .= "\n";
|
|
indvdum@0
|
2236 |
|
|
indvdum@0
|
2237 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
2238 |
|
|
indvdum@0
|
2239 |
$sql = 'SELECT D1.RDB$DEPENDENT_NAME as DNAME, D1.RDB$FIELD_NAME as FNAME, D1.RDB$DEPENDENT_TYPE, R1.RDB$RELATION_NAME
|
|
indvdum@0
|
2240 |
FROM RDB$DEPENDENCIES D1
|
|
indvdum@0
|
2241 |
LEFT JOIN RDB$RELATIONS R1 ON ((D1.RDB$DEPENDENT_NAME = R1.RDB$RELATION_NAME) AND (NOT (R1.RDB$VIEW_BLR IS NULL)))
|
|
indvdum@0
|
2242 |
WHERE (D1.RDB$DEPENDED_ON_TYPE = 0)
|
|
indvdum@0
|
2243 |
AND (D1.RDB$DEPENDENT_TYPE <> 3)
|
|
indvdum@0
|
2244 |
AND (D1.RDB$DEPENDED_ON_NAME = \'' . $table_name . '\')
|
|
indvdum@0
|
2245 |
UNION SELECT DISTINCT F2.RDB$RELATION_NAME, D2.RDB$FIELD_NAME, D2.RDB$DEPENDENT_TYPE, R2.RDB$RELATION_NAME FROM RDB$DEPENDENCIES D2, RDB$RELATION_FIELDS F2
|
|
indvdum@0
|
2246 |
LEFT JOIN RDB$RELATIONS R2 ON ((F2.RDB$RELATION_NAME = R2.RDB$RELATION_NAME) AND (NOT (R2.RDB$VIEW_BLR IS NULL)))
|
|
indvdum@0
|
2247 |
WHERE (D2.RDB$DEPENDENT_TYPE = 3)
|
|
indvdum@0
|
2248 |
AND (D2.RDB$DEPENDENT_NAME = F2.RDB$FIELD_SOURCE)
|
|
indvdum@0
|
2249 |
AND (D2.RDB$DEPENDED_ON_NAME = \'' . $table_name . '\')
|
|
indvdum@0
|
2250 |
ORDER BY 1, 2';
|
|
indvdum@0
|
2251 |
$result = $db->sql_query($sql);
|
|
indvdum@0
|
2252 |
while ($row = $db->sql_fetchrow($result))
|
|
indvdum@0
|
2253 |
{
|
|
indvdum@0
|
2254 |
$sql = 'SELECT T1.RDB$DEPENDED_ON_NAME as GEN, T1.RDB$FIELD_NAME, T1.RDB$DEPENDED_ON_TYPE
|
|
indvdum@0
|
2255 |
FROM RDB$DEPENDENCIES T1
|
|
indvdum@0
|
2256 |
WHERE (T1.RDB$DEPENDENT_NAME = \'' . $row['dname'] . '\')
|
|
indvdum@0
|
2257 |
AND (T1.RDB$DEPENDENT_TYPE = 2 AND T1.RDB$DEPENDED_ON_TYPE = 14)
|
|
indvdum@0
|
2258 |
UNION ALL SELECT DISTINCT D.RDB$DEPENDED_ON_NAME, D.RDB$FIELD_NAME, D.RDB$DEPENDED_ON_TYPE
|
|
indvdum@0
|
2259 |
FROM RDB$DEPENDENCIES D, RDB$RELATION_FIELDS F
|
|
indvdum@0
|
2260 |
WHERE (D.RDB$DEPENDENT_TYPE = 3)
|
|
indvdum@0
|
2261 |
AND (D.RDB$DEPENDENT_NAME = F.RDB$FIELD_SOURCE)
|
|
indvdum@0
|
2262 |
AND (F.RDB$RELATION_NAME = \'' . $row['dname'] . '\')
|
|
indvdum@0
|
2263 |
ORDER BY 1,2';
|
|
indvdum@0
|
2264 |
$result2 = $db->sql_query($sql);
|
|
indvdum@0
|
2265 |
$row2 = $db->sql_fetchrow($result2);
|
|
indvdum@0
|
2266 |
$db->sql_freeresult($result2);
|
|
indvdum@0
|
2267 |
$gen_name = $row2['gen'];
|
|
indvdum@0
|
2268 |
|
|
indvdum@0
|
2269 |
$sql_data .= "\nDROP GENERATOR " . $gen_name . ";";
|
|
indvdum@0
|
2270 |
$sql_data .= "\nSET TERM ^ ;";
|
|
indvdum@0
|
2271 |
$sql_data .= "\nCREATE GENERATOR " . $gen_name . "^";
|
|
indvdum@0
|
2272 |
$sql_data .= "\nSET GENERATOR " . $gen_name . " TO 0^\n";
|
|
indvdum@0
|
2273 |
$sql_data .= "\nCREATE TRIGGER {$row['dname']} FOR $table_name";
|
|
indvdum@0
|
2274 |
$sql_data .= "\nBEFORE INSERT\nAS\nBEGIN";
|
|
indvdum@0
|
2275 |
$sql_data .= "\n NEW.{$row['fname']} = GEN_ID(" . $gen_name . ", 1);";
|
|
indvdum@0
|
2276 |
$sql_data .= "\nEND^\n";
|
|
indvdum@0
|
2277 |
$sql_data .= "\nSET TERM ; ^\n";
|
|
indvdum@0
|
2278 |
}
|
|
indvdum@0
|
2279 |
|
|
indvdum@0
|
2280 |
$this->flush($sql_data);
|
|
indvdum@0
|
2281 |
|
|
indvdum@0
|
2282 |
$db->sql_freeresult($result);
|
|
indvdum@0
|
2283 |
}
|
|
indvdum@0
|
2284 |
}
|
|
indvdum@0
|
2285 |
|
|
indvdum@0
|
2286 |
// get how much space we allow for a chunk of data, very similar to phpMyAdmin's way of doing things ;-) (hey, we only do this for MySQL anyway :P)
|
|
indvdum@0
|
2287 |
function get_usable_memory()
|
|
indvdum@0
|
2288 |
{
|
|
indvdum@0
|
2289 |
$val = trim(@ini_get('memory_limit'));
|
|
indvdum@0
|
2290 |
|
|
indvdum@0
|
2291 |
if (preg_match('/(\\d+)([mkg]?)/i', $val, $regs))
|
|
indvdum@0
|
2292 |
{
|
|
indvdum@0
|
2293 |
$memory_limit = (int) $regs[1];
|
|
indvdum@0
|
2294 |
switch ($regs[2])
|
|
indvdum@0
|
2295 |
{
|
|
indvdum@0
|
2296 |
|
|
indvdum@0
|
2297 |
case 'k':
|
|
indvdum@0
|
2298 |
case 'K':
|
|
indvdum@0
|
2299 |
$memory_limit *= 1024;
|
|
indvdum@0
|
2300 |
break;
|
|
indvdum@0
|
2301 |
|
|
indvdum@0
|
2302 |
case 'm':
|
|
indvdum@0
|
2303 |
case 'M':
|
|
indvdum@0
|
2304 |
$memory_limit *= 1048576;
|
|
indvdum@0
|
2305 |
break;
|
|
indvdum@0
|
2306 |
|
|
indvdum@0
|
2307 |
case 'g':
|
|
indvdum@0
|
2308 |
case 'G':
|
|
indvdum@0
|
2309 |
$memory_limit *= 1073741824;
|
|
indvdum@0
|
2310 |
break;
|
|
indvdum@0
|
2311 |
}
|
|
indvdum@0
|
2312 |
|
|
indvdum@0
|
2313 |
// how much memory PHP requires at the start of export (it is really a little less)
|
|
indvdum@0
|
2314 |
if ($memory_limit > 6100000)
|
|
indvdum@0
|
2315 |
{
|
|
indvdum@0
|
2316 |
$memory_limit -= 6100000;
|
|
indvdum@0
|
2317 |
}
|
|
indvdum@0
|
2318 |
|
|
indvdum@0
|
2319 |
// allow us to consume half of the total memory available
|
|
indvdum@0
|
2320 |
$memory_limit /= 2;
|
|
indvdum@0
|
2321 |
}
|
|
indvdum@0
|
2322 |
else
|
|
indvdum@0
|
2323 |
{
|
|
indvdum@0
|
2324 |
// set the buffer to 1M if we have no clue how much memory PHP will give us :P
|
|
indvdum@0
|
2325 |
$memory_limit = 1048576;
|
|
indvdum@0
|
2326 |
}
|
|
indvdum@0
|
2327 |
|
|
indvdum@0
|
2328 |
return $memory_limit;
|
|
indvdum@0
|
2329 |
}
|
|
indvdum@0
|
2330 |
|
|
indvdum@0
|
2331 |
function sanitize_data_mssql($text)
|
|
indvdum@0
|
2332 |
{
|
|
indvdum@0
|
2333 |
$data = preg_split('/[\n\t\r\b\f]/', $text);
|
|
indvdum@0
|
2334 |
preg_match_all('/[\n\t\r\b\f]/', $text, $matches);
|
|
indvdum@0
|
2335 |
|
|
indvdum@0
|
2336 |
$val = array();
|
|
indvdum@0
|
2337 |
|
|
indvdum@0
|
2338 |
foreach ($data as $value)
|
|
indvdum@0
|
2339 |
{
|
|
indvdum@0
|
2340 |
if (strlen($value))
|
|
indvdum@0
|
2341 |
{
|
|
indvdum@0
|
2342 |
$val[] = "'" . $value . "'";
|
|
indvdum@0
|
2343 |
}
|
|
indvdum@0
|
2344 |
if (sizeof($matches[0]))
|
|
indvdum@0
|
2345 |
{
|
|
indvdum@0
|
2346 |
$val[] = 'char(' . ord(array_shift($matches[0])) . ')';
|
|
indvdum@0
|
2347 |
}
|
|
indvdum@0
|
2348 |
}
|
|
indvdum@0
|
2349 |
|
|
indvdum@0
|
2350 |
return implode('+', $val);
|
|
indvdum@0
|
2351 |
}
|
|
indvdum@0
|
2352 |
|
|
indvdum@0
|
2353 |
function sanitize_data_oracle($text)
|
|
indvdum@0
|
2354 |
{
|
|
indvdum@0
|
2355 |
// $data = preg_split('/[\0\n\t\r\b\f\'"\/\\\]/', $text);
|
|
indvdum@0
|
2356 |
// preg_match_all('/[\0\n\t\r\b\f\'"\/\\\]/', $text, $matches);
|
|
indvdum@0
|
2357 |
$data = preg_split('/[\0\b\f\'\/]/', $text);
|
|
indvdum@0
|
2358 |
preg_match_all('/[\0\r\b\f\'\/]/', $text, $matches);
|
|
indvdum@0
|
2359 |
|
|
indvdum@0
|
2360 |
$val = array();
|
|
indvdum@0
|
2361 |
|
|
indvdum@0
|
2362 |
foreach ($data as $value)
|
|
indvdum@0
|
2363 |
{
|
|
indvdum@0
|
2364 |
if (strlen($value))
|
|
indvdum@0
|
2365 |
{
|
|
indvdum@0
|
2366 |
$val[] = "'" . $value . "'";
|
|
indvdum@0
|
2367 |
}
|
|
indvdum@0
|
2368 |
if (sizeof($matches[0]))
|
|
indvdum@0
|
2369 |
{
|
|
indvdum@0
|
2370 |
$val[] = 'chr(' . ord(array_shift($matches[0])) . ')';
|
|
indvdum@0
|
2371 |
}
|
|
indvdum@0
|
2372 |
}
|
|
indvdum@0
|
2373 |
|
|
indvdum@0
|
2374 |
return implode('||', $val);
|
|
indvdum@0
|
2375 |
}
|
|
indvdum@0
|
2376 |
|
|
indvdum@0
|
2377 |
function sanitize_data_generic($text)
|
|
indvdum@0
|
2378 |
{
|
|
indvdum@0
|
2379 |
$data = preg_split('/[\n\t\r\b\f]/', $text);
|
|
indvdum@0
|
2380 |
preg_match_all('/[\n\t\r\b\f]/', $text, $matches);
|
|
indvdum@0
|
2381 |
|
|
indvdum@0
|
2382 |
$val = array();
|
|
indvdum@0
|
2383 |
|
|
indvdum@0
|
2384 |
foreach ($data as $value)
|
|
indvdum@0
|
2385 |
{
|
|
indvdum@0
|
2386 |
if (strlen($value))
|
|
indvdum@0
|
2387 |
{
|
|
indvdum@0
|
2388 |
$val[] = "'" . $value . "'";
|
|
indvdum@0
|
2389 |
}
|
|
indvdum@0
|
2390 |
if (sizeof($matches[0]))
|
|
indvdum@0
|
2391 |
{
|
|
indvdum@0
|
2392 |
$val[] = "'" . array_shift($matches[0]) . "'";
|
|
indvdum@0
|
2393 |
}
|
|
indvdum@0
|
2394 |
}
|
|
indvdum@0
|
2395 |
|
|
indvdum@0
|
2396 |
return implode('||', $val);
|
|
indvdum@0
|
2397 |
}
|
|
indvdum@0
|
2398 |
|
|
indvdum@0
|
2399 |
// modified from PHP.net
|
|
indvdum@0
|
2400 |
function fgetd(&$fp, $delim, $read, $seek, $eof, $buffer = 8192)
|
|
indvdum@0
|
2401 |
{
|
|
indvdum@0
|
2402 |
$record = '';
|
|
indvdum@0
|
2403 |
$delim_len = strlen($delim);
|
|
indvdum@0
|
2404 |
|
|
indvdum@0
|
2405 |
while (!$eof($fp))
|
|
indvdum@0
|
2406 |
{
|
|
indvdum@0
|
2407 |
$pos = strpos($record, $delim);
|
|
indvdum@0
|
2408 |
if ($pos === false)
|
|
indvdum@0
|
2409 |
{
|
|
indvdum@0
|
2410 |
$record .= $read($fp, $buffer);
|
|
indvdum@0
|
2411 |
if ($eof($fp) && ($pos = strpos($record, $delim)) !== false)
|
|
indvdum@0
|
2412 |
{
|
|
indvdum@0
|
2413 |
$seek($fp, $pos + $delim_len - strlen($record), SEEK_CUR);
|
|
indvdum@0
|
2414 |
return substr($record, 0, $pos);
|
|
indvdum@0
|
2415 |
}
|
|
indvdum@0
|
2416 |
}
|
|
indvdum@0
|
2417 |
else
|
|
indvdum@0
|
2418 |
{
|
|
indvdum@0
|
2419 |
$seek($fp, $pos + $delim_len - strlen($record), SEEK_CUR);
|
|
indvdum@0
|
2420 |
return substr($record, 0, $pos);
|
|
indvdum@0
|
2421 |
}
|
|
indvdum@0
|
2422 |
}
|
|
indvdum@0
|
2423 |
|
|
indvdum@0
|
2424 |
return false;
|
|
indvdum@0
|
2425 |
}
|
|
indvdum@0
|
2426 |
|
|
indvdum@0
|
2427 |
function fgetd_seekless(&$fp, $delim, $read, $seek, $eof, $buffer = 8192)
|
|
indvdum@0
|
2428 |
{
|
|
indvdum@0
|
2429 |
static $array = array();
|
|
indvdum@0
|
2430 |
static $record = '';
|
|
indvdum@0
|
2431 |
|
|
indvdum@0
|
2432 |
if (!sizeof($array))
|
|
indvdum@0
|
2433 |
{
|
|
indvdum@0
|
2434 |
while (!$eof($fp))
|
|
indvdum@0
|
2435 |
{
|
|
indvdum@0
|
2436 |
if (strpos($record, $delim) !== false)
|
|
indvdum@0
|
2437 |
{
|
|
indvdum@0
|
2438 |
$array = explode($delim, $record);
|
|
indvdum@0
|
2439 |
$record = array_pop($array);
|
|
indvdum@0
|
2440 |
break;
|
|
indvdum@0
|
2441 |
}
|
|
indvdum@0
|
2442 |
else
|
|
indvdum@0
|
2443 |
{
|
|
indvdum@0
|
2444 |
$record .= $read($fp, $buffer);
|
|
indvdum@0
|
2445 |
}
|
|
indvdum@0
|
2446 |
}
|
|
indvdum@0
|
2447 |
if ($eof($fp) && strpos($record, $delim) !== false)
|
|
indvdum@0
|
2448 |
{
|
|
indvdum@0
|
2449 |
$array = explode($delim, $record);
|
|
indvdum@0
|
2450 |
$record = array_pop($array);
|
|
indvdum@0
|
2451 |
}
|
|
indvdum@0
|
2452 |
}
|
|
indvdum@0
|
2453 |
|
|
indvdum@0
|
2454 |
if (sizeof($array))
|
|
indvdum@0
|
2455 |
{
|
|
indvdum@0
|
2456 |
return array_shift($array);
|
|
indvdum@0
|
2457 |
}
|
|
indvdum@0
|
2458 |
|
|
indvdum@0
|
2459 |
return false;
|
|
indvdum@0
|
2460 |
}
|
|
indvdum@0
|
2461 |
|
|
indvdum@0
|
2462 |
?> |