Subversion Repositories php_library

Rev

Details | Last modification | View Log | RSS feed

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