initial merge of Schwern's CDBICompat work, with many thanks
[dbsrgits/DBIx-Class-Historic.git] / lib / SQL / Translator / Parser / DBIx / Class.pm
CommitLineData
75d07914 1package # hide from PAUSE
c0e7b4e5 2 SQL::Translator::Parser::DBIx::Class;
b02b20b5 3
4# AUTHOR: Jess Robinson
5
1d996af5 6# Some mistakes the fault of Matt S Trout
7
b02b20b5 8use strict;
9use warnings;
10use vars qw($DEBUG $VERSION @EXPORT_OK);
11$DEBUG = 0 unless defined $DEBUG;
12$VERSION = sprintf "%d.%02d", q$Revision 1.0$ =~ /(\d+)\.(\d+)/;
13
14use Exporter;
15use Data::Dumper;
16use SQL::Translator::Utils qw(debug normalize_name);
17
18use base qw(Exporter);
19
20@EXPORT_OK = qw(parse);
21
22# -------------------------------------------------------------------
23# parse($tr, $data)
24#
880a1a0c 25# Note that $data, in the case of this parser, is not useful.
b02b20b5 26# We're working with DBIx::Class Schemas, not data streams.
27# -------------------------------------------------------------------
28sub parse {
499adf63 29 my ($tr, $data) = @_;
30 my $args = $tr->parser_args;
31 my $dbixschema = $args->{'DBIx::Schema'} || $data;
32 $dbixschema ||= $args->{'package'};
33 my $limit_sources = $args->{'sources'};
b02b20b5 34
35 die 'No DBIx::Schema' unless ($dbixschema);
36 if (!ref $dbixschema) {
37 eval "use $dbixschema;";
38 die "Can't load $dbixschema ($@)" if($@);
39 }
40
41 my $schema = $tr->schema;
42 my $table_no = 0;
43
44# print Dumper($dbixschema->registered_classes);
45
38e48163 46 my %seen_tables;
47
499adf63 48 my @monikers = $dbixschema->sources;
49 if ($limit_sources) {
50 my $ref = ref $limit_sources || '';
5bdf1b3d 51 die "'sources' parameter must be an array or hash ref" unless $ref eq 'ARRAY' || ref eq 'HASH';
499adf63 52
53 # limit monikers to those specified in
54 my $sources;
55 if ($ref eq 'ARRAY') {
56 $sources->{$_} = 1 for (@$limit_sources);
57 } else {
58 $sources = $limit_sources;
59 }
60 @monikers = grep { $sources->{$_} } @monikers;
61 }
62
63
64 foreach my $moniker (@monikers)
b02b20b5 65 {
0009fa49 66 #eval "use $tableclass";
67 #print("Can't load $tableclass"), next if($@);
68 my $source = $dbixschema->source($moniker);
b02b20b5 69
38e48163 70 next if $seen_tables{$source->name}++;
71
b02b20b5 72 my $table = $schema->add_table(
73 name => $source->name,
74 type => 'TABLE',
75 ) || die $schema->error;
76 my $colcount = 0;
77 foreach my $col ($source->columns)
78 {
79 # assuming column_info in dbix is the same as DBI (?)
80 # data_type is a number, column_type is text?
81 my %colinfo = (
82 name => $col,
b02b20b5 83 size => 0,
84 is_auto_increment => 0,
85 is_foreign_key => 0,
86 is_nullable => 0,
87 %{$source->column_info($col)}
88 );
0009fa49 89 if ($colinfo{is_nullable}) {
90 $colinfo{default} = '' unless exists $colinfo{default};
91 }
b02b20b5 92 my $f = $table->add_field(%colinfo) || die $table->error;
93 }
94 $table->primary_key($source->primary_columns);
95
7b90bb13 96 my @primary = $source->primary_columns;
97 my %unique_constraints = $source->unique_constraints;
98 foreach my $uniq (keys %unique_constraints) {
de60a93d 99 if (!$source->compare_relationship_keys($unique_constraints{$uniq}, \@primary)) {
7b90bb13 100 $table->add_constraint(
101 type => 'unique',
102 name => "$uniq",
103 fields => $unique_constraints{$uniq}
104 );
105 }
106 }
107
b02b20b5 108 my @rels = $source->relationships();
499adf63 109
110 my %created_FK_rels;
111
b02b20b5 112 foreach my $rel (@rels)
113 {
114 my $rel_info = $source->relationship_info($rel);
637ca936 115
637ca936 116 # Ignore any rel cond that isn't a straight hash
117 next unless ref $rel_info->{cond} eq 'HASH';
118
de60a93d 119 my $othertable = $source->related_source($rel);
120 my $rel_table = $othertable->name;
121
637ca936 122 # Get the key information, mapping off the foreign/self markers
123 my @cond = keys(%{$rel_info->{cond}});
124 my @refkeys = map {/^\w+\.(\w+)$/} @cond;
125 my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
126
127 if($rel_table)
75d07914 128 {
637ca936 129
de60a93d 130 my $reverse_rels = $source->reverse_relationship_info($rel);
131 my ($otherrelname, $otherrelationship) = each %{$reverse_rels};
132
133 my $on_delete = '';
134 my $on_update = '';
135
136 if (defined $otherrelationship) {
137 $on_delete = $otherrelationship->{'attrs'}->{cascade_delete} ? 'CASCADE' : '';
138 $on_update = $otherrelationship->{'attrs'}->{cascade_copy} ? 'CASCADE' : '';
139 }
140
499adf63 141 # Make sure we dont create the same foreign key constraint twice
142 my $key_test = join("\x00", @keys);
143
637ca936 144 #Decide if this is a foreign key based on whether the self
145 #items are our primary columns.
3d618782 146 $DB::single = 1 if $moniker eq 'Tests::MBTI::Result';
637ca936 147
637ca936 148 # If the sets are different, then we assume it's a foreign key from
149 # us to another table.
3d618782 150 # OR: If is_foreign_key_constraint attr is explicity set (or set to false) on the relation
151 if ( ! exists $created_FK_rels{$rel_table}->{$key_test} &&
152 ( exists $rel_info->{attrs}{is_foreign_key_constraint} &&
153 $rel_info->{attrs}{is_foreign_key_constraint} ||
154 !$source->compare_relationship_keys(\@keys, \@primary)
155 )
156 )
157 {
499adf63 158 $created_FK_rels{$rel_table}->{$key_test} = 1;
637ca936 159 $table->add_constraint(
160 type => 'foreign_key',
161 name => "fk_$keys[0]",
162 fields => \@keys,
163 reference_fields => \@refkeys,
164 reference_table => $rel_table,
de60a93d 165 on_delete => $on_delete,
166 on_update => $on_update
637ca936 167 );
168 }
b02b20b5 169 }
170 }
171 }
637ca936 172 return 1;
75d07914 173}
b02b20b5 174
1751;
de60a93d 176