-
Notifications
You must be signed in to change notification settings - Fork 2
/
Mysql_connect.pm
executable file
·116 lines (93 loc) · 2.71 KB
/
Mysql_connect.pm
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
#!/usr/bin/env perl
package main;
our ($SEE, $DEBUG);
package Mysql_connect;
require 5.6.0;
require Exporter;
use Carp;
use strict;
use Data::Dumper;
our @ISA = qw(Exporter);
## export the following for general use
our @EXPORT = qw ($QUERYFAIL do_sql_2D connect_to_db RunMod first_result_sql very_first_result_sql);
our $QUERYFAIL = 0; #intialize. Status flag, indicating the success of a query.
############### DATABASE CONNECTIVITY ################################
####
sub connect_to_db {
my ($server, $db, $username, $password) = @_;
unless ($server) {
$server = "localhost";
}
my $dbproc = DBI->connect("DBI:mysql:$db:$server", $username, $password);
unless (ref $dbproc) {
croak "Cannot connect to $server: $DBI::errstr";
}
$dbproc->{RaiseError} = 1; #turn on raise error. Must use exception handling now.
return($dbproc);
}
## return results in 2-Dimensional array.
sub do_sql_2D {
my ($dbproc,$query, @values) = @_;
my ($statementHandle,@x,@results);
my ($i,$result,@row);
## Use $QUERYFAIL Global variable to detect query failures.
$QUERYFAIL = 0; #initialize
print "QUERY: $query\tVALUES: @values\n" if($::DEBUG||$::SEE);
$statementHandle = $dbproc->prepare($query);
if ( !defined $statementHandle) {
print "Cannot prepare statement: $DBI::errstr\n";
$QUERYFAIL = 1;
} else {
# Keep trying to query thru deadlocks:
do {
$QUERYFAIL = 0; #initialize
eval {
$statementHandle->execute(@values);
while ( @row = $statementHandle->fetchrow_array() ) {
push(@results,[@row]);
}
};
## exception handling code:
if ($@) {
print STDERR "failed query: <$query>\tvalues: @values\nErrors: $DBI::errstr\n";
$QUERYFAIL = 1;
}
} while ($statementHandle->errstr() =~ /deadlock/);
#release the statement handle resources
$statementHandle->finish;
}
if ($QUERYFAIL) {
die "Failed query: <$query>\tvalues: @values\nErrors: $DBI::errstr\n";
}
return(@results);
}
sub RunMod {
my ($dbproc,$query, @values) = @_;
my ($result);
if($::DEBUG||$::SEE) {print "QUERY: $query\tVALUES: @values\n";}
if($::DEBUG) {
$result = "NOT READY";
} else {
eval {
$dbproc->do($query, undef, @values);
};
if ($@) { #error occurred
die "failed query: <$query>\tvalues: @values\nErrors: $DBI::errstr\n";
}
}
}
sub first_result_sql {
my ($dbproc, $query, @values) = @_;
my @results = &do_sql_2D ($dbproc, $query, @values);
return ($results[0]);
}
sub very_first_result_sql {
my ($dbproc, $query, @values) = @_;
my @results = &do_sql_2D ($dbproc, $query, @values);
if ($results[0]) {
return ($results[0]->[0]);
} else {
return (undef());
}
}
1; #EOM