1 package DBIx::Class::Schema::Loader::DBI::SQLAnywhere;
5 use base 'DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault';
7 use List::MoreUtils 'any';
9 use DBIx::Class::Schema::Loader::Table ();
11 our $VERSION = '0.07040';
15 DBIx::Class::Schema::Loader::DBI::SQLAnywhere - DBIx::Class::Schema::Loader::DBI
16 SQL Anywhere Implementation.
20 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
25 return (qw/dbo SYS diagnostics rs_systabgroup SA_DEBUG/);
31 $self->next::method(@_);
33 $self->preserve_case(1)
34 unless defined $self->preserve_case;
36 $self->schema->storage->sql_maker->quote_char('"');
37 $self->schema->storage->sql_maker->name_sep('.');
39 $self->db_schema([($self->dbh->selectrow_array('select user'))[0]])
40 unless $self->db_schema;
42 if (ref $self->db_schema eq 'ARRAY' && $self->db_schema->[0] eq '%') {
43 my @users = grep { my $uname = $_; not any { $_ eq $uname } $self->_system_schemas }
44 @{ $self->dbh->selectcol_arrayref('select user_name from sysuser') };
46 $self->db_schema(\@users);
51 my ($self, $opts) = @_;
55 foreach my $schema (@{ $self->db_schema }) {
56 my $sth = $self->dbh->prepare(<<'EOF');
57 SELECT t.table_name name
60 ON t.creator = u.user_id
63 $sth->execute($schema);
65 my @table_names = map @$_, @{ $sth->fetchall_arrayref };
67 foreach my $table_name (@table_names) {
68 push @tables, DBIx::Class::Schema::Loader::Table->new(
76 return $self->_filter_tables(\@tables, $opts);
79 sub _columns_info_for {
83 my $result = $self->next::method(@_);
85 my $dbh = $self->schema->storage->dbh;
87 while (my ($col, $info) = each %$result) {
88 my $def = $info->{default_value};
89 if (ref $def eq 'SCALAR' && $$def eq 'autoincrement') {
90 delete $info->{default_value};
91 $info->{is_auto_increment} = 1;
94 my ($user_type) = $dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, lc($col));
98 ON tc.table_id = t.table_id
100 ON t.creator = u.user_id
102 ON tc.user_type = ut.type_id
103 WHERE u.user_name = ? AND t.table_name = ? AND lower(tc.column_name) = ?
105 $info->{data_type} = $user_type if defined $user_type;
107 if ($info->{data_type} eq 'double') {
108 $info->{data_type} = 'double precision';
111 if ($info->{data_type} =~ /^(?:char|varchar|binary|varbinary)\z/ && ref($info->{size}) eq 'ARRAY') {
112 $info->{size} = $info->{size}[0];
114 elsif ($info->{data_type} !~ /^(?:char|varchar|binary|varbinary|numeric|decimal)\z/) {
115 delete $info->{size};
118 my $sth = $dbh->prepare(<<'EOF');
119 SELECT tc.width, tc.scale
122 ON t.table_id = tc.table_id
124 ON t.creator = u.user_id
125 WHERE u.user_name = ? AND t.table_name = ? AND lower(tc.column_name) = ?
127 $sth->execute($table->schema, $table->name, lc($col));
128 my ($width, $scale) = $sth->fetchrow_array;
131 if ($info->{data_type} =~ /^(?:numeric|decimal)\z/) {
132 # We do not check for the default precision/scale, because they can be changed as PUBLIC database options.
133 $info->{size} = [$width, $scale];
135 elsif ($info->{data_type} =~ /^(?:n(?:varchar|char) | varbit)\z/x) {
136 $info->{size} = $width;
138 elsif ($info->{data_type} eq 'float') {
139 $info->{data_type} = 'real';
142 if ((eval { lc ${ $info->{default_value} } }||'') eq 'current timestamp') {
143 ${ $info->{default_value} } = 'current_timestamp';
145 my $orig_deflt = 'current timestamp';
146 $info->{original}{default_value} = \$orig_deflt;
154 my ($self, $table) = @_;
155 local $self->dbh->{FetchHashKeyName} = 'NAME_lc';
156 my $sth = $self->dbh->prepare(qq{sp_pkeys ?, ?});
157 $sth->execute($table->name, $table->schema);
161 while (my $row = $sth->fetchrow_hashref) {
162 push @keydata, $self->_lc($row->{column_name});
176 my ($self, $table) = @_;
178 my ($local_cols, $remote_cols, $remote_table, $attrs, @rels);
179 my $sth = $self->dbh->prepare(<<'EOF');
180 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, on_delete.referential_action, on_update.referential_action
183 select foreign_table_id, foreign_index_id,
184 row_number() over (partition by foreign_table_id order by foreign_index_id) foreign_key_num
187 ON fkid.foreign_table_id = fk.foreign_table_id and fkid.foreign_index_id = fk.foreign_index_id
189 ON fk.primary_table_id = pkt.table_id
191 ON pkt.creator = pku.user_id
193 ON fk.foreign_table_id = fkt.table_id
195 ON fkt.creator = fku.user_id
197 ON fk.primary_table_id = pki.table_id AND fk.primary_index_id = pki.index_id
199 ON fk.foreign_table_id = fki.table_id AND fk.foreign_index_id = fki.index_id
201 ON fkt.table_id = fkic.table_id AND fki.index_id = fkic.index_id
203 ON pkt.table_id = pktc.table_id AND fkic.primary_column_id = pktc.column_id
205 ON fkt.table_id = fktc.table_id AND fkic.column_id = fktc.column_id
206 LEFT JOIN systrigger on_delete
207 ON on_delete.foreign_table_id = fkt.table_id AND on_delete.foreign_key_id = fkid.foreign_key_num
208 AND on_delete.event = 'D'
209 LEFT JOIN systrigger on_update
210 ON on_update.foreign_table_id = fkt.table_id AND on_update.foreign_key_id = fkid.foreign_key_num
211 AND on_update.event = 'C'
212 WHERE fku.user_name = ? AND fkt.table_name = ?
213 ORDER BY fk.primary_table_id, pktc.column_id
215 $sth->execute($table->schema, $table->name);
217 while (my ($fk, $local_col, $remote_schema, $remote_tab, $remote_col, $on_delete, $on_update)
218 = $sth->fetchrow_array) {
220 push @{$local_cols->{$fk}}, $self->_lc($local_col);
222 push @{$remote_cols->{$fk}}, $self->_lc($remote_col);
224 $remote_table->{$fk} = DBIx::Class::Schema::Loader::Table->new(
227 schema => $remote_schema,
231 on_delete => $sqlany_rules{$on_delete||''} || 'RESTRICT',
232 on_update => $sqlany_rules{$on_update||''} || 'RESTRICT',
233 # We may be able to use the value of the 'CHECK ON COMMIT' option, as it seems
234 # to be some sort of workaround for lack of deferred constraints. Unclear on
235 # how good of a substitute it is, and it requires the 'RESTRICT' rule. Also it
236 # only works for INSERT and UPDATE, not DELETE. Will get back to this.
241 foreach my $fk (keys %$remote_table) {
243 local_columns => $local_cols->{$fk},
244 remote_columns => $remote_cols->{$fk},
245 remote_table => $remote_table->{$fk},
246 attrs => $attrs->{$fk},
252 sub _table_uniq_info {
253 my ($self, $table) = @_;
255 my $sth = $self->dbh->prepare(<<'EOF');
256 SELECT c.constraint_name, tc.column_name
259 ON c.table_object_id = t.object_id
261 ON t.creator = u.user_id
263 ON c.ref_object_id = i.object_id
265 ON i.table_id = ic.table_id AND i.index_id = ic.index_id
267 ON ic.table_id = tc.table_id AND ic.column_id = tc.column_id
268 WHERE c.constraint_type = 'U' AND u.user_name = ? AND t.table_name = ?
270 $sth->execute($table->schema, $table->name);
273 while (my ($constraint_name, $column) = $sth->fetchrow_array) {
274 push @{$constraints->{$constraint_name}}, $self->_lc($column);
277 return [ map { [ $_ => $constraints->{$_} ] } sort keys %$constraints ];
282 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
283 L<DBIx::Class::Schema::Loader::DBI>
287 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
291 This library is free software; you can redistribute it and/or modify it under
292 the same terms as Perl itself.
297 # vim:et sw=4 sts=4 tw=0: