Thread Subclassing DBI (4 answers)
Opened by linse at 2008-01-08 15:26

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.

View full thread Subclassing DBI