1e6df30945f2e0e4dbd9437263b23683ac48f0ef
[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     fallback => 1;
39
40 =head2 new
41
42 The constructor, takes L</loader>, L</name>, L</schema>, and L</ignore_schema>
43 as key-value parameters.
44
45 =cut
46
47 sub 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
61 =head2 clone
62
63 Make a shallow copy of the object.
64
65 =cut
66
67 sub clone {
68     my $self = shift;
69
70     return bless { %$self }, ref $self;
71 }
72
73 =head2 schema
74
75 The schema (or owner) of the object. Returns nothing if L</ignore_schema> is
76 true.
77
78 =head2 ignore_schema
79
80 Set to true to make L</schema> and L</sql_name> not use the defined L</schema>.
81 Does not affect L</dbic_name> (for
82 L<qualify_objects|DBIx::Class::Schema::Loader::Base/qualify_objects> testing on
83 SQLite.)
84
85 =cut
86
87 sub schema {
88     my $self = shift;
89
90     return $self->_schema(@_) unless $self->ignore_schema;
91
92     return undef;
93 }
94
95 sub _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
112 Returns the properly quoted full identifier with L</schema> and L</name>.
113
114 =cut
115
116 sub 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
132 Returns a value suitable for the C<< __PACKAGE__->table >> call in L<DBIx::Class> Result files.
133
134 =cut
135
136 sub 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
158 L<DBIx::Class::Schema::Loader::Table>, L<DBIx::Class::Schema::Loader>,
159 L<DBIx::Class::Schema::Loader::Base>
160
161 =head1 AUTHOR
162
163 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
164
165 =head1 LICENSE
166
167 This library is free software; you can redistribute it and/or modify it under
168 the same terms as Perl itself.
169
170 =cut
171
172 1;
173 # vim:et sts=4 sw=4 tw=0: