| 1 | package EntityModel::Storage::PostgreSQL; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use EntityModel::Class { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | _isa => [qw{EntityModel::Storage}], | |||||||||||||||||||||||||||
| 4 | db => { type => 'EntityModel::DB' }, | ||||||||||||||||||||||||||||
| 5 | schema => { type => 'string' }, | ||||||||||||||||||||||||||||
| 6 | field_cache => { type => 'hash' }, | ||||||||||||||||||||||||||||
| 7 | primary_cache => { type => 'array' }, | ||||||||||||||||||||||||||||
| 8 | 1 | }; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '0.003'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | EntityModel::Storage::PostgreSQL - backend storage interface for L<EntityModel> | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | See L<EntityModel>. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | See L<EntityModel>. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 METHODS | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head2 setup | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | Open the initial database connection and set schema if provided. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =cut | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub setup { | ||||||||||||||||||||||||||
|
Subroutine
setup: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 40 | 0 | my %args = %{+shift}; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # If we have a DB object already, just use that. | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | $self->db(delete $args{db}) if exists $args{db}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $self->schema(delete $args{schema}) if exists $args{schema}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Otherwise we'll need to pick one up from the parameters | ||||||||||||||||||||||||||||
| 47 | $self->db(EntityModel::DB->new( | ||||||||||||||||||||||||||||
| 48 | user => delete $args{user}, | ||||||||||||||||||||||||||||
| 49 | password => delete $args{password}, | ||||||||||||||||||||||||||||
| 50 | host => delete $args{host}, | ||||||||||||||||||||||||||||
| 51 | port => delete $args{port}, | ||||||||||||||||||||||||||||
| 52 | dbname => delete $args{dbname} | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | )) unless $self->db; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # Without a database, we can't do much, bail out here | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | die "no db" unless $self->db; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | return $self; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =head2 apply_model | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | Applies the requested model to the database. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub apply_model { | ||||||||||||||||||||||||||
|
Subroutine
apply_model: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 68 | 0 | my $model = shift; | |||||||||||||||||||||||||||
| 69 | 0 | logDebug("Apply model"); | |||||||||||||||||||||||||||
| 70 | 0 | Scalar::Util::weaken $self; | |||||||||||||||||||||||||||
| 71 | 0 | Scalar::Util::weaken $model; | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | $model->transaction(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 73 | 0 | $self->apply_model_and_schema($model); | |||||||||||||||||||||||||||
| 74 | 0 | }); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =head2 apply_model_and_schema | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | Given a model, apply it to the database, optionally creating the requested schema. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub apply_model_and_schema { | ||||||||||||||||||||||||||
|
Subroutine
apply_model_and_schema: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 85 | 0 | my $model = shift; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | $self->create_schema if $self->schema && !$self->schema_exists; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->schema and not $self->schema_exists
MC/DC0%0/2
$self->schema and not $self->schema_exists
$self->schema
not $self->schema_exists
Truth table0%0/3
A and not(B)
A= $self->schemaB= not $self->schema_exists
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | my @pending = $model->entity->list; | |||||||||||||||||||||||||||
| 89 | 0 | my @existing; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | ITEM: | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | while(@pending) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | my $entity = shift(@pending); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | my @deps = $entity->dependencies; | |||||||||||||||||||||||||||
| 96 | 0 | my @pendingNames = map { $_->name } @pending; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Include current entity in list of available entries, so that we can allow self-reference | ||||||||||||||||||||||||||||
| 99 | 0 | foreach my $dep (@deps) { | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | unless(grep { $dep->name ~~ $_->name } @pending, @existing, $entity) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | logError("%s unresolved (pending %s, deps %s for %s)", $dep->name, join(',', @pendingNames), join(',', @deps), $entity->name); | |||||||||||||||||||||||||||
| 102 | 0 | die "Dependency error"; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | my @unsatisfied = grep { $_ ~~ [ map { $_->name } @deps ] } @pendingNames; | |||||||||||||||||||||||||||
| 107 | 0 | ▶ | if(@unsatisfied) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | logInfo("%s has %d unsatisfied deps, postponing: %s", $entity->name, scalar @unsatisfied, join(',',@unsatisfied)); | |||||||||||||||||||||||||||
| 109 | 0 | push @pending, $entity; | |||||||||||||||||||||||||||
| 110 | 0 | next ITEM; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | $self->apply_entity($entity); | |||||||||||||||||||||||||||
| 114 | 0 | push @existing, $entity; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 0 | return $self; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head2 apply_entity | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | Applies this entity to the database - currently, supports creation only. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =cut | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub apply_entity { | ||||||||||||||||||||||||||
|
Subroutine
apply_entity: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 126 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 127 | 0 | my $entity = shift; | |||||||||||||||||||||||||||
| 128 | 0 | my ($sql, @bind) = $self->create_table_query($entity); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my $sth = $self->dbh->prepare($sql); | |||||||||||||||||||||||||||
| 131 | 0 | $sth->execute(@bind); | |||||||||||||||||||||||||||
| 132 | 0 | $self->field_cache->clear; | |||||||||||||||||||||||||||
| 133 | 0 | $self->primary_cache->clear; | |||||||||||||||||||||||||||
| 134 | 0 | return $self; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head2 read_primary | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Get the primary keys for a table. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | sub read_primary { | ||||||||||||||||||||||||||
|
Subroutine
read_primary: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 145 | 0 | my $tbl = shift; | |||||||||||||||||||||||||||
| 146 | 0 | logDebug("Get primary key info for [%s]", $tbl->name); | |||||||||||||||||||||||||||
| 147 | 0 | ▶ | $self->_cache_primary if $self->primary_cache->is_empty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # Reorder the result according to the reported sequence | ||||||||||||||||||||||||||||
| 150 | my @keyList = map { | ||||||||||||||||||||||||||||
| 151 | $_->{name} | ||||||||||||||||||||||||||||
| 152 | 0 | } sort { | |||||||||||||||||||||||||||
| 153 | $a->{order} <=> $b->{order} | ||||||||||||||||||||||||||||
| 154 | 0 | } grep { | |||||||||||||||||||||||||||
| 155 | 0 | $_->{table} eq $tbl->name | |||||||||||||||||||||||||||
| 156 | } $self->primary_cache->list; | ||||||||||||||||||||||||||||
| 157 | 0 | logDebug("Keys were: [%s]", join(',', @keyList)); | |||||||||||||||||||||||||||
| 158 | 0 | return @keyList; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head2 read_fields | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | Read all fields for a given table. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Since this is typically a slow query, we cache the entire set of fields for all tables on | ||||||||||||||||||||||||||||
| 166 | the first call. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | sub read_fields { | ||||||||||||||||||||||||||
|
Subroutine
read_fields: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 172 | 0 | my $tbl = shift; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | ▶ | $self->_cache_fields unless $self->{field_cache}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 0 | my $field_list = $self->field_cache->get($tbl->name); | |||||||||||||||||||||||||||
| 176 | 0 | ▶ | unless($field_list) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 0 | logDebug("No items for [%s]", $tbl->name); | |||||||||||||||||||||||||||
| 178 | 0 | return; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 0 | logDebug("Check [%s] has %d items: %s", $tbl->name, $field_list->count, $field_list->join(',')); | |||||||||||||||||||||||||||
| 181 | 0 | return map +{ %$_ }, $field_list->list; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head1 _cache_fields | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Cache field information across all tables in the currently-selected database. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =cut | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | sub _cache_fields { | ||||||||||||||||||||||||||
|
Subroutine
_cache_fields: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 191 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 192 | 0 | logInfo("Reloading cache"); | |||||||||||||||||||||||||||
| 193 | # Get all field for all tables | ||||||||||||||||||||||||||||
| 194 | 0 | my $sth = $self->dbh->column_info(undef, $self->schema, '%', '%'); | |||||||||||||||||||||||||||
| 195 | 0 | ▶ | my $rslt = $sth->fetchall_arrayref | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | or return $self; | ||||||||||||||||||||||||||||
| 197 | 0 | my %field_cache; | |||||||||||||||||||||||||||
| 198 | 0 | foreach (@$rslt) { | |||||||||||||||||||||||||||
| 199 | # We get a load of data back from DBI, most of which isn't useful yet | ||||||||||||||||||||||||||||
| 200 | 0 | my (undef, undef, $tableName, $name, $type, $size, $length, $digits, $radix, $nullable, $default, $dataType, $datetimeSub, $octetLength, $order, $isNullable) = @$_; | |||||||||||||||||||||||||||
| 201 | 0 | $name =~ s/"//g; | |||||||||||||||||||||||||||
| 202 | 0 | $tableName =~ s/"//g; | |||||||||||||||||||||||||||
| 203 | 0 | logDebug("Have [%s] field [%s]", $tableName, $name); | |||||||||||||||||||||||||||
| 204 | 0 | ▶ | $field_cache{$tableName} = EntityModel::Array->new([ ]) unless $field_cache{$tableName}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 0 | $field_cache{$tableName}->push({ | |||||||||||||||||||||||||||
| 206 | 'name' => $name, | ||||||||||||||||||||||||||||
| 207 | 'default' => $default, | ||||||||||||||||||||||||||||
| 208 | 'null' => $isNullable, | ||||||||||||||||||||||||||||
| 209 | 'type' => $type, | ||||||||||||||||||||||||||||
| 210 | 'length' => $size, | ||||||||||||||||||||||||||||
| 211 | 'precision' => $digits, | ||||||||||||||||||||||||||||
| 212 | }); | ||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | 0 | $self->{field_cache} = \%field_cache; | |||||||||||||||||||||||||||
| 215 | 0 | $self->_cache_primary; | |||||||||||||||||||||||||||
| 216 | 0 | return $self; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =head1 _cache_primary | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | Cache primary key information across all tables in the database. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =cut | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 0 | ▶ | sub _cache_primary { | ||||||||||||||||||||||||||
|
Subroutine
_cache_primary: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 226 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 227 | 0 | logInfo("Reloading primary cache"); | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # XXX PostgreSQL only code here, because DBI default was teh slow. | ||||||||||||||||||||||||||||
| 230 | 0 | my $sql = q{ | |||||||||||||||||||||||||||
| 231 | select '' as "something", | ||||||||||||||||||||||||||||
| 232 | n.nspname as "schema", | ||||||||||||||||||||||||||||
| 233 | c.relname as "table", | ||||||||||||||||||||||||||||
| 234 | a.attname as "column", | ||||||||||||||||||||||||||||
| 235 | a.attnum as "order", | ||||||||||||||||||||||||||||
| 236 | c2.relname as "keyname" | ||||||||||||||||||||||||||||
| 237 | from pg_catalog.pg_class c | ||||||||||||||||||||||||||||
| 238 | inner join pg_catalog.pg_index i on (i.indrelid = c.oid) | ||||||||||||||||||||||||||||
| 239 | inner join pg_catalog.pg_class c2 on (c2.oid = i.indexrelid) | ||||||||||||||||||||||||||||
| 240 | inner join pg_catalog.pg_attribute a on a.attrelid = c.oid and a.attnum = any(i.indkey) | ||||||||||||||||||||||||||||
| 241 | inner join pg_catalog.pg_type t2 on a.atttypid = t2.oid | ||||||||||||||||||||||||||||
| 242 | left join pg_catalog.pg_namespace n on (n.oid = c.relnamespace) | ||||||||||||||||||||||||||||
| 243 | left join pg_catalog.pg_tablespace t on (t.oid = c.reltablespace) | ||||||||||||||||||||||||||||
| 244 | where i.indisprimary is true | ||||||||||||||||||||||||||||
| 245 | and n.nspname = ? | ||||||||||||||||||||||||||||
| 246 | order by 1,2,4 | ||||||||||||||||||||||||||||
| 247 | }; | ||||||||||||||||||||||||||||
| 248 | 0 | my $sth = $self->dbh->prepare($sql); | |||||||||||||||||||||||||||
| 249 | 0 | logDebug("Run $sql"); | |||||||||||||||||||||||||||
| 250 | 0 | $sth->execute($self->schema); | |||||||||||||||||||||||||||
| 251 | # Get all tables | ||||||||||||||||||||||||||||
| 252 | # my $sth = $self->dbh->primary_key_info(undef, $self->schema, $tbl->name); | ||||||||||||||||||||||||||||
| 253 | 0 | ▶ | my $rslt = $sth->fetchall_arrayref | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | or return $self; | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 0 | my @keyList; | |||||||||||||||||||||||||||
| 257 | 0 | foreach (@$rslt) { | |||||||||||||||||||||||||||
| 258 | 0 | my (undef, undef, $tableName, $name, $order, $constraint) = @$_; | |||||||||||||||||||||||||||
| 259 | 0 | push @keyList, { | |||||||||||||||||||||||||||
| 260 | table => $tableName, | ||||||||||||||||||||||||||||
| 261 | name => $name, | ||||||||||||||||||||||||||||
| 262 | order => $order, | ||||||||||||||||||||||||||||
| 263 | constraint => $constraint | ||||||||||||||||||||||||||||
| 264 | }; | ||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | 0 | logDebug("Had %d entries", scalar @keyList); | |||||||||||||||||||||||||||
| 267 | 0 | $self->{primary_cache} = \@keyList; | |||||||||||||||||||||||||||
| 268 | 0 | return $self; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | =head2 table_list | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | Get a list of all the existing tables in the schema. | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =cut | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 0 | ▶ | sub table_list { | ||||||||||||||||||||||||||
|
Subroutine
table_list: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 278 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 279 | 0 | my $q = EntityModel::Query->new( | |||||||||||||||||||||||||||
| 280 | select => 'table_name', | ||||||||||||||||||||||||||||
| 281 | from => 'information_schema.tables', | ||||||||||||||||||||||||||||
| 282 | where => [ | ||||||||||||||||||||||||||||
| 283 | table_type => 'BASE TABLE', | ||||||||||||||||||||||||||||
| 284 | table_schema => $self->schema, | ||||||||||||||||||||||||||||
| 285 | ], | ||||||||||||||||||||||||||||
| 286 | ); | ||||||||||||||||||||||||||||
| 287 | 0 | return $q->results; | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | =head2 field_list | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | Returns a list of all fields for the given table. | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | =cut | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 0 | ▶ | sub field_list { | ||||||||||||||||||||||||||
|
Subroutine
field_list: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 297 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 298 | 0 | my $tbl = shift; | |||||||||||||||||||||||||||
| 299 | 0 | my $schema = $self->schema; | |||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 0 | my $q = EntityModel::Query->new( | |||||||||||||||||||||||||||
| 302 | select => [ | ||||||||||||||||||||||||||||
| 303 | { name => 'column_name' }, | ||||||||||||||||||||||||||||
| 304 | { default => 'column_default' }, | ||||||||||||||||||||||||||||
| 305 | { null => 'is_nullable' }, | ||||||||||||||||||||||||||||
| 306 | { type => 'data_type' }, | ||||||||||||||||||||||||||||
| 307 | { length => 'character_maximum_length' }, | ||||||||||||||||||||||||||||
| 308 | { description => \q{''} }, | ||||||||||||||||||||||||||||
| 309 | { precision => 'numeric_precision' }, | ||||||||||||||||||||||||||||
| 310 | { scale => 'numeric_scale' }, | ||||||||||||||||||||||||||||
| 311 | ], | ||||||||||||||||||||||||||||
| 312 | from => { schema => 'information_schema', table => 'columns' }, | ||||||||||||||||||||||||||||
| 313 | where => [ | ||||||||||||||||||||||||||||
| 314 | table_schema => $schema, | ||||||||||||||||||||||||||||
| 315 | -and => table_name => $tbl, | ||||||||||||||||||||||||||||
| 316 | ], | ||||||||||||||||||||||||||||
| 317 | order => 'ordinal_position' | ||||||||||||||||||||||||||||
| 318 | ); | ||||||||||||||||||||||||||||
| 319 | 0 | return $q->results; | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | =head2 quoted_schema_name | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | Returns the quoted version of the current schema. | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | =cut | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 0 | ▶ | sub quoted_schema_name { | ||||||||||||||||||||||||||
|
Subroutine
quoted_schema_name: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 329 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 330 | 0 | ▶ | return undef unless $self->schema; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 0 | return $self->dbh->quote_identifier($self->schema); | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | =head1 quoted_table_name | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | Generate the quoted table identifier including any schema name if available. | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | =cut | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 0 | ▶ | sub quoted_table_name { | ||||||||||||||||||||||||||
|
Subroutine
quoted_table_name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 342 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 343 | 0 | my $tbl = shift; | |||||||||||||||||||||||||||
| 344 | 0 | return $self->dbh->quote_identifier(undef, $self->schema, $tbl->name); | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | =head2 quoted_field_name | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | Generate the quoted field identifier. | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | =cut | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 0 | ▶ | sub quoted_field_name { | ||||||||||||||||||||||||||
|
Subroutine
quoted_field_name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 354 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 355 | 0 | my $field = shift; | |||||||||||||||||||||||||||
| 356 | 0 | return $self->dbh->quote_identifier(undef, undef, $field->name); | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =head2 create_table_query | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | Create a new table. | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | =cut | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 0 | ▶ | sub create_table_query { | ||||||||||||||||||||||||||
|
Subroutine
create_table_query: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 366 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 367 | 0 | my $tbl = shift; | |||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 0 | my @bind; | |||||||||||||||||||||||||||
| 370 | # Put together the constituent fields | ||||||||||||||||||||||||||||
| 371 | my $content = join(', ', map { | ||||||||||||||||||||||||||||
| 372 | 0 | ▶ | $self->quoted_field_name($_) . ' ' . $_->type . ($tbl->primary eq $_->name ? ' primary key' : '') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | } $tbl->field->list); | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | # TODO Any extras such as index or constraints | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | # And build the create statement itself | ||||||||||||||||||||||||||||
| 378 | 0 | my $sql = 'create table ' . $self->quoted_table_name($tbl) . ' (' . $content . ')'; | |||||||||||||||||||||||||||
| 379 | 0 | return ($sql, @bind); | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =head1 remove_table_query | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | Query for removing the given table. | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | =cut | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | 0 | ▶ | sub remove_table_query { | ||||||||||||||||||||||||||
|
Subroutine
remove_table_query: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 389 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 390 | 0 | my $tbl = shift; | |||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | 0 | my @bind; | |||||||||||||||||||||||||||
| 393 | 0 | my $sql = 'drop table ' . $self->quoted_table_name($tbl); | |||||||||||||||||||||||||||
| 394 | 0 | return ($sql, @bind); | |||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | =head1 create_table | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | Create the given table. | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =cut | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | 0 | ▶ | sub create_table { | ||||||||||||||||||||||||||
|
Subroutine
create_table: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 404 | 0 | my ($self, $tbl) = @_; | |||||||||||||||||||||||||||
| 405 | 0 | my ($sql, @bind) = $self->create_table_query($tbl); | |||||||||||||||||||||||||||
| 406 | 0 | my $sth = $self->dbh->prepare($sql); | |||||||||||||||||||||||||||
| 407 | 0 | $sth->execute(@bind); | |||||||||||||||||||||||||||
| 408 | 0 | $self->field_cache(undef); | |||||||||||||||||||||||||||
| 409 | 0 | $self->primary_cache(undef); | |||||||||||||||||||||||||||
| 410 | 0 | return $self->SUPER::create_table($tbl); | |||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | =head2 add_field_to_table | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | Add the requested field to the given table, and clear related caches. | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | =cut | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | 0 | ▶ | sub add_field_to_table { | ||||||||||||||||||||||||||
|
Subroutine
add_field_to_table: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 420 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 421 | 0 | my $entity = shift; | |||||||||||||||||||||||||||
| 422 | 0 | my $field = shift; | |||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | 0 | my ($sql, @bind) = $self->alter_table_query( | |||||||||||||||||||||||||||
| 425 | table => $entity, | ||||||||||||||||||||||||||||
| 426 | add => [ $field ] | ||||||||||||||||||||||||||||
| 427 | ); | ||||||||||||||||||||||||||||
| 428 | 0 | my $sth = $self->dbh->prepare($sql); | |||||||||||||||||||||||||||
| 429 | 0 | logDebug($sql); | |||||||||||||||||||||||||||
| 430 | 0 | $sth->execute(@bind); | |||||||||||||||||||||||||||
| 431 | 0 | $self->field_cache(undef); | |||||||||||||||||||||||||||
| 432 | 0 | $self->primary_cache(undef); | |||||||||||||||||||||||||||
| 433 | 0 | return $self->SUPER::add_field_to_table($entity, $field); | |||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | =head2 remove_table | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | Remove a table entirely. | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | =cut | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 0 | ▶ | sub remove_table { | ||||||||||||||||||||||||||
|
Subroutine
remove_table: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 443 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 444 | 0 | my $tbl = shift; | |||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | 0 | my ($sql, @bind) = $self->remove_table_query($tbl); | |||||||||||||||||||||||||||
| 447 | 0 | my $sth = $self->dbh->prepare($sql); | |||||||||||||||||||||||||||
| 448 | 0 | logInfo($sql); | |||||||||||||||||||||||||||
| 449 | 0 | $sth->execute(@bind); | |||||||||||||||||||||||||||
| 450 | 0 | $self->field_cache(undef); | |||||||||||||||||||||||||||
| 451 | 0 | $self->primary_cache(undef); | |||||||||||||||||||||||||||
| 452 | 0 | return $self; | |||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | =head2 read_tables | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | Read all table definitions from the database. | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | =cut | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 0 | ▶ | sub read_tables { | ||||||||||||||||||||||||||
|
Subroutine
read_tables: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 462 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 463 | 0 | ▶ | die 'no schema' unless $self->schema; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 464 | 0 | logWarning("Get tables for " . $self->schema); | |||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | 0 | delete $self->{field_cache}; | |||||||||||||||||||||||||||
| 467 | 0 | $self->primary_cache(undef); | |||||||||||||||||||||||||||
| 468 | 0 | my $sth = $self->dbh->table_info(undef, $self->schema, '%'); | |||||||||||||||||||||||||||
| 469 | 0 | ▶ | my $rslt = $sth->fetchall_arrayref | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 470 | or return $self; | ||||||||||||||||||||||||||||
| 471 | 0 | my @table_list; | |||||||||||||||||||||||||||
| 472 | 0 | foreach (@$rslt) { | |||||||||||||||||||||||||||
| 473 | 0 | my (undef, undef, $name, $type) = @$_; | |||||||||||||||||||||||||||
| 474 | 0 | $name =~ s/^"//; | |||||||||||||||||||||||||||
| 475 | 0 | $name =~ s/"$//; | |||||||||||||||||||||||||||
| 476 | 0 | ▶ | push @table_list, { name => $name } if lc($type) eq 'table'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||
| 478 | 0 | return @table_list; | |||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | =head2 post_commit | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | =cut | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | 0 | ▶ | sub post_commit { | ||||||||||||||||||||||||||
|
Subroutine
post_commit: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 486 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 487 | 0 | $self->dbh->commit; | |||||||||||||||||||||||||||
| 488 | 0 | return $self; | |||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | =head2 create_schema | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | =cut | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | 0 | ▶ | sub create_schema { | ||||||||||||||||||||||||||
|
Subroutine
create_schema: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 496 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 497 | try { | ||||||||||||||||||||||||||||
| 498 | 0 | ▶ | $self->db->transaction(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 499 | 0 | my $dbh = shift->dbh; | |||||||||||||||||||||||||||
| 500 | 0 | $dbh->do('create schema ' . $self->quoted_schema_name); | |||||||||||||||||||||||||||
| 501 | 0 | }); | |||||||||||||||||||||||||||
| 502 | 0 | } catch { | |||||||||||||||||||||||||||
| 503 | 0 | logWarning($_); | |||||||||||||||||||||||||||
| 504 | }; | ||||||||||||||||||||||||||||
| 505 | 0 | return $self; | |||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | =head1 remove_schema | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | Remove the schema entry. | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | =cut | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | 0 | ▶ | sub remove_schema { | ||||||||||||||||||||||||||
|
Subroutine
remove_schema: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 515 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 516 | 0 | ▶ | die "No schema" unless $self->schema; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 517 | try { | ||||||||||||||||||||||||||||
| 518 | 0 | ▶ | $self->db->transaction(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 519 | 0 | my $dbh = shift->dbh; | |||||||||||||||||||||||||||
| 520 | 0 | $dbh->do('drop schema ' . $self->quoted_schema_name . ' cascade'); | |||||||||||||||||||||||||||
| 521 | 0 | }); | |||||||||||||||||||||||||||
| 522 | 0 | } catch { | |||||||||||||||||||||||||||
| 523 | 0 | logWarning($_); | |||||||||||||||||||||||||||
| 524 | }; | ||||||||||||||||||||||||||||
| 525 | 0 | return $self; | |||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | =head2 schema_exists | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | Returns true if the current schema exists in the database, false if not. | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | =cut | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | 0 | ▶ | sub schema_exists { | ||||||||||||||||||||||||||
|
Subroutine
schema_exists: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 535 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 536 | 0 | my $sth = $self->dbh->prepare(q{select schema_name, catalog_name from information_schema.schemata where schema_name = ?}); | |||||||||||||||||||||||||||
| 537 | 0 | $sth->execute($self->schema); | |||||||||||||||||||||||||||
| 538 | 0 | ▶ | my $rslt = $sth->fetchall_arrayref | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 539 | or return undef; | ||||||||||||||||||||||||||||
| 540 | 0 | return scalar @$rslt; | |||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | =head2 row_count | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | Reports how many rows are in the given table. | ||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | =cut | ||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||
| 549 | 0 | ▶ | sub row_count { | ||||||||||||||||||||||||||
|
Subroutine
row_count: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 550 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 551 | 0 | my $tbl = shift; | |||||||||||||||||||||||||||
| 552 | 0 | die 'not yet implemented'; | |||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | =head2 find | ||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | Find entries. | ||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | =cut | ||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||
| 561 | 0 | ▶ | sub find { | ||||||||||||||||||||||||||
|
Subroutine
find: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 562 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 563 | 0 | my $tbl = shift; | |||||||||||||||||||||||||||
| 564 | 0 | my $spec = shift; | |||||||||||||||||||||||||||
| 565 | 0 | die 'not yet implemented'; | |||||||||||||||||||||||||||
| 566 | } | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | =head2 create | ||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | Creates a new instance for the given entity. | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | =cut | ||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | 0 | ▶ | sub create { | ||||||||||||||||||||||||||
|
Subroutine
create: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 575 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 576 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 577 | 0 | logError("Creating entity [%s] with [%s]", $args{entity}, $args{data}); | |||||||||||||||||||||||||||
| 578 | my $q = EntityModel::Query->new( | ||||||||||||||||||||||||||||
| 579 | db => $self->db, | ||||||||||||||||||||||||||||
| 580 | 'insert into' => $self->quoted_table_name($args{entity}), | ||||||||||||||||||||||||||||
| 581 | values => $args{data}, | ||||||||||||||||||||||||||||
| 582 | 0 | returning => [ $args{entity}->primary ] | |||||||||||||||||||||||||||
| 583 | ); | ||||||||||||||||||||||||||||
| 584 | 0 | my ($rslt) = $q->results; | |||||||||||||||||||||||||||
| 585 | 0 | return $rslt->{$args{entity}->primary}; | |||||||||||||||||||||||||||
| 586 | } | ||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | =head2 store | ||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | Update the database with current in-memory values for the given entity instance. | ||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | =cut | ||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | 0 | ▶ | sub store { | ||||||||||||||||||||||||||
|
Subroutine
store: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 595 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 596 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 597 | 0 | logError("Creating entity [%s] with [%s]", $args{entity}, $args{data}); | |||||||||||||||||||||||||||
| 598 | my $q = EntityModel::Query->new( | ||||||||||||||||||||||||||||
| 599 | db => $self->db, | ||||||||||||||||||||||||||||
| 600 | 'update' => $self->quoted_table_name($args{entity}), | ||||||||||||||||||||||||||||
| 601 | fields => $args{data}, | ||||||||||||||||||||||||||||
| 602 | 0 | where => [ $args{entity}->primary => $args{id} ] | |||||||||||||||||||||||||||
| 603 | ); | ||||||||||||||||||||||||||||
| 604 | 0 | my $rslt = $q->results; | |||||||||||||||||||||||||||
| 605 | 0 | return $rslt; | |||||||||||||||||||||||||||
| 606 | } | ||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||
| 608 | =head2 read | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | Read information for the requested entity instance. | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | =cut | ||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | 0 | ▶ | sub read { | ||||||||||||||||||||||||||
|
Subroutine
read: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 615 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 616 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 617 | 0 | logDebug("Reading entity [%s] id [%s]", $args{entity}, $args{id}); | |||||||||||||||||||||||||||
| 618 | my $q = EntityModel::Query->new( | ||||||||||||||||||||||||||||
| 619 | db => $self->db, | ||||||||||||||||||||||||||||
| 620 | 0 | 'select' => [ map { $self->quoted_field_name($_) } $args{entity}->field->list ], | |||||||||||||||||||||||||||
| 621 | 'from' => $self->quoted_table_name($args{entity}), | ||||||||||||||||||||||||||||
| 622 | 0 | where => [ $args{entity}->primary => $args{id} ], | |||||||||||||||||||||||||||
| 623 | limit => 1 | ||||||||||||||||||||||||||||
| 624 | ); | ||||||||||||||||||||||||||||
| 625 | 0 | my ($rslt) = $q->results; | |||||||||||||||||||||||||||
| 626 | 0 | logError($rslt); | |||||||||||||||||||||||||||
| 627 | 0 | return $rslt; | |||||||||||||||||||||||||||
| 628 | } | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | =head2 dbh | ||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | Returns a database handle for this storage backend. | ||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | =cut | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | 0 | ▶ | sub dbh { | ||||||||||||||||||||||||||
|
Subroutine
dbh: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 637 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 638 | 0 | return $self->db->dbh(@_); | |||||||||||||||||||||||||||
| 639 | } | ||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||
| 641 | 1 | 1; | |||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||
| 643 | __END__ | ||||||||||||||||||||||||||||