Projekt

Obecné

Profil

Stáhnout (12 KB) Statistiky
| Větev: | Tag: | Revize:
1 6daefa8c Petr Lukašík
<?php
2
3
	/**
4
	 * List indexes on a table
5
	 *
6
	 * $Id: indexes.php,v 1.37 2005/10/18 03:45:16 chriskl Exp $
7
	 */
8
9
	// Include application functions
10
	include_once('./libraries/lib.inc.php');
11
	include_once('./classes/class.select.php');
12
		
13
	$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
14
	$PHP_SELF = $_SERVER['PHP_SELF'];
15
16
	/**
17
	 * Show confirmation of cluster index and perform actual cluster
18
	 */
19
	function doClusterIndex($confirm) {
20
		global $data, $misc, $action;
21
		global $PHP_SELF, $lang;
22
23
		if ($confirm) {
24
			// Default analyze to on
25
			$_REQUEST['analyze'] = true;
26
			
27
			$misc->printTrail('index');
28
			$misc->printTitle($lang['strclusterindex'],'pg.index.cluster');
29
30
			echo "<p>", sprintf($lang['strconfcluster'], $misc->printVal($_REQUEST['index'])), "</p>\n";
31
32
			echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
33
			echo "<p><input type=\"checkbox\" name=\"analyze\"", (isset($_REQUEST['analyze']) ? ' checked="checked"' : ''), " /> {$lang['stranalyze']}</p>\n";
34
			echo "<input type=\"hidden\" name=\"action\" value=\"cluster_index\" />\n";
35
			echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
36
			echo "<input type=\"hidden\" name=\"index\" value=\"", htmlspecialchars($_REQUEST['index']), "\" />\n";
37
			echo $misc->form;
38
			echo "<input type=\"submit\" name=\"cluster\" value=\"{$lang['strclusterindex']}\" />\n";
39
			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
40
			echo "</form>\n";
41
		}
42
		else {
43
			$status = $data->clusterIndex($_POST['index'], $_POST['table']);
44
			if ($status == 0)
45
				if (isset($_POST['analyze'])){
46
					$status = $data->analyzeDB($_POST['table']);
47
					if ($status == 0)
48
						doDefault($lang['strclusteredgood'] . ' ' . $lang['stranalyzegood']);
49
					else
50
						doDefault($lang['stranalyzebad']);
51
				} else
52
					doDefault($lang['strclusteredgood']);
53
			else
54
				doDefault($lang['strclusteredbad']);
55
		}
56
57
	}
58
59
	function doReindex() {
60
		global $data, $lang;
61
62
		$status = $data->reindex('INDEX', $_REQUEST['index']);
63
		if ($status == 0)
64
			doDefault($lang['strreindexgood']);
65
		else
66
			doDefault($lang['strreindexbad']);
67
	}
68
69
	/**
70
	 * Displays a screen where they can enter a new index
71
	 */
72
	function doCreateIndex($msg = '') {
73
		global $data, $misc;
74
		global $PHP_SELF, $lang;
75
76
		if (!isset($_POST['formIndexName'])) $_POST['formIndexName'] = '';
77
		if (!isset($_POST['formIndexType'])) $_POST['formIndexType'] = null;
78
		if (!isset($_POST['formCols'])) $_POST['formCols'] = '';
79
		if (!isset($_POST['formWhere'])) $_POST['formWhere'] = '';
80
		if (!isset($_POST['formSpc'])) $_POST['formSpc'] = '';
81
82
		$attrs = $data->getTableAttributes($_REQUEST['table']);
83
		// Fetch all tablespaces from the database
84
		if ($data->hasTablespaces()) $tablespaces = $data->getTablespaces();
85
		
86
		$misc->printTrail('table');
87
		$misc->printTitle($lang['strcreateindex'],'pg.index.create');
88
		$misc->printMsg($msg);
89
90
		$selColumns = new XHTML_select("TableColumnList",true,10);
91
		$selColumns->set_style("width: 10em;");
92
93
		if ($attrs->recordCount() > 0) {
94
			while (!$attrs->EOF) {
95
				$selColumns->add(new XHTML_Option($attrs->f['attname']));
96
				$attrs->moveNext();
97
			}
98
		}
99
100
		$selIndex = new XHTML_select("IndexColumnList[]", true, 10);
101
		$selIndex->set_style("width: 10em;");
102
		$selIndex->set_attribute("id", "IndexColumnList");
103
		$buttonAdd    = new XHTML_Button("add", ">>");
104
		$buttonAdd->set_attribute("onclick", "buttonPressed(this);");
105
		$buttonAdd->set_attribute("type", "button");
106
107
		$buttonRemove = new XHTML_Button("remove", "<<");
108
		$buttonRemove->set_attribute("onclick", "buttonPressed(this);");		
109
		$buttonRemove->set_attribute("type", "button");
110
111
		echo "<form onsubmit=\"doSelectAll();\" name=\"formIndex\" action=\"$PHP_SELF\" method=\"post\">\n";
112
113
114
		echo "<table>\n";
115
		echo "<tr><th class=\"data required\" colspan=\"3\">{$lang['strindexname']}</th></tr>";
116
		echo "<tr>";
117
		echo "<td class=\"data1\" colspan=\"3\"><input type=\"text\" name=\"formIndexName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", 
118
			htmlspecialchars($_POST['formIndexName']), "\" /></td></tr>";
119
		echo "<tr><th class=\"data\">{$lang['strtablecolumnlist']}</th><th class=\"data\">&nbsp;</th>";
120
		echo "<th class=\"data required\">{$lang['strindexcolumnlist']}</th></tr>\n";
121
		echo "<tr><td class=\"data1\">" . $selColumns->fetch() . "</td>\n";
122
		echo "<td class=\"data1\">" . $buttonRemove->fetch() . $buttonAdd->fetch() . "</td>";
123
		echo "<td class=\"data1\">" . $selIndex->fetch() . "</td></tr>\n";
124
		echo "</table>\n";
125
126
		echo "<table> \n";
127
		echo "<tr>";
128
		echo "<th class=\"data left required\" scope=\"row\">{$lang['strindextype']}</th>";
129
		echo "<td class=\"data1\"><select name=\"formIndexType\">";
130
		foreach ($data->typIndexes as $v) {
131
			echo "<option value=\"", htmlspecialchars($v), "\"",
132
				($v == $_POST['formIndexType']) ? ' selected="selected"' : '', ">", htmlspecialchars($v), "</option>\n";
133
		}
134
		echo "</select></td></tr>\n";				
135
		echo "</tr>";
136
		echo "<tr>";
137
		echo "<th class=\"data left\" scope=\"row\">{$lang['strunique']}</th>";
138
		echo "<td class=\"data1\"><input type=\"checkbox\" name=\"formUnique\"", (isset($_POST['formUnique']) ? 'checked="checked"' : ''), " /></td>";
139
		echo "</tr>";
140
		if ($data->hasPartialIndexes()) {
141
			echo "<tr>";
142
			echo "<th class=\"data left\" scope=\"row\">{$lang['strwhere']}</th>";
143
			echo "<td class=\"data1\">(<input name=\"formWhere\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", 
144
				htmlspecialchars($_POST['formWhere']), "\" />)</td>";
145
			echo "</tr>";
146
		}
147
		
148
		// Tablespace (if there are any)
149
		if ($data->hasTablespaces() && $tablespaces->recordCount() > 0) {
150
			echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>\n";
151
			echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"formSpc\">\n";
152
			// Always offer the default (empty) option
153
			echo "\t\t\t\t<option value=\"\"",
154
				($_POST['formSpc'] == '') ? ' selected="selected"' : '', "></option>\n";
155
			// Display all other tablespaces
156
			while (!$tablespaces->EOF) {
157
				$spcname = htmlspecialchars($tablespaces->f['spcname']);
158
				echo "\t\t\t\t<option value=\"{$spcname}\"",
159
					($spcname == $_POST['formSpc']) ? ' selected="selected"' : '', ">{$spcname}</option>\n";
160
				$tablespaces->moveNext();
161
			}
162
			echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n";
163
		}
164
165
		echo "</table>";
166
167
		echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create_index\" />\n";
168
		echo $misc->form;
169
		echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
170
		echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
171
		echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
172
		echo "</form>\n";
173
	}
174
175
	/**
176
	 * Actually creates the new index in the database
177
	 * @@ Note: this function can't handle columns with commas in them
178
	 */
179
	function doSaveCreateIndex() {
180
		global $data;
181
		global $lang;
182
		
183
		// Handle databases that don't have partial indexes
184
		if (!isset($_POST['formWhere'])) $_POST['formWhere'] = '';
185
		// Default tablespace to null if it isn't set
186
		if (!isset($_POST['formSpc'])) $_POST['formSpc'] = null;
187
		
188
		// Check that they've given a name and at least one column
189
		if ($_POST['formIndexName'] == '') doCreateIndex($lang['strindexneedsname']);
190
		elseif (!isset($_POST['IndexColumnList']) || $_POST['IndexColumnList'] == '') doCreateIndex($lang['strindexneedscols']);
191
		else {
192
			$status = $data->createIndex($_POST['formIndexName'], $_POST['table'], $_POST['IndexColumnList'], 
193
				$_POST['formIndexType'], isset($_POST['formUnique']), $_POST['formWhere'], $_POST['formSpc']);
194
			if ($status == 0)
195
				doDefault($lang['strindexcreated']);
196
			else
197
				doCreateIndex($lang['strindexcreatedbad']);
198
		}
199
	}
200
201
	/**
202
	 * Show confirmation of drop index and perform actual drop
203
	 */
204
	function doDropIndex($confirm) {
205
		global $data, $misc;
206
		global $PHP_SELF, $lang;
207
208
		if ($confirm) {
209
			$misc->printTrail('index');
210
			$misc->printTitle($lang['strdrop'],'pg.index.drop');
211
212
			echo "<p>", sprintf($lang['strconfdropindex'], $misc->printVal($_REQUEST['index'])), "</p>\n";
213
214
			echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
215
			echo "<input type=\"hidden\" name=\"action\" value=\"drop_index\" />\n";
216
			echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
217
			echo "<input type=\"hidden\" name=\"index\" value=\"", htmlspecialchars($_REQUEST['index']), "\" />\n";
218
			echo $misc->form;
219
			// Show cascade drop option if supportd
220
			if ($data->hasDropBehavior()) {
221
				echo "<p><input type=\"checkbox\" name=\"cascade\" /> {$lang['strcascade']}</p>\n";
222
			}
223
			echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
224
			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
225
			echo "</form>\n";
226
		}
227
		else {
228
			$status = $data->dropIndex($_POST['index'], isset($_POST['cascade']));
229
			if ($status == 0)
230
				doDefault($lang['strindexdropped']);
231
			else
232
				doDefault($lang['strindexdroppedbad']);
233
		}
234
235
	}
236
237
	function doDefault($msg = '') {
238
		global $data, $misc;
239
		global $PHP_SELF, $lang;
240
		
241
		function indPre(&$rowdata, $actions) {
242
			global $data, $lang;
243
			
244
			if ($data->phpBool($rowdata->f['indisprimary'])) {
245
				$rowdata->f['+constraints'] = $lang['strprimarykey'];
246
				$actions['drop']['disable'] = true;
247
			}
248
			elseif ($data->phpBool($rowdata->f['indisunique'])) {
249
				$rowdata->f['+constraints'] = $lang['struniquekey'];
250
				$actions['drop']['disable'] = true;
251
			}
252
			else
253
				$rowdata->f['+constraints'] = '';
254
			
255
			return $actions;
256
		}
257
		
258
		$misc->printTrail('table');
259
		$misc->printTabs('table','indexes');
260
		$misc->printMsg($msg);
261
262
		$indexes = $data->getIndexes($_REQUEST['table']);
263
		
264
		$columns = array(
265
			'index' => array(
266
				'title' => $lang['strname'],
267
				'field' => 'indname',
268
			),
269
			'definition' => array(
270
				'title' => $lang['strdefinition'],
271
				'field' => 'inddef',
272
			),
273
			'constraints' => array(
274
				'title' => $lang['strconstraints'],
275
				'field' => '+constraints',
276
				'type'  => 'verbatim',
277
				'params'=> array('align' => 'center'),
278
			),
279
			'clustered' => array(
280
				'title' => $lang['strclustered'],
281
				'field' => 'indisclustered',
282
				'type'  => 'yesno',
283
			),
284
			'actions' => array(
285
				'title' => $lang['stractions'],
286
			),
287
		);
288
		
289
		$actions = array(
290
			'cluster' => array(
291
				'title' => $lang['strclusterindex'],
292
				'url'   => "{$PHP_SELF}?action=confirm_cluster_index&amp;{$misc->href}&amp;table=".urlencode($_REQUEST['table'])."&amp;",
293
				'vars'  => array('index' => 'indname'),
294
			),
295
			'reindex' => array(
296
				'title' => $lang['strreindex'],
297
				'url'   => "{$PHP_SELF}?action=reindex&amp;{$misc->href}&amp;table=".urlencode($_REQUEST['table'])."&amp;",
298
				'vars'  => array('index' => 'indname'),
299
			),
300
			'drop' => array(
301
				'title' => $lang['strdrop'],
302
				'url'   => "{$PHP_SELF}?action=confirm_drop_index&amp;{$misc->href}&amp;table=".urlencode($_REQUEST['table'])."&amp;",
303
				'vars'  => array('index' => 'indname'),
304
			),
305
		);
306
		
307
		if (!$data->hasIsClustered()) unset($columns['clustered']);
308
		
309
		$misc->printTable($indexes, $columns, $actions, $lang['strnoindexes'], 'indPre');
310
		
311
		echo "<p><a class=\"navlink\" href=\"$PHP_SELF?action=create_index&amp;{$misc->href}&amp;table=", urlencode($_REQUEST['table']), "\">{$lang['strcreateindex']}</a></p>\n";		
312
	}
313
314
	$misc->printHeader($lang['strindexes'], "<script src=\"indexes.js\" type=\"text/javascript\"></script>");
315
316
	if ($action == 'create_index' || $action == 'save_create_index')
317
		echo "<body onload=\"init();\">";
318
	else
319
		$misc->printBody();
320
321
	switch ($action) {
322
		case 'cluster_index':
323
			if (isset($_POST['cluster'])) doClusterIndex(false);
324
			else doDefault();
325
			break;
326
		case 'confirm_cluster_index':
327
			doClusterIndex(true);
328
			break;
329
		case 'reindex':
330
			doReindex();
331
			break;
332
		case 'save_create_index':
333
			if (isset($_POST['cancel'])) doDefault();
334
			else doSaveCreateIndex();
335
			break;
336
		case 'create_index':
337
			doCreateIndex();
338
			break;
339
		case 'drop_index':
340
			if (isset($_POST['drop'])) doDropIndex(false);
341
			else doDefault();
342
			break;
343
		case 'confirm_drop_index':
344
			doDropIndex(true);
345
			break;
346
		default:
347
			doDefault();
348
			break;
349
	}
350
351
	$misc->printFooter();
352
353
?>