Repository navigation
Expand file tree
/
Copy pathsql.php
More file actions
350 lines (309 loc) · 9.33 KB
/
Copy pathsql.php
File metadata and controls
350 lines (309 loc) · 9.33 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
use PhpPgAdmin\Core\AppContainer;
use PhpPgAdmin\Database\Import\SqlParser;
use PhpPgAdmin\Database\QueryResult;
use PhpPgAdmin\Database\Actions\TypeActions;
use PhpPgAdmin\Database\Actions\SchemaActions;
use PhpPgAdmin\Database\Actions\ScriptActions;
/**
* Process an arbitrary SQL script of statements!
*
* $Id: sql.php,v 1.43 2008/01/10 20:19:27 xzilla Exp $
*/
// Prevent timeouts on large scripts (non-safe mode only)
if (!ini_get('safe_mode'))
set_time_limit(0);
// Include application functions
include_once('./libraries/bootstrap.php');
/**
* Render query results to HTML
* @param QueryResult $result The wrapped query result
* @param string $query The SQL query that was executed
* @param string|null $lineInfo Optional line info for error reporting
*/
function renderQueryResult($result, $query, $lineInfo = null)
{
$misc = AppContainer::getMisc();
$lang = AppContainer::getLang();
$pg = AppContainer::getPostgres();
/*
if (!$result->isSuccess) {
echo nl2br(html_esc($result->errorMsg)), "<br/>\n";
return;
}
*/
// Get ADODB-compatible adapter (handles both ADODB and pg_* results)
$rs = $result->getAdapterForResults();
echo "<div class=\"query-box mb-2\">\n";
echo "<pre class=\"sql-viewer\">" . htmlspecialchars($query) . "</pre>\n";
if (!$result->isSuccess) {
echo "<div class=\"error\">", nl2br(html_esc($result->errorMsg)), "</div>\n";
}
echo "<div class=\"query-stats\">\n";
if ($lineInfo !== null) {
echo "<span class=\"mr-1\">", html_esc($lineInfo), ",</span>";
}
if ($result->affectedRows() > 0) {
echo $result->affectedRows(), " {$lang['strrowsaff']}\n";
} else {
echo $result->recordCount(), " {$lang['strrows']}\n";
}
echo "</div>\n";
echo "</div>\n";
if (!$result->isSuccess || $rs === null || $result->recordCount() <= 0) {
return;
}
$pg->conn->SetFetchMode(ADODB_FETCH_ASSOC);
$typeNames = [];
for ($i = 0; $i < $rs->fieldCount(); $i++) {
$finfo = $rs->fetchField($i);
$typeNames[] = $finfo->type;
}
$typeActions = new TypeActions($pg);
$typesMeta = $typeActions->getTypeMetasByNames($typeNames);
echo "<table class=\"data query-result mb-1\">\n";
echo "<colgroup>\n";
foreach ($rs->fields as $k => $v) {
$finfo = $rs->fetchField($k);
echo "<col class=\"{$finfo->type}\" />\n";
}
echo "</colgroup>\n";
echo "<thead class=\"sticky-thead\">\n";
echo "<tr>\n";
foreach ($rs->fields as $k => $v) {
$finfo = $rs->fetchField($k);
$typeMeta = $typesMeta[$finfo->type] ?? null;
$isLargeType = $typeMeta !== null && $typeActions->isLargeTypeMeta($typeMeta);
$class = 'data';
if ($isLargeType) {
$class .= ' large_type';
}
echo "<th class=\"{$class}\">", $misc->formatVal($finfo->name), "</th>\n";
}
echo "</tr>\n";
echo "</thead>\n";
echo "<tbody>\n";
$i = 0;
while (!$rs->EOF) {
$id = (($i & 1) == 0 ? '1' : '2');
echo "<tr class=\"data{$id} data-row\">\n";
foreach ($rs->fields as $k => $v) {
$finfo = $rs->fetchField($k);
$isArray = substr_compare($finfo->type, '_', 0, 1) === 0;
$class = $isArray ? "array" : "no-array";
$hasLineBreak = isset($v) && str_contains($v, "\n");
$class .= $hasLineBreak ? " line-break" : " no-line-break";
if (!$hasLineBreak && is_string($v)) {
if (strlen($v) > 2100) {
$class .= " full-width";
} elseif (strlen($v) > 1050) {
$class .= " large-width";
} elseif (strlen($v) > 500) {
$class .= " medium-width";
} elseif (strlen($v) > 100) {
$class .= " small-width";
}
}
echo "<td class=\"auto-wrap field $finfo->type $class\">\n";
echo "<div class=\"wrapper\">\n";
echo $misc->formatVal($v, $finfo->type, array('null' => true));
echo "</div>\n";
echo "</td>\n";
}
echo "</tr>\n";
$rs->moveNext();
$i++;
}
echo "</tbody>\n";
echo "</table>\n";
//echo "<p>", $i, " {$lang['strrows']}</p>\n";
}
/**
* This is a callback function to display the result of each separate query
* @param string $query The SQL query that was executed
* @param QueryResult $result The wrapped query result from script executor
* @param int $lineno The line number in the script file
*/
$sqlCallback = function ($query, $result, $lineno) {
$lineInfo = $_FILES['script']['name'] . ':' . $lineno;
renderQueryResult($result, $query, $lineInfo);
};
$lang = AppContainer::getLang();
$misc = AppContainer::getMisc();
$pg = AppContainer::getPostgres();
$schemaActions = new SchemaActions($pg);
$subject = $_REQUEST['subject'] ?? '';
// We need to store the query in a session for editing purposes
// We avoid GPC vars to avoid truncating long queries
if ($subject == 'history') {
// Or maybe we came from the history popup
$_SESSION['sqlquery'] = $_SESSION['history'][$_REQUEST['server']][$_REQUEST['database']][$_GET['queryid']]['query'];
} elseif (isset($_REQUEST['query'])) {
// Or maybe we came from an sql form
$_SESSION['sqlquery'] = $_REQUEST['query'];
} else {
echo "could not find the query!!";
exit;
}
$isUpload = isset($_FILES['script']) && $_FILES['script']['size'] > 0;
$canPaginate = !$isUpload;
$hasNonReadQueries = $isUpload;
if (!$isUpload) {
$script = trim($_SESSION['sqlquery']);
if (substr($script, -1) !== ';') {
$script .= ';';
}
$result = SqlParser::parseFromString($script);
$statements = array_column(
array_filter(
$result['items'],
function ($item) {
return $item['type'] === 'statement';
}
),
'content'
);
$readQueryCount = 0;
foreach ($statements as $stmt) {
if (is_result_set_query($stmt)) {
$readQueryCount++;
} else {
$hasNonReadQueries = true;
}
}
$canPaginate = ($readQueryCount === 1);
}
// Pagination maybe set by a get link that has it as FALSE,
// if that's the case, unset the variable.
/*
if (isset($_REQUEST['paginate']) && $_REQUEST['paginate'] == 'f') {
unset($_REQUEST['paginate']);
unset($_POST['paginate']);
unset($_GET['paginate']);
}
*/
$paginate = $_REQUEST['paginate'] ?? '';
if ($isUpload) {
$paginate = 'f';
}
if (empty($paginate)) {
$paginate = $canPaginate ? 't' : 'f';
}
// Check to see if pagination has been specified. In that case, send to display
// script for pagination
if ($paginate == 't') {
require './display.php';
exit;
}
$misc->printHeader($lang['strqueryresults']);
$misc->printBody();
$misc->printTrail('database');
$misc->printTitle($lang['strqueryresults']);
// Set the schema search path
if (isset($_REQUEST['search_path'])) {
if ($schemaActions->setSearchPath(array_map('trim', explode(',', $_REQUEST['search_path']))) != 0) {
$misc->printFooter();
exit;
}
}
// May as well try to time the query
$start_time = microtime(true);
// Execute the query. If it's a script upload, special handling is necessary
if ($isUpload) {
// Execute the script via our ScriptActions class
$scriptActions = new ScriptActions($pg);
$scriptActions->executeScript('script', $sqlCallback);
} else {
// Execute each individual statement
foreach ($statements as $stmt) {
// Set fetch mode to NUM so that duplicate field names are properly returned
$pg->conn->setFetchMode(ADODB_FETCH_NUM);
$rs = $pg->conn->Execute($stmt);
$errorMsg = $rs === false ? $pg->conn->ErrorMsg() : '';
// Wrap result for consistent handling
$result = QueryResult::fromADORecordSet($rs, $errorMsg);
// Render the result
renderQueryResult($result, $stmt);
}
// Request was run, saving it in history
if (!isset($_REQUEST['nohistory'])) {
$misc->saveSqlHistory($_SESSION['sqlquery'], false);
}
}
// May as well try to time the query
$end_time = microtime(true);
$duration = number_format(($end_time - $start_time) * 1000, 3);
// Reload the tree as we may have made schema changes
if ($hasNonReadQueries) {
AppContainer::setShouldReloadTree(true);
}
// Display duration
if ($duration !== null) {
echo "<p>", sprintf($lang['strruntime'], $duration), "</p>\n";
}
echo "<p>{$lang['strsqlexecuted']}</p>\n";
$navlinks = array();
$fields = array(
'server' => $_REQUEST['server'],
'database' => $_REQUEST['database'],
);
if (isset($_REQUEST['schema']))
$fields['schema'] = $_REQUEST['schema'];
// Return
if (isset($_REQUEST['return'])) {
$urlvars = $misc->getSubjectParams($_REQUEST['return']);
$navlinks['back'] = array(
'attr' => array(
'href' => array(
'url' => $urlvars['url'],
'urlvars' => $urlvars['params']
)
),
'content' => $lang['strback']
);
}
// Edit
$navlinks['alter'] = array(
'attr' => array(
'href' => array(
'url' => 'database.php',
'urlvars' => array_merge($fields, array(
'action' => 'sql',
'paginate' => $paginate,
))
)
),
'icon' => $misc->icon('SqlEditor'),
'content' => $lang['streditsql']
);
// Create view and download
if (isset($_SESSION['sqlquery']) && isset($rs) && is_object($rs) && $rs->recordCount() > 0) {
// Report views don't set a schema, so we need to disable create view in that case
if (isset($_REQUEST['schema'])) {
$navlinks['createview'] = array(
'attr' => array(
'href' => array(
'url' => 'views.php',
'urlvars' => array_merge($fields, array(
'action' => 'create'
))
)
),
'content' => $lang['strcreateview']
);
}
if (isset($_REQUEST['search_path']))
$fields['search_path'] = $_REQUEST['search_path'];
$navlinks['download'] = array(
'attr' => array(
'href' => array(
'url' => 'dataexport.php',
'urlvars' => array_merge($fields, ['query' => $_SESSION['sqlquery']])
)
),
'icon' => $misc->icon('Download'),
'content' => $lang['strdownload']
);
}
$misc->printNavLinks($navlinks, 'sql-form', get_defined_vars());
$misc->printFooter();