]>
jfr.im git - irc/SurrealServices/srsv.git/blob - branches/mus-0.4.x-devel/SrSv/DB/StubGen/Stub.pm
1 # This file is part of Invid
3 # Invid is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License version 2.1 as published by the Free Software Foundation.
7 # This library is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 # Lesser General Public License for more details.
12 # You should have received a copy of the GNU Lesser General Public
13 # License along with this library; if not, write to the Free Software
14 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 # Copyright Adam Schrotenboer <adam@tabris.net> 2007, 2008
19 # This code is based in large part on the MySQL::Stub from SrSv, as well as
20 # the DB::Sub from M2000's CMS.
24 SrSv::DB::StubGen::Stub - Create functions for SQL queries
28 package SrSv
::DB
::StubGen
::Stub
;
31 use Carp
qw( confess );
34 # For INSERT queries, returns last_insert_id.
39 eval { $q->execute(@_); };
40 if($@) { confess
($@) }
42 return $dbh->last_insert_id(undef, undef, undef, undef);
46 # For UPDATE or DELETE queries; returns number of rows affected.
52 eval { $ret = $q->execute(@_) + 0; }; # Force it to be a number.
53 if($@) { confess
($@) }
59 # For queries that return only one row with one columns; returns a scalar.
64 eval { $q->execute(@_); };
65 if($@) { confess
($@) }
67 eval { ($scalar) = $q->fetchrow_array; };
68 if($@) { confess
($@) }
74 # For queries that return only one row with multiple columns; returns a 1-dimensional array.
79 eval { $q->execute(@_); };
80 if($@) { confess
($@) }
82 eval { @row = $q->fetchrow_array; };
83 if($@) { confess
($@) }
90 # For queries that return just a single column, multiple rows
96 eval { $q->execute(@_); };
97 if($@) { confess
($@) }
99 eval { $arrayref = $q->fetchall_arrayref() };
100 if($@) { confess
($@) }
103 return map({ $_->[0] } @$arrayref);
108 # For other queries; returns an arrayref.
113 #die "improper number of parameters for $sth\n" unless $q->{NUM_OF_PARAMS} == scalar(@_);
114 eval { $q->execute(@_); };
115 if($@) { confess
($@) }
116 if ($q->err) { say ("ERROR: ", $q->err); }
118 eval { $arrayref = $q->fetchall_arrayref() };
119 if($@) { confess
($@) }
126 ARRAYREF
=> sub ($) {
132 eval { $arrayref = $q->fetchall_arrayref() };
133 if($@) { confess
($@) }
141 shift @_; # Remove most-recent-caller package name from arg list.
143 # this is the _original_ package caller
144 my $package = shift @_;
147 my $printError = $dbh->{PrintError
};
148 $dbh->{PrintError
} = 1;
151 my ($name, $type, $query) = @$_;
154 $query =~ s/\s{2,}/ /g;
158 my $q = $dbh->prepare($query);
161 my $sub = $create_sub{$type}->($dbh, $q);
163 # Export subroutine into caller's namespace.
166 *{"${package}::${name}"} = $sub;
169 $dbh->{PrintError
} = $printError;