~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to tests/randgen/gensql.pl

  • Committer: patrick crews
  • Date: 2011-10-05 20:40:33 UTC
  • mfrom: (2337.1.24 dbqp_revamp2)
  • mto: This revision was merged to the branch mainline in revision 2435.
  • Revision ID: gleebix@gmail.com-20111005204033-5m6kvcii1q87yur6
Merge with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
# Copyright (C) 2008-2009 Sun Microsystems, Inc. All rights reserved.
 
4
# Use is subject to license terms.
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; version 2 of the License.
 
9
#
 
10
# This program is distributed in the hope that it will be useful, but
 
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
13
# General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 
18
# USA
 
19
 
 
20
use lib 'lib';
 
21
use lib "$ENV{RQG_HOME}/lib";
 
22
use strict;
 
23
 
 
24
use GenTest;
 
25
use GenTest::Constants;
 
26
use GenTest::Properties;
 
27
use GenTest::Generator::FromGrammar;
 
28
use GenTest::Executor;
 
29
use Getopt::Long;
 
30
 
 
31
my $DEFAULT_QUERIES = 1000;
 
32
 
 
33
my @ARGV_saved = @ARGV;
 
34
my $options = {};
 
35
my $opt_result = GetOptions($options,
 
36
                            'config=s',
 
37
                            'grammar=s',
 
38
                            'queries=i',
 
39
                            'help',
 
40
                            'seed=s',
 
41
                            'mask=i',
 
42
                            'mask-level=i',
 
43
                            'dsn=s');
 
44
 
 
45
help() if !$opt_result;
 
46
 
 
47
my $config = GenTest::Properties->new(options => $options,
 
48
                                      defaults => {seed => 1, 
 
49
                                                   queries=> $DEFAULT_QUERIES},
 
50
                                      legal => ['config',
 
51
                                                'queries',
 
52
                                                'help',
 
53
                                                'seed',
 
54
                                                'mask',
 
55
                                                'mask-level',
 
56
                                                'dsn'],
 
57
                                      required => ['grammar'],
 
58
                                      help => \&help);
 
59
 
 
60
my $seed = $config->seed;
 
61
if ($seed eq 'time') {
 
62
    $seed = time();
 
63
    say("Converting --seed=time to --seed=$seed");
 
64
}
 
65
 
 
66
my $generator = GenTest::Generator::FromGrammar->new(
 
67
    grammar_file => $config->grammar,
 
68
    seed => $seed,
 
69
    mask => $config->mask,
 
70
    mask_level => $config->property('mask-level')
 
71
    );
 
72
 
 
73
return STATUS_ENVIRONMENT_FAILURE if not defined $generator;
 
74
 
 
75
my $executor;
 
76
 
 
77
if (defined $config->dsn) {
 
78
    $executor = GenTest::Executor->newFromDSN($config->dsn);
 
79
    exit (STATUS_ENVIRONMENT_FAILURE) if not defined $executor;
 
80
}
 
81
 
 
82
if (defined $executor) {
 
83
    my $init_result = $executor->init();
 
84
    exit ($init_result) if $init_result > STATUS_OK;
 
85
    $executor->cacheMetaData();
 
86
}
 
87
 
 
88
foreach my $i (1..$config->queries) {
 
89
    my $queries = $generator->next([$executor]);
 
90
    if (
 
91
        (not defined $queries) ||
 
92
        ($queries->[0] eq '')
 
93
        ) {
 
94
        say("Grammar produced an empty query. Terminating.");
 
95
        exit(STATUS_ENVIRONMENT_FAILURE);
 
96
    }
 
97
    my $sql = join('; ',@$queries);
 
98
    print $sql.";\n";
 
99
}
 
100
 
 
101
exit(0);
 
102
 
 
103
sub help {
 
104
    print <<EOF
 
105
$0 - Generate random queries from an SQL grammar and pipe them to STDOUT
 
106
 
 
107
        --grammar   : Grammar file to use for generating the queries (REQUIRED);
 
108
        --seed      : Seed for the pseudo-random generator
 
109
        --queries   : Numer of queries to generate (default $DEFAULT_QUERIES);
 
110
        --dsn       : The DSN of the database that will be used to resolve rules such as _table , _field
 
111
        --mask      : A seed to a random mask used to mask (reeduce) the grammar.
 
112
        --mask-level: How many levels deep the mask is applied (default 1)
 
113
        --help      : This help message
 
114
EOF
 
115
        ;
 
116
    exit(1);
 
117
}
 
118