1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
#!/usr/bin/perl -w
use strict;
use Test::More tests => 7;
BEGIN
{
require 't/test-lib.pl';
use_ok('Rose::DB::Object');
}
our $HAVE_PG;
SKIP: foreach my $db_type (qw(pg pg_with_schema))
{
skip("PostgreSQL tests", 6) unless($HAVE_PG);
OVERRIDE_OK:
{
no warnings;
*MyPgObject::init_db = sub { Rose::DB->new($db_type) };
}
my $o = MyPgObject->new(name => 'John',
k1 => 1,
k2 => undef,
k3 => 3);
ok(ref $o && $o->isa('MyPgObject'), "new() 1 - $db_type");
$o->flag2('TRUE');
$o->date_created('now');
$o->last_modified($o->date_created);
$o->save_col(7);
ok($o->save, "save() 1 - $db_type");
is($o->id, 1, "auto-generated primary key - $db_type");
}
BEGIN
{
#
# PostgreSQL
#
my $dbh;
eval
{
$dbh = Rose::DB->new('pg_admin')->retain_dbh()
or die Rose::DB->error;
};
if(!$@ && $dbh)
{
our $HAVE_PG = 1;
# Drop existing table and create schema, ignoring errors
{
local $dbh->{'RaiseError'} = 0;
local $dbh->{'PrintError'} = 0;
$dbh->do('DROP TABLE Rose_db_object_test CASCADE');
$dbh->do('DROP TABLE Rose_db_object_private.Rose_db_object_test CASCADE');
$dbh->do('DROP TABLE Rose_db_object_chkpass_test');
$dbh->do('DROP SEQUENCE Rose_db_object_test_seq');
$dbh->do('DROP SEQUENCE Rose_db_object_private.Rose_db_object_test_seq');
$dbh->do('CREATE SCHEMA Rose_db_object_private');
}
our $PG_HAS_CHKPASS = pg_has_chkpass();
$dbh->do('CREATE SEQUENCE Rose_db_object_test_seq');
my $pg_vers = $dbh->{'pg_server_version'};
my $active = $pg_vers >= 80100 ? q('act''ive') : q('act\'ive');
$dbh->do(<<"EOF");
CREATE TABLE Rose_db_object_test
(
id INT DEFAULT nextval('Rose_db_object_test_seq') NOT NULL PRIMARY KEY,
k1 INT,
k2 INT,
k3 INT,
@{[ $PG_HAS_CHKPASS ? 'password CHKPASS,' : '' ]}
name VARCHAR(32) NOT NULL,
code CHAR(6),
flag BOOLEAN NOT NULL DEFAULT 't',
flag2 BOOLEAN,
status VARCHAR(32) DEFAULT $active,
bits BIT(5) NOT NULL DEFAULT B'00101',
start DATE DEFAULT '1980-12-24',
save INT,
nums INT[],
last_modified TIMESTAMP,
date_created TIMESTAMP,
UNIQUE(save),
UNIQUE(k1, k2, k3)
)
EOF
$dbh->do('CREATE SEQUENCE Rose_db_object_private.Rose_db_object_test_seq');
$dbh->do(<<"EOF");
CREATE TABLE Rose_db_object_private.Rose_db_object_test
(
id INT DEFAULT nextval('Rose_db_object_test_seq') NOT NULL PRIMARY KEY,
k1 INT,
k2 INT,
k3 INT,
@{[ $PG_HAS_CHKPASS ? 'password CHKPASS,' : '' ]}
name VARCHAR(32) NOT NULL,
code CHAR(6),
flag BOOLEAN NOT NULL DEFAULT 't',
flag2 BOOLEAN,
status VARCHAR(32) DEFAULT $active,
bits BIT(5) NOT NULL DEFAULT B'00101',
start DATE DEFAULT '1980-12-24',
save INT,
nums INT[],
last_modified TIMESTAMP,
date_created TIMESTAMP,
UNIQUE(save),
UNIQUE(k1, k2, k3)
)
EOF
$dbh->disconnect;
Rose::DB->default_type('pg');
package MyTmpPgObject;
use Rose::DB::Object::Helpers qw(clone);
our @ISA = qw(Rose::DB::Object);
sub init_db { Rose::DB->new('pg') }
MyTmpPgObject->meta->table('Rose_db_object_test');
MyTmpPgObject->meta->auto_initialize;
my $code = MyTmpPgObject->meta->perl_class_definition;
$code =~ s/\bMyTmpPgObject\b/MyPgObject/g;
eval $code;
die $@ if($@);
}
}
END
{
# Delete test table
if($HAVE_PG)
{
# PostgreSQL
my $dbh = Rose::DB->new('pg_admin')->retain_dbh()
or die Rose::DB->error;
$dbh->do('DROP TABLE Rose_db_object_test CASCADE');
$dbh->do('DROP TABLE Rose_db_object_private.Rose_db_object_test CASCADE');
$dbh->do('DROP SEQUENCE Rose_db_object_test_seq');
$dbh->do('DROP SEQUENCE Rose_db_object_private.Rose_db_object_test_seq');
$dbh->do('DROP SCHEMA Rose_db_object_private CASCADE');
$dbh->disconnect;
}
}
|