| 1 | package DBIO::Firebird::Test::Conformance; | ||
| 2 | # ABSTRACT: Answers the shared conformance case table for the Firebird driver | ||
| 3 | |||
| 4 | 5 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 5 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | our $VERSION = '0.900007'; | ||
| 8 | |||
| 9 | 5 | ▶ | use base 'DBIO::Test::Conformance'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 5 | ▶ | use DBIO::Exception (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 0 | ▶ | use DBIO::Firebird::Deploy (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | use DBIO::Firebird::DDL (); | ||
| 14 | use DBIO::Firebird::Introspect (); | ||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | sub conformance_live_available { | ||
| 19 | return 0 unless length($ENV{DBIO_TEST_FIREBIRD_DSN} // ''); | ||
| 20 | return eval { require DBD::Firebird; 1 } ? 1 : 0; | ||
| 21 | } | ||
| 22 | |||
| 23 | |||
| 24 | sub conformance_connect { | ||
| 25 | my ($class, $schema_class) = @_; | ||
| 26 | $schema_class->load_components('Firebird'); | ||
| 27 | return $schema_class->connect( | ||
| 28 | @ENV{qw(DBIO_TEST_FIREBIRD_DSN DBIO_TEST_FIREBIRD_USER DBIO_TEST_FIREBIRD_PASS)}, | ||
| 29 | { quote_names => 1, on_connect_call => [ 'use_softcommit' ], | ||
| 30 | AutoCommit => 1, RaiseError => 1, PrintError => 0, PrintWarn => 0, | ||
| 31 | LongReadLen => 1_000_000 }, | ||
| 32 | ); | ||
| 33 | } | ||
| 34 | |||
| 35 | # A connection of the conformance run's own, apart from the case's. | ||
| 36 | sub _own_dbh { | ||
| 37 | require DBI; | ||
| 38 | return DBI->connect( | ||
| 39 | @ENV{qw(DBIO_TEST_FIREBIRD_DSN DBIO_TEST_FIREBIRD_USER DBIO_TEST_FIREBIRD_PASS)}, | ||
| 40 | { RaiseError => 1, PrintError => 0, PrintWarn => 0, AutoCommit => 1, | ||
| 41 | ib_dialect => 3 }, | ||
| 42 | ); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub _trim { my ($s) = @_; $s =~ s/\s+\z// if defined $s; return $s } | ||
| 46 | |||
| 47 | # [ [ name, 'TABLE' | 'VIEW' ], ... ] of the user relations | ||
| 48 | sub _relations { | ||
| 49 | my ($dbh) = @_; | ||
| 50 | my $rows = $dbh->selectall_arrayref( | ||
| 51 | 'SELECT rdb$relation_name, rdb$view_blr FROM rdb$relations ' | ||
| 52 | . 'WHERE COALESCE(rdb$system_flag, 0) = 0'); | ||
| 53 | return [ map { [ _trim($_->[0]), defined $_->[1] ? 'VIEW' : 'TABLE' ] } @$rows ]; | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | sub conformance_namespace { | ||
| 58 | my ($class, $schema, $case) = @_; | ||
| 59 | my $dbh = _own_dbh(); | ||
| 60 | my $there = eval { _relations($dbh) }; | ||
| 61 | my $err = $@; | ||
| 62 | $dbh->disconnect; | ||
| 63 | DBIO::Exception->throw("conformance: cannot look at the database: $err") if $err; | ||
| 64 | DBIO::Exception->throw('conformance: the database holds ' | ||
| 65 | . join(', ', map { "$_->[1] $_->[0]" } @$there) | ||
| 66 | . ' already; a case needs an empty database and it is not the run\'s to drop them') | ||
| 67 | if @$there; | ||
| 68 | |||
| 69 | my (%tables, %views); | ||
| 70 | for my $source_name ($schema->sources) { | ||
| 71 | my $source = $schema->source($source_name); | ||
| 72 | my $name = $source->name; | ||
| 73 | next if ref $name; | ||
| 74 | if ($source->isa('DBIO::ResultSource::View')) { $views{$name} = 1 } | ||
| 75 | else { $tables{$name} = 1 } | ||
| 76 | } | ||
| 77 | # A Diff case upgrades to a second declaration, whose sources create more | ||
| 78 | # objects: the cleanup has to find them as well. | ||
| 79 | for my $spec (@{ $case->{input}{to}{sources} || [] }) { | ||
| 80 | next if !defined $spec->{table} or ref $spec->{table}; | ||
| 81 | if ($spec->{view}) { $views{ $spec->{table} } = 1 } | ||
| 82 | else { $tables{ $spec->{table} } = 1 } | ||
| 83 | } | ||
| 84 | return { tables => [ sort keys %tables ], views => [ sort keys %views ] }; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | sub conformance_install { | ||
| 89 | my ($class, $schema, $case) = @_; | ||
| 90 | $schema->deploy; | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | sub conformance_cleanup { | ||
| 96 | my ($class, $schema, $case, $namespace) = @_; | ||
| 97 | eval { $schema->storage->disconnect }; | ||
| 98 | |||
| 99 | return unless ref $namespace eq 'HASH'; | ||
| 100 | my %want_table = map { $_ => 1 } @{ $namespace->{tables} || [] }; | ||
| 101 | my %want_view = map { $_ => 1 } @{ $namespace->{views} || [] }; | ||
| 102 | return unless %want_table or %want_view; | ||
| 103 | |||
| 104 | my $dbh = _own_dbh(); | ||
| 105 | my $ok = eval { | ||
| 106 | my @rel = @{ _relations($dbh) }; | ||
| 107 | my @views = grep { $_->[1] eq 'VIEW' and $want_view{ $_->[0] } } @rel; | ||
| 108 | my @tables = grep { $_->[1] eq 'TABLE' and $want_table{ $_->[0] } } @rel; | ||
| 109 | |||
| 110 | my $fks = $dbh->selectall_arrayref( | ||
| 111 | 'SELECT rdb$relation_name, rdb$constraint_name FROM rdb$relation_constraints ' | ||
| 112 | . q{WHERE rdb$constraint_type = 'FOREIGN KEY'}); | ||
| 113 | for my $fk (@$fks) { | ||
| 114 | my ($table, $constraint) = map { _trim($_) } @$fk; | ||
| 115 | next unless $want_table{$table}; | ||
| 116 | $dbh->do(sprintf 'ALTER TABLE %s DROP CONSTRAINT %s', | ||
| 117 | map { DBIO::Firebird::DDL::_firebird_quote_ident($_) } $table, $constraint); | ||
| 118 | } | ||
| 119 | $dbh->do('DROP VIEW ' . DBIO::Firebird::DDL::_firebird_quote_ident($_->[0])) | ||
| 120 | for @views; | ||
| 121 | $dbh->do('DROP TABLE ' . DBIO::Firebird::DDL::_firebird_quote_ident($_->[0])) | ||
| 122 | for @tables; | ||
| 123 | 1; | ||
| 124 | }; | ||
| 125 | my $err = $@; | ||
| 126 | $dbh->disconnect; | ||
| 127 | DBIO::Exception->throw("conformance: cleanup failed: $err") unless $ok; | ||
| 128 | return; | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | sub conformance_diff_is_empty { | ||
| 133 | my ($class, $schema, $case) = @_; | ||
| 134 | my $diff = DBIO::Firebird::Deploy->new(schema => $schema)->diff; | ||
| 135 | return $diff->has_changes ? 0 : 1; | ||
| 136 | } | ||
| 137 | |||
| 138 | |||
| 139 | sub conformance_upgrade { | ||
| 140 | my ($class, $schema, $to_class, $case) = @_; | ||
| 141 | # The case's own connection holds a transaction and statements on its | ||
| 142 | # tables; the DDL of the upgrade and the DROP TABLE of the cleanup (each on | ||
| 143 | # a connection of their own) wait for them for ever. Nothing reads through | ||
| 144 | # the old schema once the upgrade starts. | ||
| 145 | eval { $schema->storage->disconnect }; | ||
| 146 | my $to = $class->conformance_connect($to_class); | ||
| 147 | DBIO::Firebird::Deploy->new(schema => $to)->upgrade(allow_destructive => 1); | ||
| 148 | eval { $to->storage->disconnect }; | ||
| 149 | return $to; | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | sub conformance_deploy { | ||
| 154 | my ($class, $schema, $to_class, $case) = @_; | ||
| 155 | eval { $schema->storage->disconnect }; | ||
| 156 | my $to = $class->conformance_connect($to_class); | ||
| 157 | return DBIO::Firebird::Deploy->new(schema => $to); | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | sub conformance_answered_through { { DDL => 2, Diff => 4, Bind => 1 } } | ||
| 162 | |||
| 163 | |||
| 164 | sub conformance_refusals { | ||
| 165 | return { | ||
| 166 | 'diff-column-added-not-null-populated' => { | ||
| 167 | kind => 'server', | ||
| 168 | reason => qr/added NOT NULL without a default, and the table holds rows/, | ||
| 169 | }, | ||
| 170 | 'diff-not-null-over-nulls' => { | ||
| 171 | kind => 'server', | ||
| 172 | reason => qr/becomes NOT NULL, and the table holds rows where it is NULL/, | ||
| 173 | }, | ||
| 174 | 'bind-long-string' => { | ||
| 175 | kind => 'server', | ||
| 176 | reason => qr/compared as a VARCHAR, which holds 8191 characters at most \(32765 bytes/, | ||
| 177 | }, | ||
| 178 | 'bind-numeric-exact' => { | ||
| 179 | kind => 'server', | ||
| 180 | reason => qr/more than 18 digits is an INT128.*cannot bind or read an INT128/s, | ||
| 181 | }, | ||
| 182 | 'ddl-type-size-bit' => { | ||
| 183 | kind => 'server', | ||
| 184 | reason => qr/no type for a bit string of a fixed width/, | ||
| 185 | }, | ||
| 186 | 'ddl-enum-value-apostrophe' => { | ||
| 187 | kind => 'server', | ||
| 188 | reason => qr/no enumerated type/, | ||
| 189 | }, | ||
| 190 | 'ddl-qualified-source-name' => { | ||
| 191 | kind => 'server', | ||
| 192 | reason => qr/no schemas before version 6/, | ||
| 193 | }, | ||
| 194 | 'ddl-default-digit-string' => { | ||
| 195 | kind => 'deviation', | ||
| 196 | fact => qr/expected '007', read back '007 '/, | ||
| 197 | reason => 'Firebird pads a CHAR(n) value with blanks to its length; that is the type\'s definition and the driver does not trim', | ||
| 198 | }, | ||
| 199 | }; | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | sub conformance_fact { | ||
| 204 | my ($class, $schema, $name, $args) = @_; | ||
| 205 | my $table = $args->{table}; | ||
| 206 | my $dbh = $schema->storage->dbh; | ||
| 207 | |||
| 208 | # Firebird has no schemas: no table is in any namespace but the database's | ||
| 209 | return 0 if $name eq 'table_in_namespace'; | ||
| 210 | |||
| 211 | my $model = DBIO::Firebird::Introspect->new(dbh => $dbh)->model; | ||
| 212 | my $entry = $model->{tables}{$table}; | ||
| 213 | my @cols = @{ $args->{columns} || [] }; | ||
| 214 | my $columns = $model->{columns}{$table} || []; | ||
| 215 | |||
| 216 | if ($name eq 'object_kind') { | ||
| 217 | return undef unless $entry; | ||
| 218 | return $entry->{kind} eq 'view' ? 'view' : 'table'; | ||
| 219 | } | ||
| 220 | if ($name eq 'table_name') { | ||
| 221 | return $entry ? $entry->{table_name} : undef; | ||
| 222 | } | ||
| 223 | if ($name eq 'column_names') { | ||
| 224 | return [ map { $_->{column_name} } @$columns ]; | ||
| 225 | } | ||
| 226 | if ($name eq 'column_size') { | ||
| 227 | my ($col) = grep { $_->{column_name} eq $args->{column} } @$columns; | ||
| 228 | return undef unless $col; | ||
| 229 | return ref $col->{size} ? $col->{size}[0] : $col->{size}; | ||
| 230 | } | ||
| 231 | if ($name eq 'column_auto_increment') { | ||
| 232 | my ($col) = grep { $_->{column_name} eq $args->{column} } @$columns; | ||
| 233 | return $col && $col->{is_auto_increment} ? 1 : 0; | ||
| 234 | } | ||
| 235 | if ($name eq 'primary_key_columns') { | ||
| 236 | return [ map { $_->{column_name} } | ||
| 237 | sort { $a->{pk_position} <=> $b->{pk_position} } | ||
| 238 | grep { $_->{is_pk} } @$columns ]; | ||
| 239 | } | ||
| 240 | if ($name eq 'unique_exists') { | ||
| 241 | my $want = join "\0", @cols; | ||
| 242 | return (grep { join("\0", @{ $_->[1] }) eq $want } | ||
| 243 | @{ $model->{unique_constraints}{$table} || [] }) ? 1 : 0; | ||
| 244 | } | ||
| 245 | if ($name eq 'foreign_key_count') { | ||
| 246 | return scalar @{ $model->{foreign_keys}{$table} || [] }; | ||
| 247 | } | ||
| 248 | if ($name eq 'index_count') { | ||
| 249 | # every index of the table over exactly these columns, the ones behind a | ||
| 250 | # unique constraint or a foreign key included, the primary key's excluded | ||
| 251 | my $rows = $dbh->selectall_arrayref(q{ | ||
| 252 | SELECT i.rdb$index_name, s.rdb$field_name | ||
| 253 | FROM rdb$indices i | ||
| 254 | JOIN rdb$index_segments s ON s.rdb$index_name = i.rdb$index_name | ||
| 255 | WHERE i.rdb$relation_name = ? | ||
| 256 | AND NOT EXISTS (SELECT 1 FROM rdb$relation_constraints c | ||
| 257 | WHERE c.rdb$index_name = i.rdb$index_name | ||
| 258 | AND c.rdb$constraint_type = 'PRIMARY KEY') | ||
| 259 | ORDER BY i.rdb$index_name, s.rdb$field_position | ||
| 260 | }, undef, $table); | ||
| 261 | my (%by_index, @order); | ||
| 262 | for my $r (@$rows) { | ||
| 263 | my $i = _trim($r->[0]); | ||
| 264 | push @order, $i unless $by_index{$i}; | ||
| 265 | push @{ $by_index{$i} }, _trim($r->[1]); | ||
| 266 | } | ||
| 267 | my $want = join "\0", @cols; | ||
| 268 | return scalar grep { join("\0", @{ $by_index{$_} }) eq $want } @order; | ||
| 269 | } | ||
| 270 | |||
| 271 | DBIO::Exception->throw("unknown conformance fact '$name'"); | ||
| 272 | } | ||
| 273 | |||
| 274 | 1; | ||
| 275 | |||
| 276 | __END__ | ||