Merge AUTHOR and CONTRIBUTORS into a single AUTHORS section
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBObject.pm
CommitLineData
c4a69b87 1package DBIx::Class::Schema::Loader::DBObject;
2
3use strict;
4use warnings;
5use base 'Class::Accessor::Grouped';
383bd2a8 6use mro 'c3';
c4a69b87 7use Carp::Clan qw/^DBIx::Class/;
8use Scalar::Util 'weaken';
9use namespace::clean;
10
11=head1 NAME
12
13DBIx::Class::Schema::Loader::DBObject - Base Class for Database Objects Such as
14Tables and Views in L<DBIx::Class::Schema::Loader>
15
16=head1 METHODS
17
18=head2 loader
19
20The loader object this object is associated with, this is a required parameter
21to L</new>.
22
23=head2 name
24
25Name 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
36use overload
7b6a8d73 37 '""' => sub { $_[0]->name },
fc972571 38 '@{}' => sub { $_[0]->name_parts },
7b6a8d73 39 fallback => 1;
c4a69b87 40
41=head2 new
42
43The constructor, takes L</loader>, L</name>, L</schema>, and L</ignore_schema>
44as key-value parameters.
45
46=cut
47
48sub 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
b21abfca 62=head2 clone
63
64Make a shallow copy of the object.
65
66=cut
67
68sub clone {
69 my $self = shift;
70
71 return bless { %$self }, ref $self;
72}
73
c4a69b87 74=head2 schema
75
76The schema (or owner) of the object. Returns nothing if L</ignore_schema> is
77true.
78
79=head2 ignore_schema
80
81Set to true to make L</schema> and L</sql_name> not use the defined L</schema>.
82Does not affect L</dbic_name> (for
83L<qualify_objects|DBIx::Class::Schema::Loader::Base/qualify_objects> testing on
84SQLite.)
85
86=cut
87
88sub schema {
89 my $self = shift;
90
91 return $self->_schema(@_) unless $self->ignore_schema;
92
93 return undef;
94}
95
96sub _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
113Returns the properly quoted full identifier with L</schema> and L</name>.
114
115=cut
116
117sub 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
133Returns a value suitable for the C<< __PACKAGE__->table >> call in L<DBIx::Class> Result files.
134
135=cut
136
137sub 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
fc972571 157=head2 name_parts
158
159Returns an arrayref of the values returned by the methods specified in
2c158baf 160the L<moniker_parts|DBIx::Class::Schema::Loader::Base/moniker_parts> of
fc972571 161the L</loader> object. The object arrayrefifies to this value.
162
163=cut
164
165sub name_parts {
166 my ($self) = shift;
167 return [ map { $self->$_ } @{$self->loader->moniker_parts} ];
168}
169
170
c4a69b87 171=head1 SEE ALSO
172
173L<DBIx::Class::Schema::Loader::Table>, L<DBIx::Class::Schema::Loader>,
174L<DBIx::Class::Schema::Loader::Base>
175
b87ab391 176=head1 AUTHORS
c4a69b87 177
b87ab391 178See L<DBIx::Class::Schema::Loader/AUTHORS>.
c4a69b87 179
180=head1 LICENSE
181
182This library is free software; you can redistribute it and/or modify it under
183the same terms as Perl itself.
184
185=cut
186
1871;
188# vim:et sts=4 sw=4 tw=0: