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