Commit | Line | Data |
8793567f |
1 | package DBIx::Class::Schema::Loader::DBI::SQLAnywhere; |
2 | |
3 | use strict; |
4 | use warnings; |
8793567f |
5 | use base qw/ |
6 | DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault |
7 | DBIx::Class::Schema::Loader::DBI |
8 | /; |
c4a69b87 |
9 | use mro 'c3'; |
10 | use List::MoreUtils 'any'; |
11 | use namespace::clean; |
8793567f |
12 | |
4295c4b4 |
13 | our $VERSION = '0.07010'; |
8793567f |
14 | |
15 | =head1 NAME |
16 | |
17 | DBIx::Class::Schema::Loader::DBI::SQLAnywhere - DBIx::Class::Schema::Loader::DBI |
18 | SQL Anywhere Implementation. |
19 | |
20 | =head1 DESCRIPTION |
21 | |
bc5afe55 |
22 | See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>. |
8793567f |
23 | |
24 | =cut |
25 | |
c4a69b87 |
26 | sub _system_schemas { |
27 | return (qw/dbo SYS diagnostics rs_systabgroup SA_DEBUG/); |
28 | } |
29 | |
d6a0cc27 |
30 | sub _setup { |
31 | my $self = shift; |
32 | |
bc1cb85e |
33 | $self->next::method(@_); |
34 | |
c4a69b87 |
35 | $self->preserve_case(1) |
36 | unless defined $self->preserve_case; |
37 | |
38 | $self->schema->storage->sql_maker->quote_char('"'); |
39 | $self->schema->storage->sql_maker->name_sep('.'); |
40 | |
41 | $self->db_schema([($self->dbh->selectrow_array('select user'))[0]]) |
42 | unless $self->db_schema; |
43 | |
44 | if (ref $self->db_schema eq 'ARRAY' && $self->db_schema->[0] eq '%') { |
45 | my @users = grep { my $uname = $_; not any { $_ eq $uname } $self->_system_schemas } |
46 | @{ $self->dbh->selectcol_arrayref('select user_name from sysuser') }; |
47 | |
48 | $self->db_schema(\@users); |
49 | } |
d6a0cc27 |
50 | } |
51 | |
52 | sub _tables_list { |
bfb43060 |
53 | my ($self, $opts) = @_; |
d6a0cc27 |
54 | |
c4a69b87 |
55 | my @tables; |
56 | |
57 | foreach my $schema (@{ $self->db_schema }) { |
58 | my $sth = $self->dbh->prepare(<<'EOF'); |
59 | SELECT t.table_name name |
60 | FROM systab t |
61 | JOIN sysuser u |
62 | ON t.creator = u.user_id |
63 | WHERE u.user_name = ? |
d6a0cc27 |
64 | EOF |
c4a69b87 |
65 | $sth->execute($schema); |
d6a0cc27 |
66 | |
c4a69b87 |
67 | my @table_names = map @$_, @{ $sth->fetchall_arrayref }; |
68 | |
69 | foreach my $table_name (@table_names) { |
70 | push @tables, DBIx::Class::Schema::Loader::Table->new( |
71 | loader => $self, |
72 | name => $table_name, |
73 | schema => $schema, |
74 | ); |
75 | } |
76 | } |
d6a0cc27 |
77 | |
bfb43060 |
78 | return $self->_filter_tables(\@tables, $opts); |
d6a0cc27 |
79 | } |
80 | |
8793567f |
81 | sub _columns_info_for { |
9dc968df |
82 | my $self = shift; |
83 | my ($table) = @_; |
84 | |
8793567f |
85 | my $result = $self->next::method(@_); |
86 | |
9dc968df |
87 | my $dbh = $self->schema->storage->dbh; |
88 | |
dd87d4c4 |
89 | while (my ($col, $info) = each %$result) { |
8793567f |
90 | my $def = $info->{default_value}; |
91 | if (ref $def eq 'SCALAR' && $$def eq 'autoincrement') { |
92 | delete $info->{default_value}; |
93 | $info->{is_auto_increment} = 1; |
94 | } |
9dc968df |
95 | |
c4a69b87 |
96 | my ($user_type) = $dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, $col); |
9dc968df |
97 | SELECT ut.type_name |
98 | FROM systabcol tc |
c4a69b87 |
99 | JOIN systab t |
100 | ON tc.table_id = t.table_id |
101 | JOIN sysuser u |
102 | ON t.creator = u.user_id |
103 | JOIN sysusertype ut |
104 | ON tc.user_type = ut.type_id |
105 | WHERE u.user_name = ? AND t.table_name = ? AND tc.column_name = ? |
9dc968df |
106 | EOF |
107 | $info->{data_type} = $user_type if defined $user_type; |
108 | |
109 | if ($info->{data_type} eq 'double') { |
110 | $info->{data_type} = 'double precision'; |
111 | } |
112 | |
113 | if ($info->{data_type} =~ /^(?:char|varchar|binary|varbinary)\z/ && ref($info->{size}) eq 'ARRAY') { |
114 | $info->{size} = $info->{size}[0]; |
115 | } |
116 | elsif ($info->{data_type} !~ /^(?:char|varchar|binary|varbinary|numeric|decimal)\z/) { |
117 | delete $info->{size}; |
118 | } |
119 | |
120 | my $sth = $dbh->prepare(<<'EOF'); |
121 | SELECT tc.width, tc.scale |
122 | FROM systabcol tc |
c4a69b87 |
123 | JOIN systab t |
124 | ON t.table_id = tc.table_id |
125 | JOIN sysuser u |
126 | ON t.creator = u.user_id |
127 | WHERE u.user_name = ? AND t.table_name = ? AND tc.column_name = ? |
9dc968df |
128 | EOF |
c4a69b87 |
129 | $sth->execute($table->schema, $table->name, $col); |
9dc968df |
130 | my ($width, $scale) = $sth->fetchrow_array; |
131 | $sth->finish; |
132 | |
133 | if ($info->{data_type} =~ /^(?:numeric|decimal)\z/) { |
134 | # We do not check for the default precision/scale, because they can be changed as PUBLIC database options. |
135 | $info->{size} = [$width, $scale]; |
136 | } |
137 | elsif ($info->{data_type} =~ /^(?:n(?:varchar|char) | varbit)\z/x) { |
138 | $info->{size} = $width; |
139 | } |
dd87d4c4 |
140 | elsif ($info->{data_type} eq 'float') { |
141 | $info->{data_type} = 'real'; |
142 | } |
9dc968df |
143 | |
144 | delete $info->{default_value} if ref($info->{default_value}) eq 'SCALAR' && ${ $info->{default_value} } eq 'NULL'; |
8a64178e |
145 | |
268cc246 |
146 | if ((eval { lc ${ $info->{default_value} } }||'') eq 'current timestamp') { |
6e566cc4 |
147 | ${ $info->{default_value} } = 'current_timestamp'; |
701cd3e3 |
148 | |
149 | my $orig_deflt = 'current timestamp'; |
150 | $info->{original}{default_value} = \$orig_deflt; |
8a64178e |
151 | } |
8793567f |
152 | } |
153 | |
154 | return $result; |
155 | } |
156 | |
157 | sub _table_pk_info { |
158 | my ($self, $table) = @_; |
c4a69b87 |
159 | local $self->dbh->{FetchHashKeyName} = 'NAME_lc'; |
160 | my $sth = $self->dbh->prepare(qq{sp_pkeys ?, ?}); |
161 | $sth->execute($table->name, $table->schema); |
8793567f |
162 | |
163 | my @keydata; |
164 | |
165 | while (my $row = $sth->fetchrow_hashref) { |
dd87d4c4 |
166 | push @keydata, $self->_lc($row->{column_name}); |
8793567f |
167 | } |
168 | |
169 | return \@keydata; |
170 | } |
171 | |
172 | sub _table_fk_info { |
173 | my ($self, $table) = @_; |
174 | |
175 | my ($local_cols, $remote_cols, $remote_table, @rels); |
c4a69b87 |
176 | my $sth = $self->dbh->prepare(<<'EOF'); |
177 | SELECT fki.index_name fk_name, fktc.column_name local_column, pku.user_name remote_schema, pkt.table_name remote_table, pktc.column_name remote_column |
178 | FROM sysfkey fk |
179 | JOIN systab pkt |
180 | ON fk.primary_table_id = pkt.table_id |
181 | JOIN sysuser pku |
182 | ON pkt.creator = pku.user_id |
183 | JOIN systab fkt |
184 | ON fk.foreign_table_id = fkt.table_id |
185 | JOIN sysuser fku |
186 | ON fkt.creator = fku.user_id |
187 | JOIN sysidx pki |
188 | ON fk.primary_table_id = pki.table_id AND fk.primary_index_id = pki.index_id |
189 | JOIN sysidx fki |
190 | ON fk.foreign_table_id = fki.table_id AND fk.foreign_index_id = fki.index_id |
191 | JOIN sysidxcol fkic |
192 | ON fkt.table_id = fkic.table_id AND fki.index_id = fkic.index_id |
193 | JOIN systabcol pktc |
194 | ON pkt.table_id = pktc.table_id AND fkic.primary_column_id = pktc.column_id |
195 | JOIN systabcol fktc |
196 | ON fkt.table_id = fktc.table_id AND fkic.column_id = fktc.column_id |
197 | WHERE fku.user_name = ? AND fkt.table_name = ? |
8793567f |
198 | EOF |
c4a69b87 |
199 | $sth->execute($table->schema, $table->name); |
8793567f |
200 | |
c4a69b87 |
201 | while (my ($fk, $local_col, $remote_schema, $remote_tab, $remote_col) = $sth->fetchrow_array) { |
dd87d4c4 |
202 | push @{$local_cols->{$fk}}, $self->_lc($local_col); |
203 | push @{$remote_cols->{$fk}}, $self->_lc($remote_col); |
c4a69b87 |
204 | $remote_table->{$fk} = DBIx::Class::Schema::Loader::Table->new( |
205 | loader => $self, |
206 | name => $remote_tab, |
207 | schema => $remote_schema, |
208 | ); |
8793567f |
209 | } |
210 | |
211 | foreach my $fk (keys %$remote_table) { |
212 | push @rels, { |
ed577901 |
213 | local_columns => $local_cols->{$fk}, |
214 | remote_columns => $remote_cols->{$fk}, |
8793567f |
215 | remote_table => $remote_table->{$fk}, |
216 | }; |
217 | } |
218 | return \@rels; |
219 | } |
220 | |
221 | sub _table_uniq_info { |
222 | my ($self, $table) = @_; |
223 | |
c4a69b87 |
224 | my $sth = $self->dbh->prepare(<<'EOF'); |
225 | SELECT c.constraint_name, tc.column_name |
226 | FROM sysconstraint c |
227 | JOIN systab t |
228 | ON c.table_object_id = t.object_id |
229 | JOIN sysuser u |
230 | ON t.creator = u.user_id |
231 | JOIN sysidx i |
232 | ON c.ref_object_id = i.object_id |
233 | JOIN sysidxcol ic |
234 | ON i.table_id = ic.table_id AND i.index_id = ic.index_id |
235 | JOIN systabcol tc |
236 | ON ic.table_id = tc.table_id AND ic.column_id = tc.column_id |
237 | WHERE c.constraint_type = 'U' AND u.user_name = ? AND t.table_name = ? |
8793567f |
238 | EOF |
c4a69b87 |
239 | $sth->execute($table->schema, $table->name); |
8793567f |
240 | |
241 | my $constraints; |
242 | while (my ($constraint_name, $column) = $sth->fetchrow_array) { |
dd87d4c4 |
243 | push @{$constraints->{$constraint_name}}, $self->_lc($column); |
8793567f |
244 | } |
245 | |
246 | my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints; |
247 | return \@uniqs; |
248 | } |
249 | |
250 | =head1 SEE ALSO |
251 | |
252 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
253 | L<DBIx::Class::Schema::Loader::DBI> |
254 | |
255 | =head1 AUTHOR |
256 | |
257 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
258 | |
259 | =head1 LICENSE |
260 | |
261 | This library is free software; you can redistribute it and/or modify it under |
262 | the same terms as Perl itself. |
263 | |
264 | =cut |
265 | |
266 | 1; |
9dc968df |
267 | # vim:et sw=4 sts=4 tw=0: |