position int,
title varchar(255),
last_updated_on date,
- last_updated_at date,
- small_dt date
+ last_updated_at date
)
EOS
$dbh->do("CREATE TABLE cd (${q}cdid${q} NUMBER(12), ${q}artist${q} NUMBER(12), ${q}title${q} VARCHAR(255), ${q}year${q} VARCHAR(4), ${q}genreid${q} NUMBER(12), ${q}single_track${q} NUMBER(12))");
$dbh->do("ALTER TABLE cd ADD (CONSTRAINT ${q}cd_pk${q} PRIMARY KEY (${q}cdid${q}))");
- $dbh->do("CREATE TABLE ${q}track${q} (${q}trackid${q} NUMBER(12), ${q}cd${q} NUMBER(12) REFERENCES CD(${q}cdid${q}) DEFERRABLE, ${q}position${q} NUMBER(12), ${q}title${q} VARCHAR(255), ${q}last_updated_on${q} DATE, ${q}last_updated_at${q} DATE, ${q}small_dt${q} DATE)");
+ $dbh->do("CREATE TABLE ${q}track${q} (${q}trackid${q} NUMBER(12), ${q}cd${q} NUMBER(12) REFERENCES CD(${q}cdid${q}) DEFERRABLE, ${q}position${q} NUMBER(12), ${q}title${q} VARCHAR(255), ${q}last_updated_on${q} DATE, ${q}last_updated_at${q} DATE)");
$dbh->do("ALTER TABLE ${q}track${q} ADD (CONSTRAINT ${q}track_pk${q} PRIMARY KEY (${q}trackid${q}))");
$dbh->do("CREATE TABLE ${q}bindtype_test${q} (${q}id${q} integer NOT NULL PRIMARY KEY, ${q}bytea${q} integer NULL, ${q}blob${q} blob NULL, ${q}clob${q} clob NULL)");
use Test::More;
use Test::Exception;
use Scope::Guard ();
+use Try::Tiny;
use lib qw(t/lib);
use DBICTest;
+DBICTest::Schema->load_classes('EventSmallDT');
+
# use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
BEGIN {
if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
plan skip_all =>
'Set $ENV{DBICTEST_MSSQL_ODBC_DSN} and/or $ENV{DBICTEST_MSSQL_DSN} _USER '
.'and _PASS to run this test' .
- "\nWarning: This test drops and creates a table called 'track'";
+ "\nWarning: This test drops and creates a table called 'small_dt'";
}
plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_dt')
my $guard = Scope::Guard->new(\&cleanup);
-# coltype, column, datehash
+ try { $schema->storage->dbh->do("DROP TABLE track") };
+ $schema->storage->dbh->do(<<"SQL");
+CREATE TABLE track (
+ trackid INT IDENTITY PRIMARY KEY,
+ cd INT,
+ position INT,
+ last_updated_at DATETIME,
+)
+SQL
+ try { $schema->storage->dbh->do("DROP TABLE event_small_dt") };
+ $schema->storage->dbh->do(<<"SQL");
+CREATE TABLE event_small_dt (
+ id INT IDENTITY PRIMARY KEY,
+ small_dt SMALLDATETIME,
+)
+SQL
+
+# coltype, column, source, pk, create_extra, datehash
my @dt_types = (
['DATETIME',
'last_updated_at',
+ 'Track',
+ 'trackid',
+ { cd => 1 },
{
year => 2004,
month => 8,
}],
['SMALLDATETIME', # minute precision
'small_dt',
+ 'EventSmallDT',
+ 'id',
+ {},
{
year => 2004,
month => 8,
);
for my $dt_type (@dt_types) {
- my ($type, $col, $sample_dt) = @$dt_type;
+ my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
- eval { $schema->storage->dbh->do("DROP TABLE track") };
- $schema->storage->dbh->do(<<"SQL");
-CREATE TABLE track (
- trackid INT IDENTITY PRIMARY KEY,
- cd INT,
- position INT,
- $col $type,
-)
-SQL
ok(my $dt = DateTime->new($sample_dt));
my $row;
- ok( $row = $schema->resultset('Track')->create({
+ ok( $row = $schema->resultset($source)->create({
$col => $dt,
- cd => 1,
+ %$create_extra,
}));
- ok( $row = $schema->resultset('Track')
- ->search({ trackid => $row->trackid }, { select => [$col] })
+ ok( $row = $schema->resultset($source)
+ ->search({ $pk => $row->$pk }, { select => [$col] })
->first
);
is( $row->$col, $dt, "$type roundtrip" );
sub cleanup {
if (my $dbh = eval { $schema->storage->dbh }) {
$dbh->do('DROP TABLE track');
+ $dbh->do('DROP TABLE event_small_dt');
}
}
eval {
$dbh->do("DROP TABLE track");
};
-$dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE, last_updated_at TIMESTAMP, small_dt DATE)");
+$dbh->do("CREATE TABLE track (trackid NUMBER(12), cd NUMBER(12), position NUMBER(12), title VARCHAR(255), last_updated_on DATE, last_updated_at TIMESTAMP)");
# insert a row to play with
my $new = $schema->resultset('Track')->create({ trackid => 1, cd => 1, position => 1, title => 'Track1', last_updated_on => '06-MAY-07', last_updated_at => '2009-05-03 21:17:18.5' });
use Test::More;
use Test::Exception;
+use Scope::Guard ();
+use Try::Tiny;
use lib qw(t/lib);
use DBICTest;
+DBICTest::Schema->load_classes('EventSmallDT');
+
my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
if (not ($dsn && $user)) {
$schema->storage_type("::$storage_type");
}
$schema->connection($dsn, $user, $pass, {
- AutoCommit => 1,
- on_connect_call => [ 'datetime_setup' ],
+ on_connect_call => 'datetime_setup',
});
+ my $guard = Scope::Guard->new(\&cleanup);
+
$schema->storage->ensure_connected;
isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
-# coltype, col, date
+ eval { $schema->storage->dbh->do("DROP TABLE track") };
+ $schema->storage->dbh->do(<<"SQL");
+CREATE TABLE track (
+ trackid INT IDENTITY PRIMARY KEY,
+ cd INT NULL,
+ position INT NULL,
+ last_updated_at DATETIME NULL
+)
+SQL
+ eval { $schema->storage->dbh->do("DROP TABLE event_small_dt") };
+ $schema->storage->dbh->do(<<"SQL");
+CREATE TABLE event_small_dt (
+ id INT IDENTITY PRIMARY KEY,
+ small_dt SMALLDATETIME NULL,
+)
+SQL
+
+# coltype, column, source, pk, create_extra, datehash
my @dt_types = (
- ['DATETIME', 'last_updated_at', '2004-08-21T14:36:48.080Z'],
-# minute precision
- ['SMALLDATETIME', 'small_dt', '2004-08-21T14:36:00.000Z'],
+ ['DATETIME',
+ 'last_updated_at',
+ 'Track',
+ 'trackid',
+ { cd => 1 },
+ {
+ year => 2004,
+ month => 8,
+ day => 21,
+ hour => 14,
+ minute => 36,
+ second => 48,
+ nanosecond => 500000000,
+ }],
+ ['SMALLDATETIME', # minute precision
+ 'small_dt',
+ 'EventSmallDT',
+ 'id',
+ {},
+ {
+ year => 2004,
+ month => 8,
+ day => 21,
+ hour => 14,
+ minute => 36,
+ }],
);
for my $dt_type (@dt_types) {
- my ($type, $col, $sample_dt) = @$dt_type;
+ my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
- eval { $schema->storage->dbh->do("DROP TABLE track") };
- $schema->storage->dbh->do(<<"SQL");
-CREATE TABLE track (
- trackid INT IDENTITY PRIMARY KEY,
- cd INT NULL,
- position INT NULL,
- $col $type NULL
-)
-SQL
- ok(my $dt = DateTime::Format::Sybase->parse_datetime($sample_dt));
+ ok(my $dt = DateTime->new($sample_dt));
my $row;
- ok( $row = $schema->resultset('Track')->create({
+ ok( $row = $schema->resultset($source)->create({
$col => $dt,
- cd => 1,
+ %$create_extra,
}));
- ok( $row = $schema->resultset('Track')
- ->search({ trackid => $row->trackid }, { select => [$col] })
+ ok( $row = $schema->resultset($source)
+ ->search({ $pk => $row->$pk }, { select => [$col] })
->first
);
is( $row->$col, $dt, "$type roundtrip" );
- is( $row->$col->nanosecond, $dt->nanosecond,
- 'fractional DateTime portion roundtrip' )
- if $dt->nanosecond > 0;
+ cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
+ 'DateTime fractional portion roundtrip' )
+ if exists $sample_dt->{nanosecond};
}
# test a computed datetime column
eval { $schema->storage->dbh->do("DROP TABLE track") };
$schema->storage->dbh->do(<<"SQL");
CREATE TABLE track (
- trackid INT IDENTITY PRIMARY KEY,
- cd INT NULL,
- position INT NULL,
- title VARCHAR(100) NULL,
- last_updated_on DATETIME NULL,
- last_updated_at AS getdate(),
- small_dt SMALLDATETIME NULL
+ trackid INT IDENTITY PRIMARY KEY,
+ cd INT NULL,
+ position INT NULL,
+ title VARCHAR(100) NULL,
+ last_updated_on DATETIME NULL,
+ last_updated_at AS getdate(),
)
SQL
- my $now = DateTime->now;
+ my $now = DateTime->now;
sleep 1;
my $new_row = $schema->resultset('Track')->create({});
$new_row->discard_changes;
done_testing;
# clean up our mess
-END {
- if (my $dbh = eval { $schema->storage->_dbh }) {
+sub cleanup {
+ if (my $dbh = eval { $schema->storage->dbh }) {
$dbh->do('DROP TABLE track');
+ $dbh->do('DROP TABLE event_small_dt');
}
}
--- /dev/null
+package DBICTest::Schema::EventSmallDT;
+
+use strict;
+use warnings;
+use base qw/DBICTest::BaseResult/;
+
+__PACKAGE__->load_components(qw/InflateColumn::DateTime/);
+
+__PACKAGE__->table('event_small_dt');
+
+__PACKAGE__->add_columns(
+ id => { data_type => 'integer', is_auto_increment => 1 },
+ small_dt => { data_type => 'smalldatetime', is_nullable => 1 },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+1;
data_type => 'datetime',
is_nullable => 1
},
- small_dt => { # for mssql and sybase DT tests
- data_type => 'smalldatetime',
- is_nullable => 1
- },
);
__PACKAGE__->set_primary_key('trackid');
--
-- Created by SQL::Translator::Producer::SQLite
--- Created on Sun Oct 17 01:51:06 2010
+-- Created on Thu Nov 18 08:18:15 2010
--
--
position int NOT NULL,
title varchar(100) NOT NULL,
last_updated_on datetime,
- last_updated_at datetime,
- small_dt smalldatetime
+ last_updated_at datetime
);
CREATE INDEX track_idx_cd ON track (cd);
last_updated_at => undef,
last_updated_on => undef,
position => 1,
- small_dt => undef,
title => "$reldir: First track of latest cd",
trackid => 19
}
-use warnings;
+use warnings;
use Test::More;
use Test::Exception;
'(
SELECT
cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track,
- single_track.trackid, single_track.cd, single_track.position, single_track.title, single_track.last_updated_on, single_track.last_updated_at, single_track.small_dt,
- single_track_2.trackid, single_track_2.cd, single_track_2.position, single_track_2.title, single_track_2.last_updated_on, single_track_2.last_updated_at, single_track_2.small_dt,
+ single_track.trackid, single_track.cd, single_track.position, single_track.title, single_track.last_updated_on, single_track.last_updated_at,
+ single_track_2.trackid, single_track_2.cd, single_track_2.position, single_track_2.title, single_track_2.last_updated_on, single_track_2.last_updated_at,
cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track
FROM artist me
JOIN cd cds ON cds.artist = me.artistid
$most_tracks_rs->as_query,
'(
SELECT me.cdid, me.track_count, me.maxtr,
- tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at, tracks.small_dt,
+ tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at,
liner_notes.liner_id, liner_notes.notes
FROM (
SELECT me.cdid, COUNT( tracks.trackid ) AS track_count, MAX( tracks.trackid ) AS maxtr
$filtered_cd_rs->as_query,
q{(
SELECT cds_unordered.cdid, cds_unordered.artist, cds_unordered.title, cds_unordered.year, cds_unordered.genreid, cds_unordered.single_track,
- tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at, tracks.small_dt
+ tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at
FROM artist me
JOIN (
SELECT cds_unordered.cdid, cds_unordered.artist, cds_unordered.title, cds_unordered.year, cds_unordered.genreid, cds_unordered.single_track
'last_updated_at' => undef,
'last_updated_on' => undef,
'position' => '1',
- 'small_dt' => undef,
'title' => 'Boring Name',
'trackid' => '10'
},
'last_updated_at' => undef,
'last_updated_on' => undef,
'position' => '2',
- 'small_dt' => undef,
'title' => 'Boring Song',
'trackid' => '11'
},
'last_updated_at' => undef,
'last_updated_on' => undef,
'position' => '3',
- 'small_dt' => undef,
'title' => 'No More Ideas',
'trackid' => '12'
}
'last_updated_at' => undef,
'last_updated_on' => undef,
'position' => '1',
- 'small_dt' => undef,
'title' => 'Sad',
'trackid' => '13'
},
'last_updated_at' => undef,
'last_updated_on' => undef,
'position' => '3',
- 'small_dt' => undef,
'title' => 'Suicidal',
'trackid' => '15'
},
'last_updated_at' => undef,
'last_updated_on' => undef,
'position' => '2',
- 'small_dt' => undef,
'title' => 'Under The Weather',
'trackid' => '14'
}
'(
SELECT
me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
- tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at, tracks.small_dt,
+ tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at,
artist.artistid, artist.name, artist.rank, artist.charfield
FROM (
SELECT