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