| 1 | # ABSTRACT: JsonSQL::Param::Conditions::EnumCondition object. Subclass for parsing enum conditions (ex: 'in', 'ni', etc). | ||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use 5.014; | ||
| 8 | |||
| 9 | package JsonSQL::Param::Conditions::EnumCondition; | ||
| 10 | |||
| 11 | our $VERSION = '0.4'; # VERSION | ||
| 12 | |||
| 13 | use base qw( JsonSQL::Param::Condition ); | ||
| 14 | |||
| 15 | use SQL::QueryMaker qw( sql_in sql_not_in ); | ||
| 16 | #use Data::Dumper; | ||
| 17 | |||
| 18 | use JsonSQL::Param::Field; | ||
| 19 | use JsonSQL::Error; | ||
| 20 | |||
| 21 | |||
| 22 | ## Define the SQL::QueryMaker methods used for building up SQL condition statements. | ||
| 23 | my %dispatch = ( | ||
| 24 | 'in' => \&sql_in, | ||
| 25 | 'ni' => \&sql_not_in | ||
| 26 | ); | ||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | sub new { | ||
| 31 | my ( $class, $conditionhashref, $queryObj, $default_table_rules ) = @_; | ||
| 32 | |||
| 33 | my $self = $class->SUPER::new($conditionhashref); | ||
| 34 | my @condition_errors; | ||
| 35 | |||
| 36 | my $field = $conditionhashref->{$self->{_op}}->{field}; | ||
| 37 | my $conditionField = JsonSQL::Param::Field->new($field, $queryObj, $default_table_rules); | ||
| 38 | if ( eval { $conditionField->is_error } ) { | ||
| 39 | push(@condition_errors, "Error using field $field in enum condition: $conditionField->{message}"); | ||
| 40 | } else { | ||
| 41 | $self->{_field} = $conditionField; | ||
| 42 | } | ||
| 43 | |||
| 44 | $self->{_list} = $conditionhashref->{$self->{_op}}->{list}; | ||
| 45 | |||
| 46 | if ( @condition_errors ) { | ||
| 47 | my $err = "Error(s) constructing JsonSQL Condition object: \n\t"; | ||
| 48 | $err .= join("\n\t", @condition_errors); | ||
| 49 | return JsonSQL::Error->new("jsonsql_enumcondition", $err); | ||
| 50 | } else { | ||
| 51 | return $self; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | sub get_sql_obj { | ||
| 57 | my ( $self, $queryObj ) = @_; | ||
| 58 | |||
| 59 | if (exists $dispatch{$self->{_op}}) { | ||
| 60 | my $field = $self->{_field}->get_field_param($queryObj); | ||
| 61 | return $dispatch{$self->{_op}}->($field => $self->{_list}); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | 1; | ||
| 67 | |||
| 68 | __END__ | ||
| 69 | |||
| 70 | =pod | ||
| 71 | |||
| 72 | =encoding UTF-8 | ||
| 73 | |||
| 74 | =head1 NAME | ||
| 75 | |||
| 76 | JsonSQL::Param::Conditions::EnumCondition - JsonSQL::Param::Conditions::EnumCondition object. Subclass for parsing enum conditions (ex: 'in', 'ni', etc). | ||
| 77 | |||
| 78 | =head1 VERSION | ||
| 79 | |||
| 80 | version 0.4 | ||
| 81 | |||
| 82 | =head1 SYNOPSIS | ||
| 83 | |||
| 84 | This module constructs a Perl object representing the VALUES parameter of an SQL INSERT statement and has methods for | ||
| 85 | generating the appropriate SQL string and bind values for use with the L<DBI> module. | ||
| 86 | |||
| 87 | =head1 DESCRIPTION | ||
| 88 | |||
| 89 | =head3 Object properties: | ||
| 90 | |||
| 91 | =over | ||
| 92 | |||
| 93 | =item _field => L<JsonSQL::Param::Field> | ||
| 94 | |||
| 95 | =item _list => Array of enum values | ||
| 96 | |||
| 97 | =back | ||
| 98 | |||
| 99 | =head3 Generated parameters: | ||
| 100 | |||
| 101 | =over | ||
| 102 | |||
| 103 | =item $enumparameter => L<SQL::QueryMaker> object. | ||
| 104 | |||
| 105 | =back | ||
| 106 | |||
| 107 | =head1 METHODS | ||
| 108 | |||
| 109 | =head2 Constructor new($conditionhashref, $queryObj, $default_table_rules) | ||
| 110 | |||
| 111 | Instantiates and returns a new JsonSQL::Param::Conditions::EnumCondition object. | ||
| 112 | |||
| 113 | $conditionhashref => A hashref of the condition statement keyed by the operator. | ||
| 114 | $queryObj => A reference to the JsonSQL::Query object that will own this object. | ||
| 115 | $default_table_rules => The default whitelist table rules to use to validate access when the table params | ||
| 116 | are not provided to the field object. Usually, these are acquired from the table params | ||
| 117 | of another object (ex: the FROM clause of a SELECT statement). | ||
| 118 | |||
| 119 | Returns a JsonSQL::Error object on failure. | ||
| 120 | |||
| 121 | =head2 ObjectMethod get_sql_obj -> $enumparameter | ||
| 122 | |||
| 123 | Generates parameters represented by the object for the SQL statement. Returns: | ||
| 124 | |||
| 125 | $enumparameter => The enum condition to append to the WHERE clause. Constructed by calling the | ||
| 126 | L<SQL::QueryMaker> function defined by the dispatcher with the $field and $list | ||
| 127 | parameters. | ||
| 128 | |||
| 129 | =head1 AUTHOR | ||
| 130 | |||
| 131 | Chris Hoefler <bhoefler@draper.com> | ||
| 132 | |||
| 133 | =head1 COPYRIGHT AND LICENSE | ||
| 134 | |||
| 135 | This software is copyright (c) 2017 by Chris Hoefler. | ||
| 136 | |||
| 137 | This is free software; you can redistribute it and/or modify it under | ||
| 138 | the same terms as the Perl 5 programming language system itself. | ||
| 139 | |||
| 140 | =cut |