SQLEngine/Schema/Table.pm

filter: Statementstmt 57.6% 68 / 118 Branchbran 28.9% 11 / 38 Conditioncond 35.7% 5 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 55.5% 25 / 45 Podpod 97.0% 33 / 34 totaltotal 56.4% 145 / 257 CC 27 SCAR 14.8
CC
27
Cov
49%
sqlengine_table_method
12.0
create_table
12.0
recreate_table
12.0
CRAP 4.4
« SQLEngine/Record/Base.pm ↑ Summary SQLEngine/Driver/NullP.pm »
1 =head1 NAME
2  
3 DBIx::SQLEngine::Schema::Table - A table in a data source
4  
5 =head1 SYNOPSIS
6  
7 $sqldb = DBIx::SQLEngine->new( ... );
8
9 $table = $sqldb->table( $table_name );
10
11 $hash_ary = $table->fetch_select( where => { status => 2 } );
12  
13 $table->do_insert( values => { somefield => 'A Value', status => 3 } );
14 $table->do_update( values => { status => 3 }, where => { status => 2 } );
15 $table->do_delete( where => { somefield => 'A Value' } );
16  
17 $hash = $table->fetch_row( $primary_key );
18  
19 $table->insert_row( { somefield => 'Some Value' } );
20 $table->update_row( { id => $primary_key, somefield => 'Some Value' } );
21 $table->delete_row( { id => $primary_key } );
22  
23  
24 =head1 DESCRIPTION
25  
26 The DBIx::SQLEngine::Schema::Table class represents database tables accessible via a particular DBIx::SQLEngine.
27  
28 By storing a reference to a SQLEngine and the name of a table to operate on, a Schema::Table object can facilitate generation of SQL queries that operate on the named table.
29  
30 Each table can retrieve and cache a ColumnSet containing information about the name and type of the columns in the table. Column information is loaded from the storage as needed, but if you are creating a new table you must provide the definition.
31  
32 The *_row() methods use this information about the table columns to facilitate common operations on table rows using their primary keys and simple hash-refs.
33  
34 =cut
35  
36 ########################################################################
37  
38 package DBIx::SQLEngine::Schema::Table;
39 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 6 ▶use Class::MakeMethods;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 6 ▶use DBIx::SQLEngine;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 6 ▶use DBIx::SQLEngine::Schema::Column;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 6 ▶use DBIx::SQLEngine::Schema::ColumnSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 ########################################################################
49  
50 =head1 INSTANTIATION AND ACCESSORS
51  
52 =head2 Table Object Creation
53  
54 =over 4
55  
56 =item SQLEngine->table()
57  
58 $sqldb->table( $tablename ) : $table
59  
60 Convenience function to create a table with the given table name and sqlengine.
61  
62 =item new()
63  
64 DBIx::SQLEngine::Schema::Table->new( sqlengine=>$sqldb, name=>$name ) : $table
65  
66 Standard hash constructor. You are expected to provde the name and sqlengine arguments.
67  
68 =back
69  
70 =cut
71  
72 6 ▶use Class::MakeMethods (
Subroutine BEGIN: called CC=1 SCAR=0.0
73 6 'Standard::Hash:new' => 'new',
74 6 );
75  
76 ########################################################################
77  
78 =head2 Name Accessor
79  
80 =over 4
81  
82 =item name()
83  
84 $table->name() : $string
85 $table->name($string)
86  
87 Get and set the table name. Required value. Identifies this table in the data
88 source.
89  
90 =item get_name()
91  
92 $table->get_name() : $string or exception
93  
94 Returns the table name, or throws an exception if it is not set.
95  
96 =back
97  
98 =cut
99  
100 6 ▶use Class::MakeMethods (
Subroutine BEGIN: called CC=1 SCAR=0.0
101 6 'Standard::Hash:scalar' => 'name',
102 6 );
103  
104 0 ▶sub get_name {
Subroutine get_name: not called CC=2 SCAR=17.9
Pod documented
105 0 ▶ ($_[0])->name() or croak("No name set for table in '$_[0]->{sqlengine}'")
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->name or croak(qq[No name set for table in '$_[0]{"sqlengine"}']) 0 0 0
106 }
107  
108 ########################################################################
109  
110 =head2 SQLEngine Accessor
111  
112 =over 4
113  
114 =item sqlengine()
115  
116 $table->sqlengine() : $sqldb
117 $table->sqlengine($sqldb)
118  
119 Get and set our current DBIx::SQLEngine. Required value. The SQLEngine
120 provides the DBI connection and SQL execution capabilities required to talk
121 to the remote data storage.
122  
123 =item get_sqlengine()
124  
125 $table->get_sqlengine() : $sqldb or exception
126  
127 Returns the SQLEngine, or throws an exception if it is not set.
128  
129 =back
130  
131 =cut
132  
133 6 ▶use Class::MakeMethods (
Subroutine BEGIN: called CC=1 SCAR=0.0
134 6 'Standard::Hash:object' => { name=>'sqlengine',
135 class=>'DBIx::SQLEngine::Driver::Default' },
136 6 );
137  
138 0 ▶sub get_sqlengine {
Subroutine get_sqlengine: not called CC=2 SCAR=17.9
Pod documented
139 0 ▶ ($_[0])->sqlengine() or croak("No sqlengine set for table '$_[0]->{name}'")
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->sqlengine or croak(qq[No sqlengine set for table '$_[0]{"name"}']) 0 0 0
140 }
141  
142 ########################################################################
143  
144 =head2 SQLEngine Method Invocation
145  
146 =over 4
147  
148 =item sqlengine_do()
149  
150 $table->sqlengine_do( $method, %sql_clauses ) : $results or exception
151  
152 Calls the provided method name on the associated SQLEngine, passing along the table name and the other provided arguments. Intended for methods with hash-based argument parsing like C<fetch_select( table =E<gt> $table_name )>.
153  
154 =item sqlengine_table_method()
155  
156 $table->sqlengine_table_method( $method, @args ) : $results or exception
157  
158 Calls the provided method name on the associated SQLEngine, passing along the table name and the other provided arguments. Intended for methods with list-based argument parsing like C<detect_table( $table_name )>.
159  
160 =back
161  
162 =cut
163  
164 49 ▶sub sqlengine_do {
Subroutine sqlengine_do: called CC=5 SCAR=16.6
Pod documented
165 49 my ($self, $method, %args) = @_;
166 49 ▶ my $name = $self->name()
Branch: 1 branch
BranchTrueFalseTotal
my $name = $self->name or croak(qq[No name set for table in '$self->{"sqlengine"}']) 49 0 49
167 or croak("No name set for table in '$self->{sqlengine}'");
168 49 ▶ my $sqlengine = $self->sqlengine()
Branch: 1 branch
BranchTrueFalseTotal
my $sqlengine = $self->sqlengine or croak(qq[No sqlengine set for table '$_[0]{"name"}']) 49 0 49
169 or croak("No sqlengine set for table '$_[0]->{name}'");
170 49 ▶ $args{table} = $name unless( $args{sql} || $args{named_query} );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"sql"} or $args{"named_query"} 45 4 49
Condition66%2/3
$args{"sql"} or $args{"named_query"}
l!l&&r!l&&!r
4045
MC/DC50%1/2
$args{"sql"} or $args{"named_query"}
$args{"sql"} $args{"named_query"}
Truth table66%2/3
A or B
A= $args{"sql"}B= $args{"named_query"}
ABresult
1X1
011
000
171 49 $sqlengine->$method( %args)
172 }
173  
174 0 ▶sub sqlengine_table_method {
Subroutine sqlengine_table_method: not called CC=3 SCAR=24.8
Pod documented
175 0 my ($self, $method, @args) = @_;
176 0 ▶ my $name = $self->name()
Branch: 1 branch
BranchTrueFalseTotal
my $name = $self->name or croak(qq[No name set for table in '$self->{"sqlengine"}']) 0 0 0
177 or croak("No name set for table in '$self->{sqlengine}'");
178 0 ▶ my $sqlengine = $self->sqlengine()
Branch: 1 branch
BranchTrueFalseTotal
my $sqlengine = $self->sqlengine or croak(qq[No sqlengine set for table '$_[0]{"name"}']) 0 0 0
179 or croak("No sqlengine set for table '$_[0]->{name}'");
180 0 $sqlengine->$method($name, @args)
181 }
182  
183 ########################################################################
184  
185 =head2 Detect Availability
186  
187 =over 4
188  
189 =item detect_sqlengine()
190  
191 $table->detect_sqlengine : $flag
192  
193 Detects whether the SQL database is avaialable by attempting to connect.
194  
195 =item detect_table()
196  
197 $table->detect_table : @columns
198  
199 Checks to see if the table exists in the SQL database by attempting to retrieve its columns.
200  
201 =back
202  
203 =cut
204  
205 # $flag = $table->detect_sqlengine;
206 0 ▶sub detect_sqlengine {
Subroutine detect_sqlengine: not called CC=1 SCAR=6.9
Pod documented
207 0 (shift)->get_sqlengine()->detect_any;
208 }
209  
210 # @columns = $table->detect_table;
211 0 ▶sub detect_table {
Subroutine detect_table: not called CC=1 SCAR=6.9
Pod documented
212 0 (shift)->sqlengine_table_method('detect_table');
213 }
214  
215 ########################################################################
216  
217 =head2 Row Class
218  
219 =over 4
220  
221 =item record_class()
222  
223 $table->record_class() : $record_class
224  
225 Returns the Record::Class which corresponds to the table.
226  
227 =back
228  
229 =cut
230  
231 5 ▶sub record_class {
Subroutine record_class: called CC=1 SCAR=0.0
Pod documented
232 5 my ($table, $classname, @traits) = @_;
233 5 require DBIx::SQLEngine::Record::Class;
234 5 DBIx::SQLEngine::Record::Class->subclass_for_table( @_ )
235 }
236  
237 ########################################################################
238  
239 ########################################################################
240  
241 =head1 FETCHING DATA (SQL DQL)
242  
243 =head2 Select to Retrieve Rows
244  
245 =over 4
246  
247 =item fetch_select()
248  
249 $table->fetch_select ( %select_clauses ) : $row_hash_array
250  
251 Calls the corresponding SQLEngine method with the table name and the provided arguments. Return rows from the table that match the provided criteria, and in the requested order, by executing a SQL select statement.
252  
253 =item visit_select()
254  
255 $table->visit_select ( $sub_ref, %select_clauses ) : @results
256 $table->visit_select ( %select_clauses, $sub_ref ) : @results
257  
258 Calls the provided subroutine on each matching row as it is retrieved. Returns the accumulated results of each subroutine call (in list context).
259  
260 =item select_row()
261  
262 $table->select_row ( $primary_key_value ) : $row_hash
263 $table->select_row ( \@compound_primary_key ) : $row_hash
264 $table->select_row ( \%hash_with_primary_key_value ) : $row_hash
265  
266 Fetches a single row by primary key.
267  
268 =item select_rows()
269  
270 $table->select_rows ( @primary_key_values_or_hashrefs ) : $row_hash_array
271  
272 Fetches a set of one or more by primary key.
273  
274 =back
275  
276 =cut
277  
278 # $rows = $self->fetch_select( %select_clauses );
279 12 ▶sub fetch_select {
Subroutine fetch_select: called CC=1 SCAR=0.0
Pod documented
280 12 (shift)->sqlengine_do('fetch_select', @_)
281 }
282  
283 # $rows = $self->fetch_one_row( %select_clauses );
284 6 ▶sub fetch_one_row {
Subroutine fetch_one_row: called CC=1 SCAR=0.0
Pod undocumented
285 6 (shift)->sqlengine_do('fetch_one_row', @_)
286 }
287  
288 # @results= $self->visit_select( %select_clauses, $sub );
289 0 ▶sub visit_select {
Subroutine visit_select: not called CC=2 SCAR=17.9
Pod documented
290 0 my $self = shift;
291 0 ▶ my $sub = ( ref($_[0]) ? shift : pop );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
292 0 $self->sqlengine_do('visit_select', @_, $sub )
293 }
294  
295 # $row = $self->select_row( $id_value );
296 # $row = $self->select_row( \@compound_id );
297 # $row = $self->select_row( \%hash_with_pk );
298 # Retrieve a specific row by id
299 1 ▶sub select_row {
Subroutine select_row: called CC=1 SCAR=0.0
Pod documented
300 1 my $self = shift;
301 1 $self->sqlengine_do('fetch_one_row', where=>$self->primary_criteria(@_) )
302 }
303  
304 # $rows = $self->select_rows( @ids_or_hashes );
305 0 ▶sub select_rows {
Subroutine select_rows: not called CC=1 SCAR=6.9
Pod documented
306 0 my $self = shift;
307 0 $self->sqlengine_do('fetch_select', where=>$self->primary_criteria(@_) )
308 }
309  
310 ########################################################################
311  
312 =head2 Selecting Agregate Values
313  
314 =over 4
315  
316 =item fetch_one_value()
317  
318 $table->fetch_one_value( %sql_clauses ) : $scalar
319  
320 Calls fetch_select, then returns the first value from the first row of results.
321  
322 =item count_rows()
323  
324 $table->count_rows ( ) : $number
325 $table->count_rows ( $criteria ) : $number
326  
327 Return the number of rows in the table. If called with criteria, returns the number of matching rows.
328  
329 =item try_count_rows()
330  
331 $table->try_count_rows ( ) : $number
332 $table->try_count_rows ( $criteria ) : $number
333  
334 Exception catching wrapper around count_rows. If the eval block catches an exception, undef is returned.
335  
336 =item fetch_max()
337  
338 $table->count_rows ( $colname, CRITERIA ) : $number
339  
340 Returns the largest value in the named column.
341  
342 =back
343  
344 =cut
345  
346 # $value = $self->fetch_one_value( %select_clauses );
347 0 ▶sub fetch_one_value {
Subroutine fetch_one_value: not called CC=1 SCAR=6.9
Pod documented
348 0 (shift)->sqlengine_do('fetch_one_value', @_ )
349 }
350  
351 # $rowcount = $self->count_rows
352 # $rowcount = $self->count_rows( $criteria );
353 0 ▶sub count_rows {
Subroutine count_rows: not called CC=1 SCAR=6.9
Pod documented
354 0 (shift)->fetch_one_value( columns => 'count(*)', where => (shift) )
355 }
356  
357 0 ▶sub try_count_rows {
Subroutine try_count_rows: not called CC=2 SCAR=17.9
Pod documented
358 0 my $count = eval { (shift)->count_rows( @_ ) };
359 0 ▶ wantarray ? ( $count, $@ ) : $count
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
360 }
361  
362 # $max_value = $self->fetch_max( $colname, $criteria );
363 0 ▶sub fetch_max {
Subroutine fetch_max: not called CC=1 SCAR=6.9
Pod documented
364 0 (shift)->fetch_one_value( columns => "max(".(shift).")", where => (shift) )
365 }
366  
367 ########################################################################
368  
369 ########################################################################
370  
371 =head1 EDITING DATA (SQL DML)
372  
373 =head2 Insert to Add Rows
374  
375 =over 4
376  
377 =item do_insert()
378  
379 $table->do_insert ( %insert_clauses ) : $row_count
380  
381 Calls the corresponding SQLEngine method with the table name and the provided arguments.
382  
383 =item insert_row()
384  
385 $table->insert_row ( $row_hash ) : $row_count
386  
387 Adds the provided row by executing a SQL insert statement. Uses column_names() and column_primary_is_sequence() to produce the proper clauses. Returns the total number of rows affected, which is typically 1.
388  
389 =item insert_rows()
390  
391 $table->insert_rows ( @row_hashes ) : $row_count
392  
393 Insert each of the rows from the provided list into the table. Returns the total number of rows affected, which is typically the same as the number of arguments passed.
394  
395 =back
396  
397 =cut
398  
399 1 ▶sub do_insert {
Subroutine do_insert: called CC=1 SCAR=0.0
Pod documented
400 1 (shift)->sqlengine_do('do_insert', @_)
401 }
402  
403 # $self->insert_row( \%row );
404 9 ▶sub insert_row {
Subroutine insert_row: called CC=3 SCAR=11.0
Pod documented
405 9 my ($self, $row) = @_;
406
407 9 my $primary = $self->column_primary_name;
408 9 ▶ my @colnames = grep { $_ eq $primary or defined $row->{$_} }
Condition100%3/3
$_ eq $primary || defined $row->{$_}
l!l&&r!l&&!r
981
MC/DC100%2/2
$_ eq $primary || defined $row->{$_}
$_ eq $primary defined $row->{$_}
Truth table100%3/3
A || B
A= $_ eq $primaryB= defined $row->{$_}
ABresult
1X1
011
000
409 $self->column_names;
410
411 9 ▶ $self->sqlengine_do('do_insert',
Branch: 1 branch
BranchTrueFalseTotal
$self->column_primary_is_sequence ? : 0 9 9
412 ( $self->column_primary_is_sequence ? ( sequence => $primary ) : () ),
413 columns => \@colnames,
414 values => $row,
415 );
416 }
417  
418 # $self->insert_rows( @hashes );
419 0 ▶sub insert_rows {
Subroutine insert_rows: not called CC=2 SCAR=17.9
Pod documented
420 0 my $self = shift;
421 0 my $rc;
422 0 foreach my $row ( @_ ) { $rc += $self->insert_row( $row ) }
423 $rc
424 0 }
425  
426 ########################################################################
427  
428 =head2 Update to Change Rows
429  
430 =over 4
431  
432 =item do_update()
433  
434 $table->do_update ( %update_clauses ) : $row_count
435  
436 Calls the corresponding SQLEngine method with the table name and the provided arguments.
437  
438 =item update_row()
439  
440 $table->update_row ( $row_hash ) : $row_count
441  
442 Update this existing row based on its primary key. Uses column_names() and column_primary_is_sequence() to produce the proper clauses. Returns the total number of rows affected, which is typically 1.
443  
444 =item update_rows()
445  
446 $table->update_rows ( @row_hashes ) : $row_count
447  
448 Update several existing rows based on their primary keys. Uses update_row(). Returns the total number of rows affected, which is typically the same as the number of arguments passed.
449  
450 =back
451  
452 =cut
453  
454 # $self->do_update( %clauses);
455 1 ▶sub do_update {
Subroutine do_update: called CC=1 SCAR=0.0
Pod documented
456 1 (shift)->sqlengine_do('do_update', @_ )
457 }
458  
459 # $self->update_row( $row );
460 5 ▶sub update_row {
Subroutine update_row: called CC=1 SCAR=0.0
Pod documented
461 5 my($self, $row) = @_;
462
463 5 $self->sqlengine_do('do_update',
464 columns => [ $self->column_names ],
465 where => $self->primary_criteria( $row ),
466 values => $row,
467 );
468 }
469  
470 # $self->update_rows( @hashes );
471 0 ▶sub update_rows {
Subroutine update_rows: not called CC=2 SCAR=17.9
Pod documented
472 0 my $self = shift;
473 0 my $rc;
474 0 foreach my $row ( @_ ) { $rc += $self->update_row( $row ) }
475 $rc
476 0 }
477  
478 ########################################################################
479  
480 =head2 Delete to Remove Rows
481  
482 =over 4
483  
484 =item do_delete()
485  
486 $table->do_delete ( %delete_clauses ) : $row_count
487  
488 Calls the corresponding SQLEngine method with the table name and the provided arguments.
489  
490 =item delete_row()
491  
492 $table->delete_row ( $row_hash_or_id ) : ()
493  
494 Deletes the provided row from the table. Returns the total number of rows affected, which is typically 1.
495  
496 =item delete_rows()
497  
498 $table->delete_rows ( @row_hashes_or_ids ) : ()
499  
500 Deletes all of the provided rows from the table. Returns the total number of rows affected, which is typically the same as the number of arguments passed.
501  
502 =back
503  
504 =cut
505  
506 # $self->do_delete( %clauses);
507 2 ▶sub do_delete {
Subroutine do_delete: called CC=1 SCAR=0.0
Pod documented
508 2 (shift)->sqlengine_do('do_delete', @_ )
509 }
510  
511 # $self->delete_row( $row );
512 5 ▶sub delete_row {
Subroutine delete_row: called CC=1 SCAR=0.0
Pod documented
513 5 my $self = shift;
514 5 $self->sqlengine_do( 'do_delete', where => $self->primary_criteria(@_ ) )
515 }
516  
517 # $self->delete_rows( @hashes );
518 0 ▶sub delete_rows {
Subroutine delete_rows: not called CC=1 SCAR=6.9
Pod documented
519 0 my $self = shift;
520 0 $self->sqlengine_do( 'do_delete', where => $self->primary_criteria(@_) )
521 }
522  
523 ########################################################################
524  
525 ########################################################################
526  
527 =head1 DEFINING STRUCTURES (SQL DDL)
528  
529 =head2 ColumnSet
530  
531 =over 4
532  
533 =item columnset()
534  
535 $table->columnset () : $columnset
536  
537 Returns the current columnset, if any.
538  
539 =item get_columnset()
540  
541 $table->get_columnset () : $columnset
542  
543 Returns the current columnset, or runs a trivial query to detect the columns in the sqlengine. If the table doesn't exist, the columnset will be empty.
544  
545 =item columns()
546  
547 $table->columns () : @columns
548  
549 Return the column objects from the current columnset.
550  
551 =item column_names()
552  
553 $table->column_names () : @column_names
554  
555 Return the names of the columns, in order.
556  
557 =item column_named()
558  
559 $table->column_named ( $name ) : $column
560  
561 Return the column info object for the specifically named column.
562  
563 =back
564  
565 =cut
566  
567 6 ▶use Class::MakeMethods (
Subroutine BEGIN: called CC=1 SCAR=0.0
568 6 'Standard::Hash:object' => { name=>'columnset',
569 class=>'DBIx::SQLEngine::Schema::ColumnSet' },
570 'Standard::Universal:delegate' => [
571 [ qw( columns column_names column_named column_primary ) ] =>
572 { target=>'get_columnset' },
573 ],
574 6 );
575  
576 49 ▶sub get_columnset {
Subroutine get_columnset: called CC=3 SCAR=16.5
Pod documented
577 49 my $self = shift;
578
579 49 ▶ $self->columnset or $self->columnset( do {
Branch: 1 branch
BranchTrueFalseTotal
$self->columnset or $self->columnset(do { my(@columns) = $self->detect_table or confess(qq[Couldn't fetch column information for table $self->{"name"}]); "DBIx::SQLEngine::Schema::ColumnSet"->new(@columns) }) 49 0 49
580 0 ▶ my @columns = $self->detect_table() or
Branch: 1 branch
BranchTrueFalseTotal
my(@columns) = $self->detect_table or confess(qq[Couldn't fetch column information for table $self->{"name"}]) 0 0 0
581 confess("Couldn't fetch column information for table $self->{name}");
582 0 DBIx::SQLEngine::Schema::ColumnSet->new( @columns )
583 }
584 );
585 }
586  
587 ########################################################################
588  
589 =head2 Primary Keys
590  
591 =over 4
592  
593 =item column_primary_is_sequence()
594  
595 Inheritable boolean which can be set for the table class or any instance.
596 Indicates that the primary key column uses an auto-incrementing sequence.
597  
598 =item column_primary_name()
599  
600 Returns the name of the primary key column. (TODO: Currently hard-coded to the first column in the column set.)
601  
602 =item primary_criteria()
603  
604 Returns a hash of key-value pairs which could be used to select this record by its primary key.
605  
606 =back
607  
608 =cut
609  
610 # To-do: finish adding support for tables with multiple-column primary keys.
611 6 ▶use Class::MakeMethods (
Subroutine BEGIN: called CC=1 SCAR=0.0
612 # 'Standard::Inheritable:scalar' => { name=>'column_primary_name', },
613 6 'Standard::Inheritable:scalar' => { name=>'column_primary_is_sequence', },
614 6 );
615  
616 35 ▶sub column_primary_name {
Subroutine column_primary_name: called CC=3 SCAR=11.7
Pod documented
617 35 ▶ my $columns = (shift)->get_columnset or return;
Branch: 1 branch
BranchTrueFalseTotal
my $columns = (shift())->get_columnset or return 35 0 35
618 35 ▶ my $column = $columns->[0] or return;
Branch: 1 branch
BranchTrueFalseTotal
my $column = $columns->[0] or return 35 0 35
619 35 $column->name;
620 }
621  
622 # (__PACKAGE__)->column_primary_name( 'id' );
623 # (__PACKAGE__)->column_primary_is_sequence( 1 );
624  
625 19 ▶sub primary_criteria {
Subroutine primary_criteria: called CC=3 SCAR=11.0
Pod documented
626 19 my $self = shift;
627 19 my $primary_col = $self->column_primary_name;
628 19 ▶ my @ids = map { UNIVERSAL::isa($_, 'HASH') ? $_->{$primary_col} : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_, 'HASH') ? : 15 4 19
629 19 ▶ return { $primary_col => ( scalar(@ids) > 1 ) ? \@ids : $ids[0] }
Branch: 1 branch
BranchTrueFalseTotal
scalar @ids > 1 ? : 0 19 19
630 }
631  
632 ########################################################################
633  
634 =head2 Create and Drop Tables
635  
636 =over 4
637  
638 =item table_exists()
639  
640 $table->table_exists() : $flag
641  
642 Detects whether the table has been created and has not been dropped.
643  
644 =item create_table()
645  
646 $table->create_table ()
647 $table->create_table ( $column_ary )
648  
649 =item drop_table()
650  
651 $table->drop_table ()
652  
653 =item ensure_table_exists()
654  
655 $table->ensure_table_exists ( $column_ary )
656  
657 Create the table's remote storage if it does not already exist.
658  
659 =item recreate_table()
660  
661 $table->recreate_table ()
662 $table->recreate_table ( $column_ary )
663  
664 Drop and then recreate the table's remote storage.
665  
666 =item recreate_table_with_rows
667  
668 $table->recreate_table_with_rows ()
669 $table->recreate_table_with_rows ( $column_ary )
670  
671 Selects all of the existing rows, then drops and recreates the table, then re-inserts all of the rows.
672  
673 =back
674  
675 =cut
676  
677 # $flag = $table->table_exists;
678 0 ▶sub table_exists { &detect_table ? 1 : 0 }
Subroutine table_exists: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
&detect_table ? : 0 0 0
679  
680 # $self->create_table();
681 0 ▶sub create_table {
Subroutine create_table: not called CC=3 SCAR=24.8
Pod documented
682 0 my $self = shift;
683 0 ▶ my $columnset = shift || $self->columnset ||
Condition0%0/5
shift() || $self->columnset || confess("No column information for table $self->{'name'}")
l!l
00
shift() or $self->columnset
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->columnset || confess("No column information for table $self->{'name'}")
shift() $self->columnset
Truth table0%0/3
A or B
A= shift()B= $self->columnset
ABresult
1X1
011
000
684 confess("No column information for table $self->{name}");
685 0 $self->sqlengine_table_method('create_table', $columnset ) ;
686 }
687  
688 # $sql_stmt = $table->drop_table();
689 0 ▶sub drop_table {
Subroutine drop_table: not called CC=1 SCAR=6.9
Pod documented
690 0 (shift)->sqlengine_table_method('drop_table') ;
691 }
692  
693 # $table->ensure_table_exists( $column_ary )
694 # Create the remote data source for a table if it does not already exist
695 0 ▶sub ensure_table_exists {
Subroutine ensure_table_exists: not called CC=2 SCAR=17.9
Pod documented
696 0 my $self = shift;
697 0 ▶ $self->create_table(@_) unless $self->table_exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->table_exists 0 0 0
698 }
699  
700 # $table->recreate_table
701 # $table->recreate_table( $column_ary )
702 # Delete the source, then create it again
703 0 ▶sub recreate_table {
Subroutine recreate_table: not called CC=3 SCAR=24.8
Pod documented
704 0 my $self = shift;
705 0 my $column_ary = shift;
706 0 ▶ if ( $self->table_exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->table_exists) 0 0 0
707 0 ▶ $column_ary ||= $self->get_columnset;
Condition0%0/3
$column_ary ||= $self->get_columnset
l!l&&r!l&&!r
000
MC/DC0%0/2
$column_ary ||= $self->get_columnset
$column_ary $self->get_columnset
Truth table0%0/3
A ||= B
A= $column_aryB= $self->get_columnset
ABresult
1X1
011
000
708 0 $self->drop_table;
709 }
710 0 $self->create_table( $column_ary );
711 }
712  
713 # $package->recreate_table_with_rows;
714 # $package->recreate_table_with_rows( $column_ary );
715 0 ▶sub recreate_table_with_rows {
Subroutine recreate_table_with_rows: not called CC=1 SCAR=6.9
Pod documented
716 0 my $self = shift;
717 0 my $rows = $self->fetch_select();
718 0 $self->recreate_table( @_ );
719 0 $self->insert_rows( $rows );
720 }
721  
722 ########################################################################
723  
724 =head1 SEE ALSO
725  
726 See L<DBIx::SQLEngine> for the overall interface and developer documentation.
727  
728 See L<DBIx::SQLEngine::Docs::ReadMe> for general information about
729 this distribution, including installation and license information.
730  
731 =cut
732  
733 ########################################################################
734  
735 6 1;
« SQLEngine/Record/Base.pm ↑ Summary SQLEngine/Driver/NullP.pm »