1 |
#!/usr/bin/perl -w |
2 |
|
3 |
|
4 |
my $php; |
5 |
|
6 |
while(<STDIN>) { |
7 |
next if (/textInmySQL/); |
8 |
$php .= $_; |
9 |
} |
10 |
|
11 |
sub fix_php { |
12 |
my ($sql,$filler1,$query,$filler2,$var,$filler3) = @_; |
13 |
|
14 |
my @vars; |
15 |
while ($sql =~ s/'*"\s*\.\s*(\$\w+)\s*\.\s*"'*/ ? /s) { |
16 |
push @vars, $1; |
17 |
} |
18 |
|
19 |
my ($table,$cols); |
20 |
if ($sql =~ m/insert\s+into\s+(\S+)\s+\(([^\)]+)\)/i) { |
21 |
($table, $cols) = ($1,$2); |
22 |
$cols =~ s/\s*,\s*/ = ? and /g; |
23 |
$cols .= " = ?"; |
24 |
} else { |
25 |
die "can't parse: $sql"; |
26 |
} |
27 |
|
28 |
my $nl = "\n\t\t\t"; |
29 |
|
30 |
my $php .= '// '.$var.$filler3.$nl; |
31 |
$php .= '$insert_res = xx_prepare_execute("select 1 as id from '.$table.' where '.$cols.'", '.join(", ",@vars).');'.$nl; |
32 |
$php .= 'list('.$var.') = $insert_res->fetchRow(DB_FETCHMODE_ORDERED);'.$nl; |
33 |
|
34 |
return $sql . $filler1 . 'xx_prepare_execute($sql, '.join(", ",@vars).')' . $filler2 . $php; |
35 |
} |
36 |
|
37 |
my $fixed = 0; |
38 |
|
39 |
$php =~ s/(\$sql\s*=\s*"[^;]+?;)([^{]+?)(xx_query\s*\(\$sql[^\)]+\))([^{]+?{[^}]+?}\s*else\s*{[^}]+?)(\$\w+)(\s*=\s*xx_insert_id\([^\)]+?\))/fix_php($1,$2,$3,$4,$5,$6)/seg; |
40 |
|
41 |
print $php; |