Subversion Repositories php_library

Rev

Blame | Last modification | View Log | RSS feed

#! /usr/bin/perl -w
#use Data::Dumper;


sub getFieldInfo {
   my $fieldInfo = shift;
   my $primary_key = shift;
   my @output;
   
   push @output, "'keyfield' => 1" if ( $primary_key);
   if ($fieldInfo =~ m/not null/i ) {
      push @output, "'required' => true";
      $fieldInfo =~ s/not null//i;  # remove the not null from the string
   }
   $fieldInfo =~ s/default +null//i; # we just ignore default null
   if ($fieldInfo =~ /default +([^ ]+)/i ) {
      $default = $1;
      $fieldInfo =~ s/default +([^ ]+)//;
      push @output, "'default' => $default";
   }
   if ($fieldInfo =~ m/auto_increment/i ) {
      push @output, "'readonly' => true";
      $fieldInfo =~ s/auto_increment//i;
   }
   # at this point, we should only have the data type left
   if ( $fieldInfo =~ m/ *([a-z]+)( *\(\d+\))?/i ) {
      $type = $1;
      $size = $2;
      $type =~ s/(varchar)|(char)/string/i;
      push @output, "'type' => '$type'";
      if ($size) {
         $size =~ s/[^0-9]//gi;
         push @output, "'width' => $size";
      } # if
   }
   #return "\n//$original\n\n" . join(" , ", @output);
   return join(" , ", @output);
}



sub getTableDef {
   my $table = shift;
   
   my $unique_key_regex = 'unique key [a-z0-9]+ \(([^)]+)\),?';
   my $primary_key_regex = 'primary key \(([^)]+)\),?';
   my %tableInformation;
   $table =~ s/ +/ /gi; # remove duplicate spaces
   $table =~ s/ ,/,/gi; # remove all spaces before comma's
   $table =~ m/create table ([a-z0-9_]+) *\(([^;]+)\)/i; # 
   $tableInformation{'table name'} = $1;
   $table = $2;
   
   if ( $table =~ m/$primary_key_regex/i ) {
      $tableInformation{'primary keys'} = $1;
      $table =~ s/$primary_key_regex//i;
   }
   if ( $table =~ m/$unique_key_regex/i ) {
      $tableInformation{'unique keys'} = $1;
      $table =~ s/$unique_key_regex//i;
   }
   my @columnInformation = split(',',$table);
   my %fieldInfo;
   for ( $i = 0; $i < @columnInformation; $i++) {
      $columnInformation[$i] =~ s/^ +//;
      if ( $columnInformation[$i] =~ m/ *([a-z0-9_]+) +(.*)/i )  {
         $fieldName = $1;
         $fieldDef = $2;
         $fieldInfo{$fieldName} = &getFieldInfo($fieldDef,$tableInformation{'primary keys'} =~ m/$fieldName/);
      }
   } # for
   #$tableInformation{'fields'} = \%fieldInfo;
   #print Dumper(\%fieldInfo );
   
   my $output = "'" .  $tableInformation{'table name'} . "' => array( \n";
   $output .= "\t'table name' => '" . $tableInformation{'table name'} . "',\n";
   if ($tableInformation{'primary keys'}) {
      if ( $tableInformation{'primary keys'} =~ m/,/ ) {
         #following line takes all items in a comma delimited list and quotes them
         $output .= "\t'key field' => array('" . join("','", split( ',',$tableInformation{'primary keys'})) . "'),\n";
      } else {
         $output .= "\t'key field' => '" . $tableInformation{'primary keys'} . "',\n";
      }
   }
   # do the display columns, basically all columns
   @fields = keys %fieldInfo;
   #$fieldNames = join( ',',@fields );
   $output .= "\t'display columns' => array('" . join( "','",@fields ) . "'),\n";
   $output .= "\t'display query' => 'select " . join( ',',@fields ) . " from " . $tableInformation{'table name'} . "',\n";
   my @fieldInfo;
   foreach $thisField (keys %fieldInfo ) {
      push @fieldInfo,  "'$thisField' => array(" . $fieldInfo{$thisField} . ")";
   }
   $output .= "\t'field info' => array(\n\t\t" . join( ",\n\t\t", @fieldInfo) . "\n\t)\n)";
      
   return $output;
}

my $inputFileName = shift;
unless ($inputFileName) {
   print "usage: sql2admin_hash.pl mysql_dump_file_name\n";
   print "mysql_dump_file_name is created as\n";
   print "   mysqldump database_name > mysql_dump_file_name\n";
   exit(1);
}
open DATA, "$inputFileName" or die "could not open $inputFileName";
@input = <DATA>;
close DATA;

chomp @input;
my @tables = split( ';', join( '', @input) );
my @results;
#push @results, &getTableDef( shift @tables );
foreach $thisTable( @tables ) {
   push @results, &getTableDef( $thisTable );
}

print "<?php\n";
print "define ( MAX_INPUT_FIELD_DISPLAY, 40 ); // this is the maximum input field size
define ( IMAGE_DIRECTORY, '/pictures/' );  // relative URL where pictures are stored
define ( EDIT_IMAGE_HEIGHT, 100 );         // height for thumbnail of pictuers
define ( MAX_UPLOAD_FILE_SIZE, 1024*1024*10 ); // 10 meg
define (DEFAULT_TEXTAREA_HEIGHT, 5 );
define define ( DEFAULT_TABLE, 'FILL ME IN');
global \$DATABASE_DEFINITION;
 
";
print "\$DATABASE_DEFINITION = array(\n" . join( ",\n", @results) . "\n);";
print "\n?>\n";
1;