Merge AUTHOR and CONTRIBUTORS into a single AUTHORS section
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBObject.pm
1 package DBIx::Class::Schema::Loader::DBObject;
2
3 use strict;
4 use warnings;
5 use base 'Class::Accessor::Grouped';
6 use mro 'c3';
7 use Carp::Clan qw/^DBIx::Class/;
8 use Scalar::Util 'weaken';
9 use namespace::clean;
10
11 =head1 NAME
12
13 DBIx::Class::Schema::Loader::DBObject - Base Class for Database Objects Such as
14 Tables and Views in L<DBIx::Class::Schema::Loader>
15
16 =head1 METHODS
17
18 =head2 loader
19
20 The loader object this object is associated with, this is a required parameter
21 to L</new>.
22
23 =head2 name
24
25 Name of the object. The object stringifies to this value.
26
27 =cut
28
29 __PACKAGE__->mk_group_accessors(simple => qw/
30     loader
31     name
32     _schema
33     ignore_schema
34 /);
35
36 use overload
37     '""' => sub { $_[0]->name },
38     '@{}' => sub { $_[0]->name_parts },
39     fallback => 1;
40
41 =head2 new
42
43 The constructor, takes L</loader>, L</name>, L</schema>, and L</ignore_schema>
44 as key-value parameters.
45
46 =cut
47
48 sub new {
49     my $class = shift;
50
51     my $self = { @_ };
52
53     croak "loader is required" unless ref $self->{loader};
54
55     weaken $self->{loader};
56
57     $self->{_schema} = delete $self->{schema};
58
59     return bless $self, $class;
60 }
61
62 =head2 clone
63
64 Make a shallow copy of the object.
65
66 =cut
67
68 sub clone {
69     my $self = shift;
70
71     return bless { %$self }, ref $self;
72 }
73
74 =head2 schema
75
76 The schema (or owner) of the object. Returns nothing if L</ignore_schema> is
77 true.
78
79 =head2 ignore_schema
80
81 Set to true to make L</schema> and L</sql_name> not use the defined L</schema>.
82 Does not affect L</dbic_name> (for
83 L<qualify_objects|DBIx::Class::Schema::Loader::Base/qualify_objects> testing on
84 SQLite.)
85
86 =cut
87
88 sub schema {
89     my $self = shift;
90
91     return $self->_schema(@_) unless $self->ignore_schema;
92
93     return undef;
94 }
95
96 sub _quote {
97     my ($self, $identifier) = @_;
98
99     $identifier = '' if not defined $identifier;
100
101     my $qt = $self->loader->quote_char || '';
102
103     if (length $qt > 1) {
104         my @qt = split //, $qt;
105         return $qt[0] . $identifier . $qt[1];
106     }
107
108     return "${qt}${identifier}${qt}";
109 }
110
111 =head1 sql_name
112
113 Returns the properly quoted full identifier with L</schema> and L</name>.
114
115 =cut
116
117 sub sql_name {
118     my $self = shift;
119
120     my $name_sep = $self->loader->name_sep;
121
122     if ($self->schema) {
123         return $self->_quote($self->schema)
124             . $name_sep
125             . $self->_quote($self->name);
126     }
127
128     return $self->_quote($self->name);
129 }
130
131 =head1 dbic_name
132
133 Returns a value suitable for the C<< __PACKAGE__->table >> call in L<DBIx::Class> Result files.
134
135 =cut
136
137 sub dbic_name {
138     my $self = shift;
139
140     my $name_sep = $self->loader->name_sep;
141
142     if ($self->loader->qualify_objects && $self->_schema) {
143         if ($self->_schema =~ /\W/ || $self->name =~ /\W/) {
144             return \ $self->sql_name;
145         }
146
147         return $self->_schema . $name_sep . $self->name;
148     }
149
150     if ($self->name =~ /\W/) {
151         return \ $self->_quote($self->name);
152     }
153
154     return $self->name;
155 }
156
157 =head2 name_parts
158
159 Returns an arrayref of the values returned by the methods specified in
160 the L<moniker_parts|DBIx::Class::Schema::Loader::Base/moniker_parts> of
161 the L</loader> object. The object arrayrefifies to this value.
162
163 =cut
164
165 sub name_parts {
166     my ($self) = shift;
167     return [ map { $self->$_ } @{$self->loader->moniker_parts} ];
168 }
169
170
171 =head1 SEE ALSO
172
173 L<DBIx::Class::Schema::Loader::Table>, L<DBIx::Class::Schema::Loader>,
174 L<DBIx::Class::Schema::Loader::Base>
175
176 =head1 AUTHORS
177
178 See L<DBIx::Class::Schema::Loader/AUTHORS>.
179
180 =head1 LICENSE
181
182 This library is free software; you can redistribute it and/or modify it under
183 the same terms as Perl itself.
184
185 =cut
186
187 1;
188 # vim:et sts=4 sw=4 tw=0: