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