#!perl use CGI ; use DBIx::Recordset ; $DBIx::Recordset::Debug = 0 ; $q = new CGI ; print $q -> header ; print $q -> start_html (title=>'Example for DBIx::Recordset') ; print '

DBIx::Recordset Example

' ; ### convert form parameter to hash ### %fdat = map { $_ => $q -> param($_) } $q -> param ; $fdat{'!IgnoreEmpty'} = 2 ; # Just to make the condition dialog work if (!defined ($fdat{'!DataSource'}) || !defined ($fdat{'!Table'})|| defined($fdat{'showdsn'})) { #### show entry form to select datasource #### delete $fdat{'showdsn'} ; @drvs = DBI-> available_drivers ; print $q -> startform (-method => GET) ; print "\n" ; print "\n" ; print "\n" ; print "
Available DBD drivers
\n" ; print $q -> scrolling_list (-name=>"driver", -values=>\@drvs, -size=>7) ; print "
First of all you have to specify which database and table you want to access and enter\n" ; print "the user and password (if required)

For the Datasource you have the following Options:
\n" ; print "1.) choose a DBD driver from the list on the left and hit the Show Datasources button,\n" ; print "then you can select a Datasource below (if your DBD driver supports the data_sources\n" ; print "method)
\n" ; print "2.) enter the Data Source directly in the text field below

\n" ; @dsns = DBI->data_sources ($fdat{driver}) if ($fdat{driver}) ; print "\n" ; print "\n" ; print " \n" ; print " \n" ; print "\n" ; print "\n" ; print " \n" ; print " \n" ; print "\n" ; print "\n" ; print " \n" ; print " \n" ; print "\n" ; print "\n" ; print " \n" ; print " \n" ; print "\n" ; print "\n" ; print " \n" ; $q -> param (-name=>'$max', -value=>5) if (!$fdat{'$max'}) ; print " \n" ; print "\n" ; print "
Datasource:\n" ; # fixup for drivers which does not support the data_sources method @dsns = () if ($dsns[0] =~ /HASH/ ) ; # fixup for mSQL/mysql driver for ($i = 0; $i <= $#dsns; $i++) { $dsns[$i] =~ s/^DBI/dbi/ ; } if ($#dsns >= 0) { print $q -> popup_menu (-name=>"!DataSource", -size=>"1", -value=>\@dsns) ; } else { print $q -> textfield (-name=>"!DataSource", -size=>20) ; print "Datasource list not available, enter DSN manual" ; } print "
Table:" , $q -> textfield (-name=>"!Table", -size=>"20"), "
User:" , $q -> textfield (-name=>"!Username", -size=>"20"), "
Password:" , $q -> password_field (-name=>"!Password", -size=>"20"), "
Rows Per Page:" , $q -> textfield (-name=>'$max', -size=>5), "
\n" ; print "

\n" ; print $q -> submit (-value=>"Show Datasources", -name=>"showdsn") ; print $q -> submit (-value=>"Show whole table", -name=>"show") ; print $q -> submit (-value=>"Specify condition", -name=>"cond") ; print $q -> reset (-name=>"Reset") ; print $q -> endform ; } elsif (defined ($fdat{'cond'})) { #### enter a search condition ##### delete $fdat{'cond'}; ### setup recordset ### $set = DBIx::Recordset -> SetupObject (\%fdat) ; ### get the names of all fields ### $names = $set -> AllNames () if ($set) ; print "\n" ; print "\n" ; print " \n" ; print " \n" ; print "\n" ; print "\n" ; print " \n" ; print " \n" ; print "\n" ; print "\n" ; print " \n" ; print " \n" ; print "\n" ; print "\n" ; print " \n" ; print " \n" ; print "\n" ; if ($DBI::errstr) { print "\n" ; print " \n" ; print " \n" ; print "\n" ; } print "
Datasource:\n" ; print $fdat{"!DataSource"} ; print "
Table:$fdat{'!Table'}
User:$fdat{'!Username'}
Rows Per Page:$fdat{'$max'}
ERROR:" , $DBI::errstr , "

\n" ; if ($set) { print $q -> startform (-method => GET) ; print " \n" ; print " \n" ; print " \n" ; print " \n" ; print " \n" ; print " \n" ; foreach $n (@$names) { print " \n" ; print " \n" ; print " \n" ; print " \n" ; print " \n" ; } print "
FieldnameOperatorValue
$n", $q -> textfield (-name=>"\*$n", -size=>5), "", $q -> textfield (-name=>$n, -size=>20), "
\n" ; print "

\n" ; print $q -> hidden (-name=>'!DataSource', -value=>$fdat{'!DataSource'}) ; print $q -> hidden (-name=>'!Table', -value=>$fdat{'!Table'}) ; print $q -> hidden (-name=>'!Username', -value=>$fdat{'!Username'}) ; print $q -> hidden (-name=>'!Password', -value=>$fdat{'!Password'}) ; print $q -> hidden (-name=>'$max', -value=>$fdat{'$max'}) ; print $q -> hidden (-name=>'driver', -value=>$fdat{'driver'}) ; print $q -> submit (-value=>"Start search", -name=>"search") ; print $q -> submit (-value=>"Change Datasource", -name=>"showdsn") ; print $q -> reset (-name=>"Reset") ; print $q -> endform ; } } else { #### show query result #### ### setup object and do the query ### *set = DBIx::Recordset -> Search (\%fdat) ; ### get fieldnames of query ### $names = $set -> Names if ($set) ; print "\n" ; print "\n" ; print " \n" ; print " \n" ; print "\n" ; print "\n" ; print " \n" ; print " \n" ; print "\n" ; print "\n" ; print " \n" ; print " \n" ; print "\n" ; print "\n" ; print " \n" ; print " \n" ; print "\n" ; if ($DBI::errstr) { print "\n" ; print " \n" ; print " \n" ; print "\n" ; } if ($set) { print "\n" ; print " \n" ; print " \n" ; print "\n" ; print "\n" ; print " \n" ; print " \n" ; print "\n" ; print "
Datasource:\n" ; print $fdat{"!DataSource"} ; print "
Table:$fdat{'!Table'}
User:$fdat{'!Username'}
Rows Per Page:$fdat{'$max'}
ERROR:" , $DBI::errstr , "
Current Start Row:" , $set -> StartRecordNo , "
SQL Statement:" , $set -> LastSQLStatement , "

\n" ; print "\n" ; print " \n" ; foreach $n (@$names) { print " \n" ; } print " \n" ; $row = 0 ; while ($r = $set[$row++]) { print " \n" ; foreach $n (@$names) { print " \n" ; } print " \n" ; } print "
$n
$$r{lc($n)}
\n" ; print $set -> PrevNextForm ('<>', \%fdat) ; print $q -> startform (-method => GET) ; while (($k, $v) = each (%fdat)) { if ($k ne 'refresh' && $k ne 'search' && $k ne 'showdsn' && $k ne 'cond') { print $q -> hidden (-name=>$k, -value=>$v) ; } } print "

\n" ; print $q ->submit (-value=>"Refresh", -name=>"refresh") ; print $q -> submit (-value=>"Specify condition", -name=>"cond") ; print $q -> submit (-value=>"Change Datasource", -name=>"showdsn") ; print $q -> endform ; } } ### cleanup ### DBIx::Recordset::Undef ('set') ; print $q -> end_html ;