1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
package DBD::mSQL;
use strict;
use vars qw(@ISA $VERSION $err $errstr $drh);
use DBI ();
use DynaLoader ();
use Carp ();
@ISA = qw(DynaLoader);
$VERSION = "0.80";
bootstrap DBD::mSQL $VERSION;
$err = 0; # holds error code for DBI::err
$errstr = ""; # holds error string for DBI::errstr
$drh = undef; # holds driver handle once initialised
sub driver{
return $drh if $drh;
my($class, $attr) = @_;
$class .= "::dr";
# not a 'my' since we use it above to prevent multiple drivers
$drh = DBI::_new_drh($class, {
'Name' => 'mSQL',
'Version' => $VERSION,
'Err' => \$DBD::mSQL::err,
'Errstr' => \$DBD::mSQL::errstr,
'Attribution' => 'DBD::mSQL by AK',
});
$drh;
}
1;
package DBD::mSQL::dr; # ====== DRIVER ======
use strict;
sub errstr {
DBD::mSQL::errstr(@_);
}
sub connect {
my($drh, $data_source, $username, $password )= @_;
if (defined $username && $username gt "") { # backwards compatible until October 1997 or so
# somebody tried to run connect("host:port", "db")
$data_source = "$username:$data_source";
Carp::carp qq{
Please switch to the new connect method documenteded in DBI 0.84:
\$dbh = DBI->connect(\$data_source).
We now emulate the call
\$drh->connect("$data_source"),
but this exception will go away soon.
} unless defined $DBD::mSQL::QUIET && $DBD::mSQL::QUIET;
}
my($dbname,$host,$port) = split /:/, $data_source;
$ENV{'MSQL_TCP_PORT'} = $port if defined $port;
my $this = DBI::_new_dbh($drh, {
'Host' => $host,
'Name' => $dbname
});
# Call mSQL msqlConnect func in mSQL.xs file
# and populate internal handle data.
DBD::mSQL::db::_login($this, $host, $dbname)
or return undef;
$this;
}
package DBD::mSQL::db; # ====== DATABASE ======
use strict;
### mSQL datatype to ANSI datatype mapping
%DBD::mSQL::db::db2ANSI = (
"INT" => "INTEGER",
"CHAR" => "CHAR",
"REAL" => "REAL",
"IDENT" => "DECIMAL"
);
### ANSI datatype mapping to mSQL datatypes
%DBD::mSQL::db::ANSI2db = (
"CHAR" => "CHAR",
"VARCHAR" => "CHAR",
"LONGVARCHAR" => "CHAR",
"NUMERIC" => "INTEGER",
"DECIMAL" => "INTEGER",
"BIT" => "INTEGER",
"TINYINT" => "INTEGER",
"SMALLINT" => "INTEGER",
"INTEGER" => "INTEGER",
"BIGINT" => "INTEGER",
"REAL" => "REAL",
"FLOAT" => "REAL",
"DOUBLE" => "REAL",
"BINARY" => "CHAR",
"VARBINARY" => "CHAR",
"LONGVARBINARY" => "CHAR",
"DATE" => "CHAR",
"TIME" => "CHAR",
"TIMESTAMP" => "CHAR"
);
sub errstr {
DBD::mSQL::errstr(@_);
}
sub prepare {
my($dbh, $statement)= @_;
# create a 'blank' dbh
my $sth = DBI::_new_sth($dbh, {
'Statement' => $statement,
});
# Call mSQL OCI oparse func in mSQL.xs file.
# (This will actually also call oopen for you.)
# and populate internal handle data.
DBD::mSQL::st::_prepare($sth, $statement)
or return undef;
$sth;
}
sub quote {
my $self = shift;
my $str = shift;
$str =~ s/'/\\'/g; # MSQL non-ISO compliant!
"'$str'";
}
sub db2ANSI {
my $self = shift;
my $type = shift;
return $DBD::mSQL::db::db2ANSI{"$type"};
}
sub ANSI2db {
my $self = shift;
my $type = shift;
return $DBD::mSQL::db::ANSI2db{"$type"};
}
package DBD::mSQL::st; # ====== STATEMENT ======
use strict;
sub errstr {
DBD::mSQL::errstr(@_);
}
1;
__END__
=head1 NAME
DBD::mSQL - mSQL-1.I<x> / 2.I<x> driver for the Perl5 Database Interface (DBI)
=head1 SYNOPSIS
$dbh = DBI->connect( "$database:$hostname:$port" );
@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' );
=head1 DESCRIPTION
B<DBD::mSQL> is the Perl5 Database Interface driver for mSQL 1.I<x> and
2.I<x> databases.
=head2 Compatibility Alert
As of version 0.70 DBD::mSQL has a new maintainer
=head2 DBD::mSQL Class Methods
=over 4
=item B<connect>
$dbh = DBI->connect( "$database" );
$dbh = DBI->connect( "$database:$hostname" );
$dbh = DBI->connect( "$database:$hostname:$port" );
A C<database> must always be specified.
The hostname, if not specified or specified as '', will default to an
mSQL daemon running on the local machine on the default port for the
UNIX socket.
Should the mSQL daemon be running on a non-standard port number, you
may explicitly state the port number to connect to in the C<hostname>
argument, by concatenating the I<hostname> and I<port number> together
separated by a colon ( C<:> ) character.
=back
=head2 DBD::mSQL Private MetaData Methods
=over 4
=item B<ListDBs>
@databases = $drh->func( $hostname, '_ListDBs' );
This private method returns an array containing the names of all
databases present on the mSQL daemon running on C<hostname>. If there
are no databases, an empty list will be returned. A sample usage of
this method is:
@databases = $drh->func( 'localhost', '_ListDBs' );
foreach $db ( @databases ) {
print "Database: $db\n";
}
=item B<ListTables>
@tables = $dbh->func( '_ListTables' );
Once connected to the desired database on the desired mSQL daemon with
the C<DBI->connect()> method, we may extract a list of the tables that
have been created within that database.
C<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";
}
=item B<ListFields>
$ref = $dbh->func( $table, '_ListFields' );
C<ListFields> returns a reference to a hashtable containing metadata
information on the fields within the given table. If the table
specified in C<table> does not exist, C<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
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";
}
=item B<ListSelectedFields>
$ref = $sth->func( '_ListSelectedFields' );
C<ListSelectedFields> is a similar function to C<ListFields>, except,
where C<ListFields> lists the fields for a given table within the
current database, C<ListSelectedFields> lists the field information
for the fields present in a B<SELECT> statement handle. This is
primarily used for extracting meta-data about the current C<sth>.
The usage of C<ListSelectedFields> is identical to C<ListFields>.
=item C<NumRows>
$numRows = $sth->func( '_NumRows' );
The C<NumRows> private method returns the number of rows affected by a
B<SELECT> statement. This functionality was introduced prior to it
becoming a standard within the DBI interface itself, where the number
of rows affected by a B<SELECT> may be obtained by checking the return
value of the C<$sth->execute> method.
=back
=head2 DBD::mSQL Database Manipulation
=over 4
=item B<CreateDB>
$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 C<localhost> and execute these
operations there.
It should be noted that database deletion is I<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.
=back
=head1 BUGS
The I<port> part of the first argument to the connect call is
implemented in an unsafe way. It 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 change.
The I<host> part of the first argument to the connect call is
currently documented as defaulting to 'localhost'. If I read this
right, it implicates that there are no provisions to connect to the
UNIX socket. This is a major speed disadvantage for application that
run on the server host. This will have to be revisited in the next
release.
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?
Despite all these func methods, AFAIK it is currently not possible to
connect to a different host and query the available databases. If
true, this is a minor nit, but needs to be resolved somehow.
I haven't yet found out how the constants CHAR_TYPE, INT_TYPE,
etc. are accessed in DBD::mSQL. Can anybody help me on the tracks
here?
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.
=head1 AUTHOR
B<DBD::mSQL> has been primarily written by Alligator Descartes
<I<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 Knig.
=head1 COPYRIGHT
This module is Copyright (c)1994-1997 Alligator Descartes, 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 I<AUTHOR> and I<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 <I<descarte@hermetica.com>>.
=head1 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!
=cut
|