Line 4... |
Line 4... |
4 |
# NOTE: don't use comma's or parentheses in your comments
|
4 |
# NOTE: don't use comma's or parentheses in your comments
|
5 |
|
5 |
|
6 |
|
6 |
|
7 |
#use Data::Dumper;
|
7 |
#use Data::Dumper;
|
8 |
|
8 |
|
- |
|
9 |
my $secondary = 0;
|
- |
|
10 |
my $indentationChar = ' ';
|
- |
|
11 |
my $inputFileName = '';
|
- |
|
12 |
|
9 |
|
13 |
|
10 |
sub getFieldInfo {
|
14 |
sub getFieldInfo {
|
- |
|
15 |
my $fieldName = shift;
|
11 |
my $fieldInfo = shift;
|
16 |
my $fieldInfo = shift;
|
12 |
my $primary_key = shift;
|
17 |
my $primary_key = shift;
|
13 |
my @output;
|
18 |
my @output;
|
14 |
my $comment = '';
|
19 |
my $comment = '';
|
15 |
|
- |
|
- |
|
20 |
push @output, "'display name' => '$fieldName'";
|
16 |
# For a lookup table, we will just build it
|
21 |
# For a lookup table, we will just build it
|
17 |
if ( $fieldInfo =~ m/references\s+([a-z0-9-_]+)\s*\(\s*([a-z0-9-_]+)\s*\)/i ) { # create a lookup
|
22 |
if ( $fieldInfo =~ m/references\s+([a-z0-9-_]+)\s*\(\s*([a-z0-9-_]+)\s*\)/i ) { # create a lookup
|
18 |
push @output, "'type' => 'lookup', 'table' => '$1', 'keyfield' => '$2', 'display_field' => 'name'";
|
23 |
push @output, "'type' => 'lookup', 'table' => '$1', 'keyfield' => '$2', 'display_field' => 'name'";
|
19 |
} else { # this is just a standard field
|
24 |
} else { # this is just a standard field
|
20 |
push @output, "'keyfield' => true" if ( $primary_key);
|
25 |
push @output, "'keyfield' => true" if ( $primary_key);
|
Line 72... |
Line 77... |
72 |
$table =~ s/ +/ /gi; # remove duplicate spaces
|
77 |
$table =~ s/ +/ /gi; # remove duplicate spaces
|
73 |
$table =~ s/ ,/,/gi; # remove all spaces before comma's
|
78 |
$table =~ s/ ,/,/gi; # remove all spaces before comma's
|
74 |
#print "[$table]\n\n";
|
79 |
#print "[$table]\n\n";
|
75 |
$table =~ m/create table ([a-z0-9_]+) *\((.+)\)([^\(\)]*)$/i; #
|
80 |
$table =~ m/create table ([a-z0-9_]+) *\((.+)\)([^\(\)]*)$/i; #
|
76 |
$tableInformation{'table name'} = $1;
|
81 |
$tableInformation{'table name'} = $1;
|
- |
|
82 |
$tableInformation{'display name'} = $tableInformation{'table name'};
|
77 |
print STDERR "[$tableInformation{'table name'}]\n";
|
83 |
print STDERR "[$tableInformation{'table name'}]\n";
|
78 |
$table = $2;
|
84 |
$table = $2;
|
79 |
$tableCommentStuff = $3;
|
85 |
$tableCommentStuff = $3;
|
80 |
if ( $tableCommentStuff and $tableCommentStuff =~ m/comment.*\'(.*)\'/i ) {
|
86 |
if ( $tableCommentStuff and $tableCommentStuff =~ m/comment.*\'(.*)\'/i ) {
|
81 |
$tableInformation{'comment'} = $1;
|
87 |
$tableInformation{'comment'} = $1;
|
Line 93... |
Line 99... |
93 |
for ( $i = 0; $i < @columnInformation; $i++) {
|
99 |
for ( $i = 0; $i < @columnInformation; $i++) {
|
94 |
$columnInformation[$i] =~ s/^ +//;
|
100 |
$columnInformation[$i] =~ s/^ +//;
|
95 |
if ( $columnInformation[$i] =~ m/ *([a-z0-9_]+) +(.*)/i ) {
|
101 |
if ( $columnInformation[$i] =~ m/ *([a-z0-9_]+) +(.*)/i ) {
|
96 |
$fieldName = $1;
|
102 |
$fieldName = $1;
|
97 |
$fieldDef = $2;
|
103 |
$fieldDef = $2;
|
98 |
($fieldInfo{$fieldName}{'comment'},$fieldInfo{$fieldName}{'data'}) = &getFieldInfo($fieldDef, $tableInformation{'primary keys'} ? ($tableInformation{'primary keys'} =~ m/$fieldName/) : 0 );
|
104 |
($fieldInfo{$fieldName}{'comment'},$fieldInfo{$fieldName}{'data'}) = &getFieldInfo($fieldName, $fieldDef, $tableInformation{'primary keys'} ? ($tableInformation{'primary keys'} =~ m/$fieldName/) : 0 );
|
99 |
}
|
105 |
}
|
100 |
} # for
|
106 |
} # for
|
101 |
#$tableInformation{'fields'} = \%fieldInfo;
|
107 |
#$tableInformation{'fields'} = \%fieldInfo;
|
102 |
#print Dumper(\%fieldInfo );
|
108 |
#print Dumper(\%fieldInfo );
|
103 |
|
109 |
|
104 |
# create the output
|
110 |
# create the output
|
105 |
my $output = '';
|
111 |
my $output = '';
|
106 |
if ( $tableInformation{'comment'} ) {
|
112 |
if ( $tableInformation{'comment'} ) {
|
107 |
$output .= "/* $tableInformation{'comment'} */\n";
|
113 |
$output .= "/* $tableInformation{'comment'} */\n";
|
108 |
}
|
114 |
}
|
- |
|
115 |
if ( $secondary ) {
|
- |
|
116 |
$output = "\$DATABASE_DEFINITION['" . $tableInformation{'table name'} . "'] = array(\n";
|
- |
|
117 |
} else {
|
109 |
$output .= "'" . $tableInformation{'table name'} . "' => array( \n";
|
118 |
$output .= "'" . $tableInformation{'table name'} . "' => array( \n";
|
- |
|
119 |
}
|
110 |
$output .= "\t'table name' => '" . $tableInformation{'table name'} . "',\n";
|
120 |
$output .= "\t'table name' => '" . $tableInformation{'table name'} . "',\n";
|
111 |
if ($tableInformation{'primary keys'}) {
|
121 |
if ($tableInformation{'primary keys'}) {
|
112 |
if ( $tableInformation{'primary keys'} =~ m/,/ ) {
|
122 |
if ( $tableInformation{'primary keys'} =~ m/,/ ) {
|
113 |
#following line takes all items in a comma delimited list and quotes them
|
123 |
#following line takes all items in a comma delimited list and quotes them
|
114 |
$output .= "\t'key field' => array('" . join("','", split( ',',$tableInformation{'primary keys'})) . "'),\n";
|
124 |
$output .= "\t'key field' => array('" . join("','", split( ',',$tableInformation{'primary keys'})) . "'),\n";
|
Line 121... |
Line 131... |
121 |
#$fieldNames = join( ',',@fields );
|
131 |
#$fieldNames = join( ',',@fields );
|
122 |
$output .= "\t'display columns' => array('" . join( "','",@fields ) . "'),\n";
|
132 |
$output .= "\t'display columns' => array('" . join( "','",@fields ) . "'),\n";
|
123 |
$output .= "\t'display query' => 'select " . join( ',',@fields ) . " from " . $tableInformation{'table name'} . "',\n";
|
133 |
$output .= "\t'display query' => 'select " . join( ',',@fields ) . " from " . $tableInformation{'table name'} . "',\n";
|
124 |
my @fieldInfo;
|
134 |
my @fieldInfo;
|
125 |
foreach $thisField (keys %fieldInfo ) {
|
135 |
foreach $thisField (keys %fieldInfo ) {
|
126 |
push @fieldInfo, ;
|
- |
|
127 |
|
- |
|
128 |
push @fieldInfo, ($fieldInfo{$thisField}{'comment'} ? "\t\t/* " . $fieldInfo{$thisField}{'comment'} . " */\n" : '') .
|
136 |
push @fieldInfo, ($fieldInfo{$thisField}{'comment'} ? "\t\t/* " . $fieldInfo{$thisField}{'comment'} . " */\n" : '') .
|
129 |
"\t\t'$thisField' => array(" . $fieldInfo{$thisField}{'data'} . ")";
|
137 |
"\t\t'$thisField' => array(" . $fieldInfo{$thisField}{'data'} . ")";
|
130 |
}
|
138 |
}
|
131 |
$output .= "\t'field info' => array(\n" . join( ",\n", @fieldInfo) . "\n\t)\n)";
|
139 |
$output .= "\t'field info' => array(\n" . join( ",\n", @fieldInfo) . "\n\t)\n)";
|
132 |
|
140 |
|
133 |
return $output;
|
141 |
return $output;
|
134 |
}
|
142 |
}
|
135 |
|
143 |
|
- |
|
144 |
sub processCommandLine {
|
- |
|
145 |
while ( $parameter = shift ) {
|
- |
|
146 |
if ($parameter =~ m/-f(.*)/i) {
|
- |
|
147 |
$inputFileName = $1;
|
- |
|
148 |
} elsif ( $parameter =~ m/-s/i) {
|
136 |
my $indentationChar = ' ';
|
149 |
$secondary = 1;
|
- |
|
150 |
}
|
- |
|
151 |
}
|
- |
|
152 |
}
|
137 |
|
153 |
|
138 |
my $inputFileName = shift;
|
154 |
&processCommandLine( @ARGV );
|
- |
|
155 |
|
- |
|
156 |
# die "Input Filename == $inputFileName, secondary = $secondary\n";
|
- |
|
157 |
|
139 |
if ($inputFileName) {
|
158 |
if ($inputFileName) { # they want us to read from this file
|
140 |
open DATA, "$inputFileName" or die "could not open $inputFileName";
|
159 |
open DATA, "$inputFileName" or die "could not open $inputFileName";
|
141 |
@input = <DATA>;
|
160 |
@input = <DATA>;
|
142 |
close DATA;
|
161 |
close DATA;
|
143 |
} else {
|
162 |
} else { # no filename entered, so read STDIN
|
144 |
@input = <STDIN>;
|
163 |
@input = <STDIN>;
|
145 |
}
|
164 |
}
|
146 |
@input = grep( !/^(--)|(\/\*\!)/, @input );
|
165 |
@input = grep( !/^(--)|(\/\*\!)/, @input ); # remove all comments
|
- |
|
166 |
chomp @input; # get rid of all line endings
|
147 |
|
167 |
|
148 |
|
- |
|
149 |
chomp @input;
|
- |
|
150 |
# next line read all input, joins it with null,then splits it based on a semi-colon
|
168 |
# next line read all input, joins it with null,then splits it based on a semi-colon
|
151 |
# this places all sql statements into one array entry
|
169 |
# this places all sql statements into one array entry
|
152 |
# It then filters for only those statements that start with create table
|
170 |
# It then filters for only those statements that start with create table
|
153 |
my @tables = grep(/^create table/i, split( ';', join( '', @input) ));
|
171 |
my @tables = grep(/^create table/i, split( ';', join( '', @input) ));
|
154 |
#foreach my $thisTable (@tables) {
|
172 |
#foreach my $thisTable (@tables) {
|
Line 169... |
Line 187... |
169 |
$results[$line] = "\t" . $results[$line];
|
187 |
$results[$line] = "\t" . $results[$line];
|
170 |
$results[$line] =~ s/\n/\n\t/g;
|
188 |
$results[$line] =~ s/\n/\n\t/g;
|
171 |
$results[$line] =~ s/\t/$indentationChar/g;
|
189 |
$results[$line] =~ s/\t/$indentationChar/g;
|
172 |
}
|
190 |
}
|
173 |
|
191 |
|
- |
|
192 |
# now, actually print out the results
|
- |
|
193 |
print "<?php\nglobal \$DATABASE_DEFINITION;\n";
|
- |
|
194 |
if ( $secondary ) { # this is an auxilary file
|
174 |
print "<?php\n";
|
195 |
print join( ";\n", @results) ;
|
- |
|
196 |
} else { # this is the main, or only database.php file
|
175 |
print "define ( MAX_INPUT_FIELD_DISPLAY, 40 ); // this is the maximum input field size
|
197 |
print "define ( MAX_INPUT_FIELD_DISPLAY, 40 ); // this is the maximum input field size
|
176 |
define ( IMAGE_DIRECTORY, '/pictures/' ); // relative URL where pictures are stored
|
198 |
define ( IMAGE_DIRECTORY, '/pictures/' ); // relative URL where pictures are stored
|
177 |
define ( EDIT_IMAGE_HEIGHT, 100 ); // height for thumbnail of pictuers
|
199 |
define ( EDIT_IMAGE_HEIGHT, 100 ); // height for thumbnail of pictuers
|
178 |
define ( MAX_UPLOAD_FILE_SIZE, 1024*1024*10 ); // 10 meg
|
200 |
define ( MAX_UPLOAD_FILE_SIZE, 1024*1024*10 ); // 10 meg
|
179 |
define (DEFAULT_TEXTAREA_HEIGHT, 5 );
|
201 |
define (DEFAULT_TEXTAREA_HEIGHT, 5 );
|
180 |
define ( DEFAULT_TABLE, 'FILL ME IN');
|
202 |
define ( DEFAULT_TABLE, 'FILL ME IN');
|
- |
|
203 |
|
- |
|
204 |
";
|
181 |
global \$DATABASE_DEFINITION;
|
205 |
print "\$DATABASE_DEFINITION = array(\n";
|
- |
|
206 |
print join( ",\n", @results) ;
|
- |
|
207 |
print "\n);";
|
182 |
|
208 |
|
183 |
";
|
209 |
}
|
184 |
print "\$DATABASE_DEFINITION = array(\n" . join( ",\n", @results) . "\n);";
|
- |
|
- |
|
210 |
|
185 |
print "\n?>\n";
|
211 |
print "\n?>\n";
|
- |
|
212 |
|
186 |
1;
|
213 |
1;
|
187 |
|
214 |
|
188 |
|
215 |
|
189 |
|
216 |
|
190 |
|
217 |
|