Repository navigation
Expand file tree
/
Copy pathMySQL.pm
More file actions
275 lines (198 loc) · 6.55 KB
/
Copy pathMySQL.pm
File metadata and controls
275 lines (198 loc) · 6.55 KB
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
package PPOBackend::MySQL;
# PPOBackend::MySQL - MySQL backend for PPO
# $Id: MySQL.pm,v 1.11 2010-03-23 13:57:51 paczian Exp $
use strict;
use warnings;
use DBI;
use base qw( PPOBackend );
1;
=pod
=head1 NAME
PPOBackend::MySQL - MySQL backend for PPO
=head1 DESCRIPTION
This package implements the abstract PPOBackend wrapper for use with MySQL database
server (based on the DBI perl module).
=head1 METHODS
=over 4
=item * B<new> (I<host>, I<port>, I<database>, I<user>, I<password>, I<create_flag>)
Connects to the database backend using the database host I<host> on port I<port>.
If either I<host> or I<port> is false (undef), it uses the default host or port.
The parameter I<database_name> specifies the database to use and is mandatory.
The parameters I<user> and I<password> are optional. If set to a username and it's
cleartext password, those information will be used to authenticate with the database.
If I<create_flag> is set, new will create the database before connecting
to it.
=cut
sub new {
my ($class, $host, $port, $database, $user, $password, $create, $socket, $file) = @_;
# build connect string from parameters
my $connect = "DBI:mysql:".( ($create) ? '' : "database=$database" );
$connect .= ";host=$host" if ($host);
$connect .= ";port=$port" if ($port);
$connect .= ";mysql_socket=$socket" if ($socket);
my $dbsock = $Conf::dbsock;
$connect .= ";mysql_socket=$dbsock" if ($dbsock);
$user = (defined $user) ? $user : '';
$password = (defined $password) ? $password : '';
# initialize database handle.
my $dbh = DBI->connect($connect, $user, $password,
{ RaiseError => 1, AutoCommit => 0, PrintError => 0,
mysql_enable_utf8 => 1 }) ||
Confess("Database connect error.");
my $self = { 'dbhandle' => $dbh,
'source' => $database,
'connect' => $connect,
'database_name' => $database,
'file' => $file
};
bless ($self,$class);
# create database if necessary
if ($create && !$file) {
eval {
$self->dbh->do( "CREATE DATABASE $database");
$self->dbh->do( "USE $database");
$self->dbh->commit;
};
if ($@) {
eval { $self->dbh->rollback };
if ($@) {
Confess("Rollback failed: $@");
}
return undef;
}
}
return $self;
}
=pod
=item * B<new_from_connect_data> (I<connect_data>, I<database>, I<user>, I<password>)
Connects to the database backend using the I<connect_data> string which is stored
by the PPO when an object reference is created. The parameter I<database_name>
specifies the database to use and is mandatory.
The parameters I<user> and I<password> are optional. If set to a username and it's
cleartext password, those information will be used to authenticate with the database.
=cut
sub new_from_connect_data {
my ($class, $connect, $database, $user, $password) = @_;
$user = (defined $user) ? $user : '';
$password = (defined $password) ? $password : '';
# initialize database handle
my $dbh = DBI->connect($connect, $user, $password,
{ RaiseError => 1, AutoCommit => 0, PrintError => 0, mysql_enable_utf8 => 1 }) ||
Confess("Database connect error.");
my $self = { 'dbhandle' => $dbh,
'source' => $database,
'connect' => $connect,
'database_name' => $database,
};
bless ($self,$class);
return $self;
}
=pod
=item * B<last_insert_id> ()
Returns the row id of the last insert command.
=cut
sub last_insert_id {
return $_[0]->dbh->{'mysql_insertid'};
}
=pod
=item * B<map_data_type> (I<data_type>)
Returns the mapping of a PPO datatype (read from the xml definition file) to
the column data type of the database table.
=cut
sub map_data_type {
my ($self, $type) = @_;
$type = uc($type);
my $types = { 'BOOLEAN' => 'BOOLEAN',
'INTEGER' => 'INTEGER',
'BIGINT' => 'BIGINT',
'FLOAT' => 'DOUBLE',
'TIMESTAMP' => 'TIMESTAMP',
'CHAR' => 'VARCHAR',
'TEXT' => 'TEXT',
'TEXT LONG' => 'LONGTEXT',
'BLOB' => 'BLOB',
'BLOB LONG' => 'LONGBLOB',
};
# simple data type
if (exists($types->{$type})) {
return $types->{$type};
}
# special case for CHAR(n)
elsif ($type =~ /^CHAR\((\d+)\)$/ or $type =~ /^CHAR \((\d+)\)$/) {
return "VARCHAR($1)";
}
# error
else {
Confess("Unknown data type '$type'.");
}
}
=pod
=item * B<create_table> (I<table>, I<columns>)
Create a new table I<table> in the database with the columns described in
I<columns>. The parameter I<columns> is an array of hashes. Each hash describes
one column and has the fields 'name', 'type', 'not_null', 'auto_increment' and
'primary_key'.
=cut
sub create_table {
my ($self, $table, $columns) = @_;
my @cols;
foreach my $c (@$columns) {
unless ($c->{name} and $c->{type}) {
die 'Method create_table called with incomplete parameters.';
}
my $column = $c->{name}.' '.$self->map_data_type($c->{type});
$column .= ' NOT NULL' if ($c->{not_null});
$column .= ' PRIMARY KEY' if ($c->{primary_key});
$column .= ' AUTO_INCREMENT' if ($c->{auto_increment});
push @cols, $column;
}
my $statement = sprintf ("CREATE TABLE %s ( %s )", $table, join(', ', @cols));
eval {
if ($self->{file}) {
open(FH, ">>".$self->{file}) or die "could not open sql output file ".$self->{file}.": $@ $!\n";
print FH $statement."\n";
close FH;
} else {
$self->dbh->do($statement);
$self->dbh->commit;
}
};
if ($@) {
eval { $self->dbh->rollback };
if ($@) {
Confess("Rollback failed: $@");
}
return undef;
}
return 1;
}
=pod
=item * B<create_index> (I<table>, I<columns>, I<unique>)
Create a new index on table I<table> in the database with the columns indexed given
in I<columns>. The parameter I<columns> is a reference to an array of column names.
If I<unique> is given and evaluates to true, the method will create an unique index.
=cut
sub create_index {
my ($self, $table, $index_name, $columns, $unique) = @_;
my $unique_string = ($unique) ? ' UNIQUE' : '';
my $statement = sprintf ("CREATE%s INDEX %s ON %s ( %s )",
$unique_string, $index_name, $table, join(', ', @{$columns}));
eval {
if ($self->{file}) {
open(FH, ">>".$self->{file}) or die "could not open sql output file ".$self->{file}.": $@ $!\n";
print FH $statement."\n";
close FH;
} else {
$self->dbh->do($statement);
$self->dbh->commit;
}
};
if ($@) {
eval { $self->dbh->rollback };
if ($@) {
Confess("Rollback failed: $@");
}
return undef;
}
return 1;
}