1#!/usr/bin/perl
2use strict;
3
4use FindBin qw/$Bin/;
5use Test::More;
6use Test::SQL::Translator;
7use Test::Differences;
8use Test::Exception;
9use Data::Dumper;
10use SQL::Translator;
11use SQL::Translator::Schema::Constants;
12
13BEGIN {
14    maybe_plan(1, 'SQL::Translator::Parser::XML::SQLFairy',
15              'SQL::Translator::Producer::DB2');
16}
17
18my $xmlfile = "$Bin/data/xml/schema.xml";
19
20my $sqlt;
21$sqlt = SQL::Translator->new(
22    no_comments => 1,
23    show_warnings  => 0,
24    add_drop_table => 1,
25);
26
27die "Can't find test schema $xmlfile" unless -e $xmlfile;
28
29my $sql = $sqlt->translate(
30    from     => 'XML-SQLFairy',
31    to       => 'DB2',
32    filename => $xmlfile,
33) or die $sqlt->error;
34
35eq_or_diff($sql, << "SQL");
36DROP TABLE Basic;
37
38CREATE TABLE Basic (
39  id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) NOT NULL,
40  title VARCHAR(100) NOT NULL DEFAULT 'hello',
41  description VARCHAR(0) DEFAULT '',
42  email VARCHAR(500),
43  explicitnulldef VARCHAR(0),
44  explicitemptystring VARCHAR(0) DEFAULT '',
45  emptytagdef VARCHAR(0) DEFAULT '',
46  another_id INTEGER DEFAULT 2,
47  timest TIMESTAMP,
48  PRIMARY KEY (id),
49  CONSTRAINT emailuniqueindex UNIQUE (email),
50  CONSTRAINT very_long_index_name_on_title_field_which_should_be_truncated_for_various_rdbms UNIQUE (title)
51);
52
53DROP TABLE Another;
54
55CREATE TABLE Another (
56  id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) NOT NULL,
57  num NUMERIC(10,2),
58  PRIMARY KEY (id)
59);
60
61ALTER TABLE Basic ADD FOREIGN KEY (another_id) REFERENCES Another(id);
62
63CREATE INDEX titleindex ON Basic ( title );
64
65CREATE VIEW email_list AS
66SELECT email FROM Basic WHERE (email IS NOT NULL);
67
68CREATE TRIGGER foo_trigger after insert ON Basic REFERENCING OLD AS oldrow NEW AS newrow FOR EACH ROW MODE DB2SQL update modified=timestamp();
69
70CREATE TRIGGER bar_trigger before insert, update ON Basic REFERENCING OLD AS oldrow NEW AS newrow FOR EACH ROW MODE DB2SQL update modified2=timestamp();
71SQL
72