1
# Copyright (C) 2008-2009 Sun Microsystems, Inc. All rights reserved.
2
# Use is subject to license terms.
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; version 2 of the License.
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
# General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18
package GenTest::Simplifier::Tables;
31
use constant SIMPLIFIER_DSN => 0;
32
use constant SIMPLIFIER_ORIG_DATABASE => 1;
33
use constant SIMPLIFIER_NEW_DATABASE => 2;
40
my $simplifier = $class->SUPER::new({
41
dsn => SIMPLIFIER_DSN,
42
orig_database => SIMPLIFIER_ORIG_DATABASE,
43
new_database => SIMPLIFIER_NEW_DATABASE,
50
my ($simplifier, $initial_query) = @_;
52
my $orig_database = $simplifier->[SIMPLIFIER_ORIG_DATABASE];
53
my $new_database = $simplifier->[SIMPLIFIER_NEW_DATABASE];
54
my $new_query = $initial_query;
56
my @tables_named = $initial_query =~ m{(table[a-z0-9_]+)}sgio;
57
my @tables_quoted = $initial_query =~ m{`(.*?)`}sgio;
58
my @tables_letters = $initial_query =~ m{[ `]([A-Z]|AA|BB|CC|DD|EE|FF|AAA|BBB|CCC|DDD|EEE|FFF)[ `]}sgo;
60
my @participating_tables = (@tables_named, @tables_quoted, @tables_letters);
62
my %participating_tables;
63
map {$participating_tables{$_} = 1 } @participating_tables;
65
my @fields_quoted = $initial_query =~ m{`(.*?)`}sgio;
66
my @fields_named = $initial_query =~ m{((?:char|varchar|int|set|enum|blob|date|time|datetime|pk)(?:`|\s|_key|_nokey))}sgo;
68
my @participating_fields = (@fields_quoted, map {'col_'.$_} @fields_named);
69
my %participating_fields;
70
map { $participating_fields{$_} = 1 } @participating_fields;
72
my $dbh = DBI->connect($simplifier->[SIMPLIFIER_DSN]);
74
$dbh->do("DROP DATABASE IF EXISTS $new_database");
75
$dbh->do("CREATE DATABASE $new_database");
79
foreach my $participating_table (keys %participating_tables) {
80
my ($table_exists) = $dbh->selectrow_array("SHOW TABLES IN $orig_database LIKE '$participating_table'");
81
next if not defined $table_exists;
83
my $new_table_name = 't'.++$table_index;
84
$dbh->do("CREATE TABLE $new_database . $new_table_name LIKE $orig_database . `$participating_table`");
85
$dbh->do("INSERT INTO $new_database . $new_table_name SELECT * FROM $orig_database . `$participating_table`");
86
$new_query =~ s{`$participating_table`}{`$new_table_name`}sg;
87
$new_query =~ s{ $participating_table }{ $new_table_name }sg;
88
$new_query =~ s{ $participating_table$}{ $new_table_name}sg;
90
## Find all fields in table
91
my $actual_fields = $dbh->selectcol_arrayref("SHOW FIELDS FROM `$new_table_name` IN $new_database");
92
## Find indexed fields in table
94
map {$indices{$_->[4]}=$_->[2]} @{$dbh->selectall_arrayref("SHOW INDEX FROM `$new_table_name` IN $new_database")};
96
## Calculate which fields to keep
98
foreach my $actual_field (@$actual_fields) {
99
if (not exists $participating_fields{$actual_field}) {
100
## Not used field, but may be part of multi-column index where other column is used
101
if (exists $indices{$actual_field}) {
102
foreach my $x (keys %indices) {
103
$keep{$actual_field} = 1
104
if (exists $participating_fields{$x}) and
105
($indices{$x} eq $indices{$actual_field});
109
## Explicitely used field
110
$keep{$actual_field}=1;
114
## Remove the fields we do not want to keep
115
foreach my $actual_field (@$actual_fields) {
116
$dbh->do("ALTER TABLE $new_database . `$new_table_name` DROP COLUMN `$actual_field`") if not $keep{$actual_field};
120
return [ map { 't'.$_ } (1..$table_index) ], $new_query;