1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2 use DBIx::Class::Optional::Dependencies -skip_all_without => qw( ic_dt test_rdbms_ase );
9 use DBIx::Class::_Util 'scope_guard';
14 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
16 DBICTest::Schema->load_classes('EventSmallDT');
20 'DBI::Sybase::ASE::NoBindVars',
24 for my $storage_type (@storage_types) {
25 $schema = DBICTest::Schema->clone;
27 unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
28 $schema->storage_type("::$storage_type");
30 $schema->connection($dsn, $user, $pass, {
31 on_connect_call => 'datetime_setup',
34 my $guard = scope_guard { cleanup($schema) };
36 $schema->storage->ensure_connected;
38 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
40 eval { $schema->storage->dbh->do("DROP TABLE track") };
41 $schema->storage->dbh->do(<<"SQL");
43 trackid INT IDENTITY PRIMARY KEY,
46 last_updated_at DATETIME NULL
49 eval { $schema->storage->dbh->do("DROP TABLE event_small_dt") };
50 $schema->storage->dbh->do(<<"SQL");
51 CREATE TABLE event_small_dt (
52 id INT IDENTITY PRIMARY KEY,
53 small_dt SMALLDATETIME NULL,
57 # coltype, column, source, pk, create_extra, datehash
71 nanosecond => 500000000,
73 ['SMALLDATETIME', # minute precision
87 for my $dt_type (@dt_types) {
88 my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
90 ok(my $dt = DateTime->new($sample_dt));
93 ok( $row = $schema->resultset($source)->create({
97 ok( $row = $schema->resultset($source)
98 ->search({ $pk => $row->$pk }, { select => [$pk, $col] })
101 is( $row->$col, $dt, "$type roundtrip" );
103 cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
104 'DateTime fractional portion roundtrip' )
105 if exists $sample_dt->{nanosecond};
107 # Testing an ugly half-solution
109 # copy() uses get_columns()
111 # The values should survive a roundtrip also, but they don't
112 # because the Sybase ICDT setup is asymmetric
113 # One *has* to force an inflation/deflation cycle to make the
114 # values usable to the database
116 # This can be done by marking the columns as dirty, and there
117 # are tests for this already in t/inflate/serialize.t
119 # But even this isn't enough - one has to reload the RDBMS-formatted
120 # values once done, otherwise the copy is just as useless... sigh
122 # Adding the test here to validate the technique works
126 local *DBICTest::BaseResult::copy = subname 'DBICTest::BaseResult::copy' => sub {
129 $self->make_column_dirty($_) for keys %{{ $self->get_inflated_columns }};
131 my $cp = $self->next::method(@_);
133 $cp->discard_changes({ columns => [ keys %{{ $cp->get_columns }} ] });
135 Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO;
138 ok( $cp->in_storage );
139 is( $cp->$col, $dt, "$type copy logical roundtrip" );
141 $cp->discard_changes({ select => [ $pk, $col ] });
142 is( $cp->$col, $dt, "$type copy server roundtrip" );
145 Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO;
148 # test a computed datetime column
149 eval { $schema->storage->dbh->do("DROP TABLE track") };
150 $schema->storage->dbh->do(<<"SQL");
152 trackid INT IDENTITY PRIMARY KEY,
155 title VARCHAR(100) NULL,
156 last_updated_on DATETIME NULL,
157 last_updated_at AS getdate(),
161 my $now = DateTime->now;
163 my $new_row = $schema->resultset('Track')->create({});
164 $new_row->discard_changes;
167 cmp_ok (($new_row->last_updated_at - $now)->seconds, '>=', 1)
168 } 'getdate() computed column works';
176 if (my $dbh = eval { $schema->storage->dbh }) {
177 $dbh->do('DROP TABLE track');
178 $dbh->do('DROP TABLE event_small_dt');