fix defaults broken by last Pg defaults fix commit
[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
59=head2 schema
60
61The schema (or owner) of the object. Returns nothing if L</ignore_schema> is
62true.
63
64=head2 ignore_schema
65
66Set to true to make L</schema> and L</sql_name> not use the defined L</schema>.
67Does not affect L</dbic_name> (for
68L<qualify_objects|DBIx::Class::Schema::Loader::Base/qualify_objects> testing on
69SQLite.)
70
71=cut
72
73sub schema {
74 my $self = shift;
75
76 return $self->_schema(@_) unless $self->ignore_schema;
77
78 return undef;
79}
80
81sub _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
98Returns the properly quoted full identifier with L</schema> and L</name>.
99
100=cut
101
102sub 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
118Returns a value suitable for the C<< __PACKAGE__->table >> call in L<DBIx::Class> Result files.
119
120=cut
121
122sub 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
144L<DBIx::Class::Schema::Loader::Table>, L<DBIx::Class::Schema::Loader>,
145L<DBIx::Class::Schema::Loader::Base>
146
147=head1 AUTHOR
148
149See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
150
151=head1 LICENSE
152
153This library is free software; you can redistribute it and/or modify it under
154the same terms as Perl itself.
155
156=cut
157
1581;
159# vim:et sts=4 sw=4 tw=0: