Menu

[r11]: / includes / phppgadmin / db_dump.php  Maximize  Restore  History

Download this file

325 lines (276 with data), 8.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/* $Id: db_dump.php,v 1.1 2003/07/02 14:47:06 fullo Exp $ */
$crlf="\n";
if (empty($asfile)) {
include("header.inc.php");
print "<div align=left><pre>\n";
} else {
include("lib.inc.php");
header("Content-disposition: attachment; filename=\"$db.sql\"");
header("Content-type: application/octetstream");
header("Pragma: no-cache");
header("Expires: 0");
// doing some DOS-CRLF magic...
$client=getenv("HTTP_USER_AGENT");
if (ereg('[^(]*\((.*)\)[^)]*',$client,$regs)) {
$os = $regs[1];
// this looks better under WinX
if (eregi("Win",$os)) $crlf="\r\n";
}
}
function my_handler($sql_insert) {
global $crlf, $asfile;
if (empty($asfile)) {
echo htmlspecialchars("$sql_insert;$crlf");
} else {
echo "$sql_insert;$crlf";
}
}
print "$crlf/* -------------------------------------------------------- $crlf";
print " $cfgProgName $cfgVersion DB Dump$crlf";
print " http://sourceforge.net/projects/phppgadmin/$crlf";
print " $strHost: " . $cfgServer['host'];
if (!empty($cfgServer['port'])) {
print ":" . $cfgServer['port'];
}
print "$crlf $strDatabase : $cfgQuotes$db$cfgQuotes$crlf";
print " " . date("Y-m-d H:m:i") . $crlf;
print "-------------------------------------------------------- */ $crlf";
$get_seq_sql = "
SELECT relname
FROM pg_class
WHERE
NOT relname ~ 'pg_.*'
AND relkind ='S'
ORDER BY relname
";
$seq = @pg_exec($link, pre_query($get_seq_sql));
if (!$num_seq = @pg_numrows($seq)) {
print "/* $strNo $strSequences $strFound */";
} else {
print "$crlf/* -------------------------------------------------------- $crlf";
print " $strSequences $crlf";
print "-------------------------------------------------------- */ $crlf";
while ($i_seq < $num_seq) {
$sequence = @pg_result($seq, $i_seq, "relname");
$sql_get_props = "SELECT * FROM $cfgQuotes$sequence$cfgQuotes";
$seq_props = @pg_exec($link, pre_query($sql_get_props));
if (@pg_numrows($seq_props)) {
$row = @pg_fetch_array($seq_props, 0);
if ($what != "data") {
$row[last_value] = 1;
}
if ($drop) print "DROP SEQUENCE $cfgQuotes$sequence$cfgQuotes;$crlf";
print "CREATE SEQUENCE $cfgQuotes$sequence$cfgQuotes START $row[last_value] INCREMENT $row[increment_by] MAXVALUE $row[max_value] MINVALUE $row[min_value] CACHE $row[cache_value]; $crlf";
}
if (($row[last_value] > 1) && ($what == "data")) {
print "SELECT NEXTVAL('$sequence'); $crlf";
unset($row[last_value]);
}
$i_seq++;
}
}
$tables = @pg_exec($link, "SELECT tablename FROM pg_tables WHERE tablename !~ 'pg_.*' ORDER BY tablename");
$num_tables = @pg_numrows($tables);
if (!$num_tables) {
echo $strNoTablesFound;
} else {
for ($i = 0; $i < $num_tables; $i++) {
$table = pg_result($tables, $i, "tablename");
print "$crlf/* -------------------------------------------------------- $crlf";
print " $strTableStructure $cfgQuotes$table$cfgQuotes $crlf";
print "-------------------------------------------------------- */";
echo $crlf;
if ($drop) print "DROP TABLE $cfgQuotes$table$cfgQuotes;$crlf";
if (!$asfile) {
echo htmlentities(get_table_def($link, $table, $crlf));
} else {
echo get_table_def($link, $table, $crlf);
}
echo $crlf;
if ($what == "data") {
print "$crlf/* -------------------------------------------------------- $crlf";
print " $strDumpingData $cfgQuotes$table$cfgQuotes $crlf";
print "-------------------------------------------------------- */ $crlf";
get_table_content($link, $table, "my_handler");
}
}
}
// tablename !~ 'pg_.*'
$sql_get_views = "SELECT * FROM pg_views WHERE viewname !~ 'pg_.*'";
$views = @pg_exec($link, pre_query($sql_get_views));
if (!$num_views = @pg_numrows($views)) {
print "$crlf/* $strNo $strViews $strFound */$crlf";
} else {
print "$crlf/* -------------------------------------------------------- $crlf";
print " $strViews $crlf";
print "-------------------------------------------------------- */ $crlf";
for ($i_views = 0; $i_views < $num_views; $i_views++) {
$view = pg_fetch_array($views, $i_views);
if ($drop) print "DROP VIEW $cfgQuotes$view[viewname]$cfgQuotes;$crlf";
print "CREATE VIEW $cfgQuotes$view[viewname]$cfgQuotes AS $view[definition] $crlf";
}
}
// Output functions
// Max built-in oidi
//$sql_get_max = "SELECT oid FROM pg_database WHERE datname = 'template1'";
//$maxes = pg_exec($link, $sql_get_max);
//$max = pg_result($maxes, 0, "oid");
//$max = $row[datlastsysoid];
//$max = 16384;
$max = $builtin_max;
// Skips system functions
if ($common_ver < 7.1)
{
$sql_get_funcs = "
SELECT
pc.oid,
proname,
lanname as language,
t.typname as return_type,
prosrc as source,
probin as binary,
oidvectortypes(pc.proargtypes) AS arguments
FROM
pg_proc pc, pg_language pl, pg_type t
WHERE
pc.oid > '$max'::oid
AND pc.prolang = pl.oid
";
}
else
{
$sql_get_funcs = "
SELECT
pc.oid,
proname,
lanname as language,
format_type(prorettype, NULL) as return_type,
prosrc as source,
probin as binary,
oidvectortypes(pc.proargtypes) AS arguments
FROM
pg_proc pc, pg_language pl
WHERE
pc.oid > '$max'::oid
AND pc.prolang = pl.oid
";
}
print $crlf;
$funcs = pg_exec($link, pre_query($sql_get_funcs)) or pg_die(pg_errormessage(), $sql_get_funcs, __FILE__, __LINE__);
if (!$num_funcs = pg_numrows($funcs)) {
print "/* $strNo $strFuncs $strFound */$crlf";
} else {
print "$crlf/* -------------------------------------------------------- $crlf";
print " $strFuncs $crlf";
print "-------------------------------------------------------- */ $crlf";
for ($i_funcs = 0; $i_funcs < $num_funcs; $i_funcs++) {
$func_info = @pg_fetch_array($funcs, $i_funcs);
if ($common_ver < 7.1) {
$strArgList = ereg_replace(" ", ", ", $func_info[arguments]);
} else {
$strArgList = $func_info[arguments];
}
if ($func_info[binary] != "-") {
$strBin = "'$func_info[binary]',";
} else {
unset($strBin);
}
if ($func_info[return_type] == "-") {
$func_info[return_type] = "OPAQUE";
}
if ($drop) print "DROP FUNCTION $cfgQuotes$func_info[proname]$cfgQuotes($strArgList);$crlf";
echo "CREATE FUNCTION $cfgQuotes$func_info[proname]$cfgQuotes($strArgList) RETURNS $func_info[return_type] AS $strBin'$func_info[source]' LANGUAGE '$func_info[language]'; $crlf";
}
}
// Output triggers
// Some definitions
$TRIGGER_TYPE_ROW = (1 << 0);
$TRIGGER_TYPE_BEFORE = (1 << 1);
$TRIGGER_TYPE_INSERT = (1 << 2);
$TRIGGER_TYPE_DELETE = (1 << 3);
$TRIGGER_TYPE_UPDATE = (1 << 4);
$sql_get_triggers = "
SELECT
pt.*, pp.proname, pc.relname
FROM
pg_trigger pt, pg_proc pp, pg_class pc
WHERE
pp.oid=pt.tgfoid
and pt.tgrelid=pc.oid
and relname !~ '^pg_'
";
$triggers = @pg_exec($link, pre_query($sql_get_triggers));
if (!$num_triggers = @pg_numrows($triggers)) {
print "$crlf/* $strNo $strTriggers $strFound */$crlf";
} else {
print "$crlf/* -------------------------------------------------------- $crlf";
print " $strTriggers $crlf";
print "-------------------------------------------------------- */ $crlf";
for ($i_triggers = 0; $i_triggers < $num_triggers; $i_triggers++) {
$trigger = pg_fetch_array($triggers, $i_triggers);
// Constraint or not
if ($trigger[tgisconstraint] == 't')
print "CREATE CONSTRAINT TRIGGER";
else
print "CREATE TRIGGER";
// Name
print " $cfgQuotes$trigger[tgname]$cfgQuotes";
// before/after
if ($trigger[tgtype] & $TRIGGER_TYPE_BEFORE)
print " BEFORE";
else
print " AFTER";
// Insert
$findx = 0;
if ($trigger[tgtype] & $TRIGGER_TYPE_INSERT) {
print " INSERT";
$findx++;
}
// Delete
if ($trigger[tgtype] & $TRIGGER_TYPE_DELETE) {
if ($findx > 0)
print " OR DELETE";
else
print " DELETE";
$findx++;
}
// Update
if ($trigger[tgtype] & $TRIGGER_TYPE_UPDATE) {
if ($findx > 0)
print " OR UPDATE";
else
print " UPDATE";
}
// On
print " ON $cfgQuotes$trigger[relname]$cfgQuotes";
// Contraints, deferrable
if ($trigger[tgisconstraint] == 't') {
if ($trigger[tgdeferrable] == 'f') print " NOT";
print " DEFERRABLE INITIALLY ";
if ($trigger[tginitdeferred] == 't')
print "DEFERRED";
else
print "IMMEDIATE";
}
echo " FOR EACH ROW";
echo " EXECUTE PROCEDURE $cfgQuotes$trigger[proname]$cfgQuotes ('";
// Strip of trailing delimiter
$tgargs = trim(substr($trigger[tgargs], 0, strlen($trigger[tgargs]) - 4));
$params = explode('\000', $tgargs);
for ($i = 0; $i < sizeof($params); $i++) {
$params[$i] = str_replace("'", "\\'", $params[$i]);
}
$params = implode("', '", $params);
if ($asfile) {
echo htmlspecialchars($params), "');$crlf";
} else {
echo $params, "');$crlf";
}
}
}
if(empty($asfile)) {
print "</pre></div>\n";
include ("footer.inc.php");
}
?>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.