1 package DBIx::Class::Schema::Loader::DBI::SQLAnywhere;
6 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7 DBIx::Class::Schema::Loader::DBI
10 use List::MoreUtils 'any';
13 our $VERSION = '0.07010';
17 DBIx::Class::Schema::Loader::DBI::SQLAnywhere - DBIx::Class::Schema::Loader::DBI
18 SQL Anywhere Implementation.
22 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
27 return (qw/dbo SYS diagnostics rs_systabgroup SA_DEBUG/);
33 $self->next::method(@_);
35 $self->preserve_case(1)
36 unless defined $self->preserve_case;
38 $self->schema->storage->sql_maker->quote_char('"');
39 $self->schema->storage->sql_maker->name_sep('.');
41 $self->db_schema([($self->dbh->selectrow_array('select user'))[0]])
42 unless $self->db_schema;
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') };
48 $self->db_schema(\@users);
53 my ($self, $opts) = @_;
57 foreach my $schema (@{ $self->db_schema }) {
58 my $sth = $self->dbh->prepare(<<'EOF');
59 SELECT t.table_name name
62 ON t.creator = u.user_id
65 $sth->execute($schema);
67 my @table_names = map @$_, @{ $sth->fetchall_arrayref };
69 foreach my $table_name (@table_names) {
70 push @tables, DBIx::Class::Schema::Loader::Table->new(
78 return $self->_filter_tables(\@tables, $opts);
81 sub _columns_info_for {
85 my $result = $self->next::method(@_);
87 my $dbh = $self->schema->storage->dbh;
89 while (my ($col, $info) = each %$result) {
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;
96 my ($user_type) = $dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, $col);
100 ON tc.table_id = t.table_id
102 ON t.creator = u.user_id
104 ON tc.user_type = ut.type_id
105 WHERE u.user_name = ? AND t.table_name = ? AND tc.column_name = ?
107 $info->{data_type} = $user_type if defined $user_type;
109 if ($info->{data_type} eq 'double') {
110 $info->{data_type} = 'double precision';
113 if ($info->{data_type} =~ /^(?:char|varchar|binary|varbinary)\z/ && ref($info->{size}) eq 'ARRAY') {
114 $info->{size} = $info->{size}[0];
116 elsif ($info->{data_type} !~ /^(?:char|varchar|binary|varbinary|numeric|decimal)\z/) {
117 delete $info->{size};
120 my $sth = $dbh->prepare(<<'EOF');
121 SELECT tc.width, tc.scale
124 ON t.table_id = tc.table_id
126 ON t.creator = u.user_id
127 WHERE u.user_name = ? AND t.table_name = ? AND tc.column_name = ?
129 $sth->execute($table->schema, $table->name, $col);
130 my ($width, $scale) = $sth->fetchrow_array;
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];
137 elsif ($info->{data_type} =~ /^(?:n(?:varchar|char) | varbit)\z/x) {
138 $info->{size} = $width;
140 elsif ($info->{data_type} eq 'float') {
141 $info->{data_type} = 'real';
144 delete $info->{default_value} if ref($info->{default_value}) eq 'SCALAR' && ${ $info->{default_value} } eq 'NULL';
146 if ((eval { lc ${ $info->{default_value} } }||'') eq 'current timestamp') {
147 ${ $info->{default_value} } = 'current_timestamp';
149 my $orig_deflt = 'current timestamp';
150 $info->{original}{default_value} = \$orig_deflt;
158 my ($self, $table) = @_;
159 local $self->dbh->{FetchHashKeyName} = 'NAME_lc';
160 my $sth = $self->dbh->prepare(qq{sp_pkeys ?, ?});
161 $sth->execute($table->name, $table->schema);
165 while (my $row = $sth->fetchrow_hashref) {
166 push @keydata, $self->_lc($row->{column_name});
173 my ($self, $table) = @_;
175 my ($local_cols, $remote_cols, $remote_table, @rels);
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
180 ON fk.primary_table_id = pkt.table_id
182 ON pkt.creator = pku.user_id
184 ON fk.foreign_table_id = fkt.table_id
186 ON fkt.creator = fku.user_id
188 ON fk.primary_table_id = pki.table_id AND fk.primary_index_id = pki.index_id
190 ON fk.foreign_table_id = fki.table_id AND fk.foreign_index_id = fki.index_id
192 ON fkt.table_id = fkic.table_id AND fki.index_id = fkic.index_id
194 ON pkt.table_id = pktc.table_id AND fkic.primary_column_id = pktc.column_id
196 ON fkt.table_id = fktc.table_id AND fkic.column_id = fktc.column_id
197 WHERE fku.user_name = ? AND fkt.table_name = ?
199 $sth->execute($table->schema, $table->name);
201 while (my ($fk, $local_col, $remote_schema, $remote_tab, $remote_col) = $sth->fetchrow_array) {
202 push @{$local_cols->{$fk}}, $self->_lc($local_col);
203 push @{$remote_cols->{$fk}}, $self->_lc($remote_col);
204 $remote_table->{$fk} = DBIx::Class::Schema::Loader::Table->new(
207 schema => $remote_schema,
211 foreach my $fk (keys %$remote_table) {
213 local_columns => $local_cols->{$fk},
214 remote_columns => $remote_cols->{$fk},
215 remote_table => $remote_table->{$fk},
221 sub _table_uniq_info {
222 my ($self, $table) = @_;
224 my $sth = $self->dbh->prepare(<<'EOF');
225 SELECT c.constraint_name, tc.column_name
228 ON c.table_object_id = t.object_id
230 ON t.creator = u.user_id
232 ON c.ref_object_id = i.object_id
234 ON i.table_id = ic.table_id AND i.index_id = ic.index_id
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 = ?
239 $sth->execute($table->schema, $table->name);
242 while (my ($constraint_name, $column) = $sth->fetchrow_array) {
243 push @{$constraints->{$constraint_name}}, $self->_lc($column);
246 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
252 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
253 L<DBIx::Class::Schema::Loader::DBI>
257 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
261 This library is free software; you can redistribute it and/or modify it under
262 the same terms as Perl itself.
267 # vim:et sw=4 sts=4 tw=0: