Schrift
[thread]11110[/thread]

Subclassing DBI



<< >> 5 Einträge, 1 Seite
linse
 2008-01-08 15:26
#104483 #104483
User since
2006-02-02
61 Artikel
BenutzerIn
[Homepage]
user image
Hallo beisammen, ich habe mir eine subclass
von DBI geschrieben, die SQL statements aus ner Tabelle liest und diese per autoload bereit stellt, habe in dem zusammenhang aber noch ein paar probleme:
Erstens: Ohne no warnings 'redefine'; bekomme ich bei jeder sub
nen warnung das die methode bereits exestiert (nicht bei den dynamischen).
Zweitens: Jemand nen vorschlag wie ich am besten nen platzhalter für den Tabellennamen
implementiere ?
Und last but not least: Probierts aus und gibt mir feedbacks oder verbesserungsvorschläge!

mfg Dirk

Code: (dl )
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
package DBI::LZE;
use strict;
use warnings;
no warnings 'redefine';
use vars qw( $dbh $dsn $DefaultClass $settings @EXPORT_OK @ISA %functions $style $right $install);
$DefaultClass = 'DBI::LZE' unless defined $DBI::LZE::DefaultClass;
@DBI::LZE::EXPORT_OK = qw( useexecute quote void fetch_hashref fetch_AoH fetch_array updateModules deleteexecute editexecute addexecute tableLength tableExists initDB $dsn $dbh);
%DBI::LZE::EXPORT_TAGS = (
'all' => [qw( useexecute quote void fetch_hashref fetch_AoH fetch_array updateModules deleteexecute editexecute addexecute tableLength tableExists initDB)],
'dynamic' => [qw( useexecute void fetch_hashref fetch_AoH fetch_array updateModules deleteexecute editexecute addexecute)],
'independent' => [qw(tableLength tableExists initDB useexecute void fetch_hashref fetch_AoH fetch_array updateModules deleteexecute editexecute addexecute)],
);
$DBI::LZE::VERSION = '0.2.1';
require Exporter;
use DBI;
@DBI::LZE::ISA = qw( Exporter DBI);

$install = 0;

=head1 NAME

DBI::LZE

=head1 SYNOPSIS

FO Syntax

use DBI::LZE qw(:all);

my $dbh = initDB({name => 'LZE',host => 'localhost',user => 'root',password =>'',style=> 'Crystal'});


OO Syntax

use DBI::LZE;

my $database = new DBI::LZE(

{

name =>'LZE',

host => 'localhost',

user => 'root',

password =>'',

style=> 'Crystal'

}

);

my %execute = (

title => 'showTables',

description => 'description',

sql => "show tables",

return => "fetch_array",

);

$database->addexecute(\%execute);

$database->showTables();

=head2 Export Tags

:all execute useexecute quote void fetch_hashref fetch_AoH fetch_array updateModules deleteexecute editexecute addexecute tableLength tableExists initDB

:dynamic execute useexecute void fetch_hashref fetch_AoH fetch_array updateModules deleteexecute editexecute addexecute

independent: tableLength tableExists initDB :dynamic

=head1 DESCRIPTION

DBI::LZE

=head1 BUGS & LIMITATIONS

you can`t use the dynamic statements under mod_perl.

=head2 new()

my $database = new DBI::LZE(optional \%initializer);

see initDB()

=cut

sub new {
my ($class, @initializer) = @_;
my $self = {};
bless $self, ref $class || $class || $DefaultClass;
$self->initDB(@initializer) if(@initializer);
return $self;
}

=head2 initDB()

my $dbh = initDB({name => 'LZE',host => 'localhost',user => 'root',password =>'',style=> 'Crystal'});

=cut

sub initDB {
my ($self, @p) = getSelf(@_);
my $hash = $p[0];
my $database = defined $hash->{name} ? $hash->{name} : 'LZE';
my $host = defined $hash->{host} ? $hash->{host} : 'localhost';
my $user = defined $hash->{user} ? $hash->{user} : 'root';
my $pass = defined $hash->{password} ? $hash->{password} : '';
$style = defined $hash->{style} ? $hash->{style} : 'Crystal';
$dsn = "DBI:mysql:database=$database;host=$host";
$dbh = DBI::LZE->connect($dsn, $user, $pass, {RaiseError => 0, PrintError => 0, AutoCommit => 1,}) or warn "DBI::LZE::errs";

unless ($install) {
my @q = $self->fetch_array("select title from querys");
$functions{$_} = $_ foreach (@q);
}
return $dbh;
}

=head1 independent functions

=head2 tableExists()

$bool = $database->tableExists($table);

=cut

sub tableExists {
my ($self, @p) = getSelf(@_);
my $table = $p[0];
my $db_clause = "";
($db_clause, $table) = (" FROM $1", $2) if $table =~ /(.*)\.(.*)/;
return ($dbh->selectrow_array("SHOW TABLES $db_clause LIKE '$table'"));
}

=head2 tableLength

$length = $database->tableLength($table);

=cut

sub tableLength {
my ($self, @p) = getSelf(@_);
my $table = $p[0];
my $sql = "select count(*) from `$table`";
if($self->tableExists($p[0])) {
if(defined $p[1]) {
$sql = "select count(*) from `$table`";
}
my $sth = $dbh->prepare($sql) or warn $dbh->errstr;
$sth->execute() or warn $dbh->errstr;
my $length = $sth->fetchrow_array;
$sth->finish();
return $length;
} else {
return 0;
}
}

=head1 dynamic statements

=head2 addexecute()

add sql statments to yourdatabase for later use witdh useexecute();

my %execute = (

title => 'showTables',

description => 'description',

sql => "show tables",

return => "fetch_array",

);

$database->addexecute(\%execute);

print join '<br/>' ,$database->showTables();

Fo Syntax:

print join '<br/>' , useexecute('showTables');

=cut

sub addexecute {
my ($self, @p) = getSelf(@_);
my $hash = $p[0];
my $title = ((defined $hash->{title})) ? $hash->{title} : 0;
my $sql = $hash->{sql} if((defined $hash->{sql}));
my $description = $hash->{description} if(defined $hash->{description});
my $return = $hash->{'return'} if(defined $hash->{'return'});
unless ($functions{$title}) {
my $sql_addexecute = qq/INSERT INTO querys(`title`,`sql`,`description`,`return`) VALUES(?,?,?,?);/;
my $sth = $dbh->prepare($sql_addexecute);
$sth->execute($title, $sql, $description, $return) or warn $dbh->errstr;
$sth->finish();
$self->updateModules();
} else {
return 0;
}
}

=head2 editexecute

my %hash = (

title => 'Titel',

newTitle => 'New Titel',

description => 'querys Abfragen',

sql => "sql statement",

return => 'fetch_hashref', #subname

);

editexecute(\%hash);

=cut

sub editexecute {
my ($self, @p) = getSelf(@_);
my $hash = $p[0];
my $title = ((defined $hash->{title})) ? $hash->{title} : 0;
my $newTitle = ((defined $hash->{newTitle})) ? $hash->{newTitle} : $title;
my $sql = $hash->{sql} if((defined $hash->{sql}));
my $description = $hash->{description} if(defined $hash->{description});
my $return = (defined $hash->{'return'}) ? $hash->{'return'} : 'array';

if($functions{$title}) {
my $sql_edit = qq(update querys set title = ?, sql=? ,description=?,return=? where title = ? );
my $sth = $dbh->prepare($sql_edit);
$sth->execute($newTitle, $sql, $description, $return, $title) or warn $dbh->errstr;
$sth->finish();

} else {
return 0;
}
}

=head2 useexecute()

useexecute($title, @parameter);

=cut

sub useexecute {
my ($self, @p) = getSelf(@_);
my $title = shift(@p);
my $sql = "select `sql`,`return` from querys where `title` = ?";
my $sth = $dbh->prepare($sql);
$sth->execute($title) or warn $dbh->errstr;
my ($sqlexec, $return) = $sth->fetchrow_array();
$sth->finish();
return eval(" \$self->$return(\$sqlexec,\@p)");
}

=head2 deleteexecute()

deleteexecute($title);

=cut

sub deleteexecute {
my ($self, @p) = getSelf(@_);
my $id = $p[0];
my $sql_delete = "DELETE FROM querys Where title = ?";
my $sth = $dbh->prepare($sql_delete);
$sth->execute($id) or warn $dbh->errstr;
$sth->finish();
}

=head2 fetch_array()

@AoA = $database->fetch_array($sql);

=cut

sub fetch_array {
my ($self, @p) = getSelf(@_);
my $sql = shift @p;
my @r;
eval('
my $sth = $dbh->prepare($sql);
if(defined $p[0]) {
$sth->execute(@p) or warn $dbh->errstr;
}else {
$sth->execute() or warn $dbh->errstr;
}
while(my @comms = $sth->fetchrow_array()) {
push(@r, @comms);
}
$sth->finish();');
@r = $@ if $@;
return @r;
}
linse
 2008-01-08 15:27
#104484 #104484
User since
2006-02-02
61 Artikel
BenutzerIn
[Homepage]
user image
Der Code war ein wenig zu lang für eine Message.
Code: (dl )
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
=head2 fetch_AoH()

@aoh = $database->fetch_AoH($sql)

=cut

sub fetch_AoH {
my ($self, @p) = getSelf(@_);
my $sql = shift @p;
my @r;
eval('
my $sth = $dbh->prepare($sql);
if(defined $p[0]) {
$sth->execute(@p) or warn $dbh->errstr;
} else {
$sth->execute() or warn $dbh->errstr;
}
while(my $h = $sth->fetchrow_hashref) {
push(@r, $h);
}
$sth->finish();');
@r = $@ if $@;
return @r;
}

=head2 fetch_hashref()

$hashref = $database->fetch_hashref($sql)

=cut

sub fetch_hashref {
my ($self, @p) = getSelf(@_);
my $sql = shift @p;
my $h;
eval('
my $sth = $dbh->prepare($sql);
if(defined $p[0]) {
$sth->execute(@p) or warn $dbh->errstr;
} else {
$sth->execute() or warn $dbh->errstr;
}
my @r;
$h = $sth->fetchrow_hashref();
$sth->finish();
');
$h = "$@" if $@;
return $h;
}

=head2 void()

void(sql)

=cut

sub void {
my ($self, @p) = getSelf(@_);
my $sql = shift @p;
my $sth = $dbh->prepare($sql);
eval('
if(defined $p[0]) {
$sth->execute(@p) or warn $dbh->errstr;
} else {
$sth->execute() or warn $dbh->errstr;
}');
$sth->finish();
return "$@" if $@;
}

=head2 quote()

$quotedString = $database->quote($sql);

=cut

sub quote {
my ($self, @p) = getSelf(@_);
my $sql = $p[0];
return $dbh->quote($sql);
}

=head1 Privat

=head2 updateModules()

=cut

sub updateModules {
my ($self, @p) = getSelf(@_);
my @q = $self->fetch_array("select title from querys");
$functions{$_} = $_ foreach (@q);
}

=head2 getSelf()

=cut

sub getSelf {
return @_ if defined($_[0]) && (!ref($_[0])) && ($_[0] eq 'DBI::LZE');
return (defined($_[0]) && (ref($_[0]) eq 'DBI::LZE' || UNIVERSAL::isa($_[0], 'DBI::LZE'))) ? @_ : ($DBI::LZE::DefaultClass->new, @_);
}

=head2 AUTOLOAD()

statements add by addexecute can called like

$database->showTables()

=cut

sub AUTOLOAD {
my ($self, @p) = getSelf(@_);
our $AUTOLOAD;
if($AUTOLOAD =~ /.*::(\w+)$/ and grep $1 eq $_, %functions) {
my $attr = $1;
{
no strict 'refs';
*{$AUTOLOAD} = sub {
$self->useexecute($attr, @p);
};
}
goto &{$AUTOLOAD};
}
}

package DBI::LZE::db;
use vars qw(@ISA);
@ISA = qw(DBI::db);

=head2 prepare()


=cut

sub prepare {
my ($dbh, @args) = @_;
my $sth = $dbh->SUPER::prepare(@args) or return;
return $sth;
}

package DBI::LZE::st;
use vars qw(@ISA);
@ISA = qw(DBI::st);

=head2 execute()


=cut

sub execute {
my ($sth, @args) = @_;
my $rv;
eval('$rv = $sth->SUPER::execute(@args)');
return "$@" if $@;
return $rv;
}

=head2 fetch()


=cut

sub fetch {
my ($sth, @args) = @_;
my $row = $sth->SUPER::fetch(@args) or return;
return $row;
}

1;


Code: (dl )
1
2
3
4
5
6
7
8
CREATE TABLE IF NOT EXISTS querys (
title varchar(100) NOT NULL default '',
description text NOT NULL,
`sql` text NOT NULL,
`return` char(3) NOT NULL default 'AoA',
`id` int(11) NOT NULL auto_increment,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Der code ist auch unter: ftp://lindnerei.de/DBI-LZE-0.2.1.tar.gz
zu finden.
renee
 2008-01-08 15:59
#104485 #104485
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Auf jeden Fall mal statt `$table` die Funktion quote_identifier aus DBI verwenden...
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
murphy
 2008-01-08 16:39
#104487 #104487
User since
2004-07-19
1776 Artikel
HausmeisterIn
[Homepage]
user image
Anstatt die Klassenhierarchie von Hand zusammenzustöpseln und das Exportermodul von Hand einzubinden könnte man doch einfach folgendes schreiben:
Code (perl): (dl )
1
2
3
4
5
6
package DBI::LZE;
...
use base qw/DBI/;
use Exporter qw/import/;
our @EXPORT_OK = ...
our %EXPORT_TAGS = ...

Ich persönlich finde das sauberer und einfacher und es müsste eigentlich auch alles Nötige veranlassen um die Redefinitionswarnungen abzustellen.
When C++ is your hammer, every problem looks like your thumb.
linse
 2008-01-08 19:46
#104498 #104498
User since
2006-02-02
61 Artikel
BenutzerIn
[Homepage]
user image
Wuste bisher gar nicht das es die funktion quote_identifier gibt, Danke !
use base qw/DBI/ ist mir bekannt, habe ich bishernur niebenutzt da das nur
mit OO modulen funktioniert. Werde es aber in betracht ziehen.

Danke schon mal
<< >> 5 Einträge, 1 Seite



View all threads created 2008-01-08 15:26.