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

« back to all changes in this revision

Viewing changes to tests/kewpie/randgen/lib/GenTest/Transform/InlineSubqueries.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::Transform::InlineSubqueries;
 
19
 
 
20
require Exporter;
 
21
@ISA = qw(GenTest GenTest::Transform);
 
22
 
 
23
use strict;
 
24
use lib 'lib';
 
25
use GenTest;
 
26
use GenTest::Transform;
 
27
use GenTest::Constants;
 
28
 
 
29
# This is a regexp to match nested brackets, taken from
 
30
# here http://www.perlmonks.org/?node_id=547596
 
31
 
 
32
my $paren_rx;
 
33
 
 
34
$paren_rx = qr{
 
35
  (?:
 
36
    \((??{$paren_rx})\) # either match another paren-set
 
37
    | [^()]+            # or match non-parens (or escaped parens
 
38
  )*
 
39
}x;
 
40
 
 
41
sub transform {
 
42
        my ($class, $query, $executor) = @_;
 
43
 
 
44
        my $inline_successful = 0;
 
45
        $query =~ s{(\(\s*SELECT\s+(??{$paren_rx})\))}{
 
46
                my $result = $executor->execute($1, 1);
 
47
 
 
48
                if (
 
49
                        ($result->status() != STATUS_OK) ||
 
50
                        ($result->rows() < 1)
 
51
                ) {
 
52
                        $1;                             # return original query
 
53
                } else {
 
54
                        $inline_successful = 1;         # return inlined literals
 
55
                        " ( ".join(', ', map {
 
56
                                if (not defined $_->[0]) {
 
57
                                        "NULL";
 
58
                                } elsif ($_->[0] =~ m{^\d+$}sio){
 
59
                                        $_->[0];
 
60
                                } else {
 
61
                                        "'".$_->[0]."'"
 
62
                                }
 
63
                        } @{$result->data()})." ) ";
 
64
                }
 
65
        }sgexi;
 
66
 
 
67
        my $final_result = $executor->execute($query, 1);
 
68
        
 
69
        if (
 
70
                ($inline_successful) &&
 
71
                ($final_result->status() == STATUS_OK)
 
72
        ) {
 
73
                return $query." /* TRANSFORM_OUTCOME_UNORDERED_MATCH */";
 
74
        } else {
 
75
                return STATUS_WONT_HANDLE;
 
76
        }
 
77
}
 
78
 
 
79
1;