~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to tests/randgen/lib/GenTest/Simplifier/Tables.pm

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.1.6)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20120619104649-e2l0ggd4oz3um0f4
Tags: upstream-7.1.36-stable
ImportĀ upstreamĀ versionĀ 7.1.36-stable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008-2009 Sun Microsystems, Inc. All rights reserved.
 
2
# Use is subject to license terms.
 
3
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
# USA
 
17
 
 
18
package GenTest::Simplifier::Tables;
 
19
 
 
20
require Exporter;
 
21
use DBI;
 
22
use GenTest;
 
23
use Data::Dumper;
 
24
 
 
25
@ISA = qw(GenTest);
 
26
 
 
27
use strict;
 
28
 
 
29
use lib 'lib';
 
30
 
 
31
use constant SIMPLIFIER_DSN             => 0;
 
32
use constant SIMPLIFIER_ORIG_DATABASE   => 1;
 
33
use constant SIMPLIFIER_NEW_DATABASE    => 2;
 
34
 
 
35
1;
 
36
 
 
37
sub new {
 
38
        my $class = shift;
 
39
 
 
40
        my $simplifier = $class->SUPER::new({
 
41
                dsn             => SIMPLIFIER_DSN,
 
42
                orig_database   => SIMPLIFIER_ORIG_DATABASE,
 
43
                new_database    => SIMPLIFIER_NEW_DATABASE,
 
44
        }, @_);
 
45
 
 
46
        return $simplifier;
 
47
}
 
48
 
 
49
sub simplify {
 
50
        my ($simplifier, $initial_query) = @_;
 
51
 
 
52
        my $orig_database = $simplifier->[SIMPLIFIER_ORIG_DATABASE];
 
53
        my $new_database = $simplifier->[SIMPLIFIER_NEW_DATABASE];
 
54
        my $new_query = $initial_query;
 
55
 
 
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;
 
59
        
 
60
        my @participating_tables = (@tables_named, @tables_quoted, @tables_letters);
 
61
 
 
62
        my %participating_tables;
 
63
        map {$participating_tables{$_} = 1 } @participating_tables;
 
64
 
 
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;
 
67
 
 
68
        my @participating_fields = (@fields_quoted, map {'col_'.$_} @fields_named);
 
69
        my %participating_fields;
 
70
        map { $participating_fields{$_} = 1 } @participating_fields;
 
71
 
 
72
        my $dbh = DBI->connect($simplifier->[SIMPLIFIER_DSN]);
 
73
 
 
74
        $dbh->do("DROP DATABASE IF EXISTS $new_database");
 
75
        $dbh->do("CREATE DATABASE $new_database");
 
76
 
 
77
        my $table_index = 0;
 
78
 
 
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;
 
82
 
 
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;
 
89
 
 
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
 
93
                my %indices;
 
94
                map {$indices{$_->[4]}=$_->[2]} @{$dbh->selectall_arrayref("SHOW INDEX FROM `$new_table_name` IN $new_database")};
 
95
 
 
96
                ## Calculate which fields to keep
 
97
                my %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});
 
106
                            }
 
107
                        }
 
108
                    } else {
 
109
                        ## Explicitely used field
 
110
                        $keep{$actual_field}=1;
 
111
                    }
 
112
                }
 
113
 
 
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};
 
117
                }
 
118
        }
 
119
        
 
120
        return [ map { 't'.$_ } (1..$table_index) ], $new_query;
 
121
}
 
122
 
 
123
1;