| 1 | #=============================================================================== | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # FILE: Table.pm | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # DESCRIPTION: NetSDS::DBI::Table - CRUD implementation for NetSDS | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # AUTHOR: Michael Bochkaryov (Rattler), <misha@rattler.kiev.ua> | ||||||||||||||||||||||||||||
| 8 | # COMPANY: Net.Style | ||||||||||||||||||||||||||||
| 9 | # CREATED: 25.07.2008 01:06:46 EEST | ||||||||||||||||||||||||||||
| 10 | #=============================================================================== | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | NetSDS::DBI::Table | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | use NetSDS::DBI::Table; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | my $q = NetSDS::DBI::Table->new( | ||||||||||||||||||||||||||||
| 21 | dsn => 'dbi:Pg:dbname=netsdsdb;host=127.0.0.1', | ||||||||||||||||||||||||||||
| 22 | user => 'netsds', | ||||||||||||||||||||||||||||
| 23 | passwd => 'test', | ||||||||||||||||||||||||||||
| 24 | table => 'public.messages', | ||||||||||||||||||||||||||||
| 25 | ) or warn NetSDS::DBI::Table->errstr(); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | C<NetSDS::DBI::Table> module provides commonly used CRUD functionality for | ||||||||||||||||||||||||||||
| 31 | data stored in single database. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Main idea was that we can agree about some limitations: | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | * every such table contains C<id> field that is primary key | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | * we use PostgreSQL DBMS with all it's features | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =cut | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | package NetSDS::DBI::Table; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | use 5.8.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2 | ▶ | use base 'NetSDS::DBI'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | ▶ | use version; our $VERSION = '1.301'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | #=============================================================================== | ||||||||||||||||||||||||||||
| 52 | # | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head1 CLASS API | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =over | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =item B<new([...])> - class constructor | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | my $tbl = NetSDS::DBI::Table->new( | ||||||||||||||||||||||||||||
| 61 | dsn => 'dbi:Pg:dbname=content', | ||||||||||||||||||||||||||||
| 62 | login => 'netsds', | ||||||||||||||||||||||||||||
| 63 | passwd => 'topsecret, | ||||||||||||||||||||||||||||
| 64 | table => 'content.meta', | ||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | my ( $class, %params ) = @_; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # Initialize base DBMS connector | ||||||||||||||||||||||||||||
| 75 | 0 | my $self = $class->SUPER::new(%params); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # Set table name | ||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ( $params{table} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | $self->{table} = $params{table}; | |||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||
| 81 | 0 | return $class->error('Table name is not specified to NetSDS::DBI::Table'); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # 'fields' paramter is hash reference describing supported/allowed fields | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if ( $params{fields} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | return $self; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | } ## end sub new | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | #*********************************************************************** | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =item B<fetch(%params)> - get records from table as array of hashrefs | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Paramters (hash): | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | * fields - fetch fields by list | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | * filter - arrayref of SQL expressions like C<status = 'active'> for C<WHERE> clause | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | * order - arrayref of SQL expressions like C<id desc> for C<ORDER BY> clause | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | * limit - max number of records to fetch (LIMIT N) | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | * offset - records to skip from beginning (OFFSET N) | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | * for_update - records selected for further update within current transaction | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Returns: message as array of hashrefs | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Sample: | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | my @messages = $q->fetch( | ||||||||||||||||||||||||||||
| 115 | fields => ['id', 'now() as time'], | ||||||||||||||||||||||||||||
| 116 | filter => ['msg_status = 5', 'date_received < now()'], # where msg_status=5 and date_received < now() | ||||||||||||||||||||||||||||
| 117 | order => ['id desc', 'src_addr'], # order by id desc, src_addr | ||||||||||||||||||||||||||||
| 118 | limit => 3, # fetch 3 records | ||||||||||||||||||||||||||||
| 119 | offset => 5, # from 6-th record | ||||||||||||||||||||||||||||
| 120 | for_update => 1, # for update | ||||||||||||||||||||||||||||
| 121 | ) | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =cut | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | my ( $self, %params ) = @_; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # Prepare expected fields list | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | my $req_fields = $params{fields} ? join( ',', @{ $params{fields} } ) : '*'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # Set default filter | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | my $default_filter = $self->{default_filter} ? " where " . join( " and ", @{ $self->{default_filter} } ) : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # Prepare WHERE filter | ||||||||||||||||||||||||||||
| 138 | 0 | my $req_filter = ($params{filter} and grep { $_ } @{ $params{filter} }) ? | |||||||||||||||||||||||||||
| 139 | 0 | ▶ | " where " . join( " and ", grep { $_ } @{ $params{filter} } ) : $default_filter; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$params{"filter"} and grep {$_;} @{$params{"filter"};}
MC/DC0%0/2
$params{"filter"} and grep {$_;} @{$params{"filter"};}
$params{"filter"}
grep {$_;} @{$params{"filter"};}
Truth table0%0/3
A and B
A= $params{"filter"}B= grep {$_;} @{$params{"filter"};}
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Prepare results order | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | my $req_order = $params{order} ? " order by " . join( ", ", @{ $params{order} } ) : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # Set limit and offset for fetching | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | my $req_limit = $params{limit} ? " limit " . $params{limit} : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | my $req_offset = $params{offset} ? " offset " . $params{offset} : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # Request for messages | ||||||||||||||||||||||||||||
| 149 | 0 | my $sql = "select $req_fields from " . $self->{table} . " $req_filter $req_order $req_limit $req_offset"; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # Set FOR UPDATE if necessary | ||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ( $params{for_update} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | $sql .= " for update"; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # Execute SQL query and fetch results | ||||||||||||||||||||||||||||
| 157 | 0 | my @ret = (); | |||||||||||||||||||||||||||
| 158 | 0 | my $sth = $self->call($sql); | |||||||||||||||||||||||||||
| 159 | 0 | ▶ | while ( my $row = $sth->fetchrow_hashref() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | push @ret, $row; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | return @ret; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | } ## end sub fetch | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | #*********************************************************************** | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =item B<insert_row(%key_val_pairs)> - insert record into table | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Paramters: record fields as hash | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Returns: id of inserted record | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | my $user_id = $tbl->insert_row( | ||||||||||||||||||||||||||||
| 176 | 'login' => 'vasya', | ||||||||||||||||||||||||||||
| 177 | 'password' => $encrypted_passwd, | ||||||||||||||||||||||||||||
| 178 | ); | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | sub insert_row { | ||||||||||||||||||||||||||
|
Subroutine
insert_row: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | my ( $self, %params ) = @_; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | my @fields = (); # Fields list | |||||||||||||||||||||||||||
| 189 | 0 | my @values = (); # Values list | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # Prepare fields and values lists from input hash | ||||||||||||||||||||||||||||
| 192 | 0 | foreach my $key ( keys %params ) { | |||||||||||||||||||||||||||
| 193 | 0 | push @fields, $key; | |||||||||||||||||||||||||||
| 194 | 0 | push @values, $self->dbh->quote( $params{$key} ); | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | ▶ | my $return_value = $self->has_field('id') ? ' returning id' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | # Prepare SQL statement from fields and values lists | ||||||||||||||||||||||||||||
| 199 | 0 | my $sql = 'insert into ' . $self->{table} . ' (' . join( ',', @fields ) . ')' # fields list | |||||||||||||||||||||||||||
| 200 | . ' values (' . join( ',', @values ) . ')' # values list | ||||||||||||||||||||||||||||
| 201 | . $return_value; # return "id" field | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # Execute SQL query and fetch result | ||||||||||||||||||||||||||||
| 204 | 0 | my $sth = $self->call($sql); | |||||||||||||||||||||||||||
| 205 | 0 | ▶ | my ($row_id) = $return_value ? $sth->fetchrow_array : $sth->rows; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # Return "id" field from inserted row | ||||||||||||||||||||||||||||
| 208 | 0 | ▶ | return $row_id || $self->error( "Can't insert table record: " . $self->dbh->errstr ); | ||||||||||||||||||||||||||
|
Condition0%0/3
$row_id || $self->error(q[Can't insert table record: ] . $self->dbh->errstr)
MC/DC0%0/2
$row_id || $self->error(q[Can't insert table record: ] . $self->dbh->errstr)
$row_id
$self->error(q[Can't insert table record: ] . $self->dbh->errstr)
Truth table0%0/3
A || B
A= $row_idB= $self->error(q[Can't insert table record: ] . $self->dbh->errstr)
| |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | } ## end sub insert_row | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | #*********************************************************************** | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =item B<insert(@records_list)> - mass insert | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | Paramters: list of records (as hashrefs) | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | Returns: array of inserted records "id" | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | This method allows mass insert of records. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | my @user_ids = $tbl->insert( | ||||||||||||||||||||||||||||
| 223 | { login => 'vasya', password => $str1 }, | ||||||||||||||||||||||||||||
| 224 | { login => 'masha', password => $str2 }, | ||||||||||||||||||||||||||||
| 225 | { login => 'petya', password => $str3, active => 'false' }, | ||||||||||||||||||||||||||||
| 226 | ); | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | B<Warning!> This method use separate INSERT queries and in fact is only | ||||||||||||||||||||||||||||
| 229 | wrapper for multiple C<insert_row()> calls. So it's not so fast as | ||||||||||||||||||||||||||||
| 230 | one insert but allows to use different key-value pairs for different records. | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =cut | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 0 | ▶ | sub insert { | ||||||||||||||||||||||||||
|
Subroutine
insert: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 0 | my ( $self, @rows ) = @_; | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 0 | my @ids = (); | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | # Go through records and insert each one | ||||||||||||||||||||||||||||
| 243 | 0 | foreach my $rec (@rows) { | |||||||||||||||||||||||||||
| 244 | 0 | push @ids, ( $self->insert_row( %{$rec} ) ); | |||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 0 | return @ids; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | #*********************************************************************** | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =item B<update_row($id, %params)> - update record parameters | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | Paramters: id, new parameters as hash | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | Returns: updated record as hash | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | Example: | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | my %upd = $table->update_row($msg_id, | ||||||||||||||||||||||||||||
| 263 | status => 'failed', | ||||||||||||||||||||||||||||
| 264 | dst_addr => '380121234567', | ||||||||||||||||||||||||||||
| 265 | ); | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | After this %upd hash will contain updated table record. | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | =cut | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 0 | ▶ | sub update_row { | ||||||||||||||||||||||||||
|
Subroutine
update_row: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 0 | my ( $self, $id, %params ) = @_; | |||||||||||||||||||||||||||
| 277 | 0 | my @up = (); | |||||||||||||||||||||||||||
| 278 | 0 | foreach my $key ( keys %params ) { | |||||||||||||||||||||||||||
| 279 | 0 | push @up, "$key = " . $self->dbh->quote( $params{$key} ); | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 0 | my $sql = "update " . $self->{table} . " set " . join( ', ', @up ) . " where id=$id"; | |||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 0 | my $res = $self->call($sql); | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 0 | ▶ | if ($res) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | 0 | return %{$res}; | |||||||||||||||||||||||||||
| 288 | } else { | ||||||||||||||||||||||||||||
| 289 | 0 | return $self->error( "Can't update table row: " . $self->dbh->errstr ); | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | #*********************************************************************** | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =item B<update(%params)> - update records by filter | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | Paramters: filter, new values | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | $tbl->update( | ||||||||||||||||||||||||||||
| 301 | filter => ['active = true', 'created > '2008-01-01'], | ||||||||||||||||||||||||||||
| 302 | set => { | ||||||||||||||||||||||||||||
| 303 | info => 'Created after 2007 year', | ||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | ); | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | =cut | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 0 | ▶ | sub update { | ||||||||||||||||||||||||||
|
Subroutine
update: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 0 | my ( $self, %params ) = @_; | |||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | # Prepare WHERE filter | ||||||||||||||||||||||||||||
| 316 | 0 | ▶ | my $req_filter = $params{filter} ? " where " . join( " and ", @{ $params{filter} } ) : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 0 | my @up = (); | |||||||||||||||||||||||||||
| 319 | 0 | foreach my $key ( keys %{ $params{set} } ) { | |||||||||||||||||||||||||||
| 320 | 0 | push @up, "$key = " . $self->dbh->quote( $params{set}->{$key} ); | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 0 | my $sql = "update " . $self->{table} . " set " . join( ', ', @up ) . $req_filter; | |||||||||||||||||||||||||||
| 324 | 0 | my $res = $self->call($sql); | |||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 0 | ▶ | if ($self->dbh->errstr) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | 0 | $self->error( "Can't update table: " . $self->dbh->errstr ); | |||||||||||||||||||||||||||
| 328 | 0 | return; | |||||||||||||||||||||||||||
| 329 | }; | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 0 | return 1; | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | #*********************************************************************** | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | =item B<get_count(%params)> - retrieve number of records | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | Just return total number of records by calling: | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | # SELECT COUNT(id) FROM schema.table | ||||||||||||||||||||||||||||
| 341 | my $count = $tbl->get_count(); | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | my $count_active = $tbl->get_count(filter => ['active = true']); | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | =cut | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | ## Returns number of records | ||||||||||||||||||||||||||||
| 350 | 0 | ▶ | sub get_count { | ||||||||||||||||||||||||||
|
Subroutine
get_count: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 353 | 0 | my $filter = \@_; | |||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | # Fetch number of records | ||||||||||||||||||||||||||||
| 356 | # SQL: select count(id) as c from $table where [filter] | ||||||||||||||||||||||||||||
| 357 | 0 | my @count = $self->fetch( | |||||||||||||||||||||||||||
| 358 | fields => ['count(id) as c'], | ||||||||||||||||||||||||||||
| 359 | filter => $filter, | ||||||||||||||||||||||||||||
| 360 | ); | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 0 | return $count[0]->{c}; | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | #*********************************************************************** | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | =item B<delete_by_id(@ids)> - delete records by identifier | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | Paramters: list of record id | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | Returns: 1 if ok, undef if error | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | Method deletes records from SQL table by it's identifiers. | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | if ($tbl->remove(5, 8 ,19)) { | ||||||||||||||||||||||||||||
| 376 | print "Records successfully removed."; | ||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =cut | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 0 | ▶ | sub delete_by_id { | ||||||||||||||||||||||||||
|
Subroutine
delete_by_id: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 0 | my ( $self, @ids ) = @_; | |||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | # TODO check for too long @id list | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | # Prepare SQL condition | ||||||||||||||||||||||||||||
| 390 | 0 | my $in_cond = "id in (" . join( ", ", @ids ) . ")"; | |||||||||||||||||||||||||||
| 391 | 0 | my $sql = "delete from " . $self->{table} . " where $in_cond"; | |||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 0 | ▶ | if ( $self->call($sql) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | 0 | return 1; | |||||||||||||||||||||||||||
| 395 | } else { | ||||||||||||||||||||||||||||
| 396 | 0 | return $self->error( "Can't delete records by Id: table='" . $self->{table} . "'" ); | |||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | #*********************************************************************** | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =item B<delete(@filters)> - delete records | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | Paramters: list of filters | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | Returns: 1 if ok, undef if error | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | $tbl->delete( | ||||||||||||||||||||||||||||
| 410 | 'active = false', | ||||||||||||||||||||||||||||
| 411 | 'expire < now()', | ||||||||||||||||||||||||||||
| 412 | ); | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | =cut | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 0 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 0 | my ( $self, @filter ) = @_; | |||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | # Prepare WHERE filter | ||||||||||||||||||||||||||||
| 423 | 0 | my $req_filter = " where " . join( " and ", @filter ); | |||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | # Remove records | ||||||||||||||||||||||||||||
| 426 | 0 | $self->call( "delete from " . $self->{table} . $req_filter ); | |||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | #*********************************************************************** | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | =item B<get_fields()> - get list of fields | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | Example: | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | my @fields = @{ $tbl->get_fields() }; | ||||||||||||||||||||||||||||
| 437 | print "Table fields: " . join (', ', @fields); | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | =cut | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | 0 | ▶ | sub get_fields { | ||||||||||||||||||||||||||
|
Subroutine
get_fields: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 444 | 0 | return [ keys %{ +shift->{'fields'} } ]; | |||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | #*********************************************************************** | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | =item B<has_field($field)> - check if field exists | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | Paramters: field name | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | Example: | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | if ($tbl->has_field('uuid')) { | ||||||||||||||||||||||||||||
| 457 | $tbl->call("delete tbldata where uuid=?", $uuid); | ||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | B<NOTE>: this method works only for restricted tables that | ||||||||||||||||||||||||||||
| 461 | use C<fields> parameter at construction time. | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | =cut | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | 0 | ▶ | sub has_field { | ||||||||||||||||||||||||||
|
Subroutine
has_field: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | # TODO | ||||||||||||||||||||||||||||
| 470 | # - check if fields defined at all | ||||||||||||||||||||||||||||
| 471 | # - think about multiple values | ||||||||||||||||||||||||||||
| 472 | 0 | return $_[0]->{'fields'}{ $_[1] } | |||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | 2 | 1; | |||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | __END__ | ||||||||||||||||||||||||||||