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

Annotation of /trunk/webwork2/bin/wwdb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 808 - (view) (download)

1 : sh002i 798 #!/usr/bin/env perl
2 :    
3 :     ################################################################################
4 :     # WeBWorK mod_perl (c) 1995-2002 WeBWorK Team, Univeristy of Rochester
5 : sh002i 808 # $Id: wwdb,v 1.2 2003-04-17 21:01:13 sh002i Exp $
6 : sh002i 798 ################################################################################
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 : sh002i 808 use FindBin;
17 :     use lib "$FindBin::Bin/../lib";
18 : sh002i 798 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 : sh002i 808 #
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 : sh002i 798
37 :     sub main(@) {
38 :     my ($course, $command, @arguments) = @_;
39 :    
40 : sh002i 808 unless ($course and $command) {
41 :     die "usage: $0 course command [arguments ...]\n";
42 :     }
43 :    
44 : sh002i 798 my $ce = WeBWorK::CourseEnvironment->new($ENV{WEBWORK_ROOT}, "", $course);
45 :     my $db = WeBWorK::DB->new($ce);
46 :    
47 : sh002i 808 if ($command eq "dumpDB") {
48 :     print $db->$command("set_user");
49 :     exit;
50 :     }
51 :    
52 : sh002i 798 die "$command: unsupported command.\n"
53 :     unless $db->can($command);
54 :    
55 : sh002i 808 my ($verb, $noun) = $command =~ m/^(list|add|get|put|delete)(.*?)s?$/;
56 : sh002i 798
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 : sh002i 808 if (m/^\\undef$/) {
66 :     $_ = undef;
67 :     } elsif (m/^{(.*)}$/) {
68 : sh002i 798 $_ = 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 : sh002i 808 } elsif ($verb eq "add") {
77 :     print "result: $result[0]\n";
78 : sh002i 798 } elsif ($verb eq "get") {
79 :     if (defined $result[0]) {
80 :     print "{", record2string($result[0]), "}\n";
81 :     } else {
82 : sh002i 808 print join("/", @arguments), ": record not found\n";
83 : sh002i 798 }
84 :     } elsif ($verb eq "put") {
85 : sh002i 808 print "result: $result[0]\n";
86 : sh002i 798 } elsif ($verb eq "delete") {
87 : sh002i 808 print "result: $result[0]\n";
88 : sh002i 798 }
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