Lines 150-191
sub _formatSource {
Link Here
|
150 |
|
150 |
|
151 |
sub _buildColumnValues { |
151 |
sub _buildColumnValues { |
152 |
my ($self, $params) = @_; |
152 |
my ($self, $params) = @_; |
153 |
my $source = _formatSource( { source => $params->{source} } ); |
153 |
my $source = _formatSource( { source => $params->{source} } ); |
154 |
my $value = $params->{value}; |
154 |
my $original_value = $params->{value}; |
155 |
|
155 |
|
156 |
my $col_values; |
156 |
my $col_values; |
157 |
my @columns = $self->schema->source($source)->columns; |
157 |
my @columns = $self->schema->source($source)->columns; |
158 |
my %unique_constraints = $self->schema->source($source)->unique_constraints(); |
158 |
my %unique_constraints = $self->schema->source($source)->unique_constraints(); |
159 |
|
159 |
|
160 |
my $values_ok = 0; |
160 |
my $build_value = 1; |
161 |
my $at_least_one_constraint_failed; |
161 |
BUILD_VALUE: while ( $build_value ) { |
162 |
|
162 |
# generate random values for all columns |
163 |
while ( not $values_ok ) { |
|
|
164 |
|
165 |
$at_least_one_constraint_failed = 0; |
166 |
# generate random values |
167 |
for my $col_name( @columns ) { |
163 |
for my $col_name( @columns ) { |
168 |
my $col_value = $self->_buildColumnValue({ |
164 |
my $col_value = $self->_buildColumnValue({ |
169 |
source => $source, |
165 |
source => $source, |
170 |
column_name => $col_name, |
166 |
column_name => $col_name, |
171 |
value => $value, |
167 |
value => $original_value, |
172 |
}); |
168 |
}); |
173 |
$col_values->{$col_name} = $col_value if( defined( $col_value ) ); |
169 |
$col_values->{$col_name} = $col_value if( defined( $col_value ) ); |
174 |
} |
170 |
} |
|
|
171 |
$build_value = 0; |
175 |
|
172 |
|
176 |
# If default values are set, maybe the data exist in the DB |
173 |
# If default values are set, maybe the data exist in the DB |
177 |
# But no need to wait for another value |
174 |
# But no need to wait for another value |
178 |
last if exists( $default_value->{$source} ); |
175 |
# FIXME this can be wrong if a default value is defined for a field |
|
|
176 |
# which is not a constraint and that the generated value for the |
177 |
# constraint already exists. |
178 |
last BUILD_VALUE if exists( $default_value->{$source} ); |
179 |
|
179 |
|
180 |
if ( scalar keys %unique_constraints > 0 ) { |
180 |
# If there is no original value given and unique constraints exist, |
|
|
181 |
# check if the generated values do not exist yet. |
182 |
if ( not defined $original_value and scalar keys %unique_constraints > 0 ) { |
181 |
|
183 |
|
182 |
# verify the data would respect each unique constraint |
184 |
# verify the data would respect each unique constraint |
183 |
foreach my $constraint (keys %unique_constraints) { |
185 |
CONSTRAINTS: foreach my $constraint (keys %unique_constraints) { |
184 |
|
186 |
|
185 |
my $condition; |
187 |
my $condition; |
186 |
my @constraint_columns = $unique_constraints{$constraint}; |
188 |
my $constraint_columns = $unique_constraints{$constraint}; |
187 |
# loop through all constraint columns and build the condition |
189 |
# loop through all constraint columns and build the condition |
188 |
foreach my $constraint_column ( @constraint_columns ) { |
190 |
foreach my $constraint_column ( @$constraint_columns ) { |
189 |
# build the filter |
191 |
# build the filter |
190 |
$condition->{ $constraint_column } = |
192 |
$condition->{ $constraint_column } = |
191 |
$col_values->{ $constraint_column }; |
193 |
$col_values->{ $constraint_column }; |
Lines 196-215
sub _buildColumnValues {
Link Here
|
196 |
->search( $condition ) |
198 |
->search( $condition ) |
197 |
->count(); |
199 |
->count(); |
198 |
if ( $count > 0 ) { |
200 |
if ( $count > 0 ) { |
199 |
$at_least_one_constraint_failed = 1; |
|
|
200 |
# no point checking more stuff, exit the loop |
201 |
# no point checking more stuff, exit the loop |
201 |
last; |
202 |
$build_value = 1; |
|
|
203 |
last CONSTRAINTS; |
202 |
} |
204 |
} |
203 |
} |
205 |
} |
204 |
|
|
|
205 |
if ( $at_least_one_constraint_failed ) { |
206 |
$values_ok = 0; |
207 |
} else { |
208 |
$values_ok = 1; |
209 |
} |
210 |
|
211 |
} else { |
212 |
$values_ok = 1; |
213 |
} |
206 |
} |
214 |
} |
207 |
} |
215 |
return $col_values; |
208 |
return $col_values; |
Lines 283-289
sub _buildColumnValue {
Link Here
|
283 |
if( exists( $value->{$col_name} ) ) { |
276 |
if( exists( $value->{$col_name} ) ) { |
284 |
$col_value = $value->{$col_name}; |
277 |
$col_value = $value->{$col_name}; |
285 |
} |
278 |
} |
286 |
elsif( exists( $default_value->{$source}->{$col_name} ) ) { |
279 |
elsif( exists $default_value->{$source} and exists $default_value->{$source}->{$col_name} ) { |
287 |
$col_value = $default_value->{$source}->{$col_name}; |
280 |
$col_value = $default_value->{$source}->{$col_name}; |
288 |
} |
281 |
} |
289 |
elsif( not $col_info->{default_value} and not $col_info->{is_auto_increment} and not $col_info->{is_foreign_key} ) { |
282 |
elsif( not $col_info->{default_value} and not $col_info->{is_auto_increment} and not $col_info->{is_foreign_key} ) { |
290 |
- |
|
|