File:  [LON-CAPA] / loncom / interface / lonmysql.pm
Revision 1.34: download - view: text, annotated - select for diffs
Wed Aug 24 19:21:05 2005 UTC (18 years, 9 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Removed/commented out debugging code.

    1: # The LearningOnline Network with CAPA
    2: # MySQL utility functions
    3: #
    4: # $Id: lonmysql.pm,v 1.34 2005/08/24 19:21:05 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 POSIX qw(strftime mktime);
   35: use Apache::lonnet;
   36: 
   37: my $mysqluser;
   38: my $mysqlpassword;
   39: my $mysqldatabase;
   40: my %db_config;
   41: 
   42: sub set_mysql_user_and_password {
   43:     # If we are running under Apache and LONCAPA, use the LON-CAPA 
   44:     # user and password.  Otherwise...? ? ? ?
   45:     my ($input_mysqluser,$input_mysqlpassword,$input_mysqldatabase) = @_;
   46:     if (! defined($mysqldatabase)) {
   47:         $mysqldatabase = 'loncapa';
   48:     }
   49:     if (defined($input_mysqldatabase)) {
   50:         $mysqldatabase = $input_mysqldatabase;
   51:     }
   52:     if (! defined($mysqluser) || ! defined($mysqlpassword)) {
   53:         if (! eval 'require Apache::lonnet();') {
   54:             $mysqluser = 'www';
   55:             $mysqlpassword = $Apache::lonnet::perlvar{'lonSqlAccess'};
   56:         } else {
   57:             $mysqluser = '';
   58:             $mysqlpassword = '';
   59:         }
   60:     }
   61:     if (defined($input_mysqluser)) {
   62:         $mysqluser = $input_mysqluser;
   63:     } 
   64:     if (defined($input_mysqlpassword)) {
   65:         $mysqlpassword = $input_mysqlpassword;
   66:     }
   67: }
   68: 
   69: ######################################################################
   70: ######################################################################
   71: 
   72: =pod 
   73: 
   74: =head1 Name
   75: 
   76: lonmysql - LONCAPA MySQL utility functions
   77: 
   78: =head1 Synopsis
   79: 
   80: lonmysql contains utility functions to make accessing the mysql loncapa
   81: database easier.  
   82: 
   83: =head1 Description
   84: 
   85: lonmysql does its best to encapsulate all the database/table functions
   86: and provide a common interface.  The goal, however, is not to provide 
   87: a complete reimplementation of the DBI interface.  Instead we try to 
   88: make using mysql as painless as possible.
   89: 
   90: Each table has a numeric ID that is a parameter to most lonmysql functions.
   91: The table id is returned by &create_table.  
   92: If you lose the table id, it is lost forever.
   93: The table names in MySQL correspond to 
   94: $env{'user.name'}.'_'.$env{'user.domain'}.'_'.$table_id.  If the table id 
   95: is non-numeric, it is assumed to be the full name of a table.  If you pass
   96: the table id in a form, you MUST ensure that what you send to lonmysql is
   97: numeric, otherwise you are opening up all the tables in the MySQL database.
   98: 
   99: =over 4
  100: 
  101: =item Creating a table
  102: 
  103: To create a table, you need a description of its structure.  See the entry
  104: for &create_table for a description of what is needed.
  105: 
  106:  $table_id = &create_table({ 
  107:      id      => 'tableid',      # usually you will use the returned id
  108:      columns => (
  109:                  { name => 'id',
  110:                    type => 'INT',
  111:                    restrictions => 'NOT NULL',
  112:                    primary_key => 'yes',
  113:                    auto_inc    => 'yes'
  114:                    },
  115:                  { name => 'verbage',
  116:                    type => 'TEXT' },
  117:                  ),
  118:                        fulltext => [qw/verbage/],
  119:         });
  120: 
  121: The above command will create a table with two columns, 'id' and 'verbage'.
  122: 
  123: 'id' will be an integer which is autoincremented and non-null.
  124: 
  125: 'verbage' will be of type 'TEXT', which (conceivably) allows any length
  126: text string to be stored.  Depending on your intentions for this database,
  127: setting restrictions => 'NOT NULL' may help you avoid storing empty data.
  128: 
  129: the fulltext element sets up the 'verbage' column for 'FULLTEXT' searching.
  130: 
  131: 
  132: 
  133: =item Storing rows
  134: 
  135: Storing a row in a table requires calling &store_row($table_id,$data)
  136: 
  137: $data is either a hash reference or an array reference.  If it is an array
  138: reference, the data is passed as is (after being escaped) to the 
  139: "INSERT INTO <table> VALUES( ... )" SQL command.  If $data is a hash reference,
  140: the data will be placed into an array in the proper column order for the table
  141: and then passed to the database.
  142: 
  143: An example of inserting into the table created above is:
  144: 
  145: &store_row($table_id,[undef,'I am not a crackpot!']);
  146: 
  147: or equivalently,
  148: 
  149: &store_row($table_id,{ verbage => 'I am not a crackpot!'});
  150: 
  151: Since the above table was created with the first column ('id') as 
  152: autoincrement, providing a value is unnecessary even though the column was
  153: marked as 'NOT NULL'.
  154: 
  155: 
  156: 
  157: =item Retrieving rows
  158: 
  159: Retrieving rows requires calling get_rows:
  160: 
  161: @row = &Apache::lonmysql::get_rows($table_id,$condition)
  162: 
  163: This results in the query "SELECT * FROM <table> HAVING $condition".
  164: 
  165: @row = &Apache::lonmysql::get_rows($table_id,'id>20'); 
  166: 
  167: returns all rows with column 'id' greater than 20.
  168: 
  169: =back
  170: 
  171: =cut
  172: 
  173: ######################################################################
  174: ######################################################################
  175: =pod
  176: 
  177: =head1 Package Variables
  178: 
  179: =over 4
  180: 
  181: =cut
  182: 
  183: ##################################################
  184: ##################################################
  185: 
  186: =pod
  187: 
  188: =item %Tables
  189: 
  190: Holds information regarding the currently open connections.  Each key
  191: in the %Tables hash will be a unique table key.  The value associated 
  192: with a key is a hash reference.  Most values are initialized when the 
  193: table is created.
  194: 
  195: The following entries are allowed in the hash reference:
  196: 
  197: =over 4
  198: 
  199: =item Name
  200: 
  201: Table name.
  202: 
  203: =item Type            
  204: 
  205: The type of table, typically MyISAM.
  206: 
  207: =item Row_format
  208: 
  209: Describes how rows should be stored in the table.  DYNAMIC or STATIC.
  210: 
  211: =item Create_time
  212: 
  213: The date of the tables creation.
  214: 
  215: =item Update_time
  216: 
  217: The date of the last modification of the table.
  218: 
  219: =item Check_time
  220: 
  221: Usually NULL. 
  222: 
  223: =item Avg_row_length
  224: 
  225: The average length of the rows.
  226: 
  227: =item Data_length
  228: 
  229: The length of the data stored in the table (bytes)
  230: 
  231: =item Max_data_length
  232: 
  233: The maximum possible size of the table (bytes).
  234: 
  235: =item Index_length
  236: 
  237: The length of the index for the table (bytes)
  238: 
  239: =item Data_free
  240: 
  241: I have no idea what this is.
  242: 
  243: =item Comment 
  244: 
  245: The comment associated with the table.
  246: 
  247: =item Rows
  248: 
  249: The number of rows in the table.
  250: 
  251: =item Auto_increment
  252: 
  253: The value of the next auto_increment field.
  254: 
  255: =item Create_options
  256: 
  257: I have no idea.
  258: 
  259: =item Col_order
  260: 
  261: an array reference which holds the order of columns in the table.
  262: 
  263: =item row_insert_sth 
  264: 
  265: The statement handler for row inserts.
  266: 
  267: =item row_replace_sth 
  268: 
  269: The statement handler for row inserts.
  270: 
  271: =back
  272: 
  273: Col_order and row_insert_sth are kept internally by lonmysql and are not
  274: part of the usual MySQL table information.
  275: 
  276: =cut
  277: 
  278: ##################################################
  279: ##################################################
  280: my %Tables;
  281: 
  282: ##################################################
  283: ##################################################
  284: =pod
  285: 
  286: =item $errorstring
  287: 
  288: Holds the last error.
  289: 
  290: =cut
  291: ##################################################
  292: ##################################################
  293: my $errorstring;
  294: 
  295: ##################################################
  296: ##################################################
  297: =pod
  298: 
  299: =item $debugstring
  300: 
  301: Describes current events within the package.
  302: 
  303: =cut
  304: ##################################################
  305: ##################################################
  306: my $debugstring;
  307: 
  308: ##################################################
  309: ##################################################
  310: 
  311: =pod
  312: 
  313: =item $dbh
  314: 
  315: The database handler; The actual connection to MySQL via the perl DBI.
  316: 
  317: =cut
  318: 
  319: ##################################################
  320: ##################################################
  321: my $dbh;
  322: 
  323: ##################################################
  324: ##################################################
  325: 
  326: # End of global variable declarations
  327: 
  328: =pod
  329: 
  330: =back
  331: 
  332: =cut
  333: 
  334: ######################################################################
  335: ######################################################################
  336: 
  337: =pod
  338: 
  339: =head1 Internals
  340: 
  341: =over 4
  342: 
  343: =cut
  344: 
  345: ######################################################################
  346: ######################################################################
  347: 
  348: =pod
  349: 
  350: =item &connect_to_db()
  351: 
  352: Inputs: none.  
  353: 
  354: Returns: undef on error, 1 on success.
  355: 
  356: Checks to make sure the database has been connected to.  If not, the
  357: connection is established.  
  358: 
  359: =cut
  360: 
  361: ###############################
  362: sub connect_to_db { 
  363:     return 1 if ($dbh);
  364:     if (! defined($mysqluser) || ! defined($mysqlpassword)) {
  365:         &set_mysql_user_and_password();
  366:     }
  367:     if (! ($dbh = DBI->connect("DBI:mysql:$mysqldatabase",$mysqluser,$mysqlpassword,
  368:                                { RaiseError=>0,PrintError=>0}))) {
  369:         $debugstring = "Unable to connect to loncapa database.";    
  370:         if (! defined($dbh)) {
  371:             $debugstring = "Unable to connect to loncapa database.";
  372:             $errorstring = "dbh was undefined.";
  373:         } elsif ($dbh->err) {
  374:             $errorstring = "Connection error: ".$dbh->errstr;
  375:         }
  376:         return undef;
  377:     }
  378:     $debugstring = "Successfully connected to loncapa database.";    
  379:     # Determine DB configuration
  380:     undef(%db_config);
  381:     my $sth = $dbh->prepare("SHOW VARIABLES");
  382:     $sth->execute();
  383:     if ($sth->err()) {
  384:         $debugstring = "Unable to retrieve db config variables";
  385:         return undef;
  386:     }
  387:     foreach my $row (@{$sth->fetchall_arrayref}) {
  388:         $db_config{$row->[0]} = $row->[1];
  389:     }
  390:     #&Apache::lonnet::logthis("MySQL configuration variables");
  391:     #while (my ($k,$v) = each(%db_config)) {
  392:     #    &Apache::lonnet::logthis("    '$k' => '$v'");
  393:     #}
  394:     #
  395:     return 1;
  396: }
  397: 
  398: ###############################
  399: 
  400: =pod
  401: 
  402: =item &verify_sql_connection()
  403: 
  404: Inputs: none.
  405: 
  406: Returns: 0 (failure) or 1 (success)
  407: 
  408: Checks to make sure the database can be connected to.  It does not
  409: initialize anything in the lonmysql package.
  410: 
  411: =cut
  412: 
  413: ###############################
  414: sub verify_sql_connection {
  415:     if (! defined($mysqluser) || ! defined($mysqlpassword)) {
  416:         &set_mysql_user_and_password();
  417:     }
  418:     my $connection;
  419:     if (! ($connection = DBI->connect("DBI:mysql:loncapa",
  420:                                       $mysqluser,$mysqlpassword,
  421:                                       { RaiseError=>0,PrintError=>0}))) {
  422:         return 0;
  423:     }
  424:     undef($connection);
  425:     return 1;
  426: }
  427: 
  428: ###############################
  429: 
  430: =pod
  431: 
  432: =item &disconnect_from_db()
  433: 
  434: Inputs: none.
  435: 
  436: Returns: Always returns 1.
  437: 
  438: Severs the connection to the mysql database.
  439: 
  440: =cut
  441: 
  442: ###############################
  443: sub disconnect_from_db { 
  444:     foreach (keys(%Tables)) {
  445:         # Supposedly, having statement handlers running around after the
  446:         # database connection has been lost will cause trouble.  So we 
  447:         # kill them off just to be sure.
  448:         if (exists($Tables{$_}->{'row_insert_sth'})) {
  449:             delete($Tables{$_}->{'row_insert_sth'});
  450:         }
  451:         if (exists($Tables{$_}->{'row_replace_sth'})) {
  452:             delete($Tables{$_}->{'row_replace_sth'});
  453:         }
  454:     }
  455:     $dbh->disconnect if ($dbh);
  456:     $debugstring = "Disconnected from database.";
  457:     $dbh = undef;
  458:     return 1;
  459: }
  460: 
  461: ###############################
  462: 
  463: =pod
  464: 
  465: =item &number_of_rows()
  466: 
  467: Input: table identifier
  468: 
  469: Returns: the number of rows in the given table, undef on error.
  470: 
  471: =cut
  472: 
  473: ###############################
  474: sub number_of_rows { 
  475:     my ($table_id) = @_;
  476:     return undef if (! defined(&connect_to_db()));
  477:     return undef if (! defined(&update_table_info($table_id)));
  478:     return $Tables{&translate_id($table_id)}->{'Rows'};
  479: }
  480: ###############################
  481: 
  482: =pod
  483: 
  484: =item &get_dbh()
  485: 
  486: Input: nothing
  487: 
  488: Returns: the database handler, or undef on error.
  489: 
  490: This routine allows the programmer to gain access to the database handler.
  491: Be careful.
  492: 
  493: =cut
  494: 
  495: ###############################
  496: sub get_dbh { 
  497:     return undef if (! defined(&connect_to_db()));
  498:     return $dbh;
  499: }
  500: 
  501: ###############################
  502: 
  503: =pod
  504: 
  505: =item &get_error()
  506: 
  507: Inputs: none.
  508: 
  509: Returns: The last error reported.
  510: 
  511: =cut
  512: 
  513: ###############################
  514: sub get_error {
  515:     return $errorstring;
  516: }
  517: 
  518: ###############################
  519: 
  520: =pod
  521: 
  522: =item &get_debug()
  523: 
  524: Inputs: none.
  525: 
  526: Returns: A string describing the internal state of the lonmysql package.
  527: 
  528: =cut
  529: 
  530: ###############################
  531: sub get_debug {
  532:     return $debugstring;
  533: }
  534: 
  535: ###############################
  536: 
  537: =pod
  538: 
  539: =item &update_table_info()
  540: 
  541: Inputs: table id
  542: 
  543: Returns: undef on error, 1 on success.
  544: 
  545: &update_table_info updates the %Tables hash with current information about
  546: the given table.  
  547: 
  548: The default MySQL table status fields are:
  549: 
  550:    Name             Type            Row_format
  551:    Max_data_length  Index_length    Data_free
  552:    Create_time      Update_time     Check_time
  553:    Avg_row_length   Data_length     Comment 
  554:    Rows             Auto_increment  Create_options
  555: 
  556: Additionally, "Col_order" is updated as well.
  557: 
  558: =cut
  559: 
  560: ###############################
  561: sub update_table_info { 
  562:     my ($table_id) = @_;
  563:     return undef if (! defined(&connect_to_db()));
  564:     my $table_status = &check_table($table_id);
  565:     return undef if (! defined($table_status));
  566:     if (! $table_status) {
  567:         $errorstring = "table $table_id does not exist.";
  568:         return undef;
  569:     }
  570:     my $tablename = &translate_id($table_id);
  571:     #
  572:     # Get MySQLs table status information.
  573:     #
  574:     my @tabledesc;
  575:     my ($major_version) = ($db_config{'version'} =~ /^(\d)\./);
  576:     if ($major_version <= 3) {
  577:         @tabledesc = qw/
  578:             Name Type Row_format Rows Avg_row_length Data_length
  579:             Max_data_length Index_length Data_free Auto_increment 
  580:             Create_time Update_time Check_time Create_options Comment/;
  581:     } else { # At least 4 has this structure...
  582:         @tabledesc = qw/
  583:             Name Engine Version Row_format Rows Avg_row_length Data_length
  584:             Max_data_length Index_length Data_free Auto_increment Create_time 
  585:             Update_time Check_time Collation Checksum Create_options Comment/;
  586:     }
  587:     my $db_command = "SHOW TABLE STATUS FROM loncapa LIKE '$tablename'";
  588:     my $sth = $dbh->prepare($db_command);
  589:     $sth->execute();
  590:     if ($sth->err) {
  591:         $errorstring = "$dbh ATTEMPTED:\n".$db_command."\nRESULTING ERROR:\n".
  592:             $sth->errstr;
  593:         &disconnect_from_db();
  594:         return undef;
  595:     }
  596:     #
  597:     my @info=$sth->fetchrow_array;
  598:     for (my $i=0;$i<= $#info ; $i++) {
  599:         if ($tabledesc[$i] !~ /^(Create_|Update_|Check_)time$/) {
  600:             $Tables{$tablename}->{$tabledesc[$i]}= 
  601:                 &unsqltime($info[$i]);
  602:         } else {
  603:             $Tables{$tablename}->{$tabledesc[$i]}= $info[$i];
  604:         }
  605:     }
  606:     #
  607:     # Determine the column order
  608:     #
  609:     $db_command = "DESCRIBE $tablename";
  610:     $sth = $dbh->prepare($db_command);
  611:     $sth->execute();
  612:     if ($sth->err) {
  613:         $errorstring = "$dbh ATTEMPTED:\n".$db_command."\nRESULTING ERROR:\n".
  614:             $sth->errstr;
  615:         &disconnect_from_db();
  616:         return undef;
  617:     }
  618:     my $aref=$sth->fetchall_arrayref;
  619:     $Tables{$tablename}->{'Col_order'}=[]; # Clear values.
  620:     # The values we want are the 'Field' entries, the first column.
  621:     for (my $i=0;$i< @$aref ; $i++) {
  622:         push @{$Tables{$tablename}->{'Col_order'}},$aref->[$i]->[0];
  623:     }
  624:     #
  625:     $debugstring = "Retrieved table info for $tablename";
  626:     return 1;
  627: }
  628: 
  629: ###############################
  630: 
  631: =pod
  632: 
  633: =item &table_information()
  634: 
  635: Inputs: table id
  636: 
  637: Returns: hash with the table status
  638: 
  639: =cut
  640: 
  641: ###############################
  642: sub table_information {
  643:     my $table_id=shift;
  644:     if (&update_table_info($table_id)) {
  645: 	return %{$Tables{$table_id}};
  646:     } else {
  647: 	return ();
  648:     }
  649: }
  650: 
  651: ###############################
  652: 
  653: =pod
  654: 
  655: =item &col_order()
  656: 
  657: Inputs: table id
  658: 
  659: Returns: array with column order
  660: 
  661: =cut
  662: 
  663: ###############################
  664: sub col_order {
  665:     my $table_id=shift;
  666:     if (&update_table_info($table_id)) {
  667: 	return @{$Tables{$table_id}->{'Col_order'}};
  668:     } else {
  669: 	return ();
  670:     }
  671: }
  672: 
  673: ###############################
  674: 
  675: =pod
  676: 
  677: =item &create_table()
  678: 
  679: Inputs: 
  680:     table description, see &build_table_creation_request
  681: Returns:
  682:     undef on error, table id on success.
  683: 
  684: =cut
  685: 
  686: ###############################
  687: sub create_table {
  688:     return undef if (!defined(&connect_to_db($dbh)));
  689:     my ($table_des)=@_;
  690:     my ($request,$table_id) = &build_table_creation_request($table_des);
  691:     #
  692:     # Execute the request to create the table
  693:     #############################################
  694:     my $count = $dbh->do($request);
  695:     if (! defined($count)) {
  696:         $errorstring = "$dbh ATTEMPTED:\n".$request."\nRESULTING ERROR:\n".
  697:             $dbh->errstr();
  698:         return undef;
  699:     }
  700:     my $tablename = &translate_id($table_id);
  701:     delete($Tables{$tablename}) if (exists($Tables{$tablename}));
  702:     return undef if (! defined(&update_table_info($table_id)));
  703:     $debugstring = "Created table $tablename at time ".time.
  704:         " with request\n$request";
  705:     return $table_id;
  706: }
  707: 
  708: ###############################
  709: 
  710: =pod
  711: 
  712: =item build_table_creation_request
  713: 
  714: Input: table description
  715: 
  716:     table description = {
  717:         permanent  => 'yes' or 'no',
  718:         columns => [
  719:                     { name         => 'colA',
  720:                       type         => mysql type,
  721:                       restrictions => 'NOT NULL' or empty,
  722:                       primary_key  => 'yes' or empty,
  723:                       auto_inc     => 'yes' or empty,
  724:                   },
  725:                     { name => 'colB',
  726:                       ...
  727:                   },
  728:                     { name => 'colC',
  729:                       ...
  730:                   },
  731:         ],
  732:         'PRIMARY KEY' => (index_col_name,...),
  733:          KEY => [{ name => 'idx_name', 
  734:                   columns => (col1,col2,..),},],
  735:          INDEX => [{ name => 'idx_name', 
  736:                     columns => (col1,col2,..),},],
  737:          UNIQUE => [{ index => 'yes',
  738:                      name => 'idx_name',
  739:                      columns => (col1,col2,..),},],
  740:          FULLTEXT => [{ index => 'yes',
  741:                        name => 'idx_name',
  742:                        columns => (col1,col2,..),},],
  743: 
  744:     }
  745: 
  746: Returns: scalar string containing mysql commands to create the table
  747: 
  748: =cut
  749: 
  750: ###############################
  751: sub build_table_creation_request {
  752:     my ($table_des)=@_;
  753:     #
  754:     # Build request to create table
  755:     ##################################
  756:     my @Columns;
  757:     my $col_des;
  758:     my $table_id;
  759:     if (exists($table_des->{'id'})) {
  760:         $table_id = $table_des->{'id'};
  761:     } else {
  762:         $table_id = &get_new_table_id();
  763:     }
  764:     my $tablename = &translate_id($table_id);
  765:     my $request = "CREATE TABLE IF NOT EXISTS ".$tablename." ";
  766:     foreach my $coldata (@{$table_des->{'columns'}}) {
  767:         my $column = $coldata->{'name'};
  768:         next if (! defined($column));
  769:         $col_des = '';
  770:         if (lc($coldata->{'type'}) =~ /(enum|set)/) { # 'enum' or 'set'
  771:             $col_des.=$column." ".$coldata->{'type'}."('".
  772:                 join("', '",@{$coldata->{'values'}})."')";
  773:         } else {
  774:             $col_des.=$column." ".$coldata->{'type'};
  775:             if (exists($coldata->{'size'})) {
  776:                 $col_des.="(".$coldata->{'size'}.")";
  777:             }
  778:         }
  779:         # Modifiers
  780:         if (exists($coldata->{'restrictions'})){
  781:             $col_des.=" ".$coldata->{'restrictions'};
  782:         }
  783:         if (exists($coldata->{'default'})) {
  784:             $col_des.=" DEFAULT '".$coldata->{'default'}."'";
  785:         }
  786:         $col_des.=' AUTO_INCREMENT' if (exists($coldata->{'auto_inc'}) &&
  787:                                         ($coldata->{'auto_inc'} eq 'yes'));
  788:         $col_des.=' PRIMARY KEY'    if (exists($coldata->{'primary_key'}) &&
  789:                                         ($coldata->{'primary_key'} eq 'yes'));
  790:     } continue {
  791:         # skip blank items.
  792:         push (@Columns,$col_des) if ($col_des ne '');
  793:     }
  794:     if (exists($table_des->{'PRIMARY KEY'})) {
  795:         push (@Columns,'PRIMARY KEY ('.join(',',@{$table_des->{'PRIMARY KEY'}})
  796:               .')');
  797:     }
  798:     #
  799:     foreach my $indextype ('KEY','INDEX') {
  800:         next if (!exists($table_des->{$indextype}));
  801:         foreach my $indexdescription (@{$table_des->{$indextype}}) {
  802:             my $text = $indextype.' ';
  803:             if (exists($indexdescription->{'name'})) {
  804:                 $text .=$indexdescription->{'name'};
  805:             }
  806:             $text .= ' ('.join(',',@{$indexdescription->{'columns'}}).')';
  807:             push (@Columns,$text);
  808:         }
  809:     }
  810:     #
  811:     foreach my $indextype ('UNIQUE','FULLTEXT') {
  812:         next if (! exists($table_des->{$indextype}));
  813:         foreach my $indexdescription (@{$table_des->{$indextype}}) {
  814:             my $text = $indextype.' ';
  815:             if (exists($indexdescription->{'index'}) &&
  816:                 $indexdescription->{'index'} eq 'yes') {
  817:                 $text .= 'INDEX ';
  818:             }
  819:             if (exists($indexdescription->{'name'})) {
  820:                 $text .=$indexdescription->{'name'};
  821:             }
  822:             $text .= ' ('.join(',',@{$indexdescription->{'columns'}}).')';
  823:             push (@Columns,$text);
  824:         }
  825:     }
  826:     #
  827:     $request .= "(".join(", ",@Columns).") ";
  828:     unless($table_des->{'permanent'} eq 'yes') {
  829:         $request.="COMMENT = 'temporary' ";
  830:     } 
  831:     $request .= "TYPE=MYISAM";
  832:     return $request,$table_id;
  833: }
  834: 
  835: ###############################
  836: 
  837: =pod
  838: 
  839: =item &get_new_table_id()
  840: 
  841: Used internally to prevent table name collisions.
  842: 
  843: =cut
  844: 
  845: ###############################
  846: sub get_new_table_id {
  847:     my $newid = 0;
  848:     my @tables = &tables_in_db();
  849:     foreach (@tables) {
  850:         if (/^$env{'user.name'}_$env{'user.domain'}_(\d+)$/) {
  851:             $newid = $1 if ($1 > $newid);
  852:         }
  853:     }
  854:     return ++$newid;
  855: }
  856: 
  857: ###############################
  858: 
  859: =pod
  860: 
  861: =item &get_rows()
  862: 
  863: Inputs: $table_id,$condition
  864: 
  865: Returns: undef on error, an array ref to (array of) results on success.
  866: 
  867: Internally, this function does a 'SELECT * FROM table WHERE $condition'.
  868: $condition = 'id>0' will result in all rows where column 'id' has a value
  869: greater than 0 being returned.
  870: 
  871: =cut
  872: 
  873: ###############################
  874: sub get_rows {
  875:     my ($table_id,$condition) = @_;
  876:     return undef if (! defined(&connect_to_db()));
  877:     my $table_status = &check_table($table_id);
  878:     return undef if (! defined($table_status));
  879:     if (! $table_status) {
  880:         $errorstring = "table $table_id does not exist.";
  881:         return undef;
  882:     }
  883:     my $tablename = &translate_id($table_id);
  884:     my $request;
  885:     if (defined($condition) && $condition ne '') {
  886:         $request = 'SELECT * FROM '.$tablename.' WHERE '.$condition;
  887:     } else {
  888:         $request = 'SELECT * FROM '.$tablename;
  889:         $condition = 'no condition';
  890:     }
  891:     my $sth=$dbh->prepare($request);
  892:     $sth->execute();
  893:     if ($sth->err) {
  894:         $errorstring = "$dbh ATTEMPTED:\n".$request."\nRESULTING ERROR:\n".
  895:             $sth->errstr;
  896:         $debugstring = "Failed to get rows matching $condition";
  897:         return undef;
  898:     }
  899:     $debugstring = "Got rows matching $condition";
  900:     my @Results = @{$sth->fetchall_arrayref};
  901:     return @Results;
  902: }
  903: 
  904: ###############################
  905: 
  906: =pod
  907: 
  908: =item &store_row()
  909: 
  910: Inputs: table id, row data
  911: 
  912: returns undef on error, 1 on success.
  913: 
  914: =cut
  915: 
  916: ###############################
  917: sub store_row {
  918:     my ($table_id,$rowdata) = @_;
  919:     # 
  920:     return undef if (! defined(&connect_to_db()));
  921:     my $table_status = &check_table($table_id);
  922:     return undef if (! defined($table_status));
  923:     if (! $table_status) {
  924:         $errorstring = "table $table_id does not exist.";
  925:         return undef;
  926:     }
  927:     #
  928:     my $tablename = &translate_id($table_id);
  929:     #
  930:     my $sth;
  931:     if (exists($Tables{$tablename}->{'row_insert_sth'})) {
  932:         $sth = $Tables{$tablename}->{'row_insert_sth'};
  933:     } else {
  934:         # Build the insert statement handler
  935:         return undef if (! defined(&update_table_info($table_id)));
  936:         my $insert_request = 'INSERT INTO '.$tablename.' VALUES(';
  937:         foreach (@{$Tables{$tablename}->{'Col_order'}}) {
  938:             $insert_request.="?,";
  939:         }
  940:         chop $insert_request;
  941:         $insert_request.=")";
  942:         $sth=$dbh->prepare($insert_request);
  943:         $Tables{$tablename}->{'row_insert_sth'}=$sth;
  944:     }
  945:     my @Parameters; 
  946:     if (ref($rowdata) eq 'ARRAY') {
  947:         @Parameters = @$rowdata;
  948:     } elsif (ref($rowdata) eq 'HASH') {
  949:         foreach (@{$Tables{$tablename}->{'Col_order'}}) {
  950:             push(@Parameters,$rowdata->{$_});
  951:         }
  952:     } 
  953:     $sth->execute(@Parameters);
  954:     if ($sth->err) {
  955:         $errorstring = "$dbh ATTEMPTED insert @Parameters RESULTING ERROR:\n".
  956:             $sth->errstr;
  957:         return undef;
  958:     }
  959:     $debugstring = "Stored row.";    
  960:     return 1;
  961: }
  962: 
  963: 
  964: ###############################
  965: 
  966: =pod
  967: 
  968: =item &bulk_store_rows()
  969: 
  970: Inputs: table id, [columns],[[row data1].[row data2],...]
  971: 
  972: returns undef on error, 1 on success.
  973: 
  974: =cut
  975: 
  976: ###############################
  977: sub bulk_store_rows {
  978:     my ($table_id,$columns,$rows) = @_;
  979:     # 
  980:     return undef if (! defined(&connect_to_db()));
  981:     my $dbh = &get_dbh();
  982:     return undef if (! defined($dbh));
  983:     my $table_status = &check_table($table_id);
  984:     return undef if (! defined($table_status));
  985:     if (! $table_status) {
  986:         $errorstring = "table $table_id does not exist.";
  987:         return undef;
  988:     }
  989:     #
  990:     my $tablename = &translate_id($table_id);
  991:     #
  992:     my $request = 'INSERT IGNORE INTO '.$tablename.' ';
  993:     if (defined($columns) && ref($columns) eq 'ARRAY') {
  994:         $request .= join(',',@$columns).' ';
  995:     }
  996:     if (! defined($rows) || ref($rows) ne 'ARRAY') {
  997:         $errorstring = "no input rows given.";
  998:         return undef;
  999:     }
 1000:     $request .= 'VALUES ';
 1001:     foreach my $row (@$rows) {
 1002:         # avoid doing row stuff here...
 1003:         $request .= '('.join(',',@$row).'),';
 1004:     }
 1005:     $request =~ s/,$//;
 1006:     # $debugstring = "Executed ".$/.$request; # commented out - this is big
 1007:     $dbh->do($request);
 1008:     if ($dbh->err) {
 1009:         $errorstring = 'Attempted '.$/.$request.$/.'Got error '.$dbh->errstr();
 1010:         return undef;
 1011:     }
 1012:     return 1;
 1013: }
 1014: 
 1015: 
 1016: ###############################
 1017: 
 1018: =pod
 1019: 
 1020: =item &replace_row()
 1021: 
 1022: Inputs: table id, row data
 1023: 
 1024: returns undef on error, 1 on success.
 1025: 
 1026: Acts like &store_row() but uses the 'REPLACE' command instead of 'INSERT'.
 1027: 
 1028: =cut
 1029: 
 1030: ###############################
 1031: sub replace_row {
 1032:     my ($table_id,$rowdata) = @_;
 1033:     # 
 1034:     return undef if (! defined(&connect_to_db()));
 1035:     my $table_status = &check_table($table_id);
 1036:     return undef if (! defined($table_status));
 1037:     if (! $table_status) {
 1038:         $errorstring = "table $table_id does not exist.";
 1039:         return undef;
 1040:     }
 1041:     #
 1042:     my $tablename = &translate_id($table_id);
 1043:     #
 1044:     my $sth;
 1045:     if (exists($Tables{$tablename}->{'row_replace_sth'})) {
 1046:         $sth = $Tables{$tablename}->{'row_replace_sth'};
 1047:     } else {
 1048:         # Build the insert statement handler
 1049:         return undef if (! defined(&update_table_info($table_id)));
 1050:         my $replace_request = 'REPLACE INTO '.$tablename.' VALUES(';
 1051:         foreach (@{$Tables{$tablename}->{'Col_order'}}) {
 1052:             $replace_request.="?,";
 1053:         }
 1054:         chop $replace_request;
 1055:         $replace_request.=")";
 1056:         $sth=$dbh->prepare($replace_request);
 1057:         $Tables{$tablename}->{'row_replace_sth'}=$sth;
 1058:     }
 1059:     my @Parameters; 
 1060:     if (ref($rowdata) eq 'ARRAY') {
 1061:         @Parameters = @$rowdata;
 1062:     } elsif (ref($rowdata) eq 'HASH') {
 1063:         foreach (@{$Tables{$tablename}->{'Col_order'}}) {
 1064:             push(@Parameters,$rowdata->{$_});
 1065:         }
 1066:     } 
 1067:     $sth->execute(@Parameters);
 1068:     if ($sth->err) {
 1069:         $errorstring = "$dbh ATTEMPTED replace @Parameters RESULTING ERROR:\n".
 1070:             $sth->errstr;
 1071:         return undef;
 1072:     }
 1073:     $debugstring = "Stored row.";    
 1074:     return 1;
 1075: }
 1076: 
 1077: ###########################################
 1078: 
 1079: =pod
 1080: 
 1081: =item &tables_in_db()
 1082: 
 1083: Returns a list containing the names of all the tables in the database.
 1084: Returns undef on error.
 1085: 
 1086: =cut
 1087: 
 1088: ###########################################
 1089: sub tables_in_db {
 1090:     return undef if (!defined(&connect_to_db()));
 1091:     my $sth=$dbh->prepare('SHOW TABLES');
 1092:     $sth->execute();
 1093:     $sth->execute();
 1094:     my $aref = $sth->fetchall_arrayref;
 1095:     if ($sth->err()) {
 1096:         $errorstring = 
 1097:             "$dbh ATTEMPTED:\n".'fetchall_arrayref after SHOW TABLES'.
 1098:             "\nRESULTING ERROR:\n".$sth->errstr;
 1099:         return undef;
 1100:     }
 1101:     my @table_list;
 1102:     foreach (@$aref) {
 1103:         push(@table_list,$_->[0]);
 1104:     }
 1105:     $debugstring = "Got list of tables in DB: ".join(',',@table_list);
 1106:     return(@table_list);
 1107: }
 1108: 
 1109: ###########################################
 1110: 
 1111: =pod
 1112: 
 1113: =item &translate_id()
 1114: 
 1115: Used internally to translate a numeric table id into a MySQL table name.
 1116: If the input $id contains non-numeric characters it is assumed to have 
 1117: already been translated.
 1118: 
 1119: Checks are NOT performed to see if the table actually exists.
 1120: 
 1121: =cut
 1122: 
 1123: ###########################################
 1124: sub translate_id {
 1125:     my $id = shift;
 1126:     # id should be a digit.  If it is not a digit we assume the given id
 1127:     # is complete and does not need to be translated.
 1128:     return $id if ($id =~ /\D/);  
 1129:     return $env{'user.name'}.'_'.$env{'user.domain'}.'_'.$id;
 1130: }
 1131: 
 1132: ###########################################
 1133: 
 1134: =pod
 1135: 
 1136: =item &check_table()
 1137: 
 1138: Input: table id
 1139: 
 1140: Checks to see if the requested table exists.  Returns 0 (no), 1 (yes), or 
 1141: undef (error).
 1142: 
 1143: =cut
 1144: 
 1145: ###########################################
 1146: sub check_table {
 1147:     my $table_id = shift;
 1148:     return undef if (!defined(&connect_to_db()));
 1149:     #
 1150:     $table_id = &translate_id($table_id);
 1151:     my @Table_list = &tables_in_db();
 1152:     my $result = 0;
 1153:     foreach (@Table_list) {
 1154:         if ($_ eq $table_id) {
 1155:             $result = 1;
 1156:             last;
 1157:         }
 1158:     }
 1159:     # If it does not exist, make sure we do not have it listed in %Tables
 1160:     delete($Tables{$table_id}) if ((! $result) && exists($Tables{$table_id}));
 1161:     $debugstring = "check_table returned $result for $table_id";
 1162:     return $result;
 1163: }
 1164: 
 1165: ###########################################
 1166: 
 1167: =pod
 1168: 
 1169: =item &remove_from_table()
 1170: 
 1171: Input: $table_id, $column, $value
 1172: 
 1173: Returns: the number of rows deleted.  undef on error.
 1174: 
 1175: Executes a "delete from $tableid where $column like binary '$value'".
 1176: 
 1177: =cut
 1178: 
 1179: ###########################################
 1180: sub remove_from_table {
 1181:     my ($table_id,$column,$value) = @_;
 1182:     return undef if (!defined(&connect_to_db()));
 1183:     #
 1184:     $table_id = &translate_id($table_id);
 1185:     my $command = 'DELETE FROM '.$table_id.' WHERE '.$column.
 1186:         " LIKE BINARY ".$dbh->quote($value);
 1187:     my $sth = $dbh->prepare($command); 
 1188:     unless ($sth->execute()) {
 1189:         $errorstring = "ERROR on execution of ".$command."\n".$sth->errstr;
 1190:         return undef;
 1191:     }
 1192:     $debugstring = $command;
 1193:     my $rows = $sth->rows;
 1194:     return $rows;
 1195: }
 1196: 
 1197: ###########################################
 1198: 
 1199: =pod
 1200: 
 1201: =item drop_table($table_id)
 1202: 
 1203: Issues a 'drop table if exists' command
 1204: 
 1205: =cut
 1206: 
 1207: ###########################################
 1208: 
 1209: sub drop_table {
 1210:     my ($table_id) = @_;
 1211:     return undef if (!defined(&connect_to_db()));
 1212:     #
 1213:     $table_id = &translate_id($table_id);
 1214:     my $command = 'DROP TABLE IF EXISTS '.$table_id;
 1215:     my $sth = $dbh->prepare($command); 
 1216:     $sth->execute();
 1217:     if ($sth->err) {
 1218:         $errorstring = "ERROR on execution of ".$command."\n".$sth->errstr;
 1219:         return undef;
 1220:     }
 1221:     $debugstring = $command;
 1222:     delete($Tables{$table_id}); # remove any knowledge of the table
 1223:     return 1; # if we got here there was no error, so return a 'true' value
 1224: }
 1225: 
 1226: ##########################################
 1227: 
 1228: =pod
 1229: 
 1230: =item fix_table_name 
 1231: 
 1232: Fixes a table name so that it will work with MySQL.
 1233: 
 1234: =cut
 1235: 
 1236: ##########################################
 1237: sub fix_table_name {
 1238:     my ($name) = @_;
 1239:     $name =~ s/^(\d+[eE]\d+)/_$1/;
 1240:     return $name;
 1241: }
 1242: 
 1243: 
 1244: # ---------------------------- convert 'time' format into a datetime sql format
 1245: sub sqltime {
 1246:     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
 1247: 	localtime(&unsqltime($_[0]));
 1248:     $mon++; $year+=1900;
 1249:     return "$year-$mon-$mday $hour:$min:$sec";
 1250: }
 1251: 
 1252: sub maketime {
 1253:     my %th=@_;
 1254:     return POSIX::mktime(($th{'seconds'},$th{'minutes'},$th{'hours'},
 1255:                           $th{'day'},$th{'month'}-1,
 1256:                           $th{'year'}-1900,0,0,$th{'dlsav'}));
 1257: }
 1258: 
 1259: 
 1260: #########################################
 1261: #
 1262: # Retro-fixing of un-backward-compatible time format
 1263: 
 1264: sub unsqltime {
 1265:     my $timestamp=shift;
 1266:     if ($timestamp=~/^(\d+)\-(\d+)\-(\d+)\s+(\d+)\:(\d+)\:(\d+)$/) {
 1267:         $timestamp=&maketime('year'=>$1,'month'=>$2,'day'=>$3,
 1268:                              'hours'=>$4,'minutes'=>$5,'seconds'=>$6);
 1269:     }
 1270:     return $timestamp;
 1271: }
 1272: 
 1273: 
 1274: 1;
 1275: 
 1276: __END__;
 1277: 
 1278: =pod
 1279: 
 1280: =back
 1281: 
 1282: =cut

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