1#!/usr/local/bin/perl
2# Create, re-create or delete an index
3
4require './mysql-lib.pl';
5&ReadParse();
6&error_setup($text{'index_err'});
7&can_edit_db($in{'db'}) || &error($text{'dbase_ecannot'});
8$access{'edonly'} && &error($text{'dbase_ecannot'});
9$access{'indexes'} || &error($text{'index_ecannot'});
10
11if ($in{'delete'}) {
12	# Just drop the index
13	$sql = "drop index ".&quotestr($in{'old'})." on ".
14	 		     &quotestr($in{'table'});
15	&execute_sql_logged($in{'db'}, $sql);
16	&webmin_log("delete", "index", $in{'old'}, \%in);
17	}
18else {
19	# Validate inputs
20	$in{'name'} =~ /^\S+$/ || &error($text{'index_ename'});
21	if (!$in{'old'} || $in{'old'} ne $in{'name'}) {
22		@indexes = &list_indexes($in{'db'});
23		&indexof($in{'name'}, @indexes) >= 0 &&
24			&error($text{'index_eclash'});
25		}
26	@cols = split(/\0/, $in{'cols'});
27	@cols || &error($text{'index_ecols'});
28
29	# Do it
30	if ($in{'old'}) {
31		# Remove the old one first
32		$sql = "drop index ".&quotestr($in{'old'})." on ".
33				     &quotestr($in{'table'});
34		&execute_sql_logged($in{'db'}, $sql);
35		}
36	$sql = "create $in{'type'} index ".&quotestr($in{'name'})." on ".
37	       &quotestr($in{'table'})." (".
38	       join(", ", map { &quotestr($_) } @cols).")";
39	&execute_sql_logged($in{'db'}, $sql);
40
41	if ($in{'old'}) {
42		&webmin_log("modify", "index", $in{'old'}, \%in);
43		}
44	else {
45		&webmin_log("create", "index", $in{'name'}, \%in);
46		}
47	}
48&redirect("edit_dbase.cgi?db=$in{'db'}");
49
50