Fixed up tests post-merge
[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
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 {
29 my ($tr, $data) = @_;
30 my $args = $tr->parser_args;
31 my $dbixschema = $args->{'DBIx::Schema'} || $data;
bddbe61c 32 $dbixschema ||= $args->{'package'};
b02b20b5 33
34 die 'No DBIx::Schema' unless ($dbixschema);
35 if (!ref $dbixschema) {
36 eval "use $dbixschema;";
37 die "Can't load $dbixschema ($@)" if($@);
38 }
39
40 my $schema = $tr->schema;
41 my $table_no = 0;
42
43# print Dumper($dbixschema->registered_classes);
44
0009fa49 45 #foreach my $tableclass ($dbixschema->registered_classes)
38e48163 46
47 my %seen_tables;
48
0009fa49 49 foreach my $moniker ($dbixschema->sources)
b02b20b5 50 {
0009fa49 51 #eval "use $tableclass";
52 #print("Can't load $tableclass"), next if($@);
53 my $source = $dbixschema->source($moniker);
b02b20b5 54
38e48163 55 next if $seen_tables{$source->name}++;
56
b02b20b5 57 my $table = $schema->add_table(
58 name => $source->name,
59 type => 'TABLE',
60 ) || die $schema->error;
61 my $colcount = 0;
62 foreach my $col ($source->columns)
63 {
64 # assuming column_info in dbix is the same as DBI (?)
65 # data_type is a number, column_type is text?
66 my %colinfo = (
67 name => $col,
b02b20b5 68 size => 0,
69 is_auto_increment => 0,
70 is_foreign_key => 0,
71 is_nullable => 0,
72 %{$source->column_info($col)}
73 );
0009fa49 74 if ($colinfo{is_nullable}) {
75 $colinfo{default} = '' unless exists $colinfo{default};
76 }
b02b20b5 77 my $f = $table->add_field(%colinfo) || die $table->error;
78 }
79 $table->primary_key($source->primary_columns);
80
7b90bb13 81 my @primary = $source->primary_columns;
82 my %unique_constraints = $source->unique_constraints;
83 foreach my $uniq (keys %unique_constraints) {
de60a93d 84 if (!$source->compare_relationship_keys($unique_constraints{$uniq}, \@primary)) {
7b90bb13 85 $table->add_constraint(
86 type => 'unique',
87 name => "$uniq",
88 fields => $unique_constraints{$uniq}
89 );
90 }
91 }
92
b02b20b5 93 my @rels = $source->relationships();
94 foreach my $rel (@rels)
95 {
96 my $rel_info = $source->relationship_info($rel);
637ca936 97
637ca936 98 # Ignore any rel cond that isn't a straight hash
99 next unless ref $rel_info->{cond} eq 'HASH';
100
de60a93d 101 my $othertable = $source->related_source($rel);
102 my $rel_table = $othertable->name;
103
637ca936 104 # Get the key information, mapping off the foreign/self markers
105 my @cond = keys(%{$rel_info->{cond}});
106 my @refkeys = map {/^\w+\.(\w+)$/} @cond;
107 my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
108
109 if($rel_table)
75d07914 110 {
637ca936 111
de60a93d 112 my $reverse_rels = $source->reverse_relationship_info($rel);
113 my ($otherrelname, $otherrelationship) = each %{$reverse_rels};
114
115 my $on_delete = '';
116 my $on_update = '';
117
118 if (defined $otherrelationship) {
119 $on_delete = $otherrelationship->{'attrs'}->{cascade_delete} ? 'CASCADE' : '';
120 $on_update = $otherrelationship->{'attrs'}->{cascade_copy} ? 'CASCADE' : '';
121 }
122
637ca936 123 #Decide if this is a foreign key based on whether the self
124 #items are our primary columns.
125
637ca936 126 # If the sets are different, then we assume it's a foreign key from
127 # us to another table.
de60a93d 128 if (!$source->compare_relationship_keys(\@keys, \@primary)) {
637ca936 129 $table->add_constraint(
130 type => 'foreign_key',
131 name => "fk_$keys[0]",
132 fields => \@keys,
133 reference_fields => \@refkeys,
134 reference_table => $rel_table,
de60a93d 135 on_delete => $on_delete,
136 on_update => $on_update
637ca936 137 );
138 }
b02b20b5 139 }
140 }
141 }
637ca936 142 return 1;
75d07914 143}
b02b20b5 144
1451;
de60a93d 146