summaryrefslogtreecommitdiff
path: root/results.php
blob: 3330fb16dd2568a84e7787b5b2d99d13f8e7e622 (plain)
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
<?php

///////////////////////////////////////////////////////////////////////////////
//
// pgPhoneHome - Postgres Monitor for iPhone
// Copyright 2008, EnterpriseDB UK Ltd.
// Dave Page (dave.page@enterprisedb.com)
//
// results.php - Execute a query and display the results
//
///////////////////////////////////////////////////////////////////////////////

require "global.php";

// Get the parameters
if (isset($_GET['s']))
	$server = intval($_GET['s']);
else 
	$server = -1;

if (isset($_POST['q']))
	$sql = $_POST['q'];
else
	$sql = "";

if (isset($_GET['d']))
	$database = $_GET['d'];
else
	$database = "";

if (isset($_GET['i']))
	$id = $_GET['i'];
else
	www_error("Invalid ID", "No query session ID was specified.");

if (isset($_GET['o']))
	$offset = intval($_GET['o']);
else
	$offset = -1;

if (isset($_GET['r']))
	$row = intval($_GET['r']);
else
	$row = -1;

// Start a session to store the results in
session_start();

// What are we doing then?
if ($sql != "") // Initial run of an SQL query
{

	if ($server == -1 || $servers[$server]["description"] == "" || $servers[$server]["connstr"] == "")
		www_error("Invalid server", "The specified server number ($server) does not have a valid configuration.");

	// Set the display panel
	$panel = "results" . $server;

	// Connect the database. 
	$connstr = preg_replace("(dbname=[^\ ]*)", "dbname=" . $database, $servers[$server]["connstr"]);
	$db = @pg_connect($connstr);

	if ($db === FALSE)
		www_error("Couldn't connect to the database.", html_entity_decode($php_errormsg, ENT_QUOTES));

	// Run the query 
	$res = @pg_query($db, $sql);

	if ($res === false)
		www_error("Query execution error", $php_errormsg);

	$list = "";
	$divs = "";
	$rownum = 0;

	// Initialise the arrays to clear any old data
	$_SESSION[$id]['columns'] = array();
	$_SESSION[$id]['rows'] = array();

	// Store metadata for future use
	$_SESSION[$id]['column_count'] = pg_num_fields($res);
	$_SESSION[$id]['actual_rows'] = pg_num_rows($res);
	$_SESSION[$id]['row_count'] = ($_SESSION[$id]['actual_rows'] < $GLOBALS['config_max_result_rows']) ? $_SESSION[$id]['actual_rows'] : $GLOBALS['config_max_result_rows'];

	// Column names
	for ($x = 0; $x < $_SESSION[$id]['column_count']; $x++) {
		$_SESSION[$id]['columns'][$x] = www_clean(pg_field_name($res, $x));
	}	

	// Data
	while (($rowarray = pg_fetch_array($res)) && ($rownum < $GLOBALS['config_max_result_rows'])) {
		$_SESSION[$id]['rows'][$rownum] = $rowarray;
		$rownum++;
	}

	// Display the results
	if ($_SESSION[$id]['column_count'] > 0) {

		$message = "Rows: " . $_SESSION[$id]['actual_rows'];

		if ($_SESSION[$id]['actual_rows'] != $_SESSION[$id]['row_count'])
			$message .= " (limited to " . $_SESSION[$id]['row_count'] . ")";

		$list .= get_block(0, $id);

		// Echo the HTML snippet
		$text = <<<EOT
<ul id="$panel" title="Results"> 
<li class="group">$message</li>
$list
</ul>
$divs
EOT;

	} else {
		$message = "Affected rows: " . pg_affected_rows($res);
	
		$text = <<<EOT
<div id="$panel" class="panel" title="Result">
<h2>Query completed</h2>
<p>$message</p>
</div>
EOT;

	}
}
else if ($offset != -1) // Retrieving the next batch of rows
{
	$text = get_block($offset, $id);
}
else if ($row != -1) // Return a row
{
	$text = get_row($row, $id);
}

echo $text;
exit();

function get_block($offset, $id)
{
	$data = "";
	$last_row = (($offset + $GLOBALS['config_query_batch_size'] < $_SESSION[$id]['row_count'])) ? ($offset + $GLOBALS['config_query_batch_size']) : $_SESSION[$id]['row_count'];

	for ($x = $offset; $x < $last_row; $x++) {

		// Build the row preview list.
		$data .= "<li class=\"d\"><a href=\"results.php?i=" . $id . "&r=" . $x . "\">";

		$data .= $_SESSION[$id]['columns'][0] . ": <span class=\"cv\">" . www_clean(truncate_title($_SESSION[$id]['rows'][$x][0])) . "</span>";

		if ($_SESSION[$id]['column_count'] >= 2)
			$data .= "<br />" . $_SESSION[$id]['columns'][1] . ": <span class=\"cv\">" . www_clean(truncate_title($_SESSION[$id]['rows'][$x][1])) . "</span>";

		if ($_SESSION[$id]['column_count'] >= 3)
			$data .= "<br />" . $_SESSION[$id]['columns'][2] . ": <span class=\"cv\">" . www_clean(truncate_title($_SESSION[$id]['rows'][$x][2])) . "</span>";

		$data .= "</a></li>";
	}

	// Add the 'Get more...' item if required
	if ($last_row < $_SESSION[$id]['row_count']) 
	{
		if (($last_row + $GLOBALS['config_query_batch_size']) > $_SESSION[$id]['row_count'])
			$rows_to_fetch = ($_SESSION[$id]['row_count'] - $last_row);
		else
			$rows_to_fetch = $GLOBALS['config_query_batch_size'];

		if ($rows_to_fetch == 1)
			$data .= "<li><a href=\"results.php?i=" . $id . "&o=" . ($offset + $GLOBALS['config_query_batch_size']) . "\" target=\"_replace\">Get last row...</a></li>";
		else
		{
			if ($rows_to_fetch < $GLOBALS['config_query_batch_size'])
				$data .= "<li><a href=\"results.php?i=" . $id . "&o=" . ($offset + $GLOBALS['config_query_batch_size']) . "\" target=\"_replace\">Get last " . $rows_to_fetch . " rows...</a></li>";
			else
				$data .= "<li><a href=\"results.php?i=" . $id . "&o=" . ($offset + $GLOBALS['config_query_batch_size']) . "\" target=\"_replace\">Get " . $rows_to_fetch . " more rows...</a></li>";
		}
	}

	return $data;
}

function get_row($row, $id)
{
	$data = "";

	// Build the panel for this row
	$data .= "<div class=\"panel\" title=\"Query row\">";
	$data .= "<div class=\"dh\">Row: " . ($row + 1) . "</div>";

	for ($col = 0; $col < $_SESSION[$id]['column_count']; $col++) {
		$data .= "<h2>" . $_SESSION[$id]['columns'][$col] . "</h2><div class=\"vb\">" . www_clean(truncate_value($_SESSION[$id]['rows'][$row][$col])) . "</div>";
	}

	$data .= "</div>";

	return $data;
}
?>