From: Brian O'Connor Date: Sun, 28 Dec 2003 12:14:59 +0000 (+0000) Subject: Changes for Turnkey Atom class creation. Right now DBI and Atom classes seem to... X-Git-Tag: v0.06~248 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a0249514ef761312c5ea2eca66802265afba8c77;p=dbsrgits%2FSQL-Translator.git Changes for Turnkey Atom class creation. Right now DBI and Atom classes seem to be created correctly with the Turnkey producer. --- diff --git a/lib/SQL/Translator/Producer/Turnkey.pm b/lib/SQL/Translator/Producer/Turnkey.pm index 10029a5..07974a8 100644 --- a/lib/SQL/Translator/Producer/Turnkey.pm +++ b/lib/SQL/Translator/Producer/Turnkey.pm @@ -1,7 +1,7 @@ package SQL::Translator::Producer::Turnkey; # ------------------------------------------------------------------- -# $Id: Turnkey.pm,v 1.9 2003-10-15 16:48:39 kycl4rk Exp $ +# $Id: Turnkey.pm,v 1.10 2003-12-28 12:14:59 boconnor Exp $ # ------------------------------------------------------------------- # Copyright (C) 2003 Allen Day , # Brian O'Connor . @@ -23,7 +23,7 @@ package SQL::Translator::Producer::Turnkey; use strict; use vars qw[ $VERSION $DEBUG ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/; $DEBUG = 1 unless defined $DEBUG; use SQL::Translator::Schema::Constants; @@ -137,7 +137,6 @@ sub produce { } } } - $meta{"nodes"} = $graph->node; return(translateForm($t, \%meta)); } @@ -145,6 +144,12 @@ sub produce { sub translateForm { my $t = shift; my $meta = shift; + + +#print "Node Data:\n"; +#print Dumper $meta->{nodes}; +#exit; + my $args = $t->producer_args; my $type = $meta->{'template'}; my $tt2; @@ -192,8 +197,11 @@ sub template { # Here documents for the tt2 templates # ########################################### - if($type eq 'classdbi'){ + if($type eq 'dbi'){ return <has_many([% edge.thatnode.table.name %]_[% edge.thatfield.name %], '[% edge.thatnode.name %]' => '[% edge.thatfield.name %]'); +[% node.name %]->has_many('[% edge.thatnode.table.name %]_[% edge.thatfield.name %]', '[% edge.thatnode.name %]' => '[% edge.thatfield.name %]'); [% IF node.via(edge.thatnode.name) >= 1 %] sub [% edge.thatnode.table.name %]_[% format_fk(edge.thatnode.table.name,edge.thatfield.name) %]s { return shift->[% edge.thatnode.table.name %]_[% edge.thatfield.name %] } [% ELSIF edge.thatnode.table.is_data %] @@ -317,7 +327,7 @@ sub [% h.vianode.table.name %]_[% format_fk(h.vianode,h.thisviafield_index(i).na # # Has Friendly # -hello, sailor! + [% END %] [% MACRO printList(array) BLOCK %][% FOREACH item = array %][% item %] [% END %][% END %] @@ -335,21 +345,25 @@ use base qw(Class::DBI::Pg); [% printPackage(node.value) %] [% END %] EOF +} + + +elsif($type eq 'atom'){ -} elsif($type eq 'atom'){ return <<'EOF'; [% ###### DOCUMENT START ###### %] -[% FOREACH node = linkable %] +[% FOREACH node = nodes %] +[% IF node.value.compoundedges %] ############################################## -package Durian::Atom::[% node.key FILTER ucfirst %]; +package Turnkey::Atom::[% node.value.name FILTER replace "Turnkey::Model::", "" %]; -[% pname = node.key FILTER ucfirst%] -[% pkey = "Durian::Model::${pname}" %] +[% pname = node.value.name FILTER replace "Turnkey::Model::", "" %] +[% pkey = "Turnkey::Model::${pname}" %] -use base qw(Durian::Atom); +use base qw(Turnkey::Atom); use Data::Dumper; sub can_render { @@ -361,7 +375,7 @@ sub render { my $dbobject = shift; # Assumption here that if it's not rendering on it's own dbobject # then it's a list. This will be updated when AtomLists are implemented -boconnor - if(ref($dbobject) eq 'Durian::Model::[% node.key FILTER ucfirst %]') { + if(ref($dbobject) eq 'Turnkey::Model::[% pname %]') { return(_render_record($dbobject)); } else { return(_render_list($dbobject)); } @@ -372,8 +386,10 @@ sub _render_record { my @output = (); my $row = {}; my $field_hash = {}; - [% FOREACH field = nodes.$pkey.columns_essential %] - $field_hash->{[% field %]} = $dbobject->[% field %](); + [% FOREACH value = node.value.data_fields %] + [% IF value != 1 %] + $field_hash->{[% value %]} = $dbobject->[% value %](); + [% END %] [% END %] $row->{data} = $field_hash; $row->{id} = $dbobject->id(); @@ -389,9 +405,11 @@ sub _render_list { { my $row = {}; my $field_hash = {}; - [% FOREACH field = nodes.$pkey.columns_essential %] - $field_hash->{[% field %]} = $object->[% field %](); - [% END %] + [% FOREACH value = node.value.data_fields %] + [% IF value != 1 %] + $field_hash->{[% value %]} = $object->[% value %](); + [% END %] + [% END %] $row->{data} = $field_hash; $row->{id} = $object->id(); push @output, $row; @@ -404,7 +422,7 @@ sub head { } 1; - +[% END %] [% END %] EOF diff --git a/lib/SQL/Translator/Schema/Graph.pm b/lib/SQL/Translator/Schema/Graph.pm index e389350..36a2850 100644 --- a/lib/SQL/Translator/Schema/Graph.pm +++ b/lib/SQL/Translator/Schema/Graph.pm @@ -39,6 +39,9 @@ sub init { $self->node_push($table->name => $node); + if ($table->is_trivial_link) { $node->is_trivial_link(1); } + else { $node->is_trivial_link(0); } + $node->order($self->order_incr()); $node->name( $self->translator->format_package_name($table->name) ); $node->base( $self->baseclass ); @@ -55,8 +58,8 @@ sub init { foreach my $node ($self->node_values){ foreach my $field ($node->table->get_fields){ - next unless $field->is_foreign_key; - + if (!$field->is_foreign_key && !$field->is_primary_key) { $node->data_fields->{$field->name} = 1; } + elsif($field->is_foreign_key) { my $that = $self->node($field->foreign_key_reference->reference_table); #this means we have an incomplete schema @@ -80,6 +83,7 @@ sub init { warn $node->name . "\t" . $that->edgecount($node->name); $node->push_edges( $edge ); $that->push_edges( $edge->flip ); + } } } diff --git a/lib/SQL/Translator/Schema/Graph/Node.pm b/lib/SQL/Translator/Schema/Graph/Node.pm index bb4e86f..db91c6c 100644 --- a/lib/SQL/Translator/Schema/Graph/Node.pm +++ b/lib/SQL/Translator/Schema/Graph/Node.pm @@ -7,8 +7,8 @@ use Class::MakeMethods::Template::Hash ( 'array_of_objects -class SQL::Translator::Schema::Graph::Edge' => [ qw( edges ) ], 'array_of_objects -class SQL::Translator::Schema::Graph::CompoundEdge' => [ qw( compoundedges ) ], 'array_of_objects -class SQL::Translator::Schema::Graph::HyperEdge' => [ qw( hyperedges ) ], - 'hash' => [ qw( many via has edgecount) ], - scalar => [ qw( base name order primary_key primary_key_accessor table ) ], + 'hash' => [ qw( many via has edgecount data_fields) ], + scalar => [ qw( base name order primary_key primary_key_accessor table is_trivial_link ) ], number => [ qw( order ) ], );