NAME

DBD::mSQL / DBD::mysql - mSQL and mysql drivers for the Perl5 Database Interface (DBI)

SYNOPSIS

    use DBI;

    $dbh = DBI->connect("DBI:mSQL:$database:$hostname:$port",
			undef, undef);

        or

    $dbh = DBI->connect("DBI:mysql:$database:$hostname:$port",
			$user, $password);

    @databases = $drh->func( $hostname, '_ListDBs' );
    @tables = $dbh->func( '_ListTables' );
    $ref = $dbh->func( $table, '_ListFields' );
    $ref = $sth->func( '_ListSelectedFields' );

    $numRows = $sth->func( '_NumRows' );

    $rc = $drh->func( $database, '_CreateDB' );
    $rc = $drh->func( $database, '_DropDB' );

DESCRIPTION

<DBD::mysql> and <DBD::mSQL> are the Perl5 Database Interface drivers for the mysql, mSQL 1.x and mSQL 2.x databases. The drivers are part of the mysql-modules and Msql-modules packages, respectively.

Compatibility Alert

As of version 0.70 DBD::mSQL has a new maintainer. Even more, the sources have been completely rewritten in August 1997, so it seemed apropriate to bump the version number: Incompatibilities are more than likely.

Class Methods

connect
    use DBI;

    $dbh = DBI->connect("DBI:mSQL:$database", undef, undef);
    $dbh = DBI->connect("DBI:mSQL:$database:$hostname", undef, undef);
    $dbh = DBI->connect("DBI:mSQL:$database:$hostname:$port",
			undef, undef);

        or

    use DBI;

    $dbh = DBI->connect("DBI:mysql:$database", $user, $password);
    $dbh = DBI->connect("DBI:mysql:$database:$hostname",
			$user, $password);
    $dbh = DBI->connect("DBI:mysql:$database:$hostname:$port",
			$user, $password);

A database must always be specified.

The hostname, if not specified or specified as '', will default to an mysql or mSQL daemon running on the local machine on the default port for the UNIX socket.

Should the mysql or mSQL daemon be running on a non-standard port number, you may explicitly state the port number to connect to in the hostname argument, by concatenating the hostname and port number together separated by a colon ( : ) character.

Private MetaData Methods

ListDBs
@dbs = $dbh->func("$hostname:$port", '_ListDBs');

Returns a list of all databases managed by the mysql daemon or mSQL daemon running on $hostname, port $port. This method is rarely needed for databases running on localhost: You should use the portable method

@dbs = DBI->data_sources("mysql");

    or

@dbs = DBI->data_sources("mSQL");

whenever possible. It is a design problem of this method, that there's no way of supplying a host name or port number to data_sources, that's the only reason why we still support ListDBs. :-(

ListTables
@tables = $dbh->func('_ListTables');

Once connected to the desired database on the desired mysql or mSQL mSQL daemon with the DBI-connect()> method, we may extract a list of the tables that have been created within that database.

ListTables returns an array containing the names of all the tables present within the selected database. If no tables have been created, an empty list is returned.

@tables = $dbh->func( '_ListTables' );
foreach $table ( @tables ) {
    print "Table: $table\n";
  }
ListFields
$ref = $dbh->func( $table, '_ListFields' );

ListFields returns a reference to a hashtable containing metadata information on the fields within the given table. If the table specified in table does not exist, undef will be returned and an error flagged.

The valid keys within the hashtable that may be referenced are:

NAME           The name of the field
TYPE           The datatype of the field: CHAR, REAL, INTEGER, NULL
IS_NOT_NULL    Indicates whether the field is NULLable or not
IS_PRI_KEY     Indicates whether the field is a Primary Key ( this is
                 only valid in mSQL 1.x databases. mSQL 2.x uses indices )
LENGTH         The size of the field
NUMFIELDS      The number of fields within the table

Mysql also supports the following keys:

IS_KEY         Indicates whether the field is part of any key.
IS_BLOB        Indicates whether the given field is of blob type;
               don't put too much interpretation in this: A char
               field may contain binary data too, though size
               restricted.
IS_NUM         Indicates whether the given field contains numeric
               data. (The driver will return strings anyways,
               as the database does.)
MAXLENGTH      Perhaps the maximum length of all fields of the
               given column; as far as I know this isn't documented,
               so don't trust in that. (Monty?)

Since a reference is returned, it requires slightly more work to extract the pertinent information from it. Here's an example of how to do it:

$ref = $dbh->func( 'someTable', '_ListFields' );
@fieldNames = @{ $ref->{NAME} };
@fieldTypes = @{ $ref->{TYPE} };
@fieldNulls = @{ $ref->{IS_NOT_NULL} };
@fieldKeys  = @{ $ref->{IS_PRI_KEY} };
@fieldLength = @{ $ref->{LENGTH} };
for ( $i = 0 ; $i < $ref->{NUMFIELDS} ; $i++ ) {
    print "Field: $fieldNames[$i]\n";
    print "\tType: $fieldTypes[$i]\n";
    print "\tNullable: $fieldNulls[$i]\n";
    print "\tKey?: $fieldKeys[$i]\n";
    print "\tLength: $fieldLength[$i]\n";
  }
ListSelectedFields
$ref = $sth->func( '_ListSelectedFields' );

ListSelectedFields is a similar function to ListFields, except, where ListFields lists the fields for a given table within the current database, ListSelectedFields lists the field information for the fields present in a SELECT statement handle. This is primarily used for extracting meta-data about the current sth.

The usage of ListSelectedFields is identical to ListFields, but you might use the following additional keys:

NUMFIELDS       The number of fields selected in one row; this
                should be valid even if the query didn't return
                any rows.
NUMROWS         The number of rows selected.

Database Manipulation

CreateDB
DropDB
$rc = $drh->func( $database, '_CreateDB' );
$rc = $drh->func( $database, '_DropDB' );

These two methods allow programmers to create and drop databases from DBI scripts. Since mSQL disallows the creation and deletion of databases over the network, these methods explicitly connect to the mSQL daemon running on the machine localhost and execute these operations there.

It should be noted that database deletion is not prompted for in any way. Nor is it undo-able from DBI.

Once you issue the dropDB() method, the database will be gone!

These methods should be used at your own risk.

BUGS

The port part of the first argument to the connect call is implemented in an unsafe way. In fact it never did more than set the environment variable MSQL_TCP_PORT during the connect call. If another connect call uses another port and the handles are used simultaneously, they will interfere. In a future version this behaviour will hoefully change, depending on David and Monty. :-)

The func method call on a driver handle seems to be undocumented in the DBI manpage. DBD::mSQL has func methods on driverhandles, database handles, and statement handles. What gives?

Please speak up now (June 1997) if you encounter additional bugs. I'm still learning about the DBI API and can neither judge the quality of the code presented here nor the DBI compliancy. But I'm intending to resolve things quickly as I'd really like to get rid of the multitude of implementations ASAP.

When running "make test", you will notice that some test scripts fail. This is due to bugs in the respective databases, not in the DBI drivers:

Nulls

mSQL seems to have problems with NULL's: The following fails with mSQL 2.0.1 running on a Linux 2.0.30 machine:

[joe@laptop Msql-modules-1.18]$ msql test
Welcome to the miniSQL monitor.  Type \h for help.
mSQL > CREATE TABLE foo (id INTEGER, name CHAR(6))\g
Query OK.  1 row(s) modified or retrieved.
mSQL > INSERT INTO foo VALUES (NULL, 'joe')\g
Query OK.  1 row(s) modified or retrieved.
mSQL > SELECT * FROM foo WHERE id = NULL\g
Query OK.  0 row(s) modified or retrieved.
+----------+------+
| id       | name |
+----------+------+
+----------+------+
mSQL > 
Blanks

mysql has problems with Blanks on the right side of string fields: They get chopped of. (Tested with mysql 3.20.25 on a Linux 2.0.30 machine.)

[joe@laptop Msql-modules-1.18]$ mysql test
Welcome to the mysql monitor.  Commands ends with ; or \g.
Type 'help' for help.
mysql> CREATE TABLE foo (id INTEGER, bar CHAR(8));
Query OK, 0 rows affected (0.10 sec)
mysql> INSERT INTO foo VALUES (1, ' a b c ');
Query OK, 1 rows affected (0.00 sec)
mysql> SELECT * FROM foo;
1 rows in set (0.19 sec)
+------+--------+
| id   | bar    |
+------+--------+
|    1 |  a b c |
+------+--------+
mysql> quit;
[joe@laptop Msql-modules-1.18]$ mysqldump test foo

[deleted]

INSERT INTO foo VALUES (1,' a b c');

AUTHOR

DBD::mSQL has been primarily written by Alligator Descartes <descarte@hermetica.com>, who has been aided and abetted by Gary Shea, Andreas Koenig and Tim Bunce amongst others. Apologies if your name isn't listed, it probably is in the file called 'Acknowledgments'. As of version 0.80 the maintainer is Andreas König. Version 2.00 is an almost complete rewrite by Jochen Wiedmann.

COPYRIGHT

This module is Copyright (c)1997 Jochen Wiedmann, with code portions Copyright (c)1994-1997 their original authors. This module is released under the 'Artistic' license which you can find in the perl distribution.

This document is Copyright (c)1997 Alligator Descartes. All rights reserved. Permission to distribute this document, in full or in part, via email, Usenet, ftp archives or http is granted providing that no charges are involved, reasonable attempt is made to use the most current version and all credits and copyright notices are retained ( the AUTHOR and COPYRIGHT sections ). Requests for other distribution rights, including incorporation into commercial products, such as books, magazine articles or CD-ROMs should be made to Alligator Descartes <descarte@hermetica.com>.

Additional DBI Information

Additional information on the DBI project can be found on the World Wide Web at the following URL:

http://www.hermetica.com/technologia/perl/DBI

where documentation, pointers to the mailing lists and mailing list archives and pointers to the most current versions of the modules can be used.

Information on the DBI interface itself can be gained by typing:

perldoc DBI

right now!

1 POD Error

The following errors were encountered while parsing the POD:

Around line 508:

Non-ASCII character seen before =encoding in 'König.'. Assuming CP1252