/[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

Diff of /lib/A3C/SQL.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 82 by dpavlin, Sat Apr 12 10:28:38 2008 UTC revision 218 by dpavlin, Sat Jun 21 11:01:35 2008 UTC
# Line 3  package A3C::SQL; Line 3  package A3C::SQL;
3  use strict;  use strict;
4  use warnings;  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  =head1 NAME
12    
13  A3C::SQL  A3C::SQL
# Line 17  Issue SQL queries Line 22  Issue SQL queries
22    
23    my $sql = A3C::SQL->new({ query => 'select now()' });    my $sql = A3C::SQL->new({ query => 'select now()' });
24    
25  =cut  As a alternative, if you don't want to use Jifty's database handle,
26    specify it like this:
27    
28  use base qw(Jifty::Object Class::Accessor::Fast);    my $sql = A3C::SQL->new({
29  __PACKAGE__->mk_accessors( qw(query arguments) );          query => 'select now()',
30            dbh => $my_dbh,
31            encoding => 'UTF-8',
32      });
33    
34  use Data::Dump qw/dump/;  Mungling with C<encoding> is rearly needed, especially if using recent C<DBD::Pg> as driver.
35    
36  =head2 sth  =head2 sth
37    
# Line 33  Execute query and return statement handl Line 42  Execute query and return statement handl
42  sub sth {  sub sth {
43          my $self = shift;          my $self = shift;
44          if ( ! $self->{_sth} ) {          if ( ! $self->{_sth} ) {
45                  my $dbh = Jifty->handle->dbh;                  my $dbh = $self->dbh || Jifty->handle->dbh;
46                  my $sth = $dbh->prepare( $self->query ) or $dbh->errstr;                  my $sth = $dbh->prepare( $self->query ) or $dbh->errstr;
47                  if ( $self->arguments ) {                  if ( $self->arguments ) {
48                          Jifty->log->debug( $self->sql . ' arguments: ' . dump( $self->arguments ) );                          Jifty->log->debug( $self->sql . ' arguments: ' . dump( $self->arguments ) );
49                          $sth->execute( $self->arguments ) or $dbh->errstr;                          $sth->execute( $self->arguments ) or die $dbh->errstr;
50                  } else {                  } else {
51                          $sth->execute or $dbh->errstr;                          $sth->execute or die $dbh->errstr;
52                  }                  }
53                  $self->{_sth} = $sth;                  $self->{_sth} = $sth;
54          }          }
# Line 59  sub next { Line 68  sub next {
68          my $self = shift;          my $self = shift;
69          my $row = $self->sth->fetchrow_hashref;          my $row = $self->sth->fetchrow_hashref;
70          return unless defined $row;          return unless defined $row;
71          return A3C::SQL::row->new( $row );          return A3C::SQL::row->new( $row, $self->encoding );
72  }  }
73    
74  =head2 count  =head2 count
# Line 73  sub count { Line 82  sub count {
82          return $self->sth->rows;          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;  package A3C::SQL::row;
101    
102  use Encode qw/decode/;  use Encode qw/decode/;
103  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
104    use base qw/Jifty::Object/;
105    
106  our $AUTOLOAD;  our $AUTOLOAD;
107    
# Line 85  sub new { Line 110  sub new {
110          my $class = ref($that) || $that;          my $class = ref($that) || $that;
111          my $self = shift;          my $self = shift;
112          bless $self, $class;          bless $self, $class;
113            $self->{__encoding} = shift || 'UTF-8';
114          return $self;          return $self;
115  }    }  
116    
# Line 93  sub AUTOLOAD { Line 119  sub AUTOLOAD {
119          my $type = ref($self) or die "$self is not an object";          my $type = ref($self) or die "$self is not an object";
120          my $name = $AUTOLOAD;          my $name = $AUTOLOAD;
121          $name =~ s/.*://;          $name =~ s/.*://;
122  #       warn "SQL: $name doesn't exist" unless defined($self->{$name});          my $v = $self->{$name};
123          return decode('UTF-8', $self->{$name});          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 {}  sub DESTROY {}

Legend:
Removed from v.82  
changed lines
  Added in v.218

  ViewVC Help
Powered by ViewVC 1.1.26