| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package UR::DataSource::RDBMS::Operator::In; | ||
| 5 | |||
| 6 | # This allows the size of an autogenerated IN-clause to be adjusted. | ||
| 7 | # The limit for Oracle is 1000, and a bug requires that, in some cases | ||
| 8 | # we drop to 250. | ||
| 9 | use constant IN_CLAUSE_SIZE_LIMIT => 250; | ||
| 10 | |||
| 11 | our @CARP_NOT = qw( UR::DataSource::RDBMS ); | ||
| 12 | |||
| 13 | sub _negation_clause { '' } | ||
| 14 | |||
| 15 | sub generate_sql_for { | ||
| 16 | my($class, $expr_sql, $val, $escape) = @_; | ||
| 17 | |||
| 18 | unless (ref($val) eq 'ARRAY') { | ||
| 19 | $val = [$val]; | ||
| 20 | } | ||
| 21 | |||
| 22 | unless (@$val) { | ||
| 23 | # an empty list was passed-in. | ||
| 24 | # since "in ()", like "where 1=0", is self-contradictory, | ||
| 25 | # there is no data to return, and no SQL required | ||
| 26 | Carp::carp("Null in-clause"); | ||
| 27 | return; | ||
| 28 | } | ||
| 29 | |||
| 30 | my @list = do { no warnings 'uninitialized'; | ||
| 31 | sort @$val; }; | ||
| 32 | my $has_null = _list_contains_null(\@list); | ||
| 33 | my $wrap = ($has_null or @$val > IN_CLAUSE_SIZE_LIMIT ? 1 : 0); | ||
| 34 | my $cnt = 0; | ||
| 35 | my $sql = ''; | ||
| 36 | $sql .= "\n(\n " if $wrap; | ||
| 37 | while (my @set = splice(@list,0,IN_CLAUSE_SIZE_LIMIT)) | ||
| 38 | { | ||
| 39 | $sql .= "\n or " if $cnt++; | ||
| 40 | $sql .= $expr_sql | ||
| 41 | . $class->_negation_clause | ||
| 42 | . " in (" . join(",",map { UR::Util::sql_quote($_) } @set) . ")"; | ||
| 43 | } | ||
| 44 | if ($has_null) { | ||
| 45 | $sql .= "\n or $expr_sql is " | ||
| 46 | . $class->_negation_clause | ||
| 47 | . ' null'; | ||
| 48 | } | ||
| 49 | $sql .= "\n)\n" if $wrap; | ||
| 50 | |||
| 51 | return ($sql); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub _list_contains_null { | ||
| 55 | my $list = shift; | ||
| 56 | |||
| 57 | foreach my $elt ( @$list ) { | ||
| 58 | if (! defined($elt) | ||
| 59 | or | ||
| 60 | length($elt) == 0 | ||
| 61 | ) { | ||
| 62 | return 1; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | return ''; | ||
| 66 | } | ||
| 67 | |||
| 68 | 1; |