Presentation is loading. Please wait.

Presentation is loading. Please wait.

Databases On The Web with perl Archie Warnock

Similar presentations


Presentation on theme: "Databases On The Web with perl Archie Warnock"— Presentation transcript:

1 Databases On The Web with perl Archie Warnock warnock@awcubed.com http://www.awcubed.com

2 Arbitration System Overview The Task: Build an online legal case management system with a Web interface The Client: An Intergovernmental Organization with little IT experience Subsystems include: 4Users, Cases, Documents 4Parties to Case 4Panel of judges 4Case Manager 4Financial

3 Requirements Web interface for filing complaints - submit evidence, edit submitted information, make financial arrangements High security and confidentiality Web-based interface for case management Report generation for judges, arbitration center staff

4 Architecture

5 Getting Started with perl DBD The Toolbox: 4Linux 4perl 5 4An SQL or ODBC database for Linux - Oracle, MySQL, mSQL, … 4DBI/DBD modules & utilities 4CGI.pm module 4Text::Template module

6 Session Overview Connect to database server Do some database stuff Print results into a template Disconnect from database server

7 Client Configuration # You can load this in a module if ($DB eq $oracle) { $ENV{ORACLE_HOME} = '/ '; $ENV{ORACLE_SID} = ’ORCL'; $ENV{TWO_TASK} = ’ORCL'; $connect_str ="dbi:$oracle:"; } elsif ($DB eq $mysql) { my $database = “dbname"; my $server = “hostname"; my $port = “port"; $connect_str ="DBI:$mysql:$database:$server:$port"; } else { foo; }

8 The Database Connection sub connectToDB { # Make the connection # Username and password are stored separately $dbh = DBI->connect( $main::connect_str, $main::dbuser, $main::dbpass) or die print "Failed to connect to database server ". $DBI::errstr ; return $dbh; }

9 Connect To Database Server Now, open the connection by calling: $dbh = connectToDB(); Keep track of $dbh - it is the handle for the database connection It will stay active for a single CGI connection, but it doesn’t seem to be worth the trouble to maintain it across sessions

10 Do Some Database Stuff - 1 Non-select statement, single row result $language = ‘perl’; $language = $dbh->quote($language); $sql = qq[INSERT INTO compilers (language) VALUES ($language)]; $result = $dbh->do($sql); if ($result != 1) { # Some error handler here } $dbh->disconnect();

11 Do Some Database Stuff - 2 Select statement, single row result $language = ‘perl’; $language = $dbh->quote($language); $sql = qq[ SELECT price FROM compilers WHERE language = $language]; $result = $dbh->selectrow_array($sql);

12 Do Some Database Stuff - 3 Select statement, multiple row result $language = ‘perl’; $language = $dbh->quote($language); $sql = qq[ SELECT * FROM compilers]; $sth = $dbh->do($sql); while (@row = $sth->selectrow()) { # some processing, row-by-row }

13 Fancy Database Stuff Variable bindings for repeated statements $sth = $dbh->prepare("insert into table(foo,bar,baz) values (?,?,?)"); while( ) { chop; my ($foo,$bar,$baz) = split /,/; $sth->execute($foo,$bar,$baz); }

14 Print Results Results are returned in a row, so you can paste them into a table Use Text::Template to insert dynamic HTML into static HTML sub PrintRedirect { use Text::Template; no strict; disable diagnostics; $Title = shift; $Heading = shift; $template = new Text::Template( TYPE => FILE, SOURCE => "$main::docroot/logout.tmpl"); print header(-refresh=>"10; URL=$main::BaseCGI/Login", -type=>'text/html'); $text = $template->fill_in(OUTPUT => \*STDOUT); }

15 A Real Example - perl sub PrintComplainantList { use Text::Template; no strict; disable diagnostics; $session = shift; $template = new Text::Template( TYPE => FILE, SOURCE => "$main::docroot/parties.tmpl"); # get the list of respondents by case number $dbh = &Database::connectToDB(); if ($dbh) { $CaseNo = $dbh->quote($caseno); $sql = qq[ SELECT user_id, email, last FROM claimant_cases WHERE case_id = $CaseNo];

16 A Real Example - More perl $sth = $dbh->prepare($sql); $sth->execute; $NumberOfRows = 0; @rows = ""; while (@row = $sth->fetchrow_array) { # Grab the results, row-by-row ($row_head,@data) = @row; $row_head = " $row_head "; push(@rows,th($row_head).td(\@data)); $NumberOfRows++; } &Database::disconnectFromDB($dbh);

17 Real Example - HTML Template { $title } { $heading } <form method="post" action="{ $BaseCGI }/NewCaseMenu"> <input type="hidden" name="Session" value="{ $session }"> <input type="hidden" name=”Case" value="{ $caseno }"> { ($NumberOfRows)? $foo='' : $foo=TR([th({- align=>'left'},\@col_head),@rows]); $foo }

18 Summary perl is (of course) a superb prototyping and production tool Interfaces to existing databases are easy to build, run well CGI.pm allows simple scripting Text::Template allows you to use site management tools like FrontPage, DreamWeaver to maintain consistency


Download ppt "Databases On The Web with perl Archie Warnock"

Similar presentations


Ads by Google