/usr/share/perl5/DBIx/Easy.pm is in libdbix-easy-perl 0.17-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 | # Easy.pm - Easy to Use DBI interface
# Copyright (C) 1999,2000,2001,2002 Stefan Hornburg, Dennis Schön
# Copyright (C) 2003,2004,2005,2006 Stefan Hornburg (Racke) <racke@linuxia.de>
# Authors: Stefan Hornburg (Racke) <racke@linuxia.de>
# Dennis Schön <ds@1d10t.de>
# Maintainer: Stefan Hornburg (Racke) <racke@linuxia.de>
# Version: 0.17
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
# This file is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this file; see the file COPYING. If not, write to the Free
# Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
package DBIx::Easy;
use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
require Exporter;
@ISA = qw(Exporter);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@EXPORT = qw(
);
# Public variables
use vars qw($cache_structs);
$VERSION = '0.17';
$cache_structs = 1;
use DBI;
=head1 NAME
DBIx::Easy - Easy to Use DBI interface
=head1 SYNOPSIS
use DBIx::Easy;
my $dbi_interface = new DBIx::Easy qw(Pg template1);
$dbi_interface -> insert ('transaction',
id => serial ('transaction', 'transactionid'),
time => \$dbi_interface -> now);
$dbi_interface -> update ('components', "table='ram'", price => 100);
$rows_deleted = $dbi_interface -> delete ('components', 'stock = 0');
$dbi_interface -> makemap ('components', 'id', 'price', 'price > 10');
$components = $dbi_interface -> rows ('components');
$components_needed = $dbi_interface -> rows ('components', 'stock = 0');
=head1 DESCRIPTION
DBIx::Easy is an easy to use DBI interface.
Currently the Pg, mSQL, mysql, Sybase, ODBC and XBase drivers are supported.
=head1 CREATING A NEW DBI INTERFACE OBJECT
$dbi_interface = new DBIx::Easy qw(Pg template1);
$dbi_interface = new DBIx::Easy qw(Pg template1 racke);
$dbi_interface = new DBIx::Easy qw(Pg template1 racke aF3xD4_i);
$dbi_interface = new DBIx::Easy qw(Pg template1 racke@linuxia.de aF3xD4_i);
$dbi_interface = new DBIx::Easy qw(Pg template1 racke@linuxia.de:3306 aF3xD4_i);
The required parameters are the database driver
and the database name. Additional parameters are the database user
and the password to access the database. To specify the database host
use the USER@HOST notation for the user parameter. If you want to specify the
port to connect to use USER@HOST:PORT.
=head1 DESTROYING A DBI INTERFACE OBJECT
It is important that you commit all changes at the end of the interaction
with the DBMS. You can either explicitly commit
$dbi_interface -> commit ();
or do it implicitly:
undef $dbi_interface;
=head1 ERROR HANDLING
sub fatal {
my ($statement, $err, $msg) = @_;
die ("$0: Statement \"$statement\" failed (ERRNO: $err, ERRMSG: $msg)\n");
}
$dbi_interface -> install_handler (\&fatal);
If any of the DBI methods fails, either I<die> will be invoked
or an error handler installed with I<install_handler> will be
called.
=head1 CACHING ISSUES
By default, this module caches table structures. This can be
disabled by setting I<$DBIx::Easy::cache_structs> to 0.
=head1 XBASE DRIVER
The DBIx::Easy method rows fails to work with the DBD::XBase driver.
=cut
# Private Variables
# =================
my $maintainer_adr = 'racke@linuxia.de';
# Keywords for connect()
my %kwmap = (mSQL => 'database', mysql => 'database', Pg => 'dbname',
Sybase => 'database', ODBC => '', XBase => '');
my %kwhostmap = (mSQL => 'host', mysql => 'host', Pg => 'host',
Sybase => 'server', ODBC => '', XBase => '');
my %kwportmap = (mysql => 'port', Pg => 'port');
# Whether the DBMS supports transactions
my %transactmap = (mSQL => 0, mysql => 0, Pg => 1, Sybase => 'server',
ODBC => 0, XBase => 0);
# Statement generators for serial()
my %serialstatmap = (mSQL => sub {"SELECT _seq FROM $_[0]";},
Pg => sub {"SELECT NEXTVAL ('$_[1]')";});
# Statement for obtaining the table structure
my %obtstatmap = (mSQL => sub {my $table = shift;
"SELECT " . join (', ', @_)
. " FROM $table WHERE 0 = 1";},
mysql => sub {my $table = shift;
"SELECT " . join (', ', @_)
. " FROM $table WHERE 0 = 1";},
Pg => sub {my $table = shift;
"SELECT " . join (', ', @_)
. " FROM $table WHERE FALSE";},
Sybase => sub {my $table = shift;
"SELECT " . join (', ', @_)
. " FROM $table WHERE 0 = 1";},
ODBC => sub {my $table = shift;
"SELECT " . join (', ', @_)
. " FROM $table WHERE 0 = 1";},
XBase => sub {my $table = shift;
"SELECT " . join (', ', @_)
. " FROM $table WHERE 0 = 1";});
# Supported functions
my %funcmap = (mSQL => {COUNT => 0},
mysql => {COUNT => 1},
Pg => {COUNT => 1},
Sybase => {COUNT => 1},
ODBC => {COUNT => 0});
# Cache
my %structs;
# Preloaded methods go here.
sub new
{
my $proto = shift;
my $class = ref ($proto) || $proto;
my $self = {};
$self ->{DRIVER} = shift;
$self ->{DATABASE} = shift;
$self ->{USER} = shift;
# check for a host part
if (defined $self->{USER} && $self->{USER} =~ /@/) {
$self->{HOST} = $';
$self->{USER} = $`;
}
if (defined $self->{HOST} && $self->{HOST} =~ /:/) {
$self->{PORT} = $';
$self->{HOST} = $`;
}
$self ->{PASS} = shift;
$self ->{CONN} = undef;
$self ->{HANDLER} = undef; # error handler
bless ($self, $class);
# sanity check: driver
unless (defined ($self -> {DRIVER}) && $self->{DRIVER} =~ /\S/) {
$self -> fatal ("No driver selected for $class.");
}
unless (exists $kwmap{$self -> {DRIVER}}) {
$self -> fatal ("Sorry, $class doesn't support the \""
. $self -> {DRIVER} . "\" driver.\n"
. "Please send mail to $maintainer_adr for more information.\n");
}
# sanity check: database name
unless (defined ($self -> {DATABASE}) && $self->{DATABASE} =~ /\S/) {
# ok for sybase with host
unless ($self->{DRIVER} eq 'Sybase' && $self->{HOST}) {
$self -> fatal ("No database selected for $class.");
}
}
return $self if $^O eq 'MSWin32';
# we may try to get password from DBMS specific
# configuration file
unless (defined $self->{PASS}) {
unless (defined $self->{'USER'}
&& $self->{'USER'} ne getpwuid($<)) {
$self->passwd();
}
}
return ($self);
}
# ------------------------------------------------------
# DESTRUCTOR
#
# If called for an object with established connection we
# commit any changes.
# ------------------------------------------------------
sub DESTROY {
my $self = shift;
if (defined ($self -> {CONN})) {
unless ($self -> {CONN} -> {AutoCommit}) {
$self -> {CONN} -> commit;
}
$self -> {CONN} -> disconnect;
}
}
# ------------------------------
# METHOD: fatal
#
# Error handler for this module.
# ------------------------------
sub fatal {
my ($self, $info, $err) = @_;
my $errstr = '';
if (defined $self -> {CONN}) {
$err = $DBI::err;
$errstr = $DBI::errstr;
unless ($self -> {CONN} -> {AutoCommit}) {
# something has gone wrong, rollback anything
$self -> {CONN} -> rollback ();
}
}
if (defined $self -> {'HANDLER'}) {
&{$self -> {'HANDLER'}} ($info, $err, $errstr);
} elsif (defined $self -> {CONN}) {
die "$info (DBERR: $err, DBMSG: $errstr)\n";
} elsif ($err) {
die "$info ($err)\n";
} else {
die "$info\n";
}
}
# ---------------------------------------------------------------
# METHOD: connect
#
# Establishes the connection to the database if not already done.
# Returns database handle if successful, dies otherwise.
# ---------------------------------------------------------------
sub connect ()
{
my $self = shift;
my ($dsn, $oldwarn);
my $msg = '';
unless (defined $self -> {CONN})
{
# build the data source string for DBI
# ... the driver name
$dsn .= 'dbi:' . $self -> {DRIVER} . ':';
# ... optionally the var part (ODBC has no vars)
if ($kwmap{$self -> {DRIVER}}) {
$dsn .= $kwmap{$self -> {DRIVER}} . "=";
}
# ... database name
$dsn .= $self -> {DATABASE};
# ... optionally the host part
if ($self -> {HOST}) {
$dsn .= ';' . $kwhostmap{$self->{DRIVER}}
. '=' . $self -> {HOST};
}
# ... optionally the host part
if ($self -> {PORT}) {
$dsn .= ';' . $kwportmap{$self->{DRIVER}}
. '=' . $self -> {PORT};
}
# install warn() handler to catch DBI error messages
$oldwarn = $SIG{__WARN__};
$SIG{__WARN__} = sub {$msg = "@_";};
$self -> {CONN} = DBI
-> connect ($dsn, $self -> {USER}, $self -> {PASS},
{AutoCommit => !$transactmap{$self->{DRIVER}}});
# deinstall warn() handler
$SIG{__WARN__} = $oldwarn;
unless (defined $self -> {CONN})
{
# remove file/line information from error message
$msg =~ s/\s+at .*?line \d+\s*$//;
# print error message in any case
$self -> fatal ("Connection to database \"" . $self -> {DATABASE}
. "\" couldn't be established", $msg);
return;
}
}
# no need to see SQL errors twice
$self -> {CONN} -> {'PrintError'} = 0;
$self -> {CONN};
}
# -------------------------
# METHOD: process STATEMENT
# -------------------------
=head1 METHODS
=head2 DATABASE ACCESS
=over 4
=item process I<statement>
$sth = $dbi_interface -> process ("SELECT * FROM foo");
print "Table foo contains ", $sth -> rows, " rows.\n";
Processes I<statement> by just combining the I<prepare> and I<execute>
steps of the DBI. Returns statement handle in case of success.
=back
=cut
sub process
{
my ($self, $statement) = @_;
my ($sth, $rv);
return unless $self -> connect ();
# prepare and execute it
$sth = $self -> {CONN} -> prepare ($statement)
|| $self -> fatal ("Couldn't prepare statement \"$statement\"");
$rv = $sth -> execute ()
|| $self -> fatal ("Couldn't execute statement \"$statement\"");
$sth;
}
# ------------------------------------------------------
# METHOD: insert TABLE COLUMN VALUE [COLUMN VALUE] ...
#
# Inserts the given COLUMN/VALUE pairs into TABLE.
# ------------------------------------------------------
=over 4
=item insert I<table> I<column> I<value> [I<column> I<value>] ...
$sth = $dbi_interface -> insert ('bar', drink => 'Caipirinha');
Inserts the given I<column>/I<value> pairs into I<table>. Determines from the
SQL data type which values has to been quoted. Just pass a reference to
the value to protect values with SQL functions from quoting.
=back
=cut
sub insert ($$$;@)
{
my $self = shift;
my $table = shift;
my (@columns, @values);
my ($statement, $sthtest, $flags);
my ($column, $value);
return unless $self -> connect ();
while ($#_ >= 0)
{
$column = shift; $value = shift;
push (@columns, $column);
push (@values, $value);
}
# get the table structure
$sthtest = $self -> process
(&{$obtstatmap{$self -> {'DRIVER'}}} ($table, @columns));
$flags = $sthtest -> {'TYPE'};
$sthtest -> finish ();
for (my $i = 0; $i <= $#values; $i++) {
if (ref ($values[$i]) eq 'SCALAR') {
$values[$i] = ${$values[$i]};
} elsif ($$flags[$i] == DBI::SQL_INTEGER
|| $$flags[$i] == DBI::SQL_SMALLINT
|| $$flags[$i] == DBI::SQL_DECIMAL
|| $$flags[$i] == DBI::SQL_FLOAT
|| $$flags[$i] == DBI::SQL_REAL
|| $$flags[$i] == DBI::SQL_DOUBLE
|| $$flags[$i] == DBI::SQL_NUMERIC) {
# we don't need to quote numeric values, but
# we have to check for empty input
unless (defined $values[$i] && $values[$i] =~ /\S/) {
$values[$i] = 'NULL';
}
} elsif (defined $values[$i]) {
$values[$i] = $self -> quote ($values[$i]);
} else {
$values[$i] = 'NULL';
}
}
# now the statement
$statement = "INSERT INTO $table ("
. join (', ', @columns) . ") VALUES ("
. join (', ', @values) . ")";
# process it
$self -> {CONN} -> do ($statement)
|| $self -> fatal ("Couldn't execute statement \"$statement\"");
}
# ---------------------------------------------------------------
# METHOD: update TABLE CONDITIONS COLUMN VALUE [COLUMN VALUE] ...
#
# Updates the rows matching CONDITIONS with the given
# COLUMN/VALUE pairs and returns the number of the
# modified rows.
# ---------------------------------------------------------------
=over 4
=item update I<table> I<conditions> I<column> I<value> [I<column> I<value>] ...
$dbi_interface -> update ('components', "table='ram'", price => 100);
$dbi_interface -> update ('components', "table='ram'", price => \"price + 20");
Updates any row of I<table> which fulfill the I<conditions> by inserting the given I<column>/I<value> pairs. Scalar references can be used to embed strings without further quoting into the resulting SQL statement. Returns the number of rows modified.
=back
=cut
sub update
{
my $self = shift;
my $table = shift;
my $conditions = shift;
my (@columns);
my ($statement, $rv);
my ($column, $value);
# ensure that connection is established
return unless $self -> connect ();
while ($#_ >= 0) {
$column = shift; $value = shift;
# avoid Perl warning
if (defined $value) {
if (ref($value) eq 'SCALAR') {
$value = $$value;
} else {
$value = $self -> {CONN} -> quote ($value);
}
} else {
$value = 'NULL';
}
push (@columns, $column . ' = ' . $value);
}
# now the statement
$statement = "UPDATE $table SET "
. join (', ', @columns) . " WHERE $conditions";
# process it
$rv = $self -> {CONN} -> do ($statement);
if (defined $rv) {
# return the number of rows changed
$rv;
} else {
$self -> fatal ("Couldn't execute statement \"$statement\"");
}
}
# ---------------------------------------------------------------
# METHOD: put TABLE CONDITIONS COLUMN VALUE [COLUMN VALUE] ...
#
# Either updates the rows matching CONDITIONS with the given
# COLUMN/VALUE pairs or puts (inserts) them into TABLE.
# Returns the number of modified rows (1 in case of an insert).
# ---------------------------------------------------------------
=over 4
=item put I<table> I<conditions> I<column> I<value> [I<column> I<value>] ...
=back
=cut
sub put
{
my $self = shift;
my $table = shift;
my $conditions = shift;
# ensure that connection is established
return unless $self -> connect ();
# check for existing rows
if ($self->rows($table, $conditions)) {
$self->update($table, $conditions, @_);
} else {
$self->insert($table, @_);
1;
}
}
# ---------------------------------
# METHOD: delete TABLE [CONDITIONS]
# ---------------------------------
=over 4
=item delete I<table> I<conditions>
$dbi_interface -> delete ('components', "stock=0");
Deletes any row of I<table> which fulfill the I<conditions>. Without conditions
all rows are deleted. Returns the number of rows deleted.
=back
=cut
sub delete {
my ($self, $table, $conditions) = @_;
my $sth;
if ($conditions) {
$sth = $self -> process ("delete from $table where $conditions");
} else {
$sth = $self -> process ("delete from $table");
}
$sth -> rows();
}
# -------------------------------
# METHOD: rows TABLE [CONDITIONS]
# -------------------------------
=over 4
=item rows I<table> [I<conditions>]
$components = $dbi_interface -> rows ('components');
$components_needed = $dbi_interface -> rows ('components', 'stock = 0');
Returns the number of rows within I<table> satisfying I<conditions> if any.
=back
=cut
sub rows
{
my $self = shift;
my ($table, $conditions) = @_;
my ($sth, $aref, $rows);
my $where = '';
if (defined ($conditions))
{
$where = " WHERE $conditions";
}
# use COUNT(*) if available
if ($funcmap{$self -> {DRIVER}}->{COUNT})
{
$sth = $self -> process ("SELECT COUNT(*) FROM $table$where");
$aref = $sth->fetch;
$rows = $$aref[0];
}
else
{
$sth = $self -> process ("SELECT * FROM $table$where");
$rows = $sth -> rows;
}
$rows;
}
# -----------------------------------------------
# METHOD: makemap TABLE KEYCOL VALCOL [CONDITION]
# -----------------------------------------------
=over 4
=item makemap I<table> I<keycol> I<valcol> [I<condition>]
$dbi_interface -> makemap ('components', 'idf', 'price');
$dbi_interface -> makemap ('components', 'idf', 'price', 'price > 10');
$dbi_interface -> makemap ('components', 'idf', '*');
$dbi_interface -> makemap ('components', 'idf', '*', 'price > 10');
Produces a mapping between the values within column
I<keycol> and column I<valcol> from I<table>. If an
I<condition> is given, only rows matching this
I<condition> are used for the mapping.
In order to get the hash reference to the record as value of the
mapping, use the asterisk as the I<valcol> parameter.
=back
=cut
sub makemap {
my ($self, $table, $keycol, $valcol, $condition) = @_;
my ($sth, $row, %map, $sel);
my $condstring = '';
# check for condition
if ($condition) {
$condstring = " WHERE $condition";
}
if ($valcol eq '*') {
# need hash reference as value
$sth = $self->process("SELECT * FROM $table$condstring");
while ($row = $sth -> fetchrow_hashref) {
$map{$row->{$keycol}} = $row;
}
} else {
# need particular field as value
$sth = $self -> process ("SELECT $keycol, $valcol FROM $table$condstring");
while ($row = $sth -> fetch) {
$map{$$row[0]} = $$row[1];
}
}
\%map;
}
# -----------------------------------------
# METHOD: random_row TABLE CONDITIONS [MAP]
# -----------------------------------------
=over 4
=item random_row I<table> I<conditions> [I<map>]
Returns random row of the specified I<table>. If I<map> is set,
the result is a hash reference of the selected row, otherwise
an array reference. If the table doesn't contains rows, undefined
is returned.
=back
=cut
#'
sub random_row {
my ($self, $table, $conditions, $map) = @_;
my ($sth, $aref, $row);
if ($conditions) {
$sth = $self -> process ("select * from $table where $conditions");
} else {
$sth = $self -> process ("select * from $table");
}
cache ($table, 'NAME', $sth);
$aref = $sth -> fetchall_arrayref ();
if (@$aref) {
$row = $aref->[int(rand(@$aref))];
if ($map) {
# pass back hash reference
fold ([$self->columns($table)], $row);
} else {
# pass back array reference
$row;
}
}
}
# -------------------------------
# METHOD: serial TABLE SEQUENCE
# -------------------------------
=over 4
=item serial I<table> I<sequence>
Returns a serial number for I<table> by querying the next value from
I<sequence>. Depending on the DBMS one of the parameters is ignored.
This is I<sequence> for mSQL resp. I<table> for PostgreSQL. mysql
doesn't support sequences, but the AUTO_INCREMENT keyword for fields.
In this case this method returns 0 and mysql generates a serial
number for this field.
=back
=cut
#'
sub serial
{
my $self = shift;
my ($table, $sequence) = @_;
my ($statement, $sth, $rv, $resref);
return unless $self -> connect ();
return ('0') if $self->{DRIVER} eq 'mysql';
# get the appropriate statement
$statement = &{$serialstatmap{$self->{DRIVER}}};
# prepare and execute it
$sth = $self -> process ($statement);
unless (defined ($resref = $sth -> fetch))
{
$self -> fatal ("Unexpected result for statement \"$statement\"");
}
$$resref[0];
}
# ---------------------------------------------------------
# METHOD: fill STH HASHREF [FLAG COLUMN ...]
#
# Fetches the next table row from the result stored in STH.
# ---------------------------------------------------------
=over 4
=item fill I<sth> I<hashref> [I<flag> I<column> ...]
Fetches the next table row from the result stored into I<sth>
and records the value of each field in I<hashref>. If I<flag>
is set, only the fields specified by the I<column> arguments are
considered, otherwise the fields specified by the I<column> arguments
are omitted.
=back
=cut
sub fill
{
my ($dbi_interface, $sth, $hashref, $flag, @columns) = @_;
my ($fetchref);
$fetchref = $sth -> fetchrow_hashref;
if ($flag)
{
foreach my $col (@columns)
{
$$hashref{$col} = $$fetchref{$col};
}
}
else
{
foreach my $col (@columns)
{
delete $$fetchref{$col};
}
foreach my $col (keys %$fetchref)
{
$$hashref{$col} = $$fetchref{$col};
}
}
}
# ------------------------------------------------------
# METHOD: view TABLE
#
# Produces text representation for database table TABLE.
# ------------------------------------------------------
=over 4
=item view I<table> [I<name> I<value> ...]
foreach my $table (sort $dbi_interface -> tables)
{
print $cgi -> h2 ('Contents of ', $cgi -> code ($table));
print $dbi_interface -> view ($table);
}
Produces plain text representation of the database table
I<table>. This method accepts the following options as I<name>/I<value>
pairs:
B<columns>: Which columns to display.
B<order>: Which column to sort the row after.
B<limit>: Maximum number of rows to display.
B<separator>: Separator inserted between the columns.
B<where>: Display only rows matching this condition.
print $dbi_interface -> view ($table,
order => $cgi -> param ('order') || '',
where => "price > 0");
=back
=cut
sub view
{
my ($self, $table, %options) = @_;
my ($view, $sth);
my ($orderstr, $condstr) = ('', '');
my (@fields);
unless (exists $options{'limit'}) {$options{'limit'} = 0}
unless (exists $options{'separator'}) {$options{'separator'} = "\t"}
# anonymous function for cells in top row
# get contents of the table
if ((exists ($options{'order'}) && $options{'order'})) {
$orderstr = " ORDER BY $options{'order'}";
}
if ((exists ($options{'where'}) && $options{'where'})) {
$condstr = " WHERE $options{'where'}";
}
if ((exists ($options{'columns'}) && $options{'columns'})) {
$sth = $self -> process ('SELECT ' . $options{'columns'}
. " FROM $table$condstr$orderstr");
} else {
$sth = $self -> process ("SELECT * FROM $table$condstr$orderstr");
}
my $names = $sth -> {NAME};
$view = join($options{'separator'}, map {$_} @$names) . "\n";
my ($count, $ref);
while($ref = $sth->fetch) {
$count++;
undef @fields;
for (@$ref) {
if (defined $_) {
s/\n/\\n/sg;
s/\t/\\t/g;
push (@fields, $_);
} else {
push (@fields, '');
}
}
$view .= join($options{'separator'}, @fields) . "\n";
last if $count == $options{'limit'};
}
# my $rows = $sth -> rows;
# $view .="($rows rows)";
$view;
}
=head2 DATABASE INFORMATION
=over 4
=item is_table I<NAME>
Returns truth value if there exists a table I<NAME> in
this database.
=back
=cut
sub is_table {
my ($self, $name) = shift;
grep {$_ eq $name} ($self->tables);
}
=over 4
=item tables
Returns list of all tables in this database.
=back
=cut
sub tables {
my $self = shift;
my @t;
if ($self->{DRIVER} eq 'mysql') {
map {s/^`(.*)`$/$1/; $_} ($self -> connect () -> tables ());
} else {
$self -> connect () -> tables ();
}
}
=over 4
=item sequences
Returns list of all sequences in this database (Postgres only).
=back
=cut
sub sequences {
my $self = shift;
my (@sequences, $sth, $row);
if ($self->{DRIVER} eq 'Pg') {
$sth = $self -> process ("SELECT relname FROM pg_class WHERE relkind = 'S'");
while ($row = $sth -> fetch ()) {
push (@sequences, $$row[0]);
}
}
return @sequences;
}
# ------------------------------------------
# METHOD: columns TABLE
#
# Returns list of the column names of TABLE.
# ------------------------------------------
=over 4
=item columns I<TABLE>
Returns list of the column names of I<TABLE>.
=back
=cut
sub columns {
my ($self, $table) = @_;
my ($sth);
my (@cols);
if (@cols = cache($table, 'NAME')) {
return @cols;
}
$sth = $self -> process ("SELECT * FROM $table WHERE 0 = 1");
cache($table, 'NAME', $sth);
@{$sth->{NAME}};
}
# -------------------
# METHOD: types TABLE
# -------------------
=over 4
=item types I<TABLE>
Returns list of the column types of I<TABLE>.
=back
=cut
sub types {
my ($self, $table) = @_;
$self->info_proc($table, 'TYPE');
}
# -------------------
# METHOD: sizes TABLE
# -------------------
=over 4
=item sizes I<TABLE>
Returns list of the column sizes of I<TABLE>.
=back
=cut
sub sizes {
my ($self, $table) = @_;
$self->info_proc ($table, 'PRECISION');
}
# ---------------------
# METHOD: typemap TABLE
# ---------------------
=over 4
=item typemap I<TABLE>
Returns mapping between column names and column types
for table I<TABLE>.
=back
=cut
sub typemap {
my ($self, $table) = @_;
$self->info_proc ($table, 'TYPE', 1);
}
# ---------------------
# METHOD: sizemap TABLE
# ---------------------
=over 4
=item sizemap I<TABLE>
Returns mapping between column names and column sizes
for table I<TABLE>.
=back
=cut
sub sizemap {
my ($self, $table) = @_;
$self->info_proc ($table, 'PRECISION', 1);
}
# ---------------------------------------------------------------
# METHOD: add_columns
#
# Creates columns from a representation supplied by describe_table.
# ---------------------------------------------------------------
sub add_columns {
my ($self, $table, $repref, @columns) = @_;
my (%colref, $col, $cref, $null, $line, @stmts, $cmd);
for $col (@columns) {
$colref{$col} = 1;
}
for $cref (@{$repref->{columns}}) {
next unless exists $colref{$cref->{Field}};
$colref{$cref->{Field}} = $cref;
}
for $col (@columns) {
$cref = $colref{$col};
if ($cref->{Null} ne 'YES') {
$null = ' NOT NULL ';
} else {
$null = '';
}
$line = qq{alter table $table add $cref->{Field} $cref->{Type}$null $cref->{Extra} default '$cref->{Default}'};
push (@stmts, $line);
}
for $cmd (@stmts) {
$self->process($cmd);
}
}
# ---------------------------------------------------------------
# METHOD: create_table
#
# Creates table from a representation supplied by describe_table.
# ---------------------------------------------------------------
sub create_table {
my ($self, $table, $repref) = @_;
my $crtstr = '';
my (@stmts, $line, $null, %icols, $colstr);
for my $cref (@{$repref->{columns}}) {
if ($cref->{Null} ne 'YES') {
$null = ' NOT NULL ';
} else {
$null = '';
}
$line = qq{$cref->{Field} $cref->{Type}$null $cref->{Extra} default '$cref->{Default}'};
push (@stmts, $line);
}
for my $cref (@{$repref->{indices}}) {
push (@{$icols{$cref->{Key_name}}}, $cref->{Column_name});
}
for my $cref (@{$repref->{indices}}) {
next unless exists $icols{$cref->{Key_name}};
$colstr = join(', ', @{$icols{$cref->{Key_name}}});
if ($cref->{Key_name} eq 'PRIMARY') {
$line = qq{PRIMARY KEY ($colstr)};
} else {
$line = qq{KEY $cref->{Key_name} ($colstr)};
}
push (@stmts, $line);
delete $icols{$cref->{Key_name}};
}
$crtstr = "create table $table (\n" . join(",\n", @stmts) . ")";
$self->process($crtstr);
}
# ------------------------------------------
# METHOD: describe_table
#
# Returns representation of the given table.
# ------------------------------------------
sub describe_table {
my ($self, $table) = @_;
my ($sth, $href);
my %rep = (columns => [], indices => []);
$sth = $self->process("show columns from $table");
while (my $href = $sth->fetchrow_hashref()) {
push (@{$rep{columns}}, $href);
}
$sth->finish();
$sth = $self->process("show index from $table");
while (my $href = $sth->fetchrow_hashref()) {
push (@{$rep{indices}}, $href);
}
$sth->finish();
\%rep;
}
# --------------------------------------------
# METHOD: now
#
# Returns representation for the current time.
# --------------------------------------------
=head2 TIME VALUES
=over 4
=item now
$dbi_interface -> insert ('transaction',
id => serial ('transaction', 'transactionid'),
time => \$dbi_interface -> now);
Returns representation for the current time. Uses special values of
the DBMS if possible.
=back
=cut
sub now
{
my $self = shift;
# Postgres and mysql have an special value for the current time
if ($self->{DRIVER} eq 'Pg'
|| $self->{DRIVER} eq 'mysql') {
return 'now()';
}
# determine current time by yourself
scalar (gmtime ());
}
# --------------------------------------------------
# METHOD: money2num MONEY
#
# Converts the monetary value MONEY (e.g. $10.00) to
# a numeric one.
# --------------------------------------------------
=head2 MONETARY VALUES
=over 4
=item money2num I<money>
Converts the monetary value I<money> to a numeric one.
=back
=cut
sub money2num
{
my ($self, $money) = @_;
# strip leading dollar sign
$money =~ s/\$//;
# remove commas
$money =~ s/,//g;
# ignore empty pennies
$money =~ s/\.00$//;
$money;
}
# METHOD: filter HANDLE FUNC [TABLE] OPT
my %filter_default_opts = (col_delim => "\t",
col_delim_rep => '\t',
prepend_key => undef,
row_delim => "\n",
row_delim_rep => '\n',
return => '',
prepend_row => undef);
sub filter {
my ($self, $sth, $opt) = @_;
my (@keys, $row, @ret);
for (keys %filter_default_opts) {
$opt->{$_} = $filter_default_opts{$_}
unless defined $opt->{$_};
}
if ($opt->{prepend_key}) {
@keys = @{$sth->{NAME}};
}
while ($row = $sth->fetch()) {
if ($opt->{return} eq 'keys') {
push(@ret, $row->[0]);
}
my @f;
my $i = 0;
for my $f (@$row) {
$f = '' unless defined $f;
$f =~ s/$opt->{row_delim}/$opt->{row_delim_rep}/g;
$f =~ s/$opt->{col_delim}/$opt->{col_delim_rep}/g;
if (defined $opt->{prepend_key}) {
$f = $keys[$i++] . $opt->{prepend_key} . $f;
}
push(@f, $f);
}
if (defined $opt->{prepend_row}) {
print $opt->{prepend_row};
}
print join($opt->{col_delim}, @f), $opt->{row_delim};
}
@ret;
}
# -----------------------------------------------------
# METHOD: is_auth_error MSG
# -----------------------------------------------------
=head2 MISCELLANEOUS
=over 4
=item is_auth_error I<msg>
This method decides if the error message I<msg>
is caused by an authentification error or not.
=back
=cut
sub is_auth_error {
my ($self, $msg) = @_;
if ($self->{DRIVER} eq 'mysql') {
if ($msg =~ /^DBI\sconnect(\('database=.*?(;host=.*?)?',.*?\))? failed: Access denied for user\s/) {
return 1;
}
if ($msg =~ /^DBI->connect(\(database=.*?(;host=.*?)?\))? failed: Access denied for user:/) {
return 1;
}
} elsif ($self->{DRIVER} eq 'Pg') {
if ($msg =~ /^DBI->connect failed.+no password supplied/) {
return 1;
}
}
}
# ------------------------------------------
# METHOD: passwd
#
# Determines password for current user.
# This method is implemented only for Mysql,
# where we can look it up in ~/my.cnf.
# ------------------------------------------
sub passwd {
my ($self) = shift;
my $clientsec = 0;
my ($mycnf, $option, $value);
# implemented only for mysql
return unless $self->{'DRIVER'} eq 'mysql';
# makes sense only for the localhost
return if $self->{'HOST'};
# determine home directory
if (exists $ENV{'HOME'} && $ENV{'HOME'} =~ /\S/ && -d $ENV{'HOME'}) {
$mycnf = $ENV{'HOME'};
} else {
$mycnf = (getpwuid($>)) [7];
}
$mycnf .= '/.my.cnf';
# just give up if file is not accessible
open (CNF, $mycnf) || return;
while (<CNF>) {
# ignore comments and blank lines
next if /^\#/ or /^;/;
next unless /\S/;
# section ?
if (/\[(.*?)\]/) {
if (lc($1) eq 'client') {
$clientsec = 1;
} else {
$clientsec = 0;
}
} elsif ($clientsec) {
# in the [client] section check for password option
($option, $value) = split (/=/, $_, 2);
if ($option =~ /\s*password\s*/) {
$value =~ s/^\s+//;
$value =~ s/\s+$//;
$self->{'PASS'} = $value;
last;
}
}
}
close (CNF);
}
# install error handler
sub install_handler {$_[0] -> {'HANDLER'} = $_[1];}
# direct interface to DBI
sub prepare {my $self = shift; $self -> connect () -> prepare (@_);}
sub commit {my $self = shift; $self->connect (); return if $self->{CONN}->{AutoCommit}; $self->{CONN}->commit();}
sub rollback {$_[0] -> connect () -> rollback ();}
sub quote {$_[0] -> connect () -> quote ($_[1]);}
# auxiliary functions
# ----------------------------------------------------------------
# FUNCTION: cache TABLE TYPE [HANDLE]
#
# This function handles the internal caching of table informations
# like column names and types.
#
# If HANDLE is provided, the information will be fetched from
# HANDLE and stored cache, otherwise the information from the
# cache will be returned.
# ----------------------------------------------------------------
sub cache {
my ($table, $type, $handle) = @_;
my (@types);
if ($cache_structs) {
if ($handle) {
$structs{$table}->{$type} = $handle->{$type};
} else {
if (exists $structs{$table} && exists $structs{$table}->{$type}) {
return @{$structs{$table}->{$type}};
}
}
}
return;
}
# ----------------------------------------------
# FUNCTION: fold ARRAY1 ARRAY2
#
# Returns mapping between the elements of ARRAY1
# and the elements fo ARRAY2.
# ----------------------------------------------
sub fold {
my ($array1, $array2) = @_;
my (%hash);
for (my $i = 0; $i < @$array1; $i++) {
$hash{$$array1[$i]} = $$array2[$i];
}
\%hash;
}
# -----------------------------------------------
# METHOD: info_proc TABLE TOKEN [WANTHASH]
#
# Returns information about the columns of TABLE.
# TOKEN should be either NAME or PRECISION.
# -----------------------------------------------
sub info_proc {
my ($self, $table, $token, $wanthash) = @_;
my $sth;
if ($cache_structs) {
unless (exists $structs{$table}) {
$sth = $self -> process ("SELECT * FROM $table WHERE 0 = 1");
for ('NAME', 'PRECISION', 'TYPE') {
$structs{$table}->{$_} = $sth->{$_};
}
}
if ($wanthash) {
fold ($structs{$table}->{NAME},
$structs{$table}->{$token});
} else {
@{$structs{$table}->{$token}};
}
} else {
$sth = $self -> process ("SELECT * FROM $table WHERE 0 = 1");
if ($wanthash) {
fold ($sth->{NAME}, $sth->{PRECISION});
} else {
@{$sth->{$token}};
}
}
}
1;
__END__
=head1 AUTHORS
Stefan Hornburg (Racke), racke@linuxia.de
Dennis Sch\[:o]n, ds@1d10t.de
Support for Sybase and ODBC provided by
David B. Bitton <david@codenoevil.com>.
=head1 VERSION
0.17
=head1 SEE ALSO
perl(1), DBI(3), DBD::Pg(3), DBD::mysql(3), DBD::msql(3), DBD::Sybase(3), DBD::ODBC(3).
=cut
|