/[A3C]/lib/A3C/SQL.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /lib/A3C/SQL.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 218 - (show annotations)
Sat Jun 21 11:01:35 2008 UTC (11 years, 9 months ago) by dpavlin
File size: 2439 byte(s)
Check did we get valid utf-8 from DBD driver
1 package A3C::SQL;
2
3 use strict;
4 use warnings;
5
6 use base qw(Jifty::Object Class::Accessor::Fast);
7 __PACKAGE__->mk_accessors( qw(query arguments dbh encoding) );
8
9 use Data::Dump qw/dump/;
10
11 =head1 NAME
12
13 A3C::SQL
14
15 =head1 DESCRIPTION
16
17 Issue SQL queries
18
19 =head1 METHODS
20
21 =head2 new
22
23 my $sql = A3C::SQL->new({ query => 'select now()' });
24
25 As a alternative, if you don't want to use Jifty's database handle,
26 specify it like this:
27
28 my $sql = A3C::SQL->new({
29 query => 'select now()',
30 dbh => $my_dbh,
31 encoding => 'UTF-8',
32 });
33
34 Mungling with C<encoding> is rearly needed, especially if using recent C<DBD::Pg> as driver.
35
36 =head2 sth
37
38 Execute query and return statement handle. Ususally you don't have to call this manually.
39
40 =cut
41
42 sub sth {
43 my $self = shift;
44 if ( ! $self->{_sth} ) {
45 my $dbh = $self->dbh || Jifty->handle->dbh;
46 my $sth = $dbh->prepare( $self->query ) or $dbh->errstr;
47 if ( $self->arguments ) {
48 Jifty->log->debug( $self->sql . ' arguments: ' . dump( $self->arguments ) );
49 $sth->execute( $self->arguments ) or die $dbh->errstr;
50 } else {
51 $sth->execute or die $dbh->errstr;
52 }
53 $self->{_sth} = $sth;
54 }
55
56 return $self->{_sth};
57 }
58
59 =head2 next
60
61 while ( my $row = $sql->next ) {
62 print $row->now;
63 }
64
65 =cut
66
67 sub next {
68 my $self = shift;
69 my $row = $self->sth->fetchrow_hashref;
70 return unless defined $row;
71 return A3C::SQL::row->new( $row, $self->encoding );
72 }
73
74 =head2 count
75
76 print $sql->count;
77
78 =cut
79
80 sub count {
81 my $self = shift;
82 return $self->sth->rows;
83 }
84
85 =head1 HELPERS
86
87 This helpers are accessor to L<DBI>
88
89 =head2 _column_names
90
91 my @columns = $sql->_column_names;
92
93 =cut
94
95 sub _column_names {
96 my $self = shift;
97 return @{ $self->sth->{NAME} };
98 }
99
100 package A3C::SQL::row;
101
102 use Encode qw/decode/;
103 use Data::Dump qw/dump/;
104 use base qw/Jifty::Object/;
105
106 our $AUTOLOAD;
107
108 sub new {
109 my $that = shift;
110 my $class = ref($that) || $that;
111 my $self = shift;
112 bless $self, $class;
113 $self->{__encoding} = shift || 'UTF-8';
114 return $self;
115 }
116
117 sub AUTOLOAD {
118 my $self = shift;
119 my $type = ref($self) or die "$self is not an object";
120 my $name = $AUTOLOAD;
121 $name =~ s/.*://;
122 my $v = $self->{$name};
123 Jifty->log->error("SQL: $name doesn't exist") unless defined $v;
124 if ( ! Encode::is_utf8( $v ) ) {
125 eval { $v = decode( $self->{__encoding}, $self->{$name} ) };
126 if ( $@ ) {
127 warn "## column $name can't decode ",dump( $self->{$name} );
128 $v = $self->{$name};
129 }
130 }
131 return $v;
132 }
133
134 sub DESTROY {}
135
136 1;

  ViewVC Help
Powered by ViewVC 1.1.26