| 1 | package Crop::Object::Warehouse::Pg; | ||
| 2 | use base qw/ Crop::Object::Warehouse /; | ||
| 3 | |||
| 4 | =begin nd | ||
| 5 | Class: Crop::Object::Warehouse::Pg | ||
| 6 | General Postgres driver. | ||
| 7 | |||
| 8 | Attribute 'dbi' keeps a connection to db. | ||
| 9 | =cut | ||
| 10 | |||
| 11 | use v5.14; | ||
| 12 | use warnings; | ||
| 13 | no warnings 'experimental::smartmatch'; | ||
| 14 | |||
| 15 | use Crop::Error; | ||
| 16 | use Crop::Util; | ||
| 17 | use Crop::Object::Warehouse::Agent::DBI; | ||
| 18 | use Crop::Object::Constants; | ||
| 19 | use Crop::Object::Warehouse::Lang::SQL::Query::Select; | ||
| 20 | use Crop::Object::Warehouse::Lang::SQL::Query::Insert; | ||
| 21 | use Crop::Object::Warehouse::Lang::SQL::Query::Delete; | ||
| 22 | use Crop::Object::Warehouse::Lang::SQL::Query::Update; | ||
| 23 | |||
| 24 | use Crop::Debug; | ||
| 25 | |||
| 26 | =begin nd | ||
| 27 | Constant: KEYWORDS | ||
| 28 | Key words for build query to a database. | ||
| 29 | =cut | ||
| 30 | use constant { KEYWORDS => [qw/ EXT LIMIT LOCK OFFSET ORDER SLICEN SORT /] }; | ||
| 31 | |||
| 32 | =begin nd | ||
| 33 | Constructor: new ($conn) | ||
| 34 | Establish connection to a specified database. | ||
| 35 | |||
| 36 | Parameters: | ||
| 37 | $conn - credentials for database connection (login, pass, etc.) | ||
| 38 | |||
| 39 | Returns: | ||
| 40 | $self - if OK | ||
| 41 | undef - if connection to database fails | ||
| 42 | =cut | ||
| 43 | sub new { | ||
| 44 | my ($class, @conn) = @_; | ||
| 45 | |||
| 46 | my $self = bless { | ||
| 47 | dbi => undef, | ||
| 48 | }, $class; | ||
| 49 | |||
| 50 | $self->{dbi} = Crop::Object::Warehouse::Agent::DBI->new(@conn); | ||
| 51 | |||
| 52 | $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | =begin nd | ||
| 56 | Method: all ($object, @filter) | ||
| 57 | Get all exemplars that match the @filter clause. | ||
| 58 | |||
| 59 | Parameters: | ||
| 60 | $object - class name of objects | ||
| 61 | @filter - complex clause contains WHERE, ORDER BY, and EXT tokens | ||
| 62 | |||
| 63 | Returns: | ||
| 64 | collection of objects - if ok | ||
| 65 | undef - otherwise | ||
| 66 | |||
| 67 | =cut | ||
| 68 | sub all { | ||
| 69 | my ($self, $object, @filter) = @_; | ||
| 70 | my $in = {@filter}; | ||
| 71 | my $class = ref $self || $self; | ||
| 72 | |||
| 73 | for my $field (keys %{$in}) { | ||
| 74 | next if $field ~~ KEYWORDS; | ||
| 75 | |||
| 76 | for (split ' OR ', $field) { | ||
| 77 | return warn "OBJECT|ALERT: Unknown attribute: $object.$_" unless $object->Attributes->have(STORED, $_); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | if (exists $in->{SORT}) { | ||
| 82 | $in->{ORDER} = delete $in->{SORT}; | ||
| 83 | $in->{ORDER} = [$in->{ORDER}] unless ref $in->{ORDER}; | ||
| 84 | } | ||
| 85 | |||
| 86 | my %clause = %$in; | ||
| 87 | my $ext = delete $clause{EXT}; | ||
| 88 | my $sort = delete $clause{ORDER}; | ||
| 89 | my $limit = delete $clause{LIMIT}; | ||
| 90 | my $offset = delete $clause{OFFSET}; | ||
| 91 | |||
| 92 | # my %key; | ||
| 93 | |||
| 94 | my $q = Crop::Object::Warehouse::Lang::SQL::Query::Select->new( | ||
| 95 | start_class => $object, | ||
| 96 | clause => \%clause, | ||
| 97 | order => $sort, | ||
| 98 | limit => $limit, | ||
| 99 | ext => $ext, | ||
| 100 | offset => $offset, | ||
| 101 | )->build; | ||
| 102 | my $rows = $self->{dbi}->fetch_all($q); | ||
| 103 | |||
| 104 | # While the SELECT uses the LEFT JOIN only, following chunk of code don't work | ||
| 105 | # if (not @$rows and not $q->parsed->child->Is_empty) { | ||
| 106 | # return warn 'DBASE: Complex inner clauses is not implemented' if $q->parsed->child->Size > 1; | ||
| 107 | # | ||
| 108 | # my $n_clauses; | ||
| 109 | # $q->foreach_prepared($q->parsed, sub { | ||
| 110 | # my $node = shift; | ||
| 111 | # | ||
| 112 | # ++$n_clauses if $node->clause; | ||
| 113 | # }); | ||
| 114 | # | ||
| 115 | # my $inner_clauses = $q->parsed->clause ? $n_clauses - 1 : $n_clauses; | ||
| 116 | # | ||
| 117 | # if ($inner_clauses) { | ||
| 118 | # my @root_sort = grep not /[.]/, @$sort; | ||
| 119 | # | ||
| 120 | # my $root_q = Crop::Object::Warehouse::Lang::SQL::Query::Select->new( | ||
| 121 | # start_class => $object, | ||
| 122 | # clause => \%clause, | ||
| 123 | # @root_sort ? (order => \@root_sort) : (), | ||
| 124 | # limit => $limit, | ||
| 125 | # )->build; | ||
| 126 | # $rows = $self->{dbi}->fetch_all($root_q); | ||
| 127 | # | ||
| 128 | # if (@$rows) { | ||
| 129 | # for ($q->parsed->child->List) { | ||
| 130 | # $q->foreach_prepared($_, sub { | ||
| 131 | # my $node = shift; | ||
| 132 | # | ||
| 133 | # $node->row_action('PARENTINIT'); | ||
| 134 | # }); | ||
| 135 | # } | ||
| 136 | # } | ||
| 137 | # } | ||
| 138 | # } | ||
| 139 | |||
| 140 | for (@$rows) { | ||
| 141 | my %data; | ||
| 142 | while (my ($field, $val) = each %$_) { | ||
| 143 | my ($table, $attr) = split '\$', $field; | ||
| 144 | $data{$table}{$attr} = $val; | ||
| 145 | } | ||
| 146 | |||
| 147 | $q->foreach_prepared ($q->parsed, sub { | ||
| 148 | my $node = shift; | ||
| 149 | |||
| 150 | $node->parse_row(\%data); | ||
| 151 | }); | ||
| 152 | } | ||
| 153 | |||
| 154 | return $q->parsed->object; | ||
| 155 | } | ||
| 156 | |||
| 157 | =begin nd | ||
| 158 | Method: create ($object) | ||
| 159 | Insert an object to the database. | ||
| 160 | |||
| 161 | Parameters: | ||
| 162 | $obj - object data to insert | ||
| 163 | =cut | ||
| 164 | sub create { | ||
| 165 | my ($self, $object) = @_; | ||
| 166 | |||
| 167 | my %attr; | ||
| 168 | for (@{$object->Attributes(STORED)}) { | ||
| 169 | next unless defined $object->{$_->name}; | ||
| 170 | $attr{$_->name} = $object->{$_->name}; | ||
| 171 | } | ||
| 172 | |||
| 173 | my $table = $object->Table; | ||
| 174 | my $q = Crop::Object::Warehouse::Lang::SQL::Query::Insert->new( | ||
| 175 | table => $table, | ||
| 176 | attr => \%attr, | ||
| 177 | ); | ||
| 178 | $self->{dbi}->exec($q) or return warn 'DBASE: Create object in Warehouse fails'; | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | =begin nd | ||
| 183 | Method: create_auto_id ($object) | ||
| 184 | Insert a new $object in the database, generate 'id'. | ||
| 185 | |||
| 186 | Parameters: | ||
| 187 | $object - exemplar to create | ||
| 188 | |||
| 189 | Returns: | ||
| 190 | id - if ok | ||
| 191 | undef - fail | ||
| 192 | =cut | ||
| 193 | sub create_auto_id { | ||
| 194 | my ($self, $object) = @_; | ||
| 195 | |||
| 196 | my %attr; | ||
| 197 | for (@{$object->Attributes(STORED)}) { | ||
| 198 | next unless defined $object->{$_->name}; | ||
| 199 | $attr{$_->name} = $object->{$_->name}; | ||
| 200 | } | ||
| 201 | my $table = $object->Table; | ||
| 202 | my $q = Crop::Object::Warehouse::Lang::SQL::Query::Insert->new( | ||
| 203 | table => $table, | ||
| 204 | attr => \%attr, | ||
| 205 | returning => 'id', | ||
| 206 | ); | ||
| 207 | |||
| 208 | $self->{dbi}->fetch($q)->{id}; | ||
| 209 | } | ||
| 210 | |||
| 211 | =begin nd | ||
| 212 | Method: global_delete ($obj_class, $clause) | ||
| 213 | Delete rows on etire class. | ||
| 214 | |||
| 215 | Parameters: | ||
| 216 | $obj_class - class to update | ||
| 217 | $clause - {a1 => {'EQ' => 25}}; or {a1=>25} default EQ | ||
| 218 | =cut | ||
| 219 | sub global_delete { | ||
| 220 | my ($self, $obj_class, $clause) = @_; | ||
| 221 | |||
| 222 | my $table = $obj_class->Table or return warn 'OBJECT: Table required for global Delete'; | ||
| 223 | |||
| 224 | my $q = Crop::Object::Warehouse::Lang::SQL::Query::Delete->new( | ||
| 225 | table => $table, | ||
| 226 | clause => $clause, | ||
| 227 | ); | ||
| 228 | |||
| 229 | $self->{dbi}->exec($q) or return warn 'DBASE: Can not global Delete'; | ||
| 230 | } | ||
| 231 | |||
| 232 | =begin nd | ||
| 233 | Method: global_safe_update ($obj_class, $val, $clause) | ||
| 234 | Update rows on entire class with care to table restrictions. | ||
| 235 | |||
| 236 | Param: | ||
| 237 | $obj_class - class to update | ||
| 238 | $val - hashref $attr=>val where 'val' is an expression with direct references to table fields 'a1 = a2+1' | ||
| 239 | $clause - rule to select exemplars of $obj_class to update {a1 =>{'EQ'=>25}, a2=>7, ...} | ||
| 240 | |||
| 241 | Returns: | ||
| 242 | true - ok | ||
| 243 | false - error | ||
| 244 | =cut | ||
| 245 | sub global_safe_update { | ||
| 246 | # debug 'CROPOBJECTWAREHOUSEPG_GLOBALSAFEUPDATE_ARGV=', \@_; | ||
| 247 | # ...; | ||
| 248 | # my ($self, $obj_class) = splice @_, 0, 2; | ||
| 249 | |||
| 250 | my ($self, $obj_class, $val, $clause) = @_; | ||
| 251 | # debug 'CROPOBJECTWAREHOUSEPG_GLOBALSAFEUPDATE_VAL=', $val; | ||
| 252 | |||
| 253 | # invert the value sign | ||
| 254 | my %negative; | ||
| 255 | while (my ($attr, $v) = each %$val) { | ||
| 256 | $negative{$attr} = "($attr + $v) * -1"; | ||
| 257 | } | ||
| 258 | # debug 'CROPOBJECTWAREHOUSEPG_GLOBALSAFEUPDATE_NEGATIVE=', \%negative; | ||
| 259 | # ...; | ||
| 260 | $self->global_update($obj_class, \%negative, $clause); | ||
| 261 | # ...; | ||
| 262 | |||
| 263 | # restore the value sign for all the changed attributes | ||
| 264 | my (%restore_val, %restore_clause); | ||
| 265 | for (keys %negative) { | ||
| 266 | $restore_val{$_} = "$_ * -1"; | ||
| 267 | $restore_clause{$_} = {LT => 0}; | ||
| 268 | } | ||
| 269 | $self->global_update( | ||
| 270 | $obj_class, | ||
| 271 | \%restore_val, | ||
| 272 | \%restore_clause, | ||
| 273 | ); | ||
| 274 | } | ||
| 275 | |||
| 276 | =begin nd | ||
| 277 | Method: global_update ($obj_class, $values, $clause) | ||
| 278 | Update rows on entire class. | ||
| 279 | |||
| 280 | Parameters: | ||
| 281 | $obj_class - class to update | ||
| 282 | $value - hash $attr=>val where 'val' is an expression with direct references to table fields 'a1 = a2+1' | ||
| 283 | $clause - {a1 =>{EQ=>25}, a2=>7, ...}; | ||
| 284 | |||
| 285 | Returns: | ||
| 286 | true - ok | ||
| 287 | false - error | ||
| 288 | =cut | ||
| 289 | sub global_update { | ||
| 290 | my ($self, $obj_class, $value, $clause) = @_; | ||
| 291 | # debug 'CROPOBJECTWAREHOUSEPG_GLOBALUPDATE_ARGV=', \@_; | ||
| 292 | |||
| 293 | my $table = $obj_class->Table or return warn 'OBJECT: Table required for global Update'; | ||
| 294 | |||
| 295 | my %val_ref; | ||
| 296 | for (keys %$value) { | ||
| 297 | # debug 'CROPOBJECTWAREHOUSEPG_GLOBALSAFEUPDATE_VALKEY=', $_; | ||
| 298 | $val_ref{$_} = \$value->{$_}; | ||
| 299 | } | ||
| 300 | # my %val_ref = map ($_ => \$value->{$_}), keys %$value; | ||
| 301 | # debug 'CROPOBJECTWAREHOUSEPG_GLOBALUPDATE_VALREF=', \%val_ref; | ||
| 302 | # ...; | ||
| 303 | |||
| 304 | my $q = Crop::Object::Warehouse::Lang::SQL::Query::Update->new( | ||
| 305 | table => $table, | ||
| 306 | new_val => \%val_ref, | ||
| 307 | clause => $clause, | ||
| 308 | ); | ||
| 309 | # debug 'CROPOBJECTWAREHOUSEPG_GLOBALUPDATE_UPDATEQ=', $q; | ||
| 310 | # ...; | ||
| 311 | |||
| 312 | $self->{dbi}->exec($q); | ||
| 313 | # my ($sql, $val) = $q->print_sql; | ||
| 314 | # $self->{dbi}->exec($sql, @$val); | ||
| 315 | } | ||
| 316 | |||
| 317 | =begin nd | ||
| 318 | Method: get_id ($obj) | ||
| 319 | Get id for object. | ||
| 320 | |||
| 321 | Parameters: | ||
| 322 | $obj - object that needs ID | ||
| 323 | |||
| 324 | Returns: | ||
| 325 | $obj with ID defined | ||
| 326 | =cut | ||
| 327 | sub get_id { | ||
| 328 | my ($self, $obj) = @_; | ||
| 329 | |||
| 330 | my $sequence = $obj->Table . '_id_seq'; | ||
| 331 | my $q = Crop::Object::Warehouse::Lang::SQL::Query::Select->new(expression => "nextval('$sequence')"); | ||
| 332 | |||
| 333 | my $id = $self->{dbi}->fetch($q)->{nextval}; | ||
| 334 | |||
| 335 | $obj->{id} = $id; | ||
| 336 | |||
| 337 | $obj; | ||
| 338 | } | ||
| 339 | |||
| 340 | =begin nd | ||
| 341 | Method: max ($class, $attr) | ||
| 342 | Get the maximum value of the attribute | ||
| 343 | |||
| 344 | Param: | ||
| 345 | $class | ||
| 346 | $attr | ||
| 347 | =cut | ||
| 348 | sub max { | ||
| 349 | my ($self, $obj, $attr) = @_; | ||
| 350 | |||
| 351 | my $table = $obj->Table; | ||
| 352 | my $rc = "max_$attr"; | ||
| 353 | my $q = Crop::Object::Warehouse::Lang::SQL::Query::Select->new( | ||
| 354 | expression => "max($attr) AS $rc FROM $table", | ||
| 355 | ); | ||
| 356 | |||
| 357 | my $max = $self->{dbi}->fetch($q)->{$rc}; | ||
| 358 | $max //= 0; | ||
| 359 | # debug 'CROPOBJECTWAREHOUSEPG_MAX_MAX=', $max; | ||
| 360 | |||
| 361 | $max; | ||
| 362 | } | ||
| 363 | |||
| 364 | =begin nd | ||
| 365 | Method: refresh ($obj) | ||
| 366 | Update an object in the database. | ||
| 367 | |||
| 368 | Parameters: | ||
| 369 | $obj - exemplar to update | ||
| 370 | |||
| 371 | Returns: | ||
| 372 | $obj - if OK | ||
| 373 | undef - otherwise | ||
| 374 | =cut | ||
| 375 | sub refresh { | ||
| 376 | my ($self, $obj) = @_; | ||
| 377 | |||
| 378 | my $table = $obj->Table or return warn 'OBJECT: Refresh exemplar requires a Table'; | ||
| 379 | |||
| 380 | my (%clause, %set); | ||
| 381 | for (@{$obj->Attributes(STORED)}) { | ||
| 382 | my $attr = $_->name; | ||
| 383 | if ($_->of_type(KEY)) { | ||
| 384 | $clause{$attr} = $obj->{$attr}; | ||
| 385 | } else { | ||
| 386 | $set{$attr} = $obj->{$attr}; | ||
| 387 | } | ||
| 388 | } | ||
| 389 | |||
| 390 | my $q = Crop::Object::Warehouse::Lang::SQL::Query::Update->new( | ||
| 391 | table => $table, | ||
| 392 | new_val => \%set, | ||
| 393 | clause => \%clause, | ||
| 394 | ); | ||
| 395 | # debug 'CROPOBJECTWAREHOUSEPG_Q=', $q; | ||
| 396 | $self->{dbi}->exec($q); | ||
| 397 | |||
| 398 | $obj; | ||
| 399 | } | ||
| 400 | |||
| 401 | =begin nd | ||
| 402 | Method: remove ($obj) | ||
| 403 | Remove an $obj from database. | ||
| 404 | |||
| 405 | Parameters: | ||
| 406 | $obj - exemplar to remove | ||
| 407 | |||
| 408 | Returns: | ||
| 409 | undef | ||
| 410 | =cut | ||
| 411 | sub remove { | ||
| 412 | my ($self, $obj) = @_; | ||
| 413 | |||
| 414 | my $table = $obj->Table or return warn 'OBJECT: Remove exemplar requires a Table'; | ||
| 415 | |||
| 416 | my %keys = map { | ||
| 417 | ($_->name => $obj->{$_->name}); | ||
| 418 | } @{$obj->Attributes(KEY)}; | ||
| 419 | return warn 'OBJECT: Remove requires the primary key defined' unless keys %keys; | ||
| 420 | |||
| 421 | my $q = Crop::Object::Warehouse::Lang::SQL::Query::Delete->new( | ||
| 422 | table => $obj->Table, | ||
| 423 | clause => \%keys, | ||
| 424 | ); | ||
| 425 | $self->{dbi}->exec($q); | ||
| 426 | |||
| 427 | undef; | ||
| 428 | } | ||
| 429 | |||
| 430 | 1; |