Merge 'trunk' into 'cdbicompat_integration'
[dbsrgits/DBIx-Class.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
c385ecea 8# Others the fault of Ash Berlin
9
b02b20b5 10use strict;
11use warnings;
12use vars qw($DEBUG $VERSION @EXPORT_OK);
13$DEBUG = 0 unless defined $DEBUG;
14$VERSION = sprintf "%d.%02d", q$Revision 1.0$ =~ /(\d+)\.(\d+)/;
15
16use Exporter;
17use Data::Dumper;
18use SQL::Translator::Utils qw(debug normalize_name);
19
20use base qw(Exporter);
21
22@EXPORT_OK = qw(parse);
23
24# -------------------------------------------------------------------
25# parse($tr, $data)
26#
880a1a0c 27# Note that $data, in the case of this parser, is not useful.
b02b20b5 28# We're working with DBIx::Class Schemas, not data streams.
29# -------------------------------------------------------------------
30sub parse {
499adf63 31 my ($tr, $data) = @_;
32 my $args = $tr->parser_args;
33 my $dbixschema = $args->{'DBIx::Schema'} || $data;
34 $dbixschema ||= $args->{'package'};
35 my $limit_sources = $args->{'sources'};
b02b20b5 36
37 die 'No DBIx::Schema' unless ($dbixschema);
38 if (!ref $dbixschema) {
39 eval "use $dbixschema;";
40 die "Can't load $dbixschema ($@)" if($@);
41 }
42
43 my $schema = $tr->schema;
44 my $table_no = 0;
45
46# print Dumper($dbixschema->registered_classes);
47
38e48163 48 my %seen_tables;
49
499adf63 50 my @monikers = $dbixschema->sources;
51 if ($limit_sources) {
52 my $ref = ref $limit_sources || '';
5bdf1b3d 53 die "'sources' parameter must be an array or hash ref" unless $ref eq 'ARRAY' || ref eq 'HASH';
499adf63 54
55 # limit monikers to those specified in
56 my $sources;
57 if ($ref eq 'ARRAY') {
58 $sources->{$_} = 1 for (@$limit_sources);
59 } else {
60 $sources = $limit_sources;
61 }
62 @monikers = grep { $sources->{$_} } @monikers;
63 }
64
65
454a5a42 66 foreach my $moniker (sort @monikers)
b02b20b5 67 {
0009fa49 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 {
d6c79cb3 79 # assuming column_info in dbic is the same as DBI (?)
b02b20b5 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
454a5a42 112 foreach my $rel (sort @rels)
b02b20b5 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 {
de60a93d 129 my $reverse_rels = $source->reverse_relationship_info($rel);
130 my ($otherrelname, $otherrelationship) = each %{$reverse_rels};
131
132 my $on_delete = '';
133 my $on_update = '';
134
135 if (defined $otherrelationship) {
136 $on_delete = $otherrelationship->{'attrs'}->{cascade_delete} ? 'CASCADE' : '';
137 $on_update = $otherrelationship->{'attrs'}->{cascade_copy} ? 'CASCADE' : '';
138 }
139
499adf63 140 # Make sure we dont create the same foreign key constraint twice
141 my $key_test = join("\x00", @keys);
142
637ca936 143 #Decide if this is a foreign key based on whether the self
144 #items are our primary columns.
145
637ca936 146 # If the sets are different, then we assume it's a foreign key from
147 # us to another table.
3d618782 148 # OR: If is_foreign_key_constraint attr is explicity set (or set to false) on the relation
149 if ( ! exists $created_FK_rels{$rel_table}->{$key_test} &&
a0024650 150 ( exists $rel_info->{attrs}{is_foreign_key_constraint} ?
151 $rel_info->{attrs}{is_foreign_key_constraint} :
3d618782 152 !$source->compare_relationship_keys(\@keys, \@primary)
a0024650 153 )
3d618782 154 )
155 {
499adf63 156 $created_FK_rels{$rel_table}->{$key_test} = 1;
637ca936 157 $table->add_constraint(
158 type => 'foreign_key',
159 name => "fk_$keys[0]",
160 fields => \@keys,
161 reference_fields => \@refkeys,
162 reference_table => $rel_table,
de60a93d 163 on_delete => $on_delete,
164 on_update => $on_update
637ca936 165 );
166 }
b02b20b5 167 }
168 }
aaf2403d 169
170 if ($source->result_class->can('sqlt_deploy_hook')) {
171 $source->result_class->sqlt_deploy_hook($table);
172 }
b02b20b5 173 }
d6c79cb3 174
175 if ($dbixschema->can('sqlt_deploy_hook')) {
176 $dbixschema->sqlt_deploy_hook($schema);
177 }
178
637ca936 179 return 1;
75d07914 180}
b02b20b5 181
1821;
de60a93d 183