| 1 | package Daje::Workflow::Database::Model::Context; | ||
| 2 | 1 | ▶ | use Mojo::Base -base, -signatures; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use v5.40; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 1 | ▶ | use Mojo::JSON qw { to_json from_json }; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use DBD::Pg ':pg_types'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Data::Dumper; | ||
| 8 | |||
| 9 | # NAME | ||
| 10 | # ==== | ||
| 11 | # | ||
| 12 | # Daje::Workflow::Database::Model::Context | ||
| 13 | # | ||
| 14 | # SYNOPSIS | ||
| 15 | # ======== | ||
| 16 | # | ||
| 17 | # use Daje::Workflow::Database::Model::Context; | ||
| 18 | # | ||
| 19 | # my $context_obj = Daje::Workflow::Database::Model::Context->new( | ||
| 20 | # db => $db, | ||
| 21 | # workflow_pkey => $workflow_pkey | ||
| 22 | # context_pkey => $context_pkey, | ||
| 23 | # context => $context, | ||
| 24 | # ); | ||
| 25 | # | ||
| 26 | # my $context = $context_obj->load_fk($self) | ||
| 27 | # | ||
| 28 | # my $context = $context_obj->load_pk($self) | ||
| 29 | # | ||
| 30 | # my $context_pkey = $context_obj->save($self, $context) | ||
| 31 | # | ||
| 32 | # | ||
| 33 | # REQUIRES | ||
| 34 | # ======== | ||
| 35 | # | ||
| 36 | # Mojo::Base> | ||
| 37 | # | ||
| 38 | # DBD::Pg | ||
| 39 | # | ||
| 40 | # | ||
| 41 | # METHODS | ||
| 42 | # ======= | ||
| 43 | # | ||
| 44 | # load_fk($self) | ||
| 45 | # | ||
| 46 | # load_pk($self) | ||
| 47 | # | ||
| 48 | # save($self, $context) | ||
| 49 | # | ||
| 50 | # | ||
| 51 | # LICENSE | ||
| 52 | # ======= | ||
| 53 | # | ||
| 54 | # Copyright (C) janeskil1525. | ||
| 55 | # | ||
| 56 | # This library is free software; you can redistribute it and/or modify | ||
| 57 | # it under the same terms as Perl itself. | ||
| 58 | # | ||
| 59 | # AUTHOR | ||
| 60 | # ====== | ||
| 61 | # | ||
| 62 | # janeskil1525 E<lt>janeskil1525@gmail.comE<gt> | ||
| 63 | # | ||
| 64 | |||
| 65 | |||
| 66 | has 'db'; | ||
| 67 | has 'workflow_pkey'; | ||
| 68 | has 'context_pkey'; | ||
| 69 | has 'context'; | ||
| 70 | |||
| 71 | sub load_pk($self) { | ||
| 72 | my $context = $self->_load_pk(); | ||
| 73 | unless(exists $context->{context_pkey} and $context->{context_pkey} > 0) { | ||
| 74 | $context->{context_pkey} = 0; | ||
| 75 | $context->{workflow_fkey} = $self->workflow_pkey; | ||
| 76 | $context->{context} = $self->context->{context}; | ||
| 77 | my $context_pkey = $self->save($context); | ||
| 78 | $self->context_pkey($context_pkey); | ||
| 79 | $context = $self->_load_pk(); | ||
| 80 | } | ||
| 81 | |||
| 82 | return $context; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _load_pk($self) { | ||
| 86 | my $data = $self->db->select( | ||
| 87 | 'context', ['*'], | ||
| 88 | { | ||
| 89 | context_pkey => $self->context_pkey | ||
| 90 | } | ||
| 91 | ); | ||
| 92 | |||
| 93 | my $context; | ||
| 94 | $context = $data->hash if $data->rows > 0; | ||
| 95 | if (defined $context and exists $context->{context}) { | ||
| 96 | $context->{context} = from_json($context->{context}); | ||
| 97 | } | ||
| 98 | return $context; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub load_fk($self) { | ||
| 102 | my $context = $self->_load_fk(); | ||
| 103 | unless(exists $context->{context_pkey} and $context->{context_pkey} > 0) { | ||
| 104 | $context->{context_pkey} = 0; | ||
| 105 | $context->{workflow_fkey} = $self->workflow_pkey; | ||
| 106 | $context->{context} = $self->context->{context}; | ||
| 107 | my $context_pkey = $self->save($context); | ||
| 108 | $self->context_pkey($context_pkey); | ||
| 109 | $context = $self->_load_pk(); | ||
| 110 | } | ||
| 111 | |||
| 112 | return $context; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub _load_fk($self) { | ||
| 116 | my $data = $self->db->select( | ||
| 117 | 'context', ['*'], | ||
| 118 | { | ||
| 119 | workflow_fkey => $self->workflow_pkey | ||
| 120 | } | ||
| 121 | ); | ||
| 122 | |||
| 123 | my $context; | ||
| 124 | $context = $data->hash if $data->rows > 0; | ||
| 125 | if (defined $context and exists $context->{context}) { | ||
| 126 | $context->{context} = from_json($context->{context}); | ||
| 127 | } | ||
| 128 | return $context; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub save($self, $context) { | ||
| 132 | |||
| 133 | $context->{context_pkey} = 0 unless exists $context->{context_pkey}; | ||
| 134 | $context->{context} = to_json($context->{context}); | ||
| 135 | my $context_pkey; | ||
| 136 | |||
| 137 | if ($context->{context_pkey} > 0) { | ||
| 138 | try { | ||
| 139 | $self->db->update( | ||
| 140 | "context", | ||
| 141 | { | ||
| 142 | context => $context->{context} | ||
| 143 | }, | ||
| 144 | { | ||
| 145 | context_pkey => $context->{context_pkey} | ||
| 146 | } | ||
| 147 | ); | ||
| 148 | } catch ($e) { | ||
| 149 | my $test = $e; | ||
| 150 | say $e->to_string; | ||
| 151 | $e = $e; | ||
| 152 | } | ||
| 153 | } else { | ||
| 154 | try { | ||
| 155 | delete %$context{context_pkey}; | ||
| 156 | $context->{workflow_fkey} = $self->workflow_pkey; | ||
| 157 | |||
| 158 | $context_pkey = $self->db->insert( | ||
| 159 | "context", | ||
| 160 | { | ||
| 161 | context => $context->{context}, | ||
| 162 | workflow_fkey => $context->{workflow_fkey} | ||
| 163 | }, | ||
| 164 | { | ||
| 165 | returning => 'context_pkey' | ||
| 166 | } | ||
| 167 | )->hash->{context_pkey}; | ||
| 168 | } catch ($e) { | ||
| 169 | my $test = $e; | ||
| 170 | say $e->to_string; | ||
| 171 | $e = $e; | ||
| 172 | } | ||
| 173 | # $context->{context_pkey} = $self->db->query( | ||
| 174 | # "INSERT INTO context (context, workflow_fkey) VALUES (?,?) RETURNING context_pkey", | ||
| 175 | # ("$context->{context}", "$context->{workflow_fkey}") | ||
| 176 | # )->hash->{context_pkey}; | ||
| 177 | } | ||
| 178 | |||
| 179 | return $context_pkey; | ||
| 180 | } | ||
| 181 | |||
| 182 | 1; | ||
| 183 | |||
| 184 | |||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | |||
| 196 | |||
| 197 | |||
| 198 | |||
| 199 | |||
| 200 | |||
| 201 | |||
| 202 | |||
| 203 | |||
| 204 | #################### pod generated by Pod::Autopod - keep this line to make pod updates possible #################### | ||
| 205 | |||
| 206 | =head1 NAME | ||
| 207 | |||
| 208 | |||
| 209 | Daje::Workflow::Database::Model::Context | ||
| 210 | |||
| 211 | |||
| 212 | |||
| 213 | =head1 SYNOPSIS | ||
| 214 | |||
| 215 | |||
| 216 | use Daje::Workflow::Database::Model::Context; | ||
| 217 | |||
| 218 | my $context_obj = Daje::Workflow::Database::Model::Context->new( | ||
| 219 | db => $db, | ||
| 220 | workflow_pkey => $workflow_pkey | ||
| 221 | context_pkey => $context_pkey, | ||
| 222 | context => $context, | ||
| 223 | ); | ||
| 224 | |||
| 225 | my $context = $context_obj->load_fk($self) | ||
| 226 | |||
| 227 | my $context = $context_obj->load_pk($self) | ||
| 228 | |||
| 229 | my $context_pkey = $context_obj->save($self, $context) | ||
| 230 | |||
| 231 | |||
| 232 | |||
| 233 | |||
| 234 | =head1 REQUIRES | ||
| 235 | |||
| 236 | |||
| 237 | Mojo::Base> | ||
| 238 | |||
| 239 | DBD::Pg | ||
| 240 | |||
| 241 | |||
| 242 | |||
| 243 | |||
| 244 | =head1 METHODS | ||
| 245 | |||
| 246 | |||
| 247 | load_fk($self) | ||
| 248 | |||
| 249 | load_pk($self) | ||
| 250 | |||
| 251 | save($self, $context) | ||
| 252 | |||
| 253 | |||
| 254 | |||
| 255 | |||
| 256 | =head1 AUTHOR | ||
| 257 | |||
| 258 | |||
| 259 | janeskil1525 E<lt>janeskil1525@gmail.comE<gt> | ||
| 260 | |||
| 261 | |||
| 262 | |||
| 263 | =head1 LICENSE | ||
| 264 | |||
| 265 | |||
| 266 | Copyright (C) janeskil1525. | ||
| 267 | |||
| 268 | This library is free software; you can redistribute it and/or modify | ||
| 269 | it under the same terms as Perl itself. | ||
| 270 | |||
| 271 | |||
| 272 | |||
| 273 | =cut | ||
| 274 | |||