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