home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Mysql.pm < prev    next >
Encoding:
Perl POD Document  |  2003-10-17  |  23.3 KB  |  842 lines

  1. # -*- perl -*-
  2.  
  3. package Mysql;
  4.  
  5. use 5.004;
  6. use strict;
  7.  
  8. require Carp;
  9. require DynaLoader;
  10. require Exporter;
  11. require DBI;
  12. require Mysql::Statement;
  13. require DBD::mysql;
  14.  
  15. use vars qw($QUIET @ISA @EXPORT @EXPORT_OK $VERSION $db_errstr);
  16.  
  17. $db_errstr = '';
  18. $QUIET  = 0;
  19. @ISA    = qw(DBI); # Inherits Exporter and DynaLoader via DBI
  20. $VERSION = '1.2401';
  21.  
  22. # @EXPORT is a relict from old times...
  23. @EXPORT = qw(
  24.          CHAR_TYPE
  25.          INT_TYPE
  26.          REAL_TYPE
  27.         );
  28. @EXPORT_OK = qw(
  29.         DATE_TYPE
  30.         TIME_TYPE
  31.            );
  32.  
  33. my $FETCH_map = {
  34.     'HOST' => '_host',
  35.     'DATABASE' => 'database'
  36. };
  37.  
  38. sub FETCH ($$) {
  39.     my($self, $key) = @_;
  40.     if ($key eq 'COMPATIBILITY') {
  41.     return $self->{'COMPATIBILITY'};
  42.     }
  43.     if (exists($FETCH_map->{$key})) {
  44.     $key = $FETCH_map->{$key};
  45.     }
  46.     my($dbh) = $self->{'dbh'};
  47.     $dbh->{$key};
  48. }
  49.  
  50. sub STORE ($$$) {
  51.     my($self, $key, $val) = @_;
  52.     if ($key eq 'COMPATIBILITY') {
  53.     $self->{'COMPATIBILITY'} = $val;
  54.     } else {
  55.     $self->{'dbh'}->{$key} = $val;
  56.     }
  57. }
  58.  
  59. sub connect ($;$$$$) {
  60.     my($class, $host, $db, $user, $password) = @_;
  61.     my($self) = { 'host' => ($host || ''),
  62.           'user' => $user,
  63.           'password' => $password,
  64.           'db' => $db,
  65.               'driver' => 'mysql',
  66.               'COMPATIBILITY' => 1 };
  67.     bless($self, $class);
  68.     $self->{'drh'} = DBI->install_driver($self->{'driver'});
  69.     if ($db) {
  70.     my $dsn = "DBI:mysql:database=$db;host=$host";
  71.     my $dbh = $class->SUPER::connect($dsn, $user, $password);
  72.     if (!$dbh) {
  73.         $db_errstr = $DBI::errstr;
  74.         return undef;
  75.     }
  76.     $self->{'dbh'} = $dbh;
  77.     $dbh->{'CompatMode'} = 1;
  78.     $dbh->{'PrintError'} = !$Mysql::QUIET;
  79.     }
  80.     $self;
  81. }
  82.  
  83. sub DESTROY {
  84.     my $self = shift;
  85.     my $dbh = $self->{'dbh'};
  86.     if ($dbh) {
  87.     local $SIG{'__WARN__'} = sub {};
  88.     $dbh->disconnect();
  89.     }
  90. }
  91.  
  92. sub selectdb ($$) {
  93.     my($self, $db) = @_;
  94.     my $dsn = "DBI:mysql:database=$db:host=" . $self->{'host'};
  95.     my $dbh = DBI->connect($dsn, $self->{'user'}, $self->{'password'});
  96.     if (!$dbh) {
  97.     $db_errstr = $self->{'errstr'} = $DBI::errstr;
  98.     $self->{'errno'} = $DBI::err;
  99.     undef;
  100.     } else {
  101.     if ($self->{'dbh'}) {
  102.         local $SIG{'__WARN__'} = sub {};
  103.         $self->{'dbh'}->disconnect();
  104.     }
  105.     $self->{'dbh'} = $dbh;
  106.     $self->{'db'} = $db;
  107.     $self;
  108.     }
  109. }
  110.  
  111. sub listdbs ($) {
  112.     my($self) = shift;
  113.     my $drh = $self->{'drh'};
  114.     my $host = $self->{'host'};
  115.     my @dbs;
  116.     if ($host) {
  117.     @dbs = $drh->func($host, "", $self->{'user'},
  118.               $self->{'password'}, "_ListDBs");
  119.     } else {
  120.     @dbs = $drh->func("", "", $self->{'user'},
  121.               $self->{'password'}, "_ListDBs");
  122.     }
  123.     $db_errstr = $drh->errstr();
  124.     @dbs;
  125. }
  126.  
  127. sub listtables ($) {
  128.     my($self) = shift;
  129.     map { $_ =~ s/^(.*)\.//; $_ } $self->{'dbh'}->tables();
  130. }
  131.  
  132. sub quote ($$) {
  133.     my($self) = shift;
  134.     my $obj = (ref($self) && $self->{'dbh'}) ?
  135.     $self->{'dbh'} : 'DBD::mysql::db';
  136.     $obj->quote(shift);
  137. }
  138.  
  139. sub errmsg ($) {
  140.     my $self = shift;
  141.     if (!ref($self)) {
  142.     $DBI::errstr || $db_errstr;
  143.     } elsif ($self->{'dbh'}) {
  144.     $self->{'dbh'}->errstr();
  145.     } else {
  146.     $self->{'drh'}->errstr();
  147.     }
  148. }
  149.  
  150. sub errno ($) {
  151.     my $self = shift;
  152.     if (!ref($self)) {
  153.     $DBI::err;
  154.     } elsif ($self->{'dbh'}) {
  155.     $self->{'dbh'}->err();
  156.     } else {
  157.     $self->{'drh'}->err();
  158.     }
  159. }
  160.  
  161. sub listfields ($$) {
  162.     my($self, $table) = @_;
  163.     $self->query("LISTFIELDS $table");
  164. }
  165.  
  166. sub query ($$) {
  167.     my($self, $statement) = @_;
  168.     my $dbh = $self->{'dbh'};
  169.     my $sth = $dbh->prepare($statement);
  170.     if (!$sth) {
  171.     $db_errstr = $dbh->errstr();
  172.     return undef;
  173.     }
  174.     $sth->{'PrintError'} = !$Mysql::QUIET;
  175.     my $result = $sth->execute();
  176.     if (!$result) {
  177.     $db_errstr = $sth->errstr();
  178.     return undef;
  179.     }
  180.     $sth->{'CompatMode'} = 1;
  181.     bless($sth, ref($self) . "::Statement");
  182.     undef $db_errstr;
  183.     $sth;
  184. }
  185.  
  186. sub shutdown ($) {
  187.     my($self) = shift;
  188.     if ($self->{'dbh'}) {
  189.     $self->{'dbh'}->admin('shutdown', 'admin');
  190.     } else {
  191.     $self->{'drh'}->func('shutdown', $self->{'host'}, $self->{'user'},
  192.                  $self->{'password'}, 'admin');
  193.     }
  194. }
  195.  
  196. sub createdb ($$) {
  197.     my($self, $db) = @_;
  198.     if ($self->{'dbh'}) {
  199.     $self->{'dbh'}->admin('createdb', $db, 'admin');
  200.     } else {
  201.     $self->{'drh'}->func('createdb', $db, $self->{'host'},
  202.                  $self->{'user'}, $self->{'password'}, 'admin');
  203.     }
  204. }
  205.  
  206. sub dropdb ($$) {
  207.     my($self, $db) = @_;
  208.     if ($self->{'dbh'}) {
  209.     $self->{'dbh'}->admin('dropdb', $db, 'admin');
  210.     } else {
  211.     $self->{'drh'}->func('dropdb', $db, $self->{'host'},
  212.                  $self->{'user'}, $self->{'password'}, 'admin');
  213.     }
  214. }
  215.  
  216. sub host     ($) { shift->{'host'} }
  217. sub database ($) { shift->{'db'} }
  218. sub info ($) { shift->{'dbh'}->{'info'} }
  219. sub sock ($) { shift->{'dbh'}->{'sock'} }
  220. sub sockfd ($) { shift->{'dbh'}->{'sockfd'} }
  221.  
  222.  
  223. sub AUTOLOAD {
  224.     my $meth = $Mysql::AUTOLOAD;
  225.     my $converted = 0;
  226.  
  227.     my $class;
  228.     if ($meth =~ /(.*)::(.*)/) {
  229.     $meth = $2;
  230.     $class = $1;
  231.     } else {
  232.     $class = "main";
  233.     }
  234.  
  235.  
  236.     TRY: {
  237.     my $val = DBD::mysql::constant($meth, @_ ? $_[0] : 0);
  238.     if ($! == 0) {
  239.         eval "sub $Mysql::AUTOLOAD { $val }";
  240.         return $val;
  241.     }
  242.  
  243.     if (!$converted) {
  244.         $meth =~ s/_//g;
  245.         $meth = lc($meth);
  246.         $converted = 1;
  247.     }
  248.  
  249.     if (defined &$meth) {
  250.         no strict 'refs';
  251.         *$meth = \&{$meth};
  252.         return &$meth(@_);
  253.     } elsif ($meth =~ s/(.*)type$/uc($1)."_TYPE"/e) {
  254.         # Try to determine the type that was requested by
  255.         # translating inttype to INT_TYPE Not that I consider it
  256.         # good style to write inttype, but we once allowed it,
  257.         # so...
  258.         redo TRY;
  259.     }
  260.     }
  261.  
  262.   Carp::croak("$Mysql::AUTOLOAD: Not defined in $class and not"
  263.           . " autoloadable (last try $meth)");
  264. }
  265.  
  266.  
  267. sub gethostinfo ($) { shift->{'dbh'}->{'hostinfo'} }
  268. sub getprotoinfo ($) { shift->{'dbh'}->{'protoinfo'} }
  269. sub getserverinfo ($) { shift->{'dbh'}->{'serverinfo'} }
  270. sub getserverstats ($) { shift->{'dbh'}->{'stats'} }
  271.  
  272.  
  273. Mysql->init_rootclass();
  274.  
  275. sub CHAR_TYPE { DBD::mysql::FIELD_TYPE_STRING() }
  276. sub INT_TYPE { DBD::mysql::FIELD_TYPE_LONG() }
  277. sub REAL_TYPE { DBD::mysql::FIELD_TYPE_DOUBLE() }
  278. sub DATE_TYPE { DBD::mysql::FIELD_TYPE_DATE() }
  279. sub TIME_TYPE { DBD::mysql::FIELD_TYPE_TIME() }
  280.  
  281.  
  282. package Mysql::dr;
  283. @Mysql::dr::ISA = qw(DBI::dr);
  284.  
  285. package Mysql::db;
  286. @Mysql::db::ISA = qw(DBI::db);
  287.  
  288. package Mysql::st;
  289. @Mysql::st::ISA = qw(Mysql::Statement);
  290.  
  291. 1;
  292. __END__
  293.  
  294. =head1 NAME
  295.  
  296. Msql / Mysql - Perl interfaces to the mSQL and mysql databases
  297.  
  298. =head1 SYNOPSIS
  299.  
  300.   use Msql;
  301.  
  302.   $dbh = Msql->connect($host);
  303.   $dbh = Msql->connect($host, $database);
  304.  
  305.       or
  306.  
  307.   use Mysql;
  308.  
  309.   $dbh = Mysql->connect(undef, $database, $user, $password);
  310.   $dbh = Mysql->connect($host, $database, $user, $password);
  311.  
  312.       or
  313.  
  314.   $dbh = Msql1->connect($host);
  315.   $dbh = Msql1->connect($host, $database);
  316.  
  317.  
  318.   $dbh->selectdb($database);
  319.     
  320.   @arr = $dbh->listdbs;
  321.   @arr = $dbh->listtables;
  322.     
  323.   $quoted_string = $dbh->quote($unquoted_string);
  324.   $error_message = $dbh->errmsg;
  325.   $error_number = $dbh->errno;   # MySQL only
  326.  
  327.   $sth = $dbh->listfields($table);
  328.   $sth = $dbh->query($sql_statement);
  329.     
  330.   @arr = $sth->fetchrow;    # Array context
  331.   $firstcol = $sth->fetchrow;    # Scalar context
  332.   @arr = $sth->fetchcol($col_number);
  333.   %hash = $sth->fetchhash;
  334.     
  335.   $sth->dataseek($row_number);
  336.  
  337.   $sth->as_string;
  338.  
  339.   @indices = $sth->listindices                   # only in mSQL 2.0
  340.   @arr = $dbh->listindex($table,$index)          # only in mSQL 2.0
  341.   ($step,$value) = $dbh->getsequenceinfo($table) # only in mSQL 2.0
  342.  
  343.   $rc = $dbh->shutdown();
  344.   $rc = $dbh->createdb($database);
  345.   $rc = $dbh->dropdb($database);
  346.  
  347. =head1 OBSOLETE SOFTWARE
  348.  
  349. As of Msql-Mysql-modules 1.19_10 M(y)sqlPerl is no longer a separate module.
  350. Instead it is emulated using the DBI drivers. You are strongly encouraged
  351. to implement new code with DBI directly. See L<COMPATIBILITY NOTES>
  352. below.
  353.  
  354. =head1 DESCRIPTION
  355.  
  356. This package is designed as close as possible to its C API
  357. counterpart. The manual that comes with mSQL or MySQL describes most things
  358. you need. Due to popular demand it was decided though, that this interface
  359. does not use StudlyCaps (see below).
  360.  
  361. As of March 1998, the Msql and Mysql modules are obsoleted by the
  362. DBI drivers DBD::mSQL and DBD::mysql, respectively. You are strongly
  363. encouraged to implement new code with the DBI drivers. In fact,
  364. Msql and Mysql are currently implemented as emulations on top of
  365. the DBI drivers.
  366.  
  367. Internally you are dealing with the two classes C<Msql> and
  368. C<Msql::Statement> or C<Mysql> and C<Mysql::Statement>, respectively.
  369. You will never see the latter, because you reach
  370. it through a statement handle returned by a query or a listfields
  371. statement. The only class you name explicitly is Msql or Mysql. They
  372. offer you the connect command:
  373.  
  374.   $dbh = Msql->connect($host);
  375.   $dbh = Msql->connect($host, $database);
  376.  
  377.     or
  378.  
  379.   $dbh = Mysql->connect($host, undef, $user, $password);
  380.   $dbh = Mysql->connect($host, $database, $user, $password);
  381.  
  382.     or
  383.  
  384.   $dbh = Msql1->connect($host);
  385.   $dbh = Msql1->connect($host, $database);
  386.  
  387.  
  388. This connects you with the desired host/database. With no argument or
  389. with an empty string as the first argument it connects to the UNIX
  390. socket, which has a much better performance than
  391. the TCP counterpart. A database name as the second argument selects
  392. the chosen database within the connection. The return value is a
  393. database handle if the connect succeeds, otherwise the return value is
  394. undef.
  395.  
  396. You will need this handle to gain further access to the database.
  397.  
  398.    $dbh->selectdb($database);
  399.  
  400. If you have not chosen a database with the C<connect> command, or if
  401. you want to change the connection to a different database using a
  402. database handle you have got from a previous C<connect>, then use
  403. selectdb.
  404.  
  405.   $sth = $dbh->listfields($table);
  406.   $sth = $dbh->query($sql_statement);
  407.  
  408. These two work rather similar as descibed in the mSQL or MySQL manual. They
  409. return a statement handle which lets you further explore what the
  410. server has to tell you. On error the return value is undef. The object
  411. returned by listfields will not know about the size of the table, so a
  412. numrows() on it will return the string "N/A";
  413.  
  414.   @arr = $dbh->listdbs();
  415.   @arr = $dbh->listtables;
  416.  
  417. An array is returned that contains the requested names without any
  418. further information.
  419.  
  420.   @arr = $sth->fetchrow;
  421.  
  422. returns an array of the values of the next row fetched from the
  423. server. Be carefull with context here! In scalar context the method
  424. behaves different than expected and returns the first column:
  425.  
  426.   $firstcol = $sth->fetchrow; # Scalar context!
  427.  
  428. Similar does
  429.  
  430.   %hash = $sth->fetchhash;
  431.  
  432. return a complete hash. The keys in this hash are the column names of
  433. the table, the values are the table values. Be aware, that when you
  434. have a table with two identical column names, you will not be able to
  435. use this method without trashing one column. In such a case, you
  436. should use the fetchrow method.
  437.  
  438.   @arr = $sth->fetchcol($colnum);
  439.  
  440. returns an array of the values of each row for column $colnum.  Note that
  441. this reads the entire table and leaves the row offset at the end of the
  442. table; be sure to use $sth->dataseek() to reset it if you want to
  443. re-examine the table.
  444.  
  445.   $sth->dataseek($row_number);
  446.  
  447. lets you specify a certain offset of the data associated with the
  448. statement handle. The next fetchrow will then return the appropriate
  449. row (first row being 0).
  450.  
  451. =head2 No close statement
  452.  
  453. Whenever the scalar that holds a database or statement handle loses
  454. its value, Msql chooses the appropriate action (frees the result or
  455. closes the database connection). So if you want to free the result or
  456. close the connection, choose to do one of the following:
  457.  
  458. =over 4
  459.  
  460. =item undef the handle
  461.  
  462. =item use the handle for another purpose
  463.  
  464. =item let the handle run out of scope
  465.  
  466. =item exit the program.
  467.  
  468. =back
  469.  
  470. =head2 Error messages
  471.  
  472. Both drivers, Msql and Mysql implement a method -E<gt>errmsg(), which
  473. returns a textual error message. Mysql additionally supports a method
  474. -E<gt>errno returning the corresponding error number.
  475.  
  476. Usually you do fetch error messages with
  477.  
  478.     $errmsg = $dbh->errmsg();
  479.  
  480. In situations where a $dbh is not available (for example when
  481. connect() failed) you may instead do a
  482.  
  483.     $errmsg = Msql->errmsg();
  484.         or
  485.     $errmsg = Mysql->errmsg();
  486.         or
  487.     $errmsg = Msql1->errmsg();
  488.  
  489.  
  490. =head2 The C<-w> switch
  491.  
  492. With Msql and Mysql the C<-w> switch is your friend! If you call your perl
  493. program with the C<-w> switch you get the warnings from -E<gt>errmsg on
  494. STDERR. This is a handy method to get the error messages from the msql
  495. server without coding it into your program.
  496.  
  497. If you want to know in greater detail what's going on, set the
  498. environment variables that are described in David's manual. David's
  499. debugging aid is excellent, there's nothing to be added.
  500.  
  501. By default errors are printed as warnings. You can suppress this
  502. behaviour by using the PrintError attribute of the respective handles:
  503.  
  504.     $dbh->{'dbh'}->{'PrintError'} = 0;
  505.  
  506.  
  507. =head2 -E<gt>quote($str [, $length])
  508.  
  509. returns the argument enclosed in single ticks ('') with any special
  510. character escaped according to the needs of the API.
  511.  
  512. For mSQL this means, any single tick within the string is escaped with
  513. a backslash and backslashes are doubled. Currently (as of msql-1.0.16)
  514. the API does not allow to insert NUL's (ASCII 0) into tables. The quote
  515. method does not fix this deficiency.
  516.  
  517. MySQL allows NUL's or any other kind of binary data in strings. Thus
  518. the quote method will additionally escape NUL's as \0.
  519.  
  520. If you pass undefined values to the quote method, it returns the
  521. string C<NULL>.
  522.  
  523. If a second parameter is passed to C<quote>, the result is truncated
  524. to that many characters.
  525.  
  526. =head2 NULL fields
  527.  
  528. NULL fields in tables are returned to perl as undefined values.
  529.  
  530. =head2 Metadata
  531.  
  532. Now lets reconsider the above methods with regard to metadata.
  533.  
  534. =head2 Database Handle
  535.  
  536. As said above you get a database handle with the connect() method.
  537. The database handle knows about the socket, the host, and the database
  538. it is connected to.
  539.  
  540. You get at the three values with the methods
  541.  
  542.   $scalar = $dbh->sock;
  543.   $scalar = $dbh->host;
  544.   $scalar = $dbh->database;
  545.  
  546. Mysql additionally supports
  547.  
  548.   $scalar = $dbh->user;
  549.   $scalar = $dbh->sockfd;
  550.  
  551. where the latter is the file descriptor of the socket used by the
  552. database connection. This is the same as $dbh->sock for mSQL.
  553.  
  554. =head2 Statement Handle
  555.  
  556. Two constructor methods return a statement handle:
  557.  
  558.   $sth = $dbh->listfields($table);
  559.   $sth = $dbh->query($sql_statement);
  560.  
  561. $sth knows about all metadata that are provided by the API:
  562.  
  563.   $scalar = $sth->numrows;    
  564.   $scalar = $sth->numfields;  
  565.  
  566.   @arr  = $sth->table;       the names of the tables of each column
  567.   @arr  = $sth->name;        the names of the columns
  568.   @arr  = $sth->type;        the type of each column, defined in msql.h
  569.                          and accessible via Msql::CHAR_TYPE,
  570.                          &Msql::INT_TYPE, &Msql::REAL_TYPE or
  571.                              &Mysql::FIELD_TYPE_STRING,
  572.                              &Mysql::FIELD_TYPE_LONG, ...
  573.   @arr  = $sth->isnotnull;   array of boolean
  574.   @arr  = $sth->isprikey;    array of boolean
  575.   @arr  = $sth->isnum;       array of boolean
  576.   @arr  = $sth->length;      array of the possibble maximum length of each
  577.                              field in bytes
  578.   @arr  = $sth->maxlength;   array of the actual maximum length of each field
  579.                              in bytes. Be careful when using this attribute
  580.                              under MsqlPerl: The server doesn't offer this
  581.                              attribute, thus it is calculated by fetching
  582.                              all rows. This might take a long time and you
  583.                              might need to call $sth->dataseek.
  584.  
  585. Mysql additionally supports
  586.  
  587.   $scalar  = $sth->affectedrows  number of rows in database affected by query
  588.   $scalar  = $sth->insertid      the unique id given to a auto_increment field.
  589.   $string  = $sth->info()        more info from some queries (ALTER TABLE...)
  590.   $arrref  = $sth->isblob;       array of boolean
  591.  
  592. The array methods (table, name, type, is_not_null, is_pri_key, length,
  593. affected_rows, is_num and blob) return an array in array context and
  594. an array reference (see L<perlref> and L<perlldsc> for details) when
  595. called in a scalar context. The scalar context is useful, if you need
  596. only the name of one column, e.g.
  597.  
  598.     $name_of_third_column = $sth->name->[2]
  599.  
  600. which is equivalent to
  601.  
  602.     @all_column_names = $sth->name;
  603.     $name_of_third_column = $all_column_names[2];
  604.  
  605. =head2 New in mSQL 2.0
  606.  
  607. The query() function in the API returns the number of rows affected by
  608. a query. To cite the mSQL API manual, this means...
  609.  
  610.   If the return code is greater than 0, not only does it imply
  611.   success, it also indicates the number of rows "touched" by the query
  612.   (i.e. the number of rows returned by a SELECT, the number of rows
  613.   modified by an update, or the number of rows removed by a delete).
  614.  
  615. As we are returning a statement handle on selects, we can easily check
  616. the number of rows returned. For non-selects we behave just the same
  617. as mSQL-2.
  618.  
  619. To find all indices associated with a table you can call the
  620. C<listindices()> method on a statement handle. To find out the columns
  621. included in an index, you can call the C<listindex($table,$index)>
  622. method on a database handle.
  623.  
  624. There are a few new column types in mSQL 2. You can access their
  625. numeric value with these functions defined in the Msql package:
  626. IDENT_TYPE, NULL_TYPE, TEXT_TYPE, DATE_TYPE, UINT_TYPE, MONEY_TYPE,
  627. TIME_TYPE, IDX_TYPE, SYSVAR_TYPE.
  628.  
  629. You cannot talk to a 1.0 server with a 2.0 client.
  630.  
  631. You cannot link to a 1.0 library I<and> to a 2.0 library I<at the same
  632. time>. So you may want to build two different Msql modules at a time,
  633. one for 1.0, another for 2.0, and load whichever you need. Check out
  634. what the C<-I> switch in perl is for.
  635.  
  636. Everything else seems to remain backwards compatible.
  637.  
  638. =head2 @EXPORT
  639.  
  640. For historical reasons the constants CHAR_TYPE, INT_TYPE, and
  641. REAL_TYPE are in @EXPORT instead of @EXPORT_OK. This means, that you
  642. always have them imported into your namespace. I consider it a bug,
  643. but not such a serious one, that I intend to break old programs by
  644. moving them into EXPORT_OK.
  645.  
  646. =head2 Displaying whole tables in one go
  647.  
  648. A handy method to show the complete contents of a statement handle is
  649. the as_string method. This works similar to the msql monitor with a
  650. few exceptions:
  651.  
  652. =over 2
  653.  
  654. =item the width of a column
  655.  
  656. is calculated by examining the width of all entries in that column
  657.  
  658. =item control characters
  659.  
  660. are mapped into their backslashed octal representation
  661.  
  662. =item backslashes
  663.  
  664. are doubled (C<\\ instead of \>)
  665.  
  666. =item numeric values
  667.  
  668. are adjusted right (both integer and floating point values)
  669.  
  670. =back
  671.  
  672. The differences are illustrated by the following table:
  673.  
  674. Input to msql (a real carriage return here replaced with ^M):
  675.  
  676.     CREATE TABLE demo (
  677.       first_field CHAR(10),
  678.       second_field INT
  679.     ) \g
  680.  
  681.     INSERT INTO demo VALUES ('new
  682.     line',2)\g
  683.     INSERT INTO demo VALUES ('back\\slash',1)\g
  684.     INSERT INTO demo VALUES ('cr^Mcrnl
  685.     nl',3)\g
  686.  
  687. Output of msql:
  688.  
  689.      +-------------+--------------+
  690.      | first_field | second_field |
  691.      +-------------+--------------+
  692.      | new
  693.     line    | 2            |
  694.      | back\slash  | 1            |
  695.     crnlr
  696.     nl  | 3            |
  697.      +-------------+--------------+
  698.  
  699. Output of pmsql:
  700.  
  701.     +----------------+------------+
  702.     |first_field     |second_field|
  703.     +----------------+------------+
  704.     |new\012line     |           2|
  705.     |back\\slash     |           1|
  706.     |cr\015crnl\012nl|           3|
  707.     +----------------+------------+
  708.  
  709.  
  710. =head2 Version information
  711.  
  712. The version of Msql and Mysql is always stored in $Msql::VERSION or
  713. $Mysql::VERSION as it is perl standard.
  714.  
  715. The mSQL API implements methods to access some internal configuration
  716. parameters: gethostinfo, getserverinfo, and getprotoinfo.  All three
  717. are available both as class methods or via a database handle. But
  718. under no circumstances they are associated with a database handle. All
  719. three return global variables that reflect the B<last> connect()
  720. command within the current program. This means, that all three return
  721. empty strings or zero I<before> the first call to connect().
  722.  
  723. This situation is better with MySQL: The methods are valid only
  724. in connection with a database handle.
  725.  
  726. =head2 Administration
  727.  
  728. shutdown, createdb, dropdb, reloadacls are all accessible via a
  729. database handle and implement the corresponding methods to what
  730. msqladmin does.
  731.  
  732. The mSQL and MySQL engines do not permit that these commands are invoked by
  733. users without sufficient privileges. So please make sure
  734. to check the return and error code when you issue one of them.
  735.  
  736.     $rc = $dbh->shutdown();
  737.     $rc = $dbh->createdb($database);
  738.     $rc = $dbh->dropdb($database);
  739.  
  740. It should be noted that database deletion is I<not prompted for> in
  741. any way. Nor is it undo-able from within Perl.
  742.  
  743.     B<Once you issue the dropdb() method, the database will be gone!>
  744.  
  745. These methods should be used at your own risk.
  746.  
  747.  
  748. =head2 StudlyCaps
  749.  
  750. Real Perl Programmers (C) usually don't like to type I<ListTables> but
  751. prefer I<list_tables> or I<listtables>. The mSQL API uses StudlyCaps
  752. everywhere and so did early versions of MsqlPerl. Beginning with
  753. $VERSION 1.06 all methods are internally in lowercase, but may be
  754. written however you please. Case is ignored and you may use the
  755. underline to improve readability.
  756.  
  757. The price for using different method names is neglectible. Any method
  758. name you use that can be transformed into a known one, will only be
  759. defined once within a program and will remain an alias until the
  760. program terminates. So feel free to run fetch_row or connecT or
  761. ListDBs as in your old programs. These, of course, will continue to
  762. work.
  763.  
  764. =head1 PREREQUISITES
  765.  
  766. mSQL is a database server and an API library written by David
  767. Hughes. To use the adaptor you definitely have to install these first.
  768.  
  769. MySQL is a libmysqlclient.a library written by Michael Widenius
  770. This was originally inspired by MySQL.
  771.  
  772.  
  773. =head1 COMPATIBILITY NOTES
  774.  
  775. M(y)sql used to be a separate module written in C. This is no longer
  776. the case, instead the old modules are emulated by their corresponding
  777. DBI drivers. I did my best to remove any incompatibilities, but the
  778. following problems are known to remain:
  779.  
  780. =over 4
  781.  
  782. =item Static methods
  783.  
  784. For whatever reason, mSQL implements some functions independent from
  785. the respective database connection that really depend on it. This
  786. made it possible to implement
  787.  
  788.     Msql->errmsg
  789.  
  790. or
  791.  
  792.     Msql->getserverinfo
  793.  
  794. as static methods. This is no longer the case, it never was for
  795. MysqlPerl. Instead you have to use
  796.  
  797.     $dbh->errmsg
  798.  
  799. or
  800.  
  801.     $dbh->getserverinfo
  802.  
  803. =item $M(Y)SQL::QUIET
  804.  
  805. This variable used to turn off the printing of error messages. Unfortunately
  806. DBI uses a completely different mechanism for that: The C<PrintError>
  807. attribute of the database and/or statement handles. We try to emulate
  808. the old behaviour by setting the C<PrintError> attribute to the current
  809. value of $M(Y)SQL::QUIET when a handle is created, that is when
  810. M(y)sql->connect or $dbh->query() are called.
  811.  
  812. You can overwrite this by using something like
  813.  
  814.     $dbh->{'dbh'}->{'PrintError'} = 1;
  815.  
  816. or
  817.  
  818.     $sth->{'PrintError'} = 0;
  819.  
  820. =back
  821.  
  822.  
  823. =head1 AUTHORS
  824.  
  825. Andreas Koenig C<koenig@franz.ww.TU-Berlin.DE> wrote the original
  826. MsqlPerl. Jochen Wiedmann wrote the M(y)sqlPerl emulation using DBI.
  827.  
  828.  
  829. =head1 SEE ALSO
  830.  
  831. Alligator Descartes wrote a database driver for Tim Bunce's DBI. I
  832. recommend anybody to carefully watch the development of this module
  833. (C<DBD::mSQL>). Msql is a simple, stable, and fast module, and it will
  834. be supported for a long time. But it's a dead end. I expect in the
  835. medium term, that the DBI efforts result in a richer module family
  836. with better support and more functionality. Alligator maintains an
  837. interesting page on the DBI development:
  838.  
  839.     http://www.symbolstone.org/technology/perl/DBI
  840.  
  841. =cut
  842.