File Coverage

blib/lib/Rose/DB/Object/Metadata/PrimaryKey.pm
Criterion Covered Total %
statement 35 55 63.6
branch 11 28 39.2
condition 2 16 12.5
subroutine 7 9 77.7
pod 3 6 50.0
total 58 114 50.8


line stmt bran cond sub pod time code
1             package Rose::DB::Object::Metadata::PrimaryKey;
2              
3 61     61   474 use strict;
  61         147  
  61         2056  
4              
5 61     61   28220 use Rose::DB::Object::Metadata::UniqueKey;
  61         192  
  61         3777  
6             our @ISA = qw(Rose::DB::Object::Metadata::UniqueKey);
7              
8             our $VERSION = '0.58';
9              
10             use Rose::Object::MakeMethods::Generic
11             (
12 61         376 scalar =>
13             [
14             'generator',
15             ],
16 61     61   475 );
  61         149  
17              
18 0     0 0 0 sub init_name { 'primary_key' }
19              
20             sub sequence_names
21             {
22 0     0 1 0 my($self) = shift;
23              
24 0         0 my $db;
25 0 0       0 $db = shift if(UNIVERSAL::isa($_[0], 'Rose::DB'));
26 0   0     0 $db ||= $self->parent->init_db;
27 0         0 my $db_id = $db->id;
28              
29 0 0       0 if(@_)
30             {
31             my $seqs = $self->{'sequence_names'}{$db->driver} =
32 0 0 0     0 $self->{'sequence_names'}{$db_id} =
33             (@_ == 1 && ref $_[0]) ? $_[0] : [ @_ ];
34              
35 0         0 my $i = 0;
36              
37 0         0 foreach my $column ($self->SUPER::columns)
38             {
39 0 0       0 next unless(ref $column); # may just be a column name
40             # Push down into column metadata object too
41 0         0 $column->default_value_sequence_name($db, $seqs->[$i++]);
42             }
43             }
44              
45             my $ret = $self->{'sequence_names'}{$db_id} ||
46 0   0     0 $self->{'sequence_names'}{$db->driver};
47              
48 0 0       0 unless(ref $ret)
49             {
50 0         0 $self->sync_sequence_names;
51             $ret = $self->{'sequence_names'}{$db_id} ||
52 0   0     0 $self->{'sequence_names'}{$db->driver};
53             }
54              
55 0 0       0 return unless(ref $ret);
56              
57 0 0       0 return wantarray ? @$ret : $ret;
58             }
59              
60             sub auto_init_columns
61             {
62 77     77 0 137 my($self) = shift;
63 77   50     175 my $meta = $self->parent || return [];
64 77   50     233 return $meta->convention_manager->auto_primary_key_column_names || [];
65             }
66              
67             sub add_columns
68             {
69 3     3 1 28 my($self) = shift;
70              
71 3         27 $self->SUPER::add_columns(@_);
72 3         14 $self->sync_sequence_names;
73 3         26 $self->parent->init_primary_key_column_info;
74 3         6 return;
75             }
76              
77             *add_column = \&add_columns;
78              
79             sub columns
80             {
81 153     153 1 739 my($self) = shift;
82              
83 153         230 my(@ret, $ret);
84              
85 153         267 my $wantarray = wantarray;
86 153 100       315 if(defined $wantarray)
87             {
88 152 100       287 if($wantarray)
89             {
90 75         327 @ret = $self->SUPER::columns(@_);
91             }
92             else
93             {
94 77         218 $ret = $self->SUPER::columns(@_);
95             }
96             }
97             else
98             {
99 1         8 $self->SUPER::columns(@_);
100             }
101              
102 153 100       365 $self->parent->init_primary_key_column_info if(@_);
103 153         438 $self->sync_sequence_names;
104              
105 153 100       532 return $wantarray ? @ret : $ret;
106             }
107              
108             sub sync_sequence_names
109             {
110 156     156 0 263 my($self) = shift;
111              
112 156         246 my @sequences;
113              
114 156         373 foreach my $column ($self->SUPER::columns)
115             {
116 160 100       450 next unless(ref $column); # may just be a column name
117 157         539 push(@sequences, $column->default_value_sequence_name);
118             }
119              
120 156 50       408 return unless(grep { defined } @sequences);
  157         588  
121              
122 0 0         $self->sequence_names(\@sequences) if(@sequences);
123              
124 0           return;
125             }
126              
127             1;
128              
129             __END__
130              
131             =head1 NAME
132              
133             Rose::DB::Object::Metadata::PrimaryKey - Primary key metadata.
134              
135             =head1 SYNOPSIS
136              
137             use Rose::DB::Object::Metadata::PrimaryKey;
138              
139             $pk = Rose::DB::Object::Metadata::PrimaryKey->new(
140             columns => [ 'id', 'type' ]);
141              
142             MyClass->meta->primary_key($pk);
143             ...
144              
145             =head1 DESCRIPTION
146              
147             Objects of this class store and manipulate metadata for primary keys in a database table. Each primary key is made up of one or more columns.
148              
149             =head1 OBJECT METHODS
150              
151             =over 4
152              
153             =item B<add_column [COLUMNS]>
154              
155             This method is an alias for the L<add_columns|/add_columns> method.
156              
157             =item B<add_columns [COLUMNS]>
158              
159             Add COLUMNS to the list of columns that make up the primary key. COLUMNS must be a list or reference to an array of column names or L<Rose::DB::Object::Metadata::Column>-derived objects.
160              
161             =item B<columns [COLUMNS]>
162              
163             Get or set the list of columns that make up the primary key. COLUMNS must a list or reference to an array of column names or L<Rose::DB::Object::Metadata::Column>-derived objects.
164              
165             This method returns all of the columns that make up the primary key. Each column is a L<Rose::DB::Object::Metadata::Column>-derived column object if the primary key's L<parent|/parent> has a column object with the same name, or just the column name otherwise. In scalar context, a reference to an array of columns is returned. In list context, a list is returned.
166              
167             =item B<column_names>
168              
169             Returns a list (in list context) or reference to an array (in scalar context) of the names of the columns that make up the primary key.
170              
171             =item B<delete_columns>
172              
173             Delete the entire list of columns that make up the primary key.
174              
175             =item B<name [NAME]>
176              
177             Get or set the name of the primary key. Traditionally, this is the name of the index that the database uses to maintain the primary key, but databases vary. If left undefined, the default value is "primary_key".
178              
179             =item B<parent [META]>
180              
181             Get or set the L<Rose::DB::Object::Metadata>-derived object that this primary key belongs to.
182              
183             =item B<sequence_names [NAMES]>
184              
185             Get or set the list of database sequence names (if any) used to generate values for the primary key columns. The sequence names must be in the same order as the L<columns|/columns>. NAMES may be a list or reference to an array of sequence names. Returns a list (in list context) or reference to the array (in scalar context) of sequence names.
186              
187             =back
188              
189             =head1 AUTHOR
190              
191             John C. Siracusa (siracusa@gmail.com)
192              
193             =head1 LICENSE
194              
195             Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is
196             free software; you can redistribute it and/or modify it under the same terms
197             as Perl itself.