File: factory

package info (click to toggle)
rtfm 2.0.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 812 kB
  • ctags: 220
  • sloc: perl: 3,426; sh: 153; makefile: 144
file content (284 lines) | stat: -rw-r--r-- 5,162 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/perl
# BEGIN LICENSE BLOCK
# 
#  Copyright (c) 2002-2003 Jesse Vincent <jesse@bestpractical.com>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of version 2 of the GNU General Public License 
#  as published by the Free Software Foundation.
# 
#  A copy of that license should have arrived with this
#  software, but in any event can be snarfed from www.gnu.org.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# 
# END LICENSE BLOCK

#$Header: /raid/cvsroot/fm/sbin/factory,v 1.1 2001/09/10 06:29:47 jesse Exp $
use DBI;

my $database = shift;
my $namespace = shift;

my $CollectionBaseclass = 'RT::FM::SearchBuilder';
my $RecordBaseclass = 'RT::FM::Record';

my $driver = 'mysql';
my $hostname = 'localhost';
my $user = 'root';
my $password = '';
my $dsn = "DBI:$driver:database=$database;host=$hostname";

my $dbh = DBI->connect($dsn, $user, $password);

 
#get all tables out of database
my @tables = $dbh->tables();

my %tablemap;

foreach my $table (@tables) {
	$tablemap{$table} = $table;
}
$tablemap{'CreatedBy'} = 'User';
$tablemap{'UpdatedBy'} = 'User';



foreach my $table (@tables) {

    my $tablesingle = $table;
    $tablesingle =~ s/s$//;

    my %requirements;

    my $CollectionClassName = $namespace."::".$tablesingle."Collection";
    my $RecordClassName = $namespace."::".$tablesingle;
    
    my $path = $namespace;
    $path =~ s/::/\//g;
    
    my $RecordClassPath = $path."/".$tablesingle.".pm";
    my $CollectionClassPath = $path."/".$tablesingle."Collection.pm";


    
    #create a collection class
    my $CreateInParams;
    my $CreateOutParams;
    my $ClassAccessible = "";
    my $FieldsPod = "";
    my $CreatePod = "";
    my $sth = $dbh->prepare("DESCRIBE $table");
    $sth->execute;
    while (my $row = $sth->fetchrow_hashref()) {
		my $field = $row->{'Field'};
		my $type = $row->{'Type'};
		my $default = $row->{'Default'};
		
		#generate the 'accessible' datastructure
		$ClassAccessible .= "        $field => {'read' => 1, 'write' => 1},\n";
		
		
		#generate pod for the accessible fields
		$FieldsPod .= "
=item $field

Returns the current value of $field. 
(In the database, $field is stored as $type.)


=item Set$field VALUE

Set $field to VALUE. 
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, $field will be stored as a $type.)

=cut

";

if ($tablemap{$field}) { 
$FieldsPod .= "
=item ${field}Obj

Returns the $tablemap{$field} Object which has the id returned by $field


=cut

sub ${field}Obj {
	my \$self = shift;
	my \$$field = new ${namespace}::$tablemap{$field}(\$self->CurrentUser);
	\$$field->Load(\$self->$field());
	return(\$$field);
}
";
$requirements{$tablemap{$field}} = "use ${namespace}::$tablemap{$field};";

}
		#generate create statement
		$CreateInParams .= "                $field => undef,\n";
                $CreateOutParams .= "                         $field => \$args{'$field'},\n";


		#gerenate pod for the create statement	
		$CreatePod .= "  $type '$field'";
		$CreatePod .= " defaults to '$default'" if ($default);
		$CreatePod .= ".\n";

	}

        $Create = "
sub Create {
    my \$self = shift;
    my \%args = ( 
$CreateInParams,
		  \@_);
    \$self->SUPER::Create(
$CreateOutParams);

}
";
	$CreatePod .= "\n=cut\n\n";


my $CollectionClass = "

# \$Header\$
# Autogenerated by DBIx::SearchBuilder factory  (by <jesse\@fsck.com>)

=head1 NAME

  $CollectionClassName -- Class Description
 
=head1 SYNOPSIS

  use $CollectionClassName

=head1 DESCRIPTION


=head1 METHODS

=cut

package $CollectionClassName;

use $CollectionBaseclass;
use $RecordClassName;

\@ISA= qw($CollectionBaseclass);


sub _Init {
    my \$self = shift;
    \$self->{'table'} = '$table';
    \$self->{'primary_key'} = 'id';
    return ( \$self->SUPER::_Init(\@_) );
}


=item NewItem

Returns an empty new $RecordClassName item

=cut

sub NewItem {
    my \$self = shift;
    return(new $RecordClassName(\@_));
}

1;
";

my $RecordClassHeader = "
# \$Header\$
# Autogenerated by DBIx::SearchBuilder factory  (by <jesse\@fsck.com>)

=head1 NAME

$RecordClassName


=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=cut

package $RecordClassName;
use $RecordBaseclass; 
";

foreach my $key (keys %requirements) {
	$RecordClassHeader .= $requirements{$key} . "\n";
}
$RecordClassHeader .="

use vars qw( \@ISA );
\@ISA= qw( $RecordBaseclass );

sub _Init {
  my \$self = shift; 

  \$self->Table('$table');
  \$self->SUPER::_Init(\@_);
}

";








my $RecordClass = "

$RecordClassHeader

$RecordInit

=item Create PARAMHASH

Create takes a hash of values and creates a row in the database:

$CreatePod

$Create

$FieldsPod

sub _ClassAccessible {
    {
     
$ClassAccessible
 }
};
1;
";


`mkdir -p $path`;

open (RECORD, ">$RecordClassPath");
print RECORD $RecordClass;
close (RECORD);

open (COL, ">$CollectionClassPath");
print COL $CollectionClass;
close($COL);

}