Subversion Repositories php_library

Rev

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

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