use Carp;
use Data::Dumper;
use List::Util qw(first);
+use List::UtilsBy qw(sort_by);
use constant META_CVARS => 'cvars_config';
my $caller_package = caller;
# TODO: if module is empty, module overloading needs to take effect
- # certain stuff may have more than one overload, odr even more than one type
+ # certain stuff may have more than one overload, or even more than one type
defined $caller_package or croak 'need to be included from a caller reference';
$params{module} ||= _calc_modules_from_overloads(%params) if $params{overloads};
@$configs
);
+ @return = sort_by { $_->config->sortkey } @return;
+
return \@return;
}
}
die "invalid config_id in $caller_package\::cvar custom filter: expected module $params{module} - got @{[ $config->module ]}";
}
+ my @filter;
+ if ($config->type eq 'bool') {
+ @filter = $value ? ($config->value_col => 1) : (or => [ $config->value_col => undef, $config->value_col => 0 ]);
+ } else {
+ @filter = ($config->value_col => $value);
+ }
+
my (%query, %bind_vals);
($query{customized}, $bind_vals{customized}) = Rose::DB::Object::QueryBuilder::build_select(
dbh => $config->dbh,
query => [
config_id => $config_id,
sub_module => $params{sub_module},
- $config->value_col => $value,
+ @filter,
],
query_is_sql => 1,
);
+ if ($config->type eq 'bool') {
+ if ($value) {
+ @filter = (
+ '!default_value' => undef,
+ '!default_value' => '',
+ default_value => '1',
+ );
+
+ } else {
+ @filter = (
+ or => [
+ default_value => '0',
+ default_value => '',
+ default_value => undef,
+ ],
+ );
+ }
+
+ } else {
+ @filter = (
+ '!default_value' => undef,
+ '!default_value' => '',
+ default_value => $value,
+ );
+ }
+
+
my $conversion = $config->type =~ m{^(?:date|timestamp)$} ? $config->type
: $config->type =~ m{^(?:customer|vendor|part)$} ? 'integer'
- : $config->type eq 'bool' ? 'boolean'
: $config->type eq 'number' ? 'numeric'
: '';
($query{config}, $bind_vals{config}) = Rose::DB::Object::QueryBuilder::build_select(
- dbh => $config->dbh,
- select => 'id',
- tables => [ 'custom_variable_configs' ],
- columns => { custom_variable_configs => [ qw(id default_value) ] },
- query => [
- id => $config->id,
- default_value => $value,
+ dbh => $config->dbh,
+ select => 'id',
+ tables => [ 'custom_variable_configs' ],
+ columns => { custom_variable_configs => [ qw(id default_value) ] },
+ query => [
+ id => $config->id,
+ @filter,
],
- query_is_sql => 1,
+ query_is_sql => 1,
);
- $query{config} =~ s{\bdefault_value\b}{default_value::${conversion}} if $conversion;
+ $query{config} =~ s{ (?<! NOT\( ) default_value (?! \s*is\s+not\s+null) }{default_value::${conversion}}x if $conversion;
($query{not_customized}, $bind_vals{not_customized}) = Rose::DB::Object::QueryBuilder::build_select(
dbh => $config->dbh,
# remove rose aliases. query builder sadly is not reentrant, and will reuse the same aliases. :(
$query{$key} =~ s{\bt\d+(?:\.)?\b}{}g;
- # manually inline the values. again, rose doen't know how to handly bind params in subqueries :(
- $query{$key} =~ s{\?}{ $config->dbh->quote($_) }xe for @{ $bind_vals{$key} };
+ # manually inline the values. again, rose doesn't know how to handle bind params in subqueries :(
+ $query{$key} =~ s{\?}{ $config->dbh->quote(shift @{ $bind_vals{$key} }) }xeg;
$query{$key} =~ s{\n}{ }g;
}
=head1 NAME
-SL::DB::Helper::CustomVariables - Mixin to provide custom variables relations
+SL::DB::Helper::CustomVariables - Mixin to provide custom variable relations
=head1 SYNOPSIS
=back
+=head1 BUGS AND CAVEATS
+
+=over 4
+
+=item * Conditional method export
+
+Prolonged use has shown that users expect all methods to be present or none.
+Future versions of this will likely remove the optional aliasing.
+
+=item * Sematics need to be updated
+
+There are a few transitions that are currently neither supported nor well
+defined, most of the happening when the config of a cvar gets changed which
+instances are already saved. This needs to be cleaned up.
+
+=back
+
=head1 AUTHOR
Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,