[system] / trunk / webwork2 / bin / wwdb Repository:
ViewVC logotype

View of /trunk/webwork2/bin/wwdb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 808 - (download) (annotate)
Thu Apr 17 21:01:17 2003 UTC (10 years, 1 month ago) by sh002i
File size: 2730 byte(s)
Lots of work on WWDBv2... WW1Hash is done! A complete dbLayout (as
detailed on global.conf.dist) works now.
-sam

    1 #!/usr/bin/env perl
    2 
    3 ################################################################################
    4 # WeBWorK mod_perl (c) 1995-2002 WeBWorK Team, Univeristy of Rochester
    5 # $Id: wwdb,v 1.2 2003-04-17 21:01:13 sh002i Exp $
    6 ################################################################################
    7 
    8 =head1 NAME
    9 
   10 wwdb - command-line interface to the WeBWorK databases (WWDBv2).
   11 
   12 =cut
   13 
   14 use strict;
   15 use warnings;
   16 use FindBin;
   17 use lib "$FindBin::Bin/../lib";
   18 use WeBWorK::ContentGenerator; # for cook_args
   19 use WeBWorK::CourseEnvironment;
   20 use WeBWorK::DB;
   21 
   22 # Command line syntax
   23 # 
   24 # wwdb course command [ argument ... ]
   25 # 
   26 # Commands correspond to the functions of WeBWorK::DB. Arguments are either
   27 # strings or records. To represent records, use the following syntax:
   28 # 
   29 #         {id="sh002i" first_name="Sam" last_name="Hathaway"}
   30 # 
   31 # You'll have to protect arguments like this from your shell by enclosing them
   32 # in single quotes.
   33 # 
   34 # The special string \undef represents an undefined value. You'll have to
   35 # protect this from your shell as well. Use \\undef or '\undef'.
   36 
   37 sub main(@) {
   38 	my ($course, $command, @arguments) = @_;
   39 	
   40 	unless ($course and $command) {
   41 		die "usage: $0 course command [arguments ...]\n";
   42 	}
   43 	
   44 	my $ce = WeBWorK::CourseEnvironment->new($ENV{WEBWORK_ROOT}, "", $course);
   45 	my $db = WeBWorK::DB->new($ce);
   46 	
   47 	if ($command eq "dumpDB") {
   48 		print $db->$command("set_user");
   49 		exit;
   50 	}
   51 	
   52 	die "$command: unsupported command.\n"
   53 		unless $db->can($command);
   54 	
   55 	my ($verb, $noun) = $command =~ m/^(list|add|get|put|delete)(.*?)s?$/;
   56 	
   57 	my $type = "WeBWorK::DB::Record::";
   58 	if ($noun =~ m/^Global(User)?(Set|Problem)$/) {
   59 		$type .= "$1$2";
   60 	} else {
   61 		$type .= $noun;
   62 	}
   63 	
   64 	foreach (@arguments) {
   65 		if (m/^\\undef$/) {
   66 			$_ = undef;
   67 		} elsif (m/^{(.*)}$/) {
   68 			$_ = string2record($type, $1);
   69 		}
   70 	}
   71 	
   72 	my @result = $db->$command(@arguments);
   73 	
   74 	if ($verb eq "list") {
   75 		print join("\n", @result), "\n";
   76 	} elsif ($verb eq "add") {
   77 		print "result: $result[0]\n";
   78 	} elsif ($verb eq "get") {
   79 		if (defined $result[0]) {
   80 			print "{", record2string($result[0]), "}\n";
   81 		} else {
   82 			print join("/", @arguments), ": record not found\n";
   83 		}
   84 	} elsif ($verb eq "put") {
   85 		print "result: $result[0]\n";
   86 	} elsif ($verb eq "delete") {
   87 		print "result: $result[0]\n";
   88 	}
   89 }
   90 
   91 sub string2record($$) {
   92 	my ($type, $string) = @_;
   93 	my %hash = WeBWorK::ContentGenerator::cook_args($string);
   94 	my $record = $type->new(%hash);
   95 	return $record;
   96 }
   97 
   98 sub record2string($) {
   99 	my ($record) = @_;
  100 	my $string;
  101 	foreach my $key ($record->FIELDS()) {
  102 		my $value = $record->$key();
  103 		$value = "" unless defined $value;
  104 		$value =~ s/"/\\"/g;
  105 		$string .= "$key=\"$value\" ";
  106 	}
  107 	chop $string;
  108 	return $string;
  109 }
  110 
  111 main(@ARGV);

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9