Parent Directory
|
Revision Log
updated copyright dates
1 #!/usr/bin/env perl 2 ################################################################################ 3 # WeBWorK Online Homework Delivery System 4 # Copyright © 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/ 5 # $CVSHeader: webwork2/bin/wwaddindexing,v 1.3 2006/01/25 23:13:45 sh002i Exp $ 6 # 7 # This program is free software; you can redistribute it and/or modify it under 8 # the terms of either: (a) the GNU General Public License as published by the 9 # Free Software Foundation; either version 2, or (at your option) any later 10 # version, or (b) the "Artistic License" which comes with this package. 11 # 12 # This program is distributed in the hope that it will be useful, but WITHOUT 13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 14 # FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the 15 # Artistic License for more details. 16 ################################################################################ 17 18 =head1 NAME 19 20 wwaddindexing - add indices to an existing sql_single course. 21 22 =head1 SYNOPSIS 23 24 wwaddindexing COURSEID 25 26 =head1 DESCRIPTION 27 28 Adds indices to the course named COURSEID. The course must use the sql_single 29 database layout. 30 31 =cut 32 33 BEGIN { 34 # hide arguments (there could be passwords there!) 35 $0 = "$0"; 36 } 37 38 use strict; 39 use warnings; 40 use DBI; 41 42 BEGIN { 43 die "WEBWORK_ROOT not found in environment.\n" 44 unless exists $ENV{WEBWORK_ROOT}; 45 } 46 47 use lib "$ENV{WEBWORK_ROOT}/lib"; 48 use WeBWorK::CourseEnvironment; 49 use WeBWorK::DB; 50 use WeBWorK::Utils qw/runtime_use/; 51 use WeBWorK::Utils::CourseManagement qw/dbLayoutSQLSources/; 52 53 sub usage { 54 print STDERR "usage: $0 COURSEID \n"; 55 exit; 56 } 57 58 sub usage_error { 59 print STDERR "$0: @_\n"; 60 usage(); 61 } 62 63 # get command-line options 64 my ($courseID) = @ARGV; 65 66 # perform sanity check 67 usage_error("must specify COURSEID.") unless $courseID and $courseID ne ""; 68 69 # bring up a minimal course environment 70 my $ce = WeBWorK::CourseEnvironment->new({ 71 webwork_dir => $ENV{WEBWORK_ROOT}, 72 courseName => $courseID, 73 }); 74 75 # make sure the course actually uses the 'sql_single' layout 76 usage_error("$courseID: does not use 'sql_single' database layout.") 77 unless $ce->{dbLayoutName} eq "sql_single"; 78 79 # get database layout source data 80 my %sources = dbLayoutSQLSources($ce->{dbLayout}); 81 82 foreach my $source (keys %sources) { 83 my %source = %{$sources{$source}}; 84 my @tables = @{$source{tables}}; 85 my $username = $source{username}; 86 my $password = $source{password}; 87 88 my $dbh = DBI->connect($source, $username, $password); 89 90 foreach my $table (@tables) { 91 # this stuff straight out of sql_single.pm 92 my %table = %{ $ce->{dbLayout}{$table} }; 93 my %params = %{ $table{params} }; 94 95 my $source = $table{source}; 96 my $tableOverride = $params{tableOverride}; 97 my $recordClass = $table{record}; 98 99 runtime_use($recordClass); 100 my @fields = $recordClass->FIELDS; 101 my @keyfields = $recordClass->KEYFIELDS; 102 103 if (exists $params{fieldOverride}) { 104 my %fieldOverride = %{ $params{fieldOverride} }; 105 foreach my $field (@fields) { 106 $field = $fieldOverride{$field} if exists $fieldOverride{$field}; 107 } 108 } 109 110 my @fieldList; 111 foreach my $start (0 .. $#keyfields) { 112 my $line = "ADD INDEX ( "; 113 $line .= join(", ", map { "`$_`(16)" } @keyfields[$start .. $#keyfields]); 114 $line .= " )"; 115 push @fieldList, $line; 116 } 117 my $fieldString = join(", ", @fieldList); 118 119 my $tableName = $tableOverride || $table; 120 my $stmt = "ALTER TABLE `$tableName` $fieldString;"; 121 122 unless ($dbh->do($stmt)) { 123 die "An error occured while trying to modify the course database.\n", 124 "It is possible that the course database is in an inconsistent state.\n", 125 "The DBI error message was:\n\n", 126 $dbh->errstr, "\n"; 127 } 128 } 129 130 $dbh->disconnect; 131 } 132 133 =head1 AUTHOR 134 135 Written by Sam Hathaway, hathaway at users.sourceforge.net. 136 137 =cut
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |