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