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.07021';
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});
169 my ($self, $table) = @_;
171 my ($local_cols, $remote_cols, $remote_table, @rels);
172 my $sth = $self->dbh->prepare(<<'EOF');
173 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
176 ON fk.primary_table_id = pkt.table_id
178 ON pkt.creator = pku.user_id
180 ON fk.foreign_table_id = fkt.table_id
182 ON fkt.creator = fku.user_id
184 ON fk.primary_table_id = pki.table_id AND fk.primary_index_id = pki.index_id
186 ON fk.foreign_table_id = fki.table_id AND fk.foreign_index_id = fki.index_id
188 ON fkt.table_id = fkic.table_id AND fki.index_id = fkic.index_id
190 ON pkt.table_id = pktc.table_id AND fkic.primary_column_id = pktc.column_id
192 ON fkt.table_id = fktc.table_id AND fkic.column_id = fktc.column_id
193 WHERE fku.user_name = ? AND fkt.table_name = ?
195 $sth->execute($table->schema, $table->name);
197 while (my ($fk, $local_col, $remote_schema, $remote_tab, $remote_col) = $sth->fetchrow_array) {
198 push @{$local_cols->{$fk}}, $self->_lc($local_col);
199 push @{$remote_cols->{$fk}}, $self->_lc($remote_col);
200 $remote_table->{$fk} = DBIx::Class::Schema::Loader::Table->new(
203 schema => $remote_schema,
207 foreach my $fk (keys %$remote_table) {
209 local_columns => $local_cols->{$fk},
210 remote_columns => $remote_cols->{$fk},
211 remote_table => $remote_table->{$fk},
217 sub _table_uniq_info {
218 my ($self, $table) = @_;
220 my $sth = $self->dbh->prepare(<<'EOF');
221 SELECT c.constraint_name, tc.column_name
224 ON c.table_object_id = t.object_id
226 ON t.creator = u.user_id
228 ON c.ref_object_id = i.object_id
230 ON i.table_id = ic.table_id AND i.index_id = ic.index_id
232 ON ic.table_id = tc.table_id AND ic.column_id = tc.column_id
233 WHERE c.constraint_type = 'U' AND u.user_name = ? AND t.table_name = ?
235 $sth->execute($table->schema, $table->name);
238 while (my ($constraint_name, $column) = $sth->fetchrow_array) {
239 push @{$constraints->{$constraint_name}}, $self->_lc($column);
242 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
248 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
249 L<DBIx::Class::Schema::Loader::DBI>
253 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
257 This library is free software; you can redistribute it and/or modify it under
258 the same terms as Perl itself.
263 # vim:et sw=4 sts=4 tw=0: