| 1 | package DBIx::DBSchema::Index; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use vars qw($VERSION $DEBUG); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | $VERSION = 0.1; | |||||||||||||||||||||||||||
| 7 | 1 | $DEBUG = 0; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | DBIx::DBSchema::Index - Index objects | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSYS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use DBIx::DBSchema::Index; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | $index = new DBIx::DBSchema::Index ( | ||||||||||||||||||||||||||||
| 18 | { | ||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | DBIx::DBSchema::Index objects represent a unique or non-unique database index. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 METHODS | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =over 4 | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =item new HASHREF | OPTION, VALUE, ... | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Creates a new DBIx::DBschema::Index object. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | Accepts either a hashref or a list of options and values. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | Options are: | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =over 8 | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =item name - Index name | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =item using - Optional index method | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =item unique - Boolean indicating whether or not this is a unique index. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =item columns - List reference of column names (or expressions) | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =back | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub new { | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my $proto = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | my %opt = ref($_[0]) ? %{$_[0]} : @_; #want a new reference | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | my $self = \%opt; | |||||||||||||||||||||||||||
| 57 | 0 | bless($self, $class); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =item name [ INDEX_NAME ] | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | Returns or sets the index name. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub name { | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | my($self, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
name: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ( defined($value) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | $self->{name} = $value; | |||||||||||||||||||||||||||
| 70 | } else { | ||||||||||||||||||||||||||||
| 71 | 0 | $self->{name}; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =item using [ INDEX_METHOD ] | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | Returns or sets the optional index method. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | sub using { | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my($self, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
using: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ( defined($value) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | $self->{using} = $value; | |||||||||||||||||||||||||||
| 85 | } else { | ||||||||||||||||||||||||||||
| 86 | defined($self->{using}) | ||||||||||||||||||||||||||||
| 87 | ? $self->{using} | ||||||||||||||||||||||||||||
| 88 | 0 | ▶ | : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =item unique [ BOOL ] | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Returns or sets the unique flag. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | sub unique { | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | my($self, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
unique: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if ( defined($value) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | $self->{unique} = $value; | |||||||||||||||||||||||||||
| 102 | } else { | ||||||||||||||||||||||||||||
| 103 | #$self->{unique}; | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $self->{unique} ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =item columns [ LISTREF ] | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Returns or sets the indexed columns (or expressions). | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =cut | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | sub columns { | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my($self, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
columns: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ( defined($value) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | $self->{columns} = $value; | |||||||||||||||||||||||||||
| 118 | } else { | ||||||||||||||||||||||||||||
| 119 | 0 | $self->{columns}; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =item columns_sql | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Returns a comma-joined list of columns, suitable for an SQL statement. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | sub columns_sql { | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
columns_sql: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | 0 | join(', ', @{ $self->columns } ); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =item sql_create_index TABLENAME | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Returns an SQL statment to create this index on the specified table. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =cut | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | sub sql_create_index { | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | my( $self, $table ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
sql_create_index: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | my $unique = $self->unique ? 'UNIQUE' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | my $name = $self->name; | |||||||||||||||||||||||||||
| 145 | 0 | my $col_sql = $self->columns_sql; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | "CREATE $unique INDEX $name ON $table ( $col_sql )"; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =item cmp OTHER_INDEX_OBJECT | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Compares this object to another supplied object. Returns true if they are | ||||||||||||||||||||||||||||
| 153 | identical, or false otherwise. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =cut | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | sub cmp { | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | my( $self, $other ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cmp: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | $self->name eq $other->name and $self->cmp_noname($other); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =item cmp_noname OTHER_INDEX_OBJECT | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Compares this object to another supplied object. Returns true if they are | ||||||||||||||||||||||||||||
| 166 | identical, disregarding index name, or false otherwise. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | sub cmp_noname { | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | my( $self, $other ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
cmp_noname: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | $self->using eq $other->using | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->using eq $other->using and $self->unique == $other->unique
MC/DC0%0/2
$self->using eq $other->using and $self->unique == $other->unique
$self->using eq $other->using
$self->unique == $other->unique
Truth table0%0/3
A and B
A= $self->using eq $other->usingB= $self->unique == $other->unique
| |||||||||||||||||||||||||||||
| 174 | and $self->unique == $other->unique | ||||||||||||||||||||||||||||
| 175 | and $self->columns_sql eq $other->columns_sql; | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =back | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | Ivan Kohler <ivan-dbix-dbschema@420.am> | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Copyright (c) 2007 Ivan Kohler | ||||||||||||||||||||||||||||
| 186 | Copyright (c) 2007 Freeside Internet Services, Inc. | ||||||||||||||||||||||||||||
| 187 | All rights reserved. | ||||||||||||||||||||||||||||
| 188 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 189 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head1 BUGS | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Is there any situation in which sql_create_index needs to return a list of | ||||||||||||||||||||||||||||
| 194 | multiple statements? | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | L<DBIx::DBSchema::Table>, L<DBIx::DBSchema>, L<DBI> | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =cut | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 1 | 1; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||