File: sql.php

package info (click to toggle)
phppgadmin 3.5.2-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,552 kB
  • ctags: 5,060
  • sloc: php: 50,620; makefile: 185; sh: 150; sql: 15; awk: 8
file content (123 lines) | stat: -rw-r--r-- 4,206 bytes parent folder | download
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
<?php

	/**
	 * Process an arbitrary SQL query - tricky!  The main problem is that
	 * unless we implement a full SQL parser, there's no way of knowing
	 * how many SQL statements have been strung together with semi-colons
	 * @param $query The SQL query string to execute
	 *
	 * $Id: sql.php,v 1.26.2.1 2005/02/09 11:31:49 chriskl Exp $
	 */

      // Prevent timeouts on large SQL sets
      set_time_limit(0);
	// Include application functions
	include_once('./libraries/lib.inc.php');

	// Determine explain version of SQL
	if ($data->hasFullExplain() && isset($_POST['explain']) && isset($_POST['query'])) {
		$_POST['query'] = $data->getExplainSQL($_POST['query'], false);
		$_REQUEST['query'] = $_POST['query'];
	}
	elseif ($data->hasFullExplain() && isset($_POST['explain_analyze']) && isset($_POST['query'])) {
		$_POST['query'] = $data->getExplainSQL($_POST['query'], true);
		$_REQUEST['query'] = $_POST['query'];
	}
	
	// Check to see if pagination has been specified. In that case, send to display
	// script for pagination
	if (isset($_POST['paginate']) && !isset($_POST['explain']) && !isset($_POST['explain_analyze'])) {
		include('./display.php');
		exit;
	}
	
	$PHP_SELF = $_SERVER['PHP_SELF'];

	$misc->printHeader($lang['strqueryresults']);
	$misc->printBody();
	$misc->printTrail('database');
	$misc->printTitle($lang['strqueryresults']);

	// Set the schema search path
	if ($data->hasSchemas() && isset($_REQUEST['search_path'])) {
		if ($data->setSearchPath(array_map('trim',explode(',',$_REQUEST['search_path']))) != 0) {
			$misc->printFooter();
			exit;
		}
	}

	// Set fetch mode to NUM so that duplicate field names are properly returned
	$data->conn->setFetchMode(ADODB_FETCH_NUM);
	
	// May as well try to time the query
	if (function_exists('microtime')) {
		list($usec, $sec) = explode(' ', microtime());
		$start_time = ((float)$usec + (float)$sec);
	}
	else $start_time = null;
	
	// Execute the query.  If it's a script upload, special handling is necessary
	if (isset($_FILES['script']) && $_FILES['script']['size'] > 0)
		$rs = $data->executeScript('script');
	else
		$rs = $data->conn->Execute($_POST['query']);

	// May as well try to time the query
	if ($start_time !== null) {
		list($usec, $sec) = explode(' ', microtime());
		$end_time = ((float)$usec + (float)$sec);	
		// Get duration in milliseconds, round to 3dp's	
		$duration = number_format(($end_time - $start_time) * 1000, 3);
	}
	else $duration = null;

	// $rs will only be an object if there is no error
	if (is_object($rs)) {
		// Now, depending on what happened do various things

		// First, if rows returned, then display the results
		if ($rs->recordCount() > 0) {
			echo "<table>\n<tr>";
			foreach ($rs->f as $k => $v) {
				$finfo = $rs->fetchField($k);
				echo "<th class=\"data\">", $misc->printVal($finfo->name), "</th>";
			}

			$i = 0;		
			while (!$rs->EOF) {
				$id = (($i % 2) == 0 ? '1' : '2');
				echo "<tr>\n";
				foreach ($rs->f as $k => $v) {
					$finfo = $rs->fetchField($k);
					echo "<td class=\"data{$id}\" nowrap=\"nowrap\">", $misc->printVal($v, $finfo->type, array('null' => true)), "</td>";
				}							
				echo "</tr>\n";
				$rs->moveNext();
				$i++;
			}
			echo "</table>\n";
			echo "<p>", $rs->recordCount(), " {$lang['strrows']}</p>\n";
		}
		// Otherwise if any rows have been affected
		elseif ($data->conn->Affected_Rows() > 0) {
			echo "<p>", $data->conn->Affected_Rows(), " {$lang['strrowsaff']}</p>\n";
		}
		// Else say success
		else echo "<p>{$lang['strsqlexecuted']}</p>\n";
		
		// Display duration if we know it
		if ($duration !== null) {
			echo "<p>", sprintf($lang['strruntime'], $duration), "</p>\n";
		}
	}

	echo "<p><a class=\"navlink\" href=\"database.php?database=", urlencode($_REQUEST['database']),
		"&amp;action=sql&amp;query=", urlencode($_POST['query']), "\">{$lang['streditsql']}</a>";
	if ($conf['show_reports'] && isset($rs) && is_object($rs) && $rs->recordCount() > 0) {
		echo " | <a class=\"navlink\" href=\"reports.php?action=create&amp;db_name=", urlencode($_REQUEST['database']), "&amp;report_sql=",
			urlencode($_POST['query']), "\">{$lang['strcreatereport']}</a>";
	}
	echo "</p>\n";
	
	$misc->printFooter();
?>