Annotation of loncom/interface/lonmysql.pm, revision 1.4

1.1       matthew     1: # The LearningOnline Network with CAPA
                      2: # MySQL utility functions
                      3: #
1.4     ! matthew     4: # $Id: lonmysql.pm,v 1.3 2002/07/30 18:26:40 matthew Exp $
1.1       matthew     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:             },
1.3       matthew    83:             column_order => [qw/id verbage idx_verbage/],
                     84:             fulltext => [qw/verbage/],
                     85:         });
1.1       matthew    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: 
1.3       matthew    95: the fulltext element sets up the 'verbage' column for 'FULLTEXT' searching.
1.1       matthew    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: 
1.3       matthew   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).
1.1       matthew   200: 
1.3       matthew   201: =item Index_length
1.1       matthew   202: 
1.3       matthew   203: The length of the index for the table (bytes)
1.1       matthew   204: 
1.3       matthew   205: =item Data_free
1.1       matthew   206: 
1.3       matthew   207: I have no idea what this is.
1.1       matthew   208: 
1.3       matthew   209: =item Comment 
1.1       matthew   210: 
1.3       matthew   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 
1.1       matthew   230: 
                    231: The statement handler for row inserts.
                    232: 
                    233: =back
                    234: 
1.3       matthew   235: Col_order and row_insert_sth are kept internally by lonmysql and are not
                    236: part of the usual MySQL table information.
                    237: 
1.1       matthew   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: 
1.2       matthew   373: =item &number_of_rows()
1.1       matthew   374: 
1.2       matthew   375: Input: table identifier
                    376: 
1.3       matthew   377: Returns: the number of rows in the given table, undef on error.
1.1       matthew   378: 
                    379: =cut
                    380: 
                    381: ###############################
1.2       matthew   382: sub number_of_rows { 
                    383:     my ($table_id) = @_;
1.3       matthew   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'};
1.1       matthew   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: 
1.3       matthew   427: =item &update_table_info($table_id)
1.1       matthew   428: 
                    429: Inputs: table id
                    430: 
1.3       matthew   431: Returns: undef on error, 1 on success.
1.1       matthew   432: 
1.3       matthew   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:
1.1       matthew   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: 
1.3       matthew   444: Additionally, "Col_order" is updated as well.
                    445: 
1.1       matthew   446: =cut
                    447: 
                    448: ###############################
1.3       matthew   449: sub update_table_info { 
1.1       matthew   450:     my ($table_id) = @_;
1.3       matthew   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:     }
1.1       matthew   458:     my $tablename = &translate_id($table_id);
1.3       matthew   459:     #
                    460:     # Get MySQLs table status information.
                    461:     #
1.1       matthew   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;
1.3       matthew   472:         &disconnect_from_db();
1.1       matthew   473:         return undef;
                    474:     }
                    475:     #
                    476:     my @info=$sth->fetchrow_array;
                    477:     for (my $i=0;$i<= $#info ; $i++) {
1.3       matthew   478:         $Tables{$tablename}->{$tabledesc[$i]}= $info[$i];
                    479:     }
                    480:     #
                    481:     # Determine the column order
                    482:     #
                    483:     $db_command = "DESCRIBE $tablename";
                    484:     my $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];
1.1       matthew   497:     }
                    498:     #
                    499:     $debugstring = "Retrieved table info for $tablename";
1.3       matthew   500:     return 1;
1.1       matthew   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 {
1.3       matthew   536:     return undef if (!defined(&connect_to_db($dbh)));
1.1       matthew   537:     my ($table_des)=@_;
                    538:     #
                    539:     # Build request to create table
                    540:     ##################################
                    541:     my @Columns;
                    542:     my $col_des;
1.3       matthew   543:     my $table_id = &get_new_table_id();
                    544:     my $tablename = &translate_id($table_id);
1.1       matthew   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};
1.3       matthew   549:         if (lc($coldata->{'type'}) =~ /(enum|set)/) { # 'enum' or 'set'
1.1       matthew   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:         }
1.3       matthew   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'));
1.1       matthew   569:     } continue {
                    570:         # skip blank items.
                    571:         push (@Columns,$col_des) if ($col_des ne '');
                    572:     }
1.4     ! matthew   573:     if (exists($table_des->{'fulltext'}) && (@{$table_des->{'fulltext'}})) {
1.3       matthew   574:         push (@Columns,'FULLTEXT ('.join(',',@{$table_des->{'fulltext'}}).')');
                    575:     }
1.1       matthew   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)) {
1.3       matthew   586:         $errorstring = "$dbh ATTEMPTED:\n".$request."\nRESULTING ERROR:\n";
1.1       matthew   587:         return undef;
                    588:     }
                    589:     #
                    590:     # Set up the internal bookkeeping
                    591:     #############################################
                    592:     delete($Tables{$tablename}) if (exists($Tables{$tablename}));
1.3       matthew   593:     return undef if (! defined(&update_table_info($table_id)));
                    594:     $debugstring = "Created table $tablename at time ".time.
1.1       matthew   595:         " with request\n$request";
1.3       matthew   596:     return $table_id;
1.1       matthew   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 $name_regex = '^'.$ENV{'user.name'}.'_'.$ENV{'user.domain'}."_(\d+)\$";
                    613:     my @tables = &tables_in_db();
                    614:     foreach (@tables) {
                    615:         if (/^$ENV{'user.name'}_$ENV{'user.domain'}_(\d+)$/) {
                    616:             $newid = $1 if ($1 > $newid);
                    617:         }
                    618:     }
                    619:     return ++$newid;
                    620: }
                    621: 
                    622: ###############################
                    623: 
                    624: =pod
                    625: 
                    626: =item &get_rows
                    627: 
                    628: Inputs: $table_id,$condition
                    629: 
                    630: Returns: undef on error, an array ref to (array of) results on success.
                    631: 
1.2       matthew   632: Internally, this function does a 'SELECT * FROM table WHERE $condition'.
1.1       matthew   633: $condition = 'id>0' will result in all rows where column 'id' has a value
                    634: greater than 0 being returned.
                    635: 
                    636: =cut
                    637: 
                    638: ###############################
                    639: sub get_rows {
                    640:     my ($table_id,$condition) = @_;
1.3       matthew   641:     return undef if (! defined(&connect_to_db()));
                    642:     my $table_status = &check_table($table_id);
                    643:     return undef if (! defined($table_status));
                    644:     if (! $table_status) {
                    645:         $errorstring = "table $table_id does not exist.";
                    646:         return undef;
                    647:     }
1.1       matthew   648:     my $tablename = &translate_id($table_id);
1.2       matthew   649:     my $request = 'SELECT * FROM '.$tablename.' WHERE '.$condition;
1.1       matthew   650:     my $sth=$dbh->prepare($request);
                    651:     $sth->execute();
                    652:     if ($sth->err) {
                    653:         $errorstring = "$dbh ATTEMPTED:\n".$request."\nRESULTING ERROR:\n".
                    654:             $sth->errstr;
                    655:         $debugstring = "Failed to get rows matching $condition";
                    656:         return undef;
                    657:     }
                    658:     $debugstring = "Got rows matching $condition";
                    659:     my @Results = @{$sth->fetchall_arrayref};
                    660:     foreach my $row (@Results) {
                    661:         for(my $i=0;$i<@$row;$i++) {
                    662:             $row->[$i]=&Apache::lonnet::unescape($row->[$i]);
                    663:         }
                    664:     }
                    665:     return @Results;
                    666: }
                    667: 
                    668: ###############################
                    669: 
                    670: =pod
                    671: 
                    672: =item &store_row
                    673: 
                    674: Inputs: table id, row data
                    675: 
                    676: returns undef on error, 1 on success.
                    677: 
                    678: =cut
                    679: 
                    680: ###############################
                    681: sub store_row {
                    682:     my ($table_id,$rowdata) = @_;
1.3       matthew   683:     # 
                    684:     return undef if (! defined(&connect_to_db()));
                    685:     my $table_status = &check_table($table_id);
                    686:     return undef if (! defined($table_status));
                    687:     if (! $table_status) {
                    688:         $errorstring = "table $table_id does not exist.";
                    689:         return undef;
                    690:     }
                    691:     #
1.1       matthew   692:     my $tablename = &translate_id($table_id);
1.3       matthew   693:     #
1.1       matthew   694:     my $sth;
1.3       matthew   695:     if (exists($Tables{$tablename}->{'row_insert_sth'})) {
                    696:         $sth = $Tables{$tablename}->{'row_insert_sth'};
1.1       matthew   697:     } else {
1.3       matthew   698:         # Build the insert statement handler
                    699:         return undef if (! defined(&update_table_info($table_id)));
1.1       matthew   700:         my $insert_request = 'INSERT INTO '.$tablename.' VALUES(';
1.3       matthew   701:         foreach (@{$Tables{$tablename}->{'Col_order'}}) {
1.1       matthew   702:             $insert_request.="?,";
                    703:         }
                    704:         chop $insert_request;
                    705:         $insert_request.=")";
                    706:         $sth=$dbh->prepare($insert_request);
1.3       matthew   707:         $Tables{$tablename}->{'row_insert_sth'}=$sth;
1.1       matthew   708:     }
                    709:     my @Parameters; 
                    710:     if (ref($rowdata) eq 'ARRAY') {
                    711:         @Parameters = @$rowdata;
                    712:     } elsif (ref($rowdata) eq 'HASH') {
1.3       matthew   713:         foreach (@{$Tables{$tablename}->{'Col_order'}}) {
1.1       matthew   714:             push(@Parameters,&Apache::lonnet::escape($rowdata->{$_}));
                    715:         }
                    716:     } 
                    717:     $sth->execute(@Parameters);
                    718:     if ($sth->err) {
                    719:         $errorstring = "$dbh ATTEMPTED insert @Parameters RESULTING ERROR:\n".
                    720:             $sth->errstr;
                    721:         return undef;
                    722:     }
                    723:     $debugstring = "Stored row.";    
                    724:     return 1;
                    725: }
                    726: 
                    727: ###########################################
                    728: 
                    729: =pod
                    730: 
                    731: =item tables_in_db
                    732: 
                    733: Returns a list containing the names of all the tables in the database.
                    734: Returns undef on error.
                    735: 
                    736: =cut
                    737: 
                    738: ###########################################
                    739: sub tables_in_db {
1.3       matthew   740:     return undef if (!defined(&connect_to_db()));
1.1       matthew   741:     my $sth=$dbh->prepare('SHOW TABLES;');
                    742:     $sth->execute();
                    743:     if ($sth->err) {
1.3       matthew   744:         $errorstring = "$dbh ATTEMPTED:\n".'SHOW TABLES'.
                    745:             "\nRESULTING ERROR:\n".$sth->errstr;
1.1       matthew   746:         return undef;
                    747:     }
                    748:     my $aref = $sth->fetchall_arrayref;
                    749:     my @table_list=();
                    750:     foreach (@$aref) {
                    751:         push @table_list,$_->[0];
                    752:     }
                    753:     $debugstring = "Got list of tables in DB: @table_list";
                    754:     return @table_list;
                    755: }
                    756: 
                    757: ###########################################
                    758: 
                    759: =pod
                    760: 
                    761: =item &translate_id
                    762: 
                    763: Used internally to translate a numeric table id into a MySQL table name.
                    764: If the input $id contains non-numeric characters it is assumed to have 
                    765: already been translated.
                    766: 
                    767: Checks are NOT performed to see if the table actually exists.
                    768: 
                    769: =cut
                    770: 
                    771: ###########################################
                    772: sub translate_id {
                    773:     my $id = shift;
                    774:     # id should be a digit.  If it is not a digit we assume the given id
                    775:     # is complete and does not need to be translated.
                    776:     return $id if ($id =~ /\D/);  
                    777:     return $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.$id;
                    778: }
                    779: 
                    780: ###########################################
                    781: 
                    782: =pod
                    783: 
                    784: =item &check_table($id)
                    785: 
                    786: Checks to see if the requested table exists.  Returns 0 (no), 1 (yes), or 
                    787: undef (error).
                    788: 
                    789: =cut
                    790: 
                    791: ###########################################
                    792: sub check_table {
                    793:     my $table_id = shift;
1.3       matthew   794:     return undef if (!defined(&connect_to_db()));
                    795:     #
1.1       matthew   796:     $table_id = &translate_id($table_id);
                    797:     my @Table_list = &tables_in_db();
                    798:     my $result = 0;
                    799:     foreach (@Table_list) {
                    800:         if (/^$table_id$/) {
                    801:             $result = 1;
                    802:             last;
                    803:         }
                    804:     }
                    805:     # If it does not exist, make sure we do not have it listed in %Tables
                    806:     delete($Tables{$table_id}) if ((! $result) && exists($Tables{$table_id}));
                    807:     $debugstring = "check_table returned $result for $table_id";
                    808:     return $result;
                    809: }
                    810: 
                    811: 1;
                    812: 
                    813: __END__;

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