File Coverage

blib/lib/Dancer2/Plugin/Pg/Core.pm
Criterion Covered Total %
statement 6 184 3.2
branch 0 72 0.0
condition 0 21 0.0
subroutine 2 14 14.2
pod 0 10 0.0
total 8 301 2.6


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::Pg::Core;
2              
3 1     1   3 use Moo;
  1         1  
  1         6  
4 1     1   1533 use DBI;
  1         11157  
  1         1283  
5              
6             has 'dbh' => (
7             is => 'ro',
8             writer => 'set_dbh'
9             );
10              
11             has ['host', 'base', 'port', 'username', 'password'] => (
12             is => 'ro'
13             );
14              
15             has 'options' => (
16             is => 'ro',
17             default => sub {
18             {AutoCommit => 1, AutoInactiveDestroy => 1, PrintError => 0, RaiseError => 1}
19             }
20             );
21              
22             has ['table', 'returning'] => (
23             is => 'rw'
24             );
25              
26             has ['keys', 'values', 'type', 'reference'] => (
27             is => 'rw',
28             default => sub {
29             []
30             }
31             );
32              
33             sub BUILD {
34 0     0 0   my $self = shift;
35            
36 0           my $dsn = 'dbi:Pg:';
37 0 0         $dsn .= 'dbname=' . $self->base if $self->base;
38 0 0         $dsn .= ';host=' . $self->host if $self->host;
39 0 0         $dsn .= ';port=' . $self->port if $self->port;
40            
41 0   0       my $dbh = DBI->connect($dsn, $self->username, $self->password, $self->options) || die $DBI::errstr;
42 0           $self->set_dbh($dbh);
43 0           return $self;
44             }
45              
46             sub query {
47 0     0 0   my $self = shift;
48            
49 0           my $sql = shift;
50 0           my $sth = $self->dbh->prepare($sql);
51 0 0         die $self->dbh->errstr if $self->dbh->err;
52 0           $sth->execute(@_);
53 0           return $sth;
54             }
55              
56             sub selectOne {
57 0     0 0   my $self = shift;
58            
59 0           my $result = $self->dbh->selectrow_arrayref(shift, undef, @_);
60 0 0         die $self->dbh->errstr if $self->dbh->err;
61 0           return $result->[0];
62             }
63              
64             sub selectRow {
65 0     0 0   my $self = shift;
66            
67 0           my $result = $self->dbh->selectrow_hashref(shift, undef, @_);
68 0 0         die $self->dbh->errstr if $self->dbh->err;
69 0           return $result;
70             }
71              
72             sub selectAll {
73 0     0 0   my $self = shift;
74            
75 0           my $result = $self->dbh->selectall_arrayref(shift, {Slice=>{}}, @_);
76 0 0         die $self->dbh->errstr if $self->dbh->err;
77 0 0         return [] unless $result;
78 0 0         return [$result] unless ref($result) eq 'ARRAY';
79 0           return $result;
80             }
81              
82             sub column {
83 0     0 0   my ($self, $key, $value) = @_;
84            
85 0           push(@{$self->keys}, $key);
  0            
86 0 0         if (ref($value) eq 'HASH') {
87 0           push(@{$self->type}, keys %{$value});
  0            
  0            
88 0           push(@{$self->values}, values %{$value});
  0            
  0            
89             }else{
90 0           push(@{$self->type}, undef);
  0            
91 0           push(@{$self->values}, $value);
  0            
92             }
93 0           push(@{$self->reference}, '?');
  0            
94             }
95              
96             sub insert {
97 0     0 0   my $self = shift;
98            
99 0           my $sql = 'INSERT INTO ' . $self->table . ' (';
100 0           $sql .= join(',',@{$self->keys});
  0            
101 0           $sql .= ') VALUES (';
102 0           $sql .= join(',', @{$self->reference});
  0            
103 0           $sql .= ')';
104 0 0         $sql .= ' RETURNING ' . $self->returning if $self->returning;
105 0           my $sth = $self->query($sql, @{$self->values});
  0            
106 0 0         $self->_clean unless $self->returning;
107 0 0         return 0 unless $sth;
108 0 0 0       if ($self->returning && $sth->rows > 0) {
109 0           return $self->_getReturning($sth, $self->returning);
110             }else{
111 0           $self->_clean;
112             }
113 0   0       return $sth || 1;
114             }
115              
116             sub update {
117 0     0 0   my ($self, %wheres) = @_;
118            
119 0           my $sql = undef;
120 0           for(my $i=0; $ikeys}); $i++){
  0            
121 0 0         unless($sql){
122 0           $sql = 'UPDATE ' . $self->table . ' SET ';
123 0 0         if (${$self->type}[$i]) {
  0            
124 0           $sql .= ${$self->keys}[$i] . ' ' . ${$self->type}[$i] . ' ?';
  0            
  0            
125             }else{
126 0           $sql .= ${$self->keys}[$i] . ' = ?';
  0            
127             }
128             }else{
129 0 0         if (${$self->type}[$i]) {
  0            
130 0           $sql .= ', ' . ${$self->keys}[$i] . ' ' . ${$self->type}[$i] . ' ?';
  0            
  0            
131             }else{
132 0           $sql .= ', ' . ${$self->keys}[$i] . ' = ?';
  0            
133             }
134             }
135             }
136 0           my $where = '';
137 0           foreach(keys %wheres){
138 0 0         if ($_ =~ /and|or/i) {
139 0           foreach my $key (keys %{$wheres{$_}}){
  0            
140 0 0         $where .= ' ' . uc($_) if $where;
141 0 0         if (ref($wheres{$_}{$key}) eq 'HASH') {
142 0           $where .= ' ' . $key;
143 0           $where .= ' ' . $_ . ' ?' for(keys %{$wheres{$_}{$key}});
  0            
144 0           push(@{$self->values}, values %{$wheres{$_}{$key}});
  0            
  0            
145             }else{
146 0           $where .= ' ' . $key . ' = ?';
147 0           push(@{$self->values}, $wheres{$_}{$key});
  0            
148             }
149             }
150             }else{
151 0 0         $where .= ' AND' if $where;
152 0 0         if (ref($wheres{$_}) eq 'HASH') {
153 0           $where .= ' ' . $_;
154 0           $where .= ' ' . $_ . ' ?' for(keys %{$wheres{$_}});
  0            
155 0           push(@{$self->values}, values %{$wheres{$_}});
  0            
  0            
156             }else{
157 0           $where .= ' ' . $_ . ' = ?';
158 0           push(@{$self->values}, $wheres{$_});
  0            
159             }
160             }
161             }
162 0           $sql .= ' WHERE' . $where;
163 0 0         $sql .= ' RETURNING ' . $self->returning if $self->returning;
164 0           my $sth = $self->query($sql, @{$self->values});
  0            
165 0 0         $self->_clean unless $self->returning;
166 0 0         return 0 unless $sth;
167 0 0 0       if ($self->returning && $sth->rows > 0) {
168 0           return $self->_getReturning($sth, $self->returning);
169             }else{
170 0           $self->_clean;
171             }
172 0   0       return $sth || 1;
173             }
174              
175             sub delete {
176 0     0 0   my ($self, %wheres) = @_;
177            
178 0           my $sql = 'DELETE FROM ' . $self->table;
179 0           my $where = '';
180 0           foreach(keys %wheres){
181 0 0         if ($_ =~ /and|or/i) {
182 0           foreach my $key (keys %{$wheres{$_}}){
  0            
183 0 0         $where .= ' ' . uc($_) if $where;
184 0 0         if (ref($wheres{$_}{$key}) eq 'HASH') {
185 0           $where .= ' ' . $key;
186 0           $where .= ' ' . $_ . ' ?' for(keys %{$wheres{$_}{$key}});
  0            
187 0           push(@{$self->values}, values %{$wheres{$_}{$key}});
  0            
  0            
188             }else{
189 0           $where .= ' ' . $key . ' = ?';
190 0           push(@{$self->values}, $wheres{$_}{$key});
  0            
191             }
192             }
193             }else{
194 0 0         $where .= ' AND' if $where;
195 0 0         if (ref($wheres{$_}) eq 'HASH') {
196 0           $where .= ' ' . $_;
197 0           $where .= ' ' . $_ . ' ?' for(keys %{$wheres{$_}});
  0            
198 0           push(@{$self->values}, values %{$wheres{$_}});
  0            
  0            
199             }else{
200 0           $where .= ' ' . $_ . ' = ?';
201 0           push(@{$self->values}, $wheres{$_});
  0            
202             }
203             }
204             }
205 0           $sql .= ' WHERE' . $where;
206 0 0         $sql .= ' RETURNING ' . $self->returning if $self->returning;
207 0           my $sth = $self->query($sql, @{$self->values});
  0            
208 0 0         $self->_clean unless $self->returning;
209 0 0         return 0 unless $sth;
210 0 0 0       if ($self->returning && $sth->rows > 0) {
211 0           return $self->_getReturning($sth, $self->returning);
212             }else{
213 0           $self->_clean;
214             }
215 0   0       return $sth || 1;
216             }
217              
218             sub lastInsertID {
219 0     0 0   my $self = shift;
220            
221 0   0       my $last_insert_id = $self->dbh->last_insert_id(undef, undef, shift||undef, shift||undef);
      0        
222 0 0         die $self->dbh->errstr if $self->dbh->err;
223 0           return $last_insert_id;
224             }
225              
226             sub _getReturning {
227 0     0     my ($self, $sth, $columns) = @_;
228            
229 0           my @keys = split(/\,/, $columns);
230 0           my @values = @{$sth->fetch};
  0            
231 0           my %hash;
232 0           for(my $i=0; $i
233 0           $keys[$i] =~ s/\s//g;
234 0           $hash{$keys[$i]} = $values[$i];
235             }
236 0           $self->_clean;
237 0           return \%hash;
238             }
239              
240             sub _clean {
241 0     0     my $self = shift;
242            
243 0           $self->table('');
244 0           $self->returning('');
245 0           $self->keys([]);
246 0           $self->values([]);
247 0           $self->type([]);
248 0           $self->reference([]);
249             }
250              
251             1;
252              
253             __END__