/[A3C]/bin/pgsql2model.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /bin/pgsql2model.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 181 - (show annotations)
Mon Jun 16 20:08:28 2008 UTC (11 years, 1 month ago) by dpavlin
File MIME type: text/plain
File size: 2666 byte(s)
make ldap2model use JiftyModelCreator and minor tweaks all over the place
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 =head1 NAME
6
7 pgsql2model.pl - convert SQL query into Jifty model
8
9 =head1 DESCRIPTION
10
11 Create model from SQL query
12
13 ./bin/pgsql2model.pl --model StrixSites --instance new --table site
14
15 With C<--debug> switch all output will go to C<STDOUT>
16 instead to files.
17
18 =cut
19
20 use lib 'lib';
21
22 use JiftyModelCreator;
23 use Strix;
24 use Data::Dump qw/dump/;
25 use Getopt::Long;
26
27 my ( $ModelName, $table, $instance, $debug ) = ( 'StrixSite', 'site', 'new' );
28
29 GetOptions(
30 'model=s' => \$ModelName,
31 'instance=s' => \$instance,
32 'debug+' => \$debug,
33 );
34
35 die "usage: $0 --model StrixSites --sql 'select * from site'\n" unless $ModelName;
36
37 my $model = qq/package A3C::Model::$ModelName;
38 use strict;
39 use warnings;
40
41 use Jifty::DBI::Schema;
42
43 use A3C::Record schema {
44
45 /;
46
47 my $methods;
48 my $columns;
49 my $create;
50
51 my $dbh = Strix->dbh( $instance );
52
53 sub model_columns {
54 my $ModelName = shift;
55 my $model;
56 warn "Creating columns for model $ModelName\n";
57
58 my $aliases = qq/\n=head1 $ModelName helper methods\n=cut\n\n/;
59
60 sub entry {
61 my ( $e, $add ) = @_;
62 warn "# entry ",dump( $e, $add );
63 my $name = delete($e->{name}) || die "no name?";
64
65 if ( $columns->{$name} ) {
66 warn "WARNING: column $name found again, skipping...\n";
67 return '';
68 }
69 $columns->{$name}++;
70
71 $aliases .= qq/sub $_ { \$_[0]->$name }\n/ foreach delete($e->{aliases});
72
73 my $label = delete($e->{label}) || ucfirst($name);
74 $label =~ s/_/ /g;
75
76 my $out = qq/\tcolumn $name =>\n\t\tlabel is _('$label')/;
77
78 foreach my $n ( keys %$e ) {
79 # filters
80 my $v = $e->{$n};
81 if ( ref($v) eq 'ARRAY' ) {
82 $out .= qq/,\n\t\t$n are / . dump($v);
83 } elsif ( $n eq 'mandatory' ) {
84 if ( $v ) {
85 $out .= qq/,\n\t\tis $n/;
86 $create->{$name} = $e->{type} =~ m/int/ ? 42 : $label;
87 }
88 } elsif ( $v =~ m/^\d+$/ ) {
89 $out .= qq/,\n\t\t$n is $v/;
90 } else {
91 $out .= qq/,\n\t\t$n is '$v'/;
92 }
93 }
94
95 $out .= qq/;\n\n/;
96 return $out;
97 }
98
99 my $sth = $dbh->column_info ( undef, undef, $table, '%' );
100
101 while ( my $row = $sth->fetchrow_hashref ) {
102 warn "# column_info ",dump( $row );
103 $model .= entry({
104 name => $row->{COLUMN_NAME},
105 type => $row->{TYPE_NAME},
106 mandatory => ! $row->{NULLABLE},
107 });
108 }
109
110 $methods .= $aliases unless $aliases;
111
112 return $model;
113 }
114
115 $model .= model_columns( $ModelName );
116
117 $methods .= qq/\n=head2 name\n\nAuto-generated human readable id for generic access to name\n\n=cut\n\nsub name { \$_[0]->id }\n/ unless $columns->{name};
118
119 $model .= qq/
120
121 };
122
123 $methods
124
125 =head2 ACL
126
127 We use L<A3C::DefaultACL> for access control
128
129 =cut
130
131 use A3C::DefaultACL;
132
133 1;
134 /;
135
136 JiftyModelCreator->write( $ModelName, $model, $create );
137

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26