/[Frey]/trunk/lib/Frey/Class/Schematize.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/Frey/Class/Schematize.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1133 - (hide annotations)
Tue Jun 30 15:10:55 2009 UTC (14 years, 10 months ago) by dpavlin
File size: 2673 byte(s)
make classes immutable and remove moose droppings to make Perl::Critic::Moose happy
1 dpavlin 1093 package Frey::Class::Schematize;
2     use Moose;
3    
4     extends 'Frey::Class::Loader';
5     extends 'Frey::PPI';
6     with 'Frey::Web';
7    
8     has class => (
9     is => 'rw',
10     isa => 'Str',
11     required => 1,
12     default => 'App::RoomReservation::Reservation',
13     );
14    
15     sub create_table_as_markup {
16     my ($self) = @_;
17     qq|<pre>| . $self->create_table . qq|</pre>|;
18     }
19    
20     use DBI;
21    
22     sub create_table {
23     my ($self) = @_;
24    
25     my ( $meta, $is_role, $instance ) = $self->class_meta( $self->class );
26    
27     my $columns;
28     map {
29     $columns->{ $_->name } = attribute($_);
30     } $meta->get_all_attributes;
31    
32     my @order = $self->attribute_order;
33    
34     return
35     header($meta)
36     . join(",\n\t",
37     "\tid SERIAL",
38     map { $columns->{$_} } @order
39     )
40     . footer()
41     ;
42    
43     }
44    
45     sub class_to_table {
46     my $class = shift;
47     $class =~ s{App::[^:]+::}{};
48     my $table = lc $class;
49     $table =~ s/::/_/g;
50     return $table;
51     }
52    
53     sub header {
54     my $meta = shift;
55    
56     my $name = class_to_table($meta->name);
57    
58     return "CREATE TABLE $name (\n";
59     }
60    
61     sub footer { "\n);\n" }
62    
63     sub attribute {
64     my $attribute = shift;
65     my @constraints;
66    
67     push @constraints, type_of($attribute);
68     push @constraints, 'NOT NULL' if $attribute->is_required;
69     push @constraints, default_of($attribute);
70     push @constraints, foreign_key_of($attribute);
71    
72     return join ' ', $attribute->name, @constraints;
73     }
74    
75     sub type_of {
76     my $attribute = shift;
77    
78     return if !$attribute->has_type_constraint;
79     my $tc = $attribute->type_constraint;
80    
81     my @type_mapping = (
82     [Int => 'INTEGER'],
83     [Num => 'REAL'],
84     [Str => 'TEXT'],
85     [Bool => 'BOOLEAN'],
86     );
87    
88     for (@type_mapping) {
89     my ($moose_type, $sql_type) = @$_;
90     return $sql_type
91     if $tc->is_a_type_of($moose_type);
92     }
93    
94     return;
95     }
96    
97     sub default_of {
98     my $attribute = shift;
99    
100     return unless $attribute->has_default;
101    
102     my $default;
103     if ( $attribute->is_default_a_coderef ) {
104     $default = eval { $attribute->default->(); };
105     if ( $@ ) {
106     warn "can't eval default: $@";
107     return;
108     }
109     my $type = type_of($attribute);
110     if ( $type eq 'BOOLEAN' ) {
111     return ('DEFAULT', $default ? 'TRUE' : 'FALSE');
112     }
113     } else {
114     $default = $attribute->default;
115     }
116    
117     if ($default =~ /^\d+$/ ) {
118     return ('DEFAULT', $default);
119     }
120    
121     return ('DEFAULT', DBD::_::db->quote($default));
122     }
123    
124     sub foreign_key_of {
125     my $attribute = shift;
126    
127     return if !$attribute->has_type_constraint;
128     my $tc = $attribute->type_constraint;
129    
130     return if !$tc->isa('Moose::Meta::TypeConstraint::Class');
131     my $table = class_to_table($tc->class);
132    
133     return ('REFERENCES', $table, '(id)');
134     }
135    
136     =head1 SEE ALSO
137    
138     Based on code from L<http://github.com/sartak/mmop/raw/master/2-schema-generator.pl>
139    
140     L<http://blog.sartak.org/2009/06/mooses-mop-schematize.html>
141    
142     =cut
143    
144 dpavlin 1133 __PACKAGE__->meta->make_immutable;
145     no Moose;
146    
147 dpavlin 1093 1;

  ViewVC Help
Powered by ViewVC 1.1.26