Subversion Repositories phpLibraryV2

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 3
Line 1... Line 1...
1
#! /usr/bin/perl -w
1
#! /usr/bin/env perl
2
 
2
 
-
 
3
# sql2admin_hash.pl
-
 
4
#
3
# Converts a standard MySQL dump file to a PHP file ready to be used by library.php.
5
# Converts a standard MySQL dump file to a PHP file ready to be used by 
-
 
6
# DBDatabase.class.php
4
# NOTE: don't use comma's or parentheses in your comments
7
# NOTE: don't use comma's or parentheses in your comments
-
 
8
#
-
 
9
# v1.1 RWR 20170728
-
 
10
# modified to use env and strict/warnings.
-
 
11
 
-
 
12
use strict;
-
 
13
use warnings;
5
 
14
 
6
 
15
 
7
#use Data::Dumper;
16
#use Data::Dumper;
8
my $version = '1.01';;
17
my $version = '1.1';
9
my $secondary = 0;
18
my $secondary = 0;
10
my $indentationChar = '   ';
19
my $indentationChar = '   ';
11
my $inputFileName = '';
20
my $inputFileName = '';
-
 
21
my $localOverrideFile = 'DatabaseDefinition.local.php';
12
 
22
 
-
 
23
# converts a field or table name to a display value by separating
-
 
24
# words, based on underscores, then doing a camel case on each
-
 
25
# word.
13
sub camelCase {
26
sub camelCase {
14
   my $name = shift;
27
   my $name = shift;
-
 
28
   $name =~ s/_/ /gi; # convert underscore to spaces
15
   $name =~ s/_/ /gi;
29
   $name =~ s/^ //gi; # remove leading spaces
16
   $name =~ s{ \b(\w+) }{\u\L$1}gx;
30
   $name =~ s{ \b(\w+) }{\u\L$1}gx; # Each "word" capitalized
17
   return $name;
31
   return $name;
18
}
32
}
19
 
33
 
20
sub getFieldInfo {
34
sub getFieldInfo {
21
   my $fieldName = shift;
35
   my $fieldName = shift;
22
   my $fieldInfo = shift;
36
   my $fieldInfo = shift;
23
#   $original = $fieldInfo;
37
#   $original = $fieldInfo;
24
   my $primary_key = shift;
38
   my $primary_key = shift;
25
   my @keys;
39
   my @keys;
26
   my @output;
40
   my @output;
-
 
41
 
-
 
42
   my $default;
-
 
43
   my $type;
-
 
44
   my $size;
-
 
45
 
27
   push @output, "'display name' => '" . &camelCase($fieldName) . "'";
46
   push @output, "'display name' => '" . &camelCase($fieldName) . "'";
28
   # For a lookup table, we will just build it
47
   # For a lookup table, we will just build it
29
   if ( $fieldInfo =~ m/references\s+([a-z0-9-_]+)\s*\(\s*([a-z0-9-_]+)\s*\)/i ) { # create a lookup
48
   if ( $fieldInfo =~ m/references\s+([a-z0-9-_]+)\s*\(\s*([a-z0-9-_]+)\s*\)/i ) { # create a lookup
30
      push @output, "'type' => 'lookup'";
49
      push @output, "'type' => 'lookup'";
31
      push @output, "'table' => '$1'";
50
      push @output, "'table' => '$1'";
Line 101... Line 120...
101
   #print "[$table]\n\n";
120
   #print "[$table]\n\n";
102
   $table =~ m/create table ([a-z0-9_]+) *\((.+)\)([^\(\)]*)$/i; # 
121
   $table =~ m/create table ([a-z0-9_]+) *\((.+)\)([^\(\)]*)$/i; # 
103
   $tableInformation{'table name'} = $1;
122
   $tableInformation{'table name'} = $1;
104
   print STDERR "[$tableInformation{'table name'}]\n";
123
   print STDERR "[$tableInformation{'table name'}]\n";
105
   $table = $2;
124
   $table = $2;
106
   $tableCommentStuff = $3;
125
   my $tableCommentStuff = $3;
107
   if ( $tableCommentStuff and $tableCommentStuff =~ m/comment.*\'(.*)\'/i ) {
126
   if ( $tableCommentStuff and $tableCommentStuff =~ m/comment.*\'(.*)\'/i ) {
108
      $tableInformation{'comment'} = $1;
127
      $tableInformation{'comment'} = $1;
109
   }
128
   }
110
   if ( $table =~ m/$primary_key_regex/i ) {
129
   if ( $table =~ m/$primary_key_regex/i ) {
111
      $tableInformation{'primary keys'} = $1;
130
      $tableInformation{'primary keys'} = $1;
Line 116... Line 135...
116
      $table =~ s/$unique_key_regex//i;
135
      $table =~ s/$unique_key_regex//i;
117
   }
136
   }
118
   my @columnInformation = split(',',$table);
137
   my @columnInformation = split(',',$table);
119
   my %fieldInfo;
138
   my %fieldInfo;
120
   my @fieldOrder; # I use an array here so we can do display, etc... in original order
139
   my @fieldOrder; # I use an array here so we can do display, etc... in original order
121
   for ( $i = 0; $i < @columnInformation; $i++) {
140
   for ( my $i = 0; $i < @columnInformation; $i++) {
122
      $columnInformation[$i] =~ s/^ +//;
141
      $columnInformation[$i] =~ s/^ +//;
123
      if ( $columnInformation[$i] =~ m/ *([a-z0-9_]+) +(.*)/i )  {
142
      if ( $columnInformation[$i] =~ m/ *([a-z0-9_]+) +(.*)/i )  {
124
         $fieldName = $1;
143
         my $fieldName = $1;
125
         push @fieldOrder, $fieldName;
144
         push @fieldOrder, $fieldName;
126
         $fieldDef = $2;
145
         my $fieldDef = $2;
127
         $fieldInfo{$fieldName}{'data'} = 
146
         $fieldInfo{$fieldName}{'data'} = 
128
           &getFieldInfo($fieldName, $fieldDef, $tableInformation{'primary keys'} 
147
           &getFieldInfo($fieldName, $fieldDef, $tableInformation{'primary keys'} 
129
              ? ($tableInformation{'primary keys'} =~ m/$fieldName/) 
148
              ? ($tableInformation{'primary keys'} =~ m/$fieldName/) 
130
              : 0
149
              : 0
131
           );
150
           );
Line 167... Line 186...
167
   #@fields = keys %fieldInfo;
186
   #@fields = keys %fieldInfo;
168
   #$fieldNames = join( ',',@fields );
187
   #$fieldNames = join( ',',@fields );
169
   $output .= "\t'display columns' => array(\n\t\t'" . join( "',\n\t\t'",@fieldOrder ) . "'\n\t),\n";
188
   $output .= "\t'display columns' => array(\n\t\t'" . join( "',\n\t\t'",@fieldOrder ) . "'\n\t),\n";
170
   $output .= "\t'display query' => 'select " . join( ',',@fieldOrder ) . " from " . $tableInformation{'table name'} . "',\n";
189
   $output .= "\t'display query' => 'select " . join( ',',@fieldOrder ) . " from " . $tableInformation{'table name'} . "',\n";
171
   my @fieldInfo;
190
   my @fieldInfo;
172
   foreach $thisField ( @fieldOrder ) {
191
   foreach my $thisField ( @fieldOrder ) {
173
      push @fieldInfo,  ($fieldInfo{$thisField}{'comment'} ? "\t\t/* " . $fieldInfo{$thisField}{'comment'} . " */\n" : '') .
192
      push @fieldInfo,  ($fieldInfo{$thisField}{'comment'} ? "\t\t/* " . $fieldInfo{$thisField}{'comment'} . " */\n" : '') .
174
                        "\t\t'$thisField' => array(" . $fieldInfo{$thisField}{'data'} . ")";
193
                        "\t\t'$thisField' => array(" . $fieldInfo{$thisField}{'data'} . ")";
175
   }
194
   }
176
   $output .= "\t'field info' => array(\n" . join( ",\n", @fieldInfo) . "\n\t)\n)";
195
   $output .= "\t'field info' => array(\n" . join( ",\n", @fieldInfo) . "\n\t)\n)";
177
 
196
 
178
   return $output;
197
   return $output;
179
}
198
}
180
 
199
 
181
sub processCommandLine {
200
sub processCommandLine {
182
   while ( $parameter = shift ) {
201
   while ( my $parameter = shift ) {
183
      if ($parameter =~ m/-f(.*)/i) {
202
      if ($parameter =~ m/-f(.*)/i) {
184
         $inputFileName = $1;
203
         $inputFileName = $1;
185
      } elsif ( $parameter =~ m/-s/i) {
204
      } elsif ( $parameter =~ m/-s/i) {
186
         $secondary = 1;
205
         $secondary = 1;
187
      }
206
      }
Line 190... Line 209...
190
 
209
 
191
&processCommandLine( @ARGV );
210
&processCommandLine( @ARGV );
192
 
211
 
193
# die "Input Filename == $inputFileName, secondary = $secondary\n";
212
# die "Input Filename == $inputFileName, secondary = $secondary\n";
194
 
213
 
-
 
214
my @input;
-
 
215
 
195
if ($inputFileName) { # they want us to read from this file
216
if ($inputFileName) { # they want us to read from this file
196
   open DATA, "$inputFileName" or die "could not open $inputFileName";
217
   open DATA, "$inputFileName" or die "could not open $inputFileName";
197
   @input = <DATA>;
218
   @input = <DATA>;
198
   close DATA;
219
   close DATA;
199
} else { # no filename entered, so read STDIN
220
} else { # no filename entered, so read STDIN
Line 211... Line 232...
211
#}
232
#}
212
#exit 1;
233
#exit 1;
213
 
234
 
214
my @results;
235
my @results;
215
#push @results, &getTableDef( shift @tables );
236
#push @results, &getTableDef( shift @tables );
216
foreach $thisTable( @tables ) {
237
foreach my $thisTable( @tables ) {
217
   $thisTable =~ s/\`//gi;
238
   $thisTable =~ s/\`//gi;
218
   push @results, &getTableDef( $thisTable ) if $thisTable;
239
   push @results, &getTableDef( $thisTable ) if $thisTable;
219
}
240
}
220
 
241
 
221
# apply proper indentation. The code inserts a tab char and does a decent job of indenting
242
# apply proper indentation. The code inserts a tab char and does a decent job of indenting
Line 229... Line 250...
229
# now, actually print out the results
250
# now, actually print out the results
230
print "<?php
251
print "<?php
231
 
252
 
232
/* 
253
/* 
233
   Auto generated from SQL script by $0 version $version. 
254
   Auto generated from SQL script by $0 version $version. 
234
   do not edit this file. Edit database_def_local.php as changes
255
   do not edit this file. Edit $localOverrideFile as changes
235
   to that file will not be overwritten by subsequent runs of this
256
   to that file will not be overwritten by subsequent runs of this
236
   script.
257
   script.
237
*/
258
*/
238
 
259
 
239
global \$DATABASE_DEFINITION; // make variable available to all scripts
260
global \$DATABASE_DEFINITION; // make variable available to all scripts
Line 254... Line 275...
254
   print join( ",\n", @results) ;
275
   print join( ",\n", @results) ;
255
   print "\n);";
276
   print "\n);";
256
 
277
 
257
}
278
}
258
 
279
 
259
print "\ninclude('database_def_local.php');\n?>\n";
280
print "\ninclude('$localOverrideFile');\n?>\n";
260
 
281
 
261
1;
282
1;
262
 
283
 
263
 
284
 
264
 
285