| 1 | package Crop::Object::Warehouse::Agent::DBI; | ||
| 2 | use base qw/ Crop::Object /; | ||
| 3 | |||
| 4 | =begin nd | ||
| 5 | Class: Crop::Object::Warehouse::Agent::DBI | ||
| 6 | DBI interface to a database. | ||
| 7 | =cut | ||
| 8 | |||
| 9 | use v5.14; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | use DBI; | ||
| 13 | |||
| 14 | use Crop::Debug; | ||
| 15 | use Crop::Error; | ||
| 16 | |||
| 17 | =begin nd | ||
| 18 | Variable: our %Attributes | ||
| 19 | Class attributes: | ||
| 20 | |||
| 21 | dbh - connection handler | ||
| 22 | host - db hostname | ||
| 23 | login - user login | ||
| 24 | name - database name | ||
| 25 | pass - password | ||
| 26 | port - port to connect | ||
| 27 | sth - query handler | ||
| 28 | =cut | ||
| 29 | our %Attributes = ( | ||
| 30 | dbh => undef, | ||
| 31 | host => undef, | ||
| 32 | login => undef, | ||
| 33 | name => undef, | ||
| 34 | pass => undef, | ||
| 35 | port => undef, | ||
| 36 | sth => undef, | ||
| 37 | ); | ||
| 38 | |||
| 39 | =begin nd | ||
| 40 | Constructor: new ($conn) | ||
| 41 | Make connection to the database. | ||
| 42 | |||
| 43 | Returns: | ||
| 44 | $self - if ok | ||
| 45 | undef - if connection fails | ||
| 46 | =cut | ||
| 47 | sub new { | ||
| 48 | my ($class, @conn) = @_; | ||
| 49 | my $self = $class->SUPER::new(@conn); | ||
| 50 | |||
| 51 | my $dsn = "dbi:Pg:database=$self->{name};host=$self->{host};port=$self->{port}"; | ||
| 52 | $self->{dbh} = DBI->connect($dsn, $self->{login}, $self->{pass}, {RaiseError => 0}) | ||
| 53 | or return warn 'SYSTEM|ALERT:', DBI->errstr; | ||
| 54 | |||
| 55 | $self; | ||
| 56 | } | ||
| 57 | |||
| 58 | =begin nd | ||
| 59 | Method: exec ($q, @param) | ||
| 60 | Execute a query. | ||
| 61 | |||
| 62 | After DBI->prepare() and bind params, the query will executed as many times as required. | ||
| 63 | |||
| 64 | Params: | ||
| 65 | $q - query as string, or <Crop::Object::Warehouse::Lang::SQL::Query> | ||
| 66 | |||
| 67 | @param - array provides values for placeholders | ||
| 68 | Array contains pseudo-hashes (i.e. pairs) field_1=>value_1, field_2=>value_2, field_name=>[value_1, value_2, ..., value_n], ... | ||
| 69 | Arrayref in value position causes multiple execution - on for every value. | ||
| 70 | |||
| 71 | Field names are redudant for now. | ||
| 72 | |||
| 73 | Returns: | ||
| 74 | $sth - if ok | ||
| 75 | false - otherwise | ||
| 76 | =cut | ||
| 77 | sub exec { | ||
| 78 | my ($self, $q) = @_; | ||
| 79 | |||
| 80 | unless (ref $q and $q->isa('Crop::Object::Warehouse::Lang::SQL::Query')) { | ||
| 81 | # debug 'DBI_EXEC_QUERY=', $q; | ||
| 82 | return warn "WAREHOUSE|CRIT: QUERY REQUIRED"; | ||
| 83 | } | ||
| 84 | |||
| 85 | return warn "WAREHOUSE|ALERT: No query specified for execute" unless $q; | ||
| 86 | |||
| 87 | my ($sql, $param) = $q->print_sql; | ||
| 88 | debug DL_SQL, 'DB::exec Query=', $sql, ";\nDB::exec QParam=", $param; | ||
| 89 | |||
| 90 | unless (defined $param) { | ||
| 91 | debug 'DBI_EXEC_UNDEFPARAM_QUERY=', $sql; | ||
| 92 | } | ||
| 93 | if (@$param % 2) { | ||
| 94 | debug 'DBI_EXEC_ODDPARAM_QUERY=', $sql; | ||
| 95 | debug 'DBI_EXEC_ODDPARAM_PARAM=', $param; | ||
| 96 | } | ||
| 97 | return warn "WAREHOUSE|ALERT: Odd number in param hash" if @$param % 2; | ||
| 98 | my $paramN = @$param / 2; | ||
| 99 | |||
| 100 | $self->{sth} = my $sth = $self->{dbh}->prepare($sql); | ||
| 101 | |||
| 102 | return warn "WAREHOUSE|ALERT: Can't prepare query: $sql: ", $sth->errstr unless $sth; | ||
| 103 | |||
| 104 | return warn "WAREHOUSE|ALERT: Placeholders number don't match values number expected $sth->{NUM_OF_PARAMS} but received $paramN=@$param;" unless $sth->{NUM_OF_PARAMS} == $paramN; | ||
| 105 | |||
| 106 | # calculate execution count | ||
| 107 | my $execN; | ||
| 108 | my ($k, $v); | ||
| 109 | for (my $i = 0; $i < @$param; ++$i) { | ||
| 110 | if ($i % 2) { | ||
| 111 | $v = $param->[$i]; | ||
| 112 | } else { | ||
| 113 | $k = $param->[$i]; | ||
| 114 | next; | ||
| 115 | } | ||
| 116 | next unless ref $v eq 'ARRAY'; | ||
| 117 | |||
| 118 | return warn "WAREHOUSE|ALERT: Empty param's array" unless @$v; | ||
| 119 | |||
| 120 | if ($execN) { | ||
| 121 | return warn "WAREHOUSE|ALERT: Distinct param's array numer" unless $execN == @$v; | ||
| 122 | } else { | ||
| 123 | $execN = @$v; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | $execN ||= 1; | ||
| 127 | |||
| 128 | for (1 .. $execN) { | ||
| 129 | my $ix = $_ - 1; | ||
| 130 | |||
| 131 | # bind params | ||
| 132 | for (1 .. $paramN) { | ||
| 133 | my $iv = $_ * 2 - 1; # index of value | ||
| 134 | my $current = $param->[$iv]; # value or value arrayref | ||
| 135 | |||
| 136 | # value to bind | ||
| 137 | my $value = ref $current eq 'ARRAY' ? $current->[$ix] : $current; | ||
| 138 | |||
| 139 | $sth->bind_param($_, $value) or return warn "DBASE: Can't bind value $value in query $sql: ", $sth->errstr; | ||
| 140 | } | ||
| 141 | |||
| 142 | $self->{affected} = $sth->execute or return warn "DBASE: Can't execute statement $sql: ", $sth->errstr; | ||
| 143 | $self->{affected} += 0; | ||
| 144 | } | ||
| 145 | |||
| 146 | $self->{sth} = $sth; | ||
| 147 | } | ||
| 148 | |||
| 149 | =begin nd | ||
| 150 | Method: fetch ($q, @param) | ||
| 151 | Execute query to get result. | ||
| 152 | |||
| 153 | Result will be the first item in scalar context, and all items in list context. | ||
| 154 | |||
| 155 | Parameters: | ||
| 156 | $q - query string, or Query object | ||
| 157 | @param - see <fetch_all ($Q, @param)>. | ||
| 158 | |||
| 159 | Returns: | ||
| 160 | arrayref of hashes - in list context | ||
| 161 | hashref - in scalar context | ||
| 162 | =cut | ||
| 163 | sub fetch { | ||
| 164 | my $self = shift; | ||
| 165 | |||
| 166 | my $rc = $self->fetch_all(@_); | ||
| 167 | |||
| 168 | if (wantarray) { | ||
| 169 | $rc | ||
| 170 | } else { | ||
| 171 | $self->{sth}->finish; | ||
| 172 | $rc->[0]; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | =begin nd | ||
| 177 | Method: fetch_all ($q) | ||
| 178 | Execute and get result. | ||
| 179 | |||
| 180 | Parameters: | ||
| 181 | $q - Query exemplar | ||
| 182 | |||
| 183 | Returns: | ||
| 184 | arrayref of hashes - if ok | ||
| 185 | undef - otherwise | ||
| 186 | =cut | ||
| 187 | sub fetch_all { | ||
| 188 | my ($self, $q) = @_; | ||
| 189 | |||
| 190 | return warn 'DBASE: fetch_all() requires the Query exemplar' unless $q; | ||
| 191 | |||
| 192 | $self->{sth} = $self->exec($q) or return warn 'OBJECT|CRIT: Can not fetch result from database'; | ||
| 193 | |||
| 194 | $self->{sth}->fetchall_arrayref({}); | ||
| 195 | } | ||
| 196 | |||
| 197 | 1; |