~ubuntu-branches/ubuntu/trusty/mysql-5.6/trusty

« back to all changes in this revision

Viewing changes to mysql-test/lib/My/Options.pm

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-12 11:54:27 UTC
  • Revision ID: package-import@ubuntu.com-20140212115427-oq6tfsqxl1wuwehi
Tags: upstream-5.6.15
ImportĀ upstreamĀ versionĀ 5.6.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- cperl -*-
 
2
# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 
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,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU 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  USA
 
16
 
 
17
 
 
18
package My::Options;
 
19
 
 
20
#
 
21
# Utility functions to work with list of options
 
22
#
 
23
 
 
24
use strict;
 
25
 
 
26
 
 
27
sub same($$) {
 
28
  my $l1= shift;
 
29
  my $l2= shift;
 
30
  return compare($l1,$l2) == 0;
 
31
}
 
32
 
 
33
 
 
34
sub compare ($$) {
 
35
  my $l1= shift;
 
36
  my $l2= shift;
 
37
 
 
38
  my @l1= @$l1;
 
39
  my @l2= @$l2;
 
40
 
 
41
  return -1 if @l1 < @l2;
 
42
  return  1 if @l1 > @l2;
 
43
 
 
44
  while ( @l1 )                         # Same length
 
45
  {
 
46
    my $e1= shift @l1;
 
47
    my $e2= shift @l2;
 
48
    my $cmp= ($e1 cmp $e2);
 
49
    return $cmp if $cmp != 0;
 
50
  }
 
51
 
 
52
  return 0;                             # They are the same
 
53
}
 
54
 
 
55
 
 
56
sub _split_option {
 
57
  my ($option)= @_;
 
58
  if ($option=~ /^--(.*)=(.*)$/){
 
59
    return ($1, $2);
 
60
  }
 
61
  elsif ($option=~ /^--(.*)$/){
 
62
    return ($1, undef)
 
63
  }
 
64
  elsif ($option=~ /^\$(.*)$/){ # $VAR
 
65
    return ($1, undef)
 
66
  }
 
67
  elsif ($option=~ /^(.*)=(.*)$/){
 
68
    return ($1, $2)
 
69
  }
 
70
  die "Unknown option format '$option'";
 
71
}
 
72
 
 
73
 
 
74
sub _build_option {
 
75
  my ($name, $value)= @_;
 
76
  if ($name =~ /^O, /){
 
77
    return "-".$name."=".$value;
 
78
  }
 
79
  elsif ($value){
 
80
    return "--".$name."=".$value;
 
81
  }
 
82
  return "--".$name;
 
83
}
 
84
 
 
85
 
 
86
#
 
87
# Compare two list of options and return what would need
 
88
# to be done to get the server running with the new settings
 
89
#
 
90
sub diff {
 
91
  my ($from_opts, $to_opts)= @_;
 
92
 
 
93
  my %from;
 
94
  foreach my $from (@$from_opts)
 
95
  {
 
96
    my ($opt, $value)= _split_option($from);
 
97
    next unless defined($opt);
 
98
    $from{$opt}= $value;
 
99
  }
 
100
 
 
101
  #print "from: ", %from, "\n";
 
102
 
 
103
  my %to;
 
104
  foreach my $to (@$to_opts)
 
105
  {
 
106
    my ($opt, $value)= _split_option($to);
 
107
    next unless defined($opt);
 
108
    $to{$opt}= $value;
 
109
  }
 
110
 
 
111
  #print "to: ", %to, "\n";
 
112
 
 
113
  # Remove the ones that are in both lists
 
114
  foreach my $name (keys %from){
 
115
    if (exists $to{$name} and $to{$name} eq $from{$name}){
 
116
      #print "removing '$name'  from both lists\n";
 
117
      delete $to{$name};
 
118
      delete $from{$name};
 
119
    }
 
120
  }
 
121
 
 
122
  #print "from: ", %from, "\n";
 
123
  #print "to: ", %to, "\n";
 
124
 
 
125
  # Add all keys in "to" to result
 
126
  my @result;
 
127
  foreach my $name (keys %to){
 
128
    push(@result, _build_option($name, $to{$name}));
 
129
  }
 
130
 
 
131
  # Add all keys in "from" that are not in "to"
 
132
  # to result as "set to default"
 
133
  foreach my $name (keys %from){
 
134
    if (not exists $to{$name}) {
 
135
      push(@result, _build_option($name, "default"));
 
136
    }
 
137
  }
 
138
 
 
139
  return @result;
 
140
}
 
141
 
 
142
 
 
143
sub is_set {
 
144
  my ($opts, $set_opts)= @_;
 
145
 
 
146
  foreach my $opt (@$opts){
 
147
 
 
148
    my ($opt_name1, $value1)= _split_option($opt);
 
149
 
 
150
    foreach my $set_opt (@$set_opts){
 
151
      my ($opt_name2, $value2)= _split_option($set_opt);
 
152
 
 
153
      if ($opt_name1 eq $opt_name2){
 
154
        # Option already set
 
155
        return 1;
 
156
      }
 
157
    }
 
158
  }
 
159
 
 
160
  return 0;
 
161
}
 
162
 
 
163
 
 
164
sub toSQL {
 
165
  my (@options)= @_;
 
166
  my @sql;
 
167
 
 
168
  foreach my $option (@options) {
 
169
    my ($sql_name, $value)= _split_option($option);
 
170
    #print "name: $sql_name\n";
 
171
    #print "value: $value\n";
 
172
    $sql_name=~ s/-/_/g;
 
173
    push(@sql, "SET GLOBAL $sql_name=$value");
 
174
  }
 
175
  return join("; ", @sql);
 
176
}
 
177
 
 
178
 
 
179
sub toStr {
 
180
  my $name= shift;
 
181
  return "$name: ",
 
182
    "['", join("', '", @_), "']\n";
 
183
}
 
184
 
 
185
 
 
186
1;
 
187