File:  [LON-CAPA] / loncom / interface / lonmysql.pm
Revision 1.5: download - view: text, annotated - select for diffs
Fri Aug 9 17:08:19 2002 UTC (21 years, 9 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
General cleanups.
Added new function &remove_from_table to remove a row from a table.

    1: # The LearningOnline Network with CAPA
    2: # MySQL utility functions
    3: #
    4: # $Id: lonmysql.pm,v 1.5 2002/08/09 17:08:19 matthew Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ######################################################################
   29: 
   30: package Apache::lonmysql;
   31: 
   32: use strict;
   33: use DBI;
   34: use Apache::lonnet();
   35: 
   36: ######################################################################
   37: ######################################################################
   38: 
   39: =pod 
   40: 
   41: =head1 Name
   42: 
   43: lonmysql - LONCAPA MySQL utility functions
   44: 
   45: =head1 Synopsis
   46: 
   47: lonmysql contains utility functions to make accessing the mysql loncapa
   48: database easier.  
   49: 
   50: =head1 Description
   51: 
   52: lonmysql does its best to encapsulate all the database/table functions
   53: and provide a common interface.  The goal, however, is not to provide 
   54: a complete reimplementation of the DBI interface.  Instead we try to 
   55: make using mysql as painless as possible.
   56: 
   57: Each table has a numeric ID that is a parameter to most lonmysql functions.
   58: The table id is returned by &create_table.  
   59: If you lose the table id, it is lost forever.
   60: The table names in MySQL correspond to 
   61: $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.$table_id.  If the table id 
   62: is non-numeric, it is assumed to be the full name of a table.  If you pass
   63: the table id in a form, you MUST ensure that what you send to lonmysql is
   64: numeric, otherwise you are opening up all the tables in the MySQL database.
   65: 
   66: =over 4
   67: 
   68: =item Creating a table
   69: 
   70: To create a table, you need a description of its structure.  See the entry
   71: for &create_table for a description of what is needed.
   72: 
   73:  $table_id = &create_table({ 
   74:             columns => {
   75:                 id => {
   76:                     type => 'INT',
   77:                     restrictions => 'NOT NULL',
   78:                     primary_key => 'yes',
   79:                     auto_inc    => 'yes'
   80:                     }
   81:                 verbage => { type => 'TEXT' },
   82:             },
   83:             column_order => [qw/id verbage idx_verbage/],
   84:             fulltext => [qw/verbage/],
   85:         });
   86: 
   87: The above command will create a table with two columns, 'id' and 'verbage'.
   88: 
   89: 'id' will be an integer which is autoincremented and non-null.
   90: 
   91: 'verbage' will be of type 'TEXT', which (conceivably) allows any length
   92: text string to be stored.  Depending on your intentions for this database,
   93: setting restrictions => 'NOT NULL' may help you avoid storing empty data.
   94: 
   95: the fulltext element sets up the 'verbage' column for 'FULLTEXT' searching.
   96: 
   97: 
   98: 
   99: =item Storing rows
  100: 
  101: Storing a row in a table requires calling &store_row($table_id,$data)
  102: 
  103: $data is either a hash reference or an array reference.  If it is an array
  104: reference, the data is passed as is (after being escaped) to the 
  105: "INSERT INTO <table> VALUES( ... )" SQL command.  If $data is a hash reference,
  106: the data will be placed into an array in the proper column order for the table
  107: and then passed to the database.
  108: 
  109: An example of inserting into the table created above is:
  110: 
  111: &store_row($table_id,[undef,'I am not a crackpot!']);
  112: 
  113: or equivalently,
  114: 
  115: &store_row($table_id,{ verbage => 'I am not a crackpot!'});
  116: 
  117: Since the above table was created with the first column ('id') as 
  118: autoincrement, providing a value is unnecessary even though the column was
  119: marked as 'NOT NULL'.
  120: 
  121: 
  122: 
  123: =item Retrieving rows
  124: 
  125: Retrieving rows requires calling get_rows:
  126: 
  127: @row = &Apache::lonmysql::get_rows($table_id,$condition)
  128: 
  129: This results in the query "SELECT * FROM <table> HAVING $condition".
  130: 
  131: @row = &Apache::lonmysql::get_rows($table_id,'id>20'); 
  132: 
  133: returns all rows with column 'id' greater than 20.
  134: 
  135: =back
  136: 
  137: =cut
  138: 
  139: ######################################################################
  140: ######################################################################
  141: =pod
  142: 
  143: =head1 Package Variables
  144: 
  145: =over 4
  146: 
  147: =cut
  148: 
  149: ##################################################
  150: ##################################################
  151: 
  152: =pod
  153: 
  154: =item %Tables
  155: 
  156: Holds information regarding the currently open connections.  Each key
  157: in the %Tables hash will be a unique table key.  The value associated 
  158: with a key is a hash reference.  Most values are initialized when the 
  159: table is created.
  160: 
  161: The following entries are allowed in the hash reference:
  162: 
  163: =over 4
  164: 
  165: =item Name
  166: 
  167: Table name.
  168: 
  169: =item Type            
  170: 
  171: The type of table, typically MyISAM.
  172: 
  173: =item Row_format
  174: 
  175: Describes how rows should be stored in the table.  DYNAMIC or STATIC.
  176: 
  177: =item Create_time
  178: 
  179: The date of the tables creation.
  180: 
  181: =item Update_time
  182: 
  183: The date of the last modification of the table.
  184: 
  185: =item Check_time
  186: 
  187: Usually NULL. 
  188: 
  189: =item Avg_row_length
  190: 
  191: The average length of the rows.
  192: 
  193: =item Data_length
  194: 
  195: The length of the data stored in the table (bytes)
  196: 
  197: =item Max_data_length
  198: 
  199: The maximum possible size of the table (bytes).
  200: 
  201: =item Index_length
  202: 
  203: The length of the index for the table (bytes)
  204: 
  205: =item Data_free
  206: 
  207: I have no idea what this is.
  208: 
  209: =item Comment 
  210: 
  211: The comment associated with the table.
  212: 
  213: =item Rows
  214: 
  215: The number of rows in the table.
  216: 
  217: =item Auto_increment
  218: 
  219: The value of the next auto_increment field.
  220: 
  221: =item Create_options
  222: 
  223: I have no idea.
  224: 
  225: =item Col_order
  226: 
  227: an array reference which holds the order of columns in the table.
  228: 
  229: =item row_insert_sth 
  230: 
  231: The statement handler for row inserts.
  232: 
  233: =back
  234: 
  235: Col_order and row_insert_sth are kept internally by lonmysql and are not
  236: part of the usual MySQL table information.
  237: 
  238: =cut
  239: 
  240: ##################################################
  241: ##################################################
  242: my %Tables;
  243: 
  244: ##################################################
  245: ##################################################
  246: =pod
  247: 
  248: =item $errorstring
  249: 
  250: Holds the last error.
  251: 
  252: =cut
  253: ##################################################
  254: ##################################################
  255: my $errorstring;
  256: 
  257: ##################################################
  258: ##################################################
  259: =pod
  260: 
  261: =item $debugstring
  262: 
  263: Describes current events within the package.
  264: 
  265: =cut
  266: ##################################################
  267: ##################################################
  268: my $debugstring;
  269: 
  270: ##################################################
  271: ##################################################
  272: 
  273: =pod
  274: 
  275: =item $dbh
  276: 
  277: The database handler; The actual connection to MySQL via the perl DBI.
  278: 
  279: =cut
  280: 
  281: ##################################################
  282: ##################################################
  283: my $dbh;
  284: 
  285: ##################################################
  286: ##################################################
  287: 
  288: # End of global variable declarations
  289: 
  290: =pod
  291: 
  292: =back
  293: 
  294: =cut
  295: 
  296: ######################################################################
  297: ######################################################################
  298: 
  299: =pod
  300: 
  301: =head1 Internals
  302: 
  303: =over 4
  304: 
  305: =cut
  306: 
  307: ######################################################################
  308: ######################################################################
  309: 
  310: =pod
  311: 
  312: =item &connect_to_db()
  313: 
  314: Inputs: none.  
  315: 
  316: Returns: undef on error, 1 on success.
  317: 
  318: Checks to make sure the database has been connected to.  If not, the
  319: connection is established.  
  320: 
  321: =cut
  322: 
  323: ###############################
  324: sub connect_to_db { 
  325:     return 1 if ($dbh);
  326:     if (! ($dbh = DBI->connect("DBI:mysql:loncapa","www",
  327:                                $Apache::lonnet::perlvar{'lonSqlAccess'},
  328:                                { RaiseError=>0,PrintError=>0}))) {
  329:         $debugstring = "Unable to connect to loncapa database.";    
  330:         if ($dbh->err) {
  331:             $errorstring = "Connection error: ".$dbh->errstr;
  332:         }
  333:         return undef;
  334:     }
  335:     $debugstring = "Successfully connected to loncapa database.";    
  336:     return 1;
  337: }
  338: 
  339: ###############################
  340: 
  341: =pod
  342: 
  343: =item &disconnect_from_db()
  344: 
  345: Inputs: none.
  346: 
  347: Returns: Always returns 1.
  348: 
  349: Severs the connection to the mysql database.
  350: 
  351: =cut
  352: 
  353: ###############################
  354: sub disconnect_from_db { 
  355:     foreach (keys(%Tables)) {
  356:         # Supposedly, having statement handlers running around after the
  357:         # database connection has been lost will cause trouble.  So we 
  358:         # kill them off just to be sure.
  359:         if (exists($Tables{$_}->{'row_insert_sth'})) {
  360:             delete($Tables{$_}->{'row_insert_sth'});
  361:         }
  362:     }
  363:     $dbh->disconnect if ($dbh);
  364:     $debugstring = "Disconnected from database.";
  365:     $dbh = undef;
  366:     return 1;
  367: }
  368: 
  369: ###############################
  370: 
  371: =pod
  372: 
  373: =item &number_of_rows()
  374: 
  375: Input: table identifier
  376: 
  377: Returns: the number of rows in the given table, undef on error.
  378: 
  379: =cut
  380: 
  381: ###############################
  382: sub number_of_rows { 
  383:     my ($table_id) = @_;
  384:     return undef if (! defined(&connect_to_db()));
  385:     return undef if (! defined(&update_table_info($table_id)));
  386:     return $Tables{&translate_id($table_id)}->{'Rows'};
  387: }
  388: 
  389: ###############################
  390: 
  391: =pod
  392: 
  393: =item &get_error()
  394: 
  395: Inputs: none.
  396: 
  397: Returns: The last error reported.
  398: 
  399: =cut
  400: 
  401: ###############################
  402: sub get_error {
  403:     return $errorstring;
  404: }
  405: 
  406: ###############################
  407: 
  408: =pod
  409: 
  410: =item &get_debug()
  411: 
  412: Inputs: none.
  413: 
  414: Returns: A string describing the internal state of the lonmysql package.
  415: 
  416: =cut
  417: 
  418: ###############################
  419: sub get_debug {
  420:     return $debugstring;
  421: }
  422: 
  423: ###############################
  424: 
  425: =pod
  426: 
  427: =item &update_table_info($table_id)
  428: 
  429: Inputs: table id
  430: 
  431: Returns: undef on error, 1 on success.
  432: 
  433: &update_table_info updates the %Tables hash with current information about
  434: the given table.  
  435: 
  436: The default MySQL table status fields are:
  437: 
  438:    Name             Type            Row_format
  439:    Max_data_length  Index_length    Data_free
  440:    Create_time      Update_time     Check_time
  441:    Avg_row_length   Data_length     Comment 
  442:    Rows             Auto_increment  Create_options
  443: 
  444: Additionally, "Col_order" is updated as well.
  445: 
  446: =cut
  447: 
  448: ###############################
  449: sub update_table_info { 
  450:     my ($table_id) = @_;
  451:     return undef if (! defined(&connect_to_db()));
  452:     my $table_status = &check_table($table_id);
  453:     return undef if (! defined($table_status));
  454:     if (! $table_status) {
  455:         $errorstring = "table $table_id does not exist.";
  456:         return undef;
  457:     }
  458:     my $tablename = &translate_id($table_id);
  459:     #
  460:     # Get MySQLs table status information.
  461:     #
  462:     my @tabledesc = qw/
  463:         Name Type Row_format Rows Avg_row_length Data_length
  464:             Max_data_length Index_length Data_free Auto_increment 
  465:                 Create_time Update_time Check_time Create_options Comment /;
  466:     my $db_command = "SHOW TABLE STATUS FROM loncapa LIKE '$tablename'";
  467:     my $sth = $dbh->prepare($db_command);
  468:     $sth->execute();
  469:     if ($sth->err) {
  470:         $errorstring = "$dbh ATTEMPTED:\n".$db_command."\nRESULTING ERROR:\n".
  471:             $sth->errstr;
  472:         &disconnect_from_db();
  473:         return undef;
  474:     }
  475:     #
  476:     my @info=$sth->fetchrow_array;
  477:     for (my $i=0;$i<= $#info ; $i++) {
  478:         $Tables{$tablename}->{$tabledesc[$i]}= $info[$i];
  479:     }
  480:     #
  481:     # Determine the column order
  482:     #
  483:     $db_command = "DESCRIBE $tablename";
  484:     $sth = $dbh->prepare($db_command);
  485:     $sth->execute();
  486:     if ($sth->err) {
  487:         $errorstring = "$dbh ATTEMPTED:\n".$db_command."\nRESULTING ERROR:\n".
  488:             $sth->errstr;
  489:         &disconnect_from_db();
  490:         return undef;
  491:     }
  492:     my $aref=$sth->fetchall_arrayref;
  493:     $Tables{$tablename}->{'Col_order'}=[]; # Clear values.
  494:     # The values we want are the 'Field' entries, the first column.
  495:     for (my $i=0;$i< @$aref ; $i++) {
  496:         push @{$Tables{$tablename}->{'Col_order'}},$aref->[$i]->[0];
  497:     }
  498:     #
  499:     $debugstring = "Retrieved table info for $tablename";
  500:     return 1;
  501: }
  502: 
  503: ###############################
  504: 
  505: =pod
  506: 
  507: =item &create_table
  508: 
  509: Inputs: 
  510:     table description
  511: 
  512: Input formats:
  513: 
  514:     table description = {
  515:         permanent  => 'yes' or 'no',
  516:         columns => {
  517:             colA => {
  518:                 type         => mysql type,
  519:                 restrictions => 'NOT NULL' or empty,
  520:                 primary_key  => 'yes' or empty,
  521:                 auto_inc     => 'yes' or empty,
  522:             }
  523:             colB  => { .. }
  524:             colZ  => { .. }
  525:         },
  526:         column_order => [ colA, colB, ..., colZ],
  527:     }
  528: 
  529: Returns:
  530:     undef on error, table id on success.
  531: 
  532: =cut
  533: 
  534: ###############################
  535: sub create_table {
  536:     return undef if (!defined(&connect_to_db($dbh)));
  537:     my ($table_des)=@_;
  538:     #
  539:     # Build request to create table
  540:     ##################################
  541:     my @Columns;
  542:     my $col_des;
  543:     my $table_id = &get_new_table_id();
  544:     my $tablename = &translate_id($table_id);
  545:     my $request = "CREATE TABLE IF NOT EXISTS ".$tablename." ";
  546:     foreach my $column (@{$table_des->{'column_order'}}) {
  547:         $col_des = '';
  548:         my $coldata = $table_des->{'columns'}->{$column};
  549:         if (lc($coldata->{'type'}) =~ /(enum|set)/) { # 'enum' or 'set'
  550:             $col_des.=$column." ".$coldata->{'type'}."('".
  551:                 join("', '",@{$coldata->{'values'}})."')";
  552:         } else {
  553:             $col_des.=$column." ".$coldata->{'type'};
  554:             if (exists($coldata->{'size'})) {
  555:                 $col_des.="(".$coldata->{'size'}.")";
  556:             }
  557:         }
  558:         # Modifiers
  559:         if (exists($coldata->{'restrictions'})){
  560:             $col_des.=" ".$coldata->{'restrictions'};
  561:         }
  562:         if (exists($coldata->{'default'})) {
  563:             $col_des.=" DEFAULT '".$coldata->{'default'}."'";
  564:         }
  565:         $col_des.=' AUTO_INCREMENT' if (exists($coldata->{'auto_inc'}) &&
  566:                                         ($coldata->{'auto_inc'} eq 'yes'));
  567:         $col_des.=' PRIMARY KEY'    if (exists($coldata->{'primary_key'}) &&
  568:                                         ($coldata->{'primary_key'} eq 'yes'));
  569:     } continue {
  570:         # skip blank items.
  571:         push (@Columns,$col_des) if ($col_des ne '');
  572:     }
  573:     if (exists($table_des->{'fulltext'}) && (@{$table_des->{'fulltext'}})) {
  574:         push (@Columns,'FULLTEXT ('.join(',',@{$table_des->{'fulltext'}}).')');
  575:     }
  576:     $request .= "(".join(", ",@Columns).") ";
  577:     unless($table_des->{'permanent'} eq 'yes') {
  578:         $request.="COMMENT = 'temporary' ";
  579:     } 
  580:     $request .= "TYPE=MYISAM";
  581:     #
  582:     # Execute the request to create the table
  583:     #############################################
  584:     my $count = $dbh->do($request);
  585:     if (! defined($count)) {
  586:         $errorstring = "$dbh ATTEMPTED:\n".$request."\nRESULTING ERROR:\n";
  587:         return undef;
  588:     }
  589:     #
  590:     # Set up the internal bookkeeping
  591:     #############################################
  592:     delete($Tables{$tablename}) if (exists($Tables{$tablename}));
  593:     return undef if (! defined(&update_table_info($table_id)));
  594:     $debugstring = "Created table $tablename at time ".time.
  595:         " with request\n$request";
  596:     return $table_id;
  597: }
  598: 
  599: ###############################
  600: 
  601: =pod
  602: 
  603: =item &get_new_table_id
  604: 
  605: Used internally to prevent table name collisions.
  606: 
  607: =cut
  608: 
  609: ###############################
  610: sub get_new_table_id {
  611:     my $newid = 0;
  612:     my @tables = &tables_in_db();
  613:     foreach (@tables) {
  614:         if (/^$ENV{'user.name'}_$ENV{'user.domain'}_(\d+)$/) {
  615:             $newid = $1 if ($1 > $newid);
  616:         }
  617:     }
  618:     return ++$newid;
  619: }
  620: 
  621: ###############################
  622: 
  623: =pod
  624: 
  625: =item &get_rows
  626: 
  627: Inputs: $table_id,$condition
  628: 
  629: Returns: undef on error, an array ref to (array of) results on success.
  630: 
  631: Internally, this function does a 'SELECT * FROM table WHERE $condition'.
  632: $condition = 'id>0' will result in all rows where column 'id' has a value
  633: greater than 0 being returned.
  634: 
  635: =cut
  636: 
  637: ###############################
  638: sub get_rows {
  639:     my ($table_id,$condition) = @_;
  640:     return undef if (! defined(&connect_to_db()));
  641:     my $table_status = &check_table($table_id);
  642:     return undef if (! defined($table_status));
  643:     if (! $table_status) {
  644:         $errorstring = "table $table_id does not exist.";
  645:         return undef;
  646:     }
  647:     my $tablename = &translate_id($table_id);
  648:     my $request = 'SELECT * FROM '.$tablename.' WHERE '.$condition;
  649:     my $sth=$dbh->prepare($request);
  650:     $sth->execute();
  651:     if ($sth->err) {
  652:         $errorstring = "$dbh ATTEMPTED:\n".$request."\nRESULTING ERROR:\n".
  653:             $sth->errstr;
  654:         $debugstring = "Failed to get rows matching $condition";
  655:         return undef;
  656:     }
  657:     $debugstring = "Got rows matching $condition";
  658:     my @Results = @{$sth->fetchall_arrayref};
  659:     foreach my $row (@Results) {
  660:         for(my $i=0;$i<@$row;$i++) {
  661:             $row->[$i]=&Apache::lonnet::unescape($row->[$i]);
  662:         }
  663:     }
  664:     return @Results;
  665: }
  666: 
  667: ###############################
  668: 
  669: =pod
  670: 
  671: =item &store_row
  672: 
  673: Inputs: table id, row data
  674: 
  675: returns undef on error, 1 on success.
  676: 
  677: =cut
  678: 
  679: ###############################
  680: sub store_row {
  681:     my ($table_id,$rowdata) = @_;
  682:     # 
  683:     return undef if (! defined(&connect_to_db()));
  684:     my $table_status = &check_table($table_id);
  685:     return undef if (! defined($table_status));
  686:     if (! $table_status) {
  687:         $errorstring = "table $table_id does not exist.";
  688:         return undef;
  689:     }
  690:     #
  691:     my $tablename = &translate_id($table_id);
  692:     #
  693:     my $sth;
  694:     if (exists($Tables{$tablename}->{'row_insert_sth'})) {
  695:         $sth = $Tables{$tablename}->{'row_insert_sth'};
  696:     } else {
  697:         # Build the insert statement handler
  698:         return undef if (! defined(&update_table_info($table_id)));
  699:         my $insert_request = 'INSERT INTO '.$tablename.' VALUES(';
  700:         foreach (@{$Tables{$tablename}->{'Col_order'}}) {
  701:             $insert_request.="?,";
  702:         }
  703:         chop $insert_request;
  704:         $insert_request.=")";
  705:         $sth=$dbh->prepare($insert_request);
  706:         $Tables{$tablename}->{'row_insert_sth'}=$sth;
  707:     }
  708:     my @Parameters; 
  709:     if (ref($rowdata) eq 'ARRAY') {
  710:         @Parameters = @$rowdata;
  711:     } elsif (ref($rowdata) eq 'HASH') {
  712:         foreach (@{$Tables{$tablename}->{'Col_order'}}) {
  713:             push(@Parameters,&Apache::lonnet::escape($rowdata->{$_}));
  714:         }
  715:     } 
  716:     $sth->execute(@Parameters);
  717:     if ($sth->err) {
  718:         $errorstring = "$dbh ATTEMPTED insert @Parameters RESULTING ERROR:\n".
  719:             $sth->errstr;
  720:         return undef;
  721:     }
  722:     $debugstring = "Stored row.";    
  723:     return 1;
  724: }
  725: 
  726: ###########################################
  727: 
  728: =pod
  729: 
  730: =item tables_in_db
  731: 
  732: Returns a list containing the names of all the tables in the database.
  733: Returns undef on error.
  734: 
  735: =cut
  736: 
  737: ###########################################
  738: sub tables_in_db {
  739:     return undef if (!defined(&connect_to_db()));
  740:     my $sth=$dbh->prepare('SHOW TABLES');
  741:     $sth->execute();
  742:     if ($sth->err) {
  743:         $errorstring = "$dbh ATTEMPTED:\n".'SHOW TABLES'.
  744:             "\nRESULTING ERROR:\n".$sth->errstr;
  745:         return undef;
  746:     }
  747:     my $aref = $sth->fetchall_arrayref;
  748:     my @table_list=();
  749:     foreach (@$aref) {
  750:         push @table_list,$_->[0];
  751:     }
  752:     $debugstring = "Got list of tables in DB: @table_list";
  753:     return @table_list;
  754: }
  755: 
  756: ###########################################
  757: 
  758: =pod
  759: 
  760: =item &translate_id
  761: 
  762: Used internally to translate a numeric table id into a MySQL table name.
  763: If the input $id contains non-numeric characters it is assumed to have 
  764: already been translated.
  765: 
  766: Checks are NOT performed to see if the table actually exists.
  767: 
  768: =cut
  769: 
  770: ###########################################
  771: sub translate_id {
  772:     my $id = shift;
  773:     # id should be a digit.  If it is not a digit we assume the given id
  774:     # is complete and does not need to be translated.
  775:     return $id if ($id =~ /\D/);  
  776:     return $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.$id;
  777: }
  778: 
  779: ###########################################
  780: 
  781: =pod
  782: 
  783: =item &check_table($id)
  784: 
  785: Checks to see if the requested table exists.  Returns 0 (no), 1 (yes), or 
  786: undef (error).
  787: 
  788: =cut
  789: 
  790: ###########################################
  791: sub check_table {
  792:     my $table_id = shift;
  793:     return undef if (!defined(&connect_to_db()));
  794:     #
  795:     $table_id = &translate_id($table_id);
  796:     my @Table_list = &tables_in_db();
  797:     my $result = 0;
  798:     foreach (@Table_list) {
  799:         if (/^$table_id$/) {
  800:             $result = 1;
  801:             last;
  802:         }
  803:     }
  804:     # If it does not exist, make sure we do not have it listed in %Tables
  805:     delete($Tables{$table_id}) if ((! $result) && exists($Tables{$table_id}));
  806:     $debugstring = "check_table returned $result for $table_id";
  807:     return $result;
  808: }
  809: 
  810: ###########################################
  811: 
  812: =pod
  813: 
  814: =item &remove_from_table($table_id,$column,$value)
  815: 
  816: Executes a "delete from $tableid where $column like binary '$value'".
  817: 
  818: =cut
  819: 
  820: ###########################################
  821: sub remove_from_table {
  822:     my ($table_id,$column,$value) = @_;
  823:     return undef if (!defined(&connect_to_db()));
  824:     #
  825:     $table_id = &translate_id($table_id);
  826:     my $command = 'DELETE FROM '.$table_id.' WHERE '.$dbh->quote($column).
  827:         " LIKE BINARY ".$dbh->quote($value);
  828:     my $sth = $dbh->prepare($command); 
  829:     $sth->execute();
  830:     if ($sth->err) {
  831:         $errorstring = "ERROR on execution of ".$command."\n".$sth->errstr;
  832:         return undef;
  833:     }
  834:     my $rows = $sth->rows;
  835:     return $rows;
  836: }
  837: 
  838: 
  839: 1;
  840: 
  841: __END__;
  842: 
  843: =pod
  844: 
  845: =back
  846: 
  847: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>