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