~drizzle-trunk/drizzle/development

0.67.1 by Philip Stoev
initial import from internal tree
1
#!/usr/bin/perl
0.67.305 by Bernt M. Johnsen
Copyright headres and license added
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
0.67.1 by Philip Stoev
initial import from internal tree
20
use lib 'lib';
21
use lib "$ENV{RQG_HOME}/lib";
22
use strict;
23
24
use GenTest;
25
use GenTest::Constants;
0.82.7 by Bernt M. Johnsen
Merger + fixes
26
use GenTest::Properties;
0.67.1 by Philip Stoev
initial import from internal tree
27
use GenTest::Generator::FromGrammar;
0.73.6 by Bernt M. Johnsen
Added simple script testing. Made gensql.pl usable against other databases
28
use GenTest::Executor;
0.67.1 by Philip Stoev
initial import from internal tree
29
use Getopt::Long;
30
0.82.7 by Bernt M. Johnsen
Merger + fixes
31
my $DEFAULT_QUERIES = 1000;
0.67.1 by Philip Stoev
initial import from internal tree
32
33
my @ARGV_saved = @ARGV;
0.82.7 by Bernt M. Johnsen
Merger + fixes
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',
0.67.160 by Bernt M. Johnsen
Changes + merge
42
			    'mask-level=i',
0.82.7 by Bernt M. Johnsen
Merger + fixes
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',
0.67.160 by Bernt M. Johnsen
Changes + merge
55
						'mask-level',
0.82.7 by Bernt M. Johnsen
Merger + fixes
56
						'dsn'],
57
				      required => ['grammar'],
58
				      help => \&help);
0.67.1 by Philip Stoev
initial import from internal tree
59
0.67.215 by Bernt M. Johnsen
Better reporting of --sed=time change. Added seed=time() concersion to Gendata.pm
60
my $seed = $config->seed;
61
if ($seed eq 'time') {
62
    $seed = time();
63
    say("Converting --seed=time to --seed=$seed");
64
}
65
0.67.1 by Philip Stoev
initial import from internal tree
66
my $generator = GenTest::Generator::FromGrammar->new(
0.82.7 by Bernt M. Johnsen
Merger + fixes
67
    grammar_file => $config->grammar,
0.67.215 by Bernt M. Johnsen
Better reporting of --sed=time change. Added seed=time() concersion to Gendata.pm
68
    seed => $seed,
0.82.7 by Bernt M. Johnsen
Merger + fixes
69
    mask => $config->mask,
0.67.160 by Bernt M. Johnsen
Changes + merge
70
    mask_level => $config->property('mask-level')
0.82.7 by Bernt M. Johnsen
Merger + fixes
71
    );
0.67.1 by Philip Stoev
initial import from internal tree
72
73
return STATUS_ENVIRONMENT_FAILURE if not defined $generator;
74
75
my $executor;
76
0.82.7 by Bernt M. Johnsen
Merger + fixes
77
if (defined $config->dsn) {
78
    $executor = GenTest::Executor->newFromDSN($config->dsn);
79
    exit (STATUS_ENVIRONMENT_FAILURE) if not defined $executor;
0.67.1 by Philip Stoev
initial import from internal tree
80
}
81
82
if (defined $executor) {
0.82.7 by Bernt M. Johnsen
Merger + fixes
83
    my $init_result = $executor->init();
84
    exit ($init_result) if $init_result > STATUS_OK;
0.67.196 by Bernt M. Johnsen
Don't call cacheMetaData on undefined executor
85
    $executor->cacheMetaData();
0.67.1 by Philip Stoev
initial import from internal tree
86
}
87
0.82.7 by Bernt M. Johnsen
Merger + fixes
88
foreach my $i (1..$config->queries) {
89
    my $queries = $generator->next([$executor]);
90
    if (
91
	(not defined $queries) ||
92
	($queries->[0] eq '')
0.67.1 by Philip Stoev
initial import from internal tree
93
	) {
0.82.7 by Bernt M. Johnsen
Merger + fixes
94
	say("Grammar produced an empty query. Terminating.");
95
	exit(STATUS_ENVIRONMENT_FAILURE);
96
    }
97
    my $sql = join('; ',@$queries);
98
    print $sql.";\n";
0.67.1 by Philip Stoev
initial import from internal tree
99
}
100
101
exit(0);
102
103
sub help {
0.82.7 by Bernt M. Johnsen
Merger + fixes
104
    print <<EOF
0.67.31 by Bernt M. Johnsen
Updated some help function (more to do here)
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
0.82.7 by Bernt M. Johnsen
Merger + fixes
109
        --queries   : Numer of queries to generate (default $DEFAULT_QUERIES);
0.67.31 by Bernt M. Johnsen
Updated some help function (more to do here)
110
        --dsn       : The DSN of the database that will be used to resolve rules such as _table , _field
0.73.4 by Bernt M. Johnsen
Let --mask be a seed to the masking process instead of the first 16 bits of the mask
111
        --mask      : A seed to a random mask used to mask (reeduce) the grammar.
0.67.160 by Bernt M. Johnsen
Changes + merge
112
        --mask-level: How many levels deep the mask is applied (default 1)
0.67.31 by Bernt M. Johnsen
Updated some help function (more to do here)
113
        --help      : This help message
0.67.1 by Philip Stoev
initial import from internal tree
114
EOF
115
        ;
0.67.31 by Bernt M. Johnsen
Updated some help function (more to do here)
116
    exit(1);
0.67.1 by Philip Stoev
initial import from internal tree
117
}
118