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