| 1 | 
           rodolico | 
           1 | 
           #! /usr/bin/perl -w
  | 
        
        
            | 
            | 
           2 | 
           #use Data::Dumper;
  | 
        
        
            | 
            | 
           3 | 
              | 
        
        
            | 
            | 
           4 | 
              | 
        
        
            | 
            | 
           5 | 
           sub getFieldInfo {
  | 
        
        
            | 
            | 
           6 | 
              my $fieldInfo = shift;
  | 
        
        
            | 
            | 
           7 | 
              my $primary_key = shift;
  | 
        
        
            | 
            | 
           8 | 
              my @output;
  | 
        
        
            | 
            | 
           9 | 
              | 
        
        
           | 5 | 
           rodolico | 
           10 | 
              | 
        
        
           | 1 | 
           rodolico | 
           11 | 
              push @output, "'keyfield' => 1" if ( $primary_key);
  | 
        
        
            | 
            | 
           12 | 
              if ($fieldInfo =~ m/not null/i ) {
  | 
        
        
            | 
            | 
           13 | 
                 push @output, "'required' => true";
  | 
        
        
            | 
            | 
           14 | 
                 $fieldInfo =~ s/not null//i;  # remove the not null from the string
  | 
        
        
            | 
            | 
           15 | 
              }
  | 
        
        
           | 5 | 
           rodolico | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
              if ($fieldInfo =~ m/comment *\'([^']+)\'/i ) {
  | 
        
        
            | 
            | 
           18 | 
                 $comment = $1;
  | 
        
        
            | 
            | 
           19 | 
                 $fieldInfo =~ s/comment *\'([^']+)\'//i
  | 
        
        
            | 
            | 
           20 | 
              } else {
  | 
        
        
            | 
            | 
           21 | 
                 $comment = '';
  | 
        
        
            | 
            | 
           22 | 
              }
  | 
        
        
            | 
            | 
           23 | 
              | 
        
        
           | 1 | 
           rodolico | 
           24 | 
              $fieldInfo =~ s/default +null//i; # we just ignore default null
  | 
        
        
           | 5 | 
           rodolico | 
           25 | 
              if ($fieldInfo =~ m/default +(\'.*\')/i ) {
  | 
        
        
           | 1 | 
           rodolico | 
           26 | 
                 $default = $1;
  | 
        
        
           | 5 | 
           rodolico | 
           27 | 
                 $fieldInfo =~ s/default +(\'.*\')//i;
  | 
        
        
            | 
            | 
           28 | 
                 push @output, "'default' => $default";
  | 
        
        
            | 
            | 
           29 | 
              } elsif ($fieldInfo =~ /default +([^ ]+)/i ) {
  | 
        
        
            | 
            | 
           30 | 
                 $default = $1;
  | 
        
        
           | 1 | 
           rodolico | 
           31 | 
                 $fieldInfo =~ s/default +([^ ]+)//;
  | 
        
        
            | 
            | 
           32 | 
                 push @output, "'default' => $default";
  | 
        
        
            | 
            | 
           33 | 
              }
  | 
        
        
            | 
            | 
           34 | 
              if ($fieldInfo =~ m/auto_increment/i ) {
  | 
        
        
            | 
            | 
           35 | 
                 push @output, "'readonly' => true";
  | 
        
        
            | 
            | 
           36 | 
                 $fieldInfo =~ s/auto_increment//i;
  | 
        
        
            | 
            | 
           37 | 
              }
  | 
        
        
            | 
            | 
           38 | 
              # at this point, we should only have the data type left
  | 
        
        
            | 
            | 
           39 | 
              if ( $fieldInfo =~ m/ *([a-z]+)( *\(\d+\))?/i ) {
  | 
        
        
            | 
            | 
           40 | 
                 $type = $1;
  | 
        
        
            | 
            | 
           41 | 
                 $size = $2;
  | 
        
        
            | 
            | 
           42 | 
                 $type =~ s/(varchar)|(char)/string/i;
  | 
        
        
            | 
            | 
           43 | 
                 push @output, "'type' => '$type'";
  | 
        
        
            | 
            | 
           44 | 
                 if ($size) {
  | 
        
        
            | 
            | 
           45 | 
                    $size =~ s/[^0-9]//gi;
  | 
        
        
            | 
            | 
           46 | 
                    push @output, "'width' => $size";
  | 
        
        
            | 
            | 
           47 | 
                 } # if
  | 
        
        
            | 
            | 
           48 | 
              }
  | 
        
        
            | 
            | 
           49 | 
              #return "\n//$original\n\n" . join(" , ", @output);
  | 
        
        
           | 5 | 
           rodolico | 
           50 | 
              return ($comment, join(" , ", @output));
  | 
        
        
           | 1 | 
           rodolico | 
           51 | 
           }
  | 
        
        
            | 
            | 
           52 | 
              | 
        
        
            | 
            | 
           53 | 
              | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
           sub getTableDef {
  | 
        
        
            | 
            | 
           56 | 
              my $table = shift;
  | 
        
        
           | 5 | 
           rodolico | 
           57 | 
              print "$table\n";
  | 
        
        
           | 1 | 
           rodolico | 
           58 | 
              my $unique_key_regex = 'unique key [a-z0-9]+ \(([^)]+)\),?';
  | 
        
        
            | 
            | 
           59 | 
              my $primary_key_regex = 'primary key \(([^)]+)\),?';
  | 
        
        
           | 5 | 
           rodolico | 
           60 | 
              my $comment_regex = "comment *'([^']+)'";
  | 
        
        
           | 1 | 
           rodolico | 
           61 | 
              my %tableInformation;
  | 
        
        
            | 
            | 
           62 | 
              $table =~ s/ +/ /gi; # remove duplicate spaces
  | 
        
        
            | 
            | 
           63 | 
              $table =~ s/ ,/,/gi; # remove all spaces before comma's
  | 
        
        
           | 5 | 
           rodolico | 
           64 | 
              print "$table\n";
  | 
        
        
            | 
            | 
           65 | 
              $table =~ m/create table ([a-z0-9_]+) *\((.+)\)([^\(\)]*)$/i; # 
  | 
        
        
           | 1 | 
           rodolico | 
           66 | 
              $tableInformation{'table name'} = $1;
  | 
        
        
            | 
            | 
           67 | 
              $table = $2;
  | 
        
        
           | 5 | 
           rodolico | 
           68 | 
              $tableCommentStuff = $3;
  | 
        
        
            | 
            | 
           69 | 
              if ( $tableCommentStuff and $tableCommentStuff =~ m/comment.*\'(.*)\'/i ) {
  | 
        
        
            | 
            | 
           70 | 
                 $tableInformation{'comment'} = $1;
  | 
        
        
            | 
            | 
           71 | 
              }
  | 
        
        
           | 1 | 
           rodolico | 
           72 | 
              if ( $table =~ m/$primary_key_regex/i ) {
  | 
        
        
            | 
            | 
           73 | 
                 $tableInformation{'primary keys'} = $1;
  | 
        
        
            | 
            | 
           74 | 
                 $table =~ s/$primary_key_regex//i;
  | 
        
        
            | 
            | 
           75 | 
              }
  | 
        
        
            | 
            | 
           76 | 
              if ( $table =~ m/$unique_key_regex/i ) {
  | 
        
        
            | 
            | 
           77 | 
                 $tableInformation{'unique keys'} = $1;
  | 
        
        
            | 
            | 
           78 | 
                 $table =~ s/$unique_key_regex//i;
  | 
        
        
            | 
            | 
           79 | 
              }
  | 
        
        
            | 
            | 
           80 | 
              my @columnInformation = split(',',$table);
  | 
        
        
            | 
            | 
           81 | 
              my %fieldInfo;
  | 
        
        
            | 
            | 
           82 | 
              for ( $i = 0; $i < @columnInformation; $i++) {
  | 
        
        
            | 
            | 
           83 | 
                 $columnInformation[$i] =~ s/^ +//;
  | 
        
        
            | 
            | 
           84 | 
                 if ( $columnInformation[$i] =~ m/ *([a-z0-9_]+) +(.*)/i )  {
  | 
        
        
            | 
            | 
           85 | 
                    $fieldName = $1;
  | 
        
        
            | 
            | 
           86 | 
                    $fieldDef = $2;
  | 
        
        
           | 5 | 
           rodolico | 
           87 | 
                    ($fieldInfo{$fieldName}{'comment'},$fieldInfo{$fieldName}{'data'}) = &getFieldInfo($fieldDef, $tableInformation{'primary keys'} ? ($tableInformation{'primary keys'} =~ m/$fieldName/) : 0 );
  | 
        
        
           | 1 | 
           rodolico | 
           88 | 
                 }
  | 
        
        
            | 
            | 
           89 | 
              } # for
  | 
        
        
            | 
            | 
           90 | 
              #$tableInformation{'fields'} = \%fieldInfo;
  | 
        
        
            | 
            | 
           91 | 
              #print Dumper(\%fieldInfo );
  | 
        
        
            | 
            | 
           92 | 
              my $output = "'" .  $tableInformation{'table name'} . "' => array( \n";
  | 
        
        
           | 5 | 
           rodolico | 
           93 | 
              if ( $tableInformation{'comment'} ) {
  | 
        
        
            | 
            | 
           94 | 
                 $output .= "/* $tableInformation{'comment'} */\n";
  | 
        
        
            | 
            | 
           95 | 
              }
  | 
        
        
           | 1 | 
           rodolico | 
           96 | 
              $output .= "\t'table name' => '" . $tableInformation{'table name'} . "',\n";
  | 
        
        
            | 
            | 
           97 | 
              if ($tableInformation{'primary keys'}) {
  | 
        
        
            | 
            | 
           98 | 
                 if ( $tableInformation{'primary keys'} =~ m/,/ ) {
  | 
        
        
            | 
            | 
           99 | 
                    #following line takes all items in a comma delimited list and quotes them
  | 
        
        
            | 
            | 
           100 | 
                    $output .= "\t'key field' => array('" . join("','", split( ',',$tableInformation{'primary keys'})) . "'),\n";
  | 
        
        
            | 
            | 
           101 | 
                 } else {
  | 
        
        
            | 
            | 
           102 | 
                    $output .= "\t'key field' => '" . $tableInformation{'primary keys'} . "',\n";
  | 
        
        
            | 
            | 
           103 | 
                 }
  | 
        
        
            | 
            | 
           104 | 
              }
  | 
        
        
            | 
            | 
           105 | 
              # do the display columns, basically all columns
  | 
        
        
            | 
            | 
           106 | 
              @fields = keys %fieldInfo;
  | 
        
        
            | 
            | 
           107 | 
              #$fieldNames = join( ',',@fields );
  | 
        
        
            | 
            | 
           108 | 
              $output .= "\t'display columns' => array('" . join( "','",@fields ) . "'),\n";
  | 
        
        
            | 
            | 
           109 | 
              $output .= "\t'display query' => 'select " . join( ',',@fields ) . " from " . $tableInformation{'table name'} . "',\n";
  | 
        
        
            | 
            | 
           110 | 
              my @fieldInfo;
  | 
        
        
            | 
            | 
           111 | 
              foreach $thisField (keys %fieldInfo ) {
  | 
        
        
           | 5 | 
           rodolico | 
           112 | 
                 push @fieldInfo, "/* " . $fieldInfo{$thisField}{'comment'} . " */" if $fieldInfo{$thisField}{'comment'};
  | 
        
        
            | 
            | 
           113 | 
                 push @fieldInfo,  "'$thisField' => array(" . $fieldInfo{$thisField}{'data'} . ")";
  | 
        
        
           | 1 | 
           rodolico | 
           114 | 
              }
  | 
        
        
            | 
            | 
           115 | 
              $output .= "\t'field info' => array(\n\t\t" . join( ",\n\t\t", @fieldInfo) . "\n\t)\n)";
  | 
        
        
           | 5 | 
           rodolico | 
           116 | 
              | 
        
        
           | 1 | 
           rodolico | 
           117 | 
              return $output;
  | 
        
        
            | 
            | 
           118 | 
           }
  | 
        
        
            | 
            | 
           119 | 
              | 
        
        
            | 
            | 
           120 | 
           my $inputFileName = shift;
  | 
        
        
           | 5 | 
           rodolico | 
           121 | 
           if ($inputFileName) {
  | 
        
        
            | 
            | 
           122 | 
              open DATA, "$inputFileName" or die "could not open $inputFileName";
  | 
        
        
            | 
            | 
           123 | 
              @input = <DATA>;
  | 
        
        
            | 
            | 
           124 | 
              close DATA;
  | 
        
        
            | 
            | 
           125 | 
           } else {
  | 
        
        
            | 
            | 
           126 | 
              @input = <STDIN>;
  | 
        
        
           | 1 | 
           rodolico | 
           127 | 
           }
  | 
        
        
           | 5 | 
           rodolico | 
           128 | 
           @input = grep( !/^(--)|(\/\*\!)/, @input );
  | 
        
        
           | 1 | 
           rodolico | 
           129 | 
              | 
        
        
           | 5 | 
           rodolico | 
           130 | 
              | 
        
        
           | 1 | 
           rodolico | 
           131 | 
           chomp @input;
  | 
        
        
            | 
            | 
           132 | 
           my @tables = split( ';', join( '', @input) );
  | 
        
        
            | 
            | 
           133 | 
           my @results;
  | 
        
        
            | 
            | 
           134 | 
           #push @results, &getTableDef( shift @tables );
  | 
        
        
            | 
            | 
           135 | 
           foreach $thisTable( @tables ) {
  | 
        
        
           | 5 | 
           rodolico | 
           136 | 
              $thisTable =~ s/\`//gi;
  | 
        
        
           | 1 | 
           rodolico | 
           137 | 
              push @results, &getTableDef( $thisTable );
  | 
        
        
            | 
            | 
           138 | 
           }
  | 
        
        
            | 
            | 
           139 | 
              | 
        
        
            | 
            | 
           140 | 
           print "<?php\n";
  | 
        
        
            | 
            | 
           141 | 
           print "define ( MAX_INPUT_FIELD_DISPLAY, 40 ); // this is the maximum input field size
  | 
        
        
            | 
            | 
           142 | 
           define ( IMAGE_DIRECTORY, '/pictures/' );  // relative URL where pictures are stored
  | 
        
        
            | 
            | 
           143 | 
           define ( EDIT_IMAGE_HEIGHT, 100 );         // height for thumbnail of pictuers
  | 
        
        
            | 
            | 
           144 | 
           define ( MAX_UPLOAD_FILE_SIZE, 1024*1024*10 ); // 10 meg
  | 
        
        
            | 
            | 
           145 | 
           define (DEFAULT_TEXTAREA_HEIGHT, 5 );
  | 
        
        
            | 
            | 
           146 | 
           define define ( DEFAULT_TABLE, 'FILL ME IN');
  | 
        
        
            | 
            | 
           147 | 
           global \$DATABASE_DEFINITION;
  | 
        
        
            | 
            | 
           148 | 
              | 
        
        
            | 
            | 
           149 | 
           ";
  | 
        
        
            | 
            | 
           150 | 
           print "\$DATABASE_DEFINITION = array(\n" . join( ",\n", @results) . "\n);";
  | 
        
        
            | 
            | 
           151 | 
           print "\n?>\n";
  | 
        
        
            | 
            | 
           152 | 
           1;
  | 
        
        
            | 
            | 
           153 | 
              | 
        
        
            | 
            | 
           154 | 
              | 
        
        
            | 
            | 
           155 | 
              | 
        
        
            | 
            | 
           156 | 
              |