| 1 | # | ||
| 2 | # Copyright (c) 2005, Presicient Corp., USA | ||
| 3 | # | ||
| 4 | # Permission is granted to use this software according to the terms of the | ||
| 5 | # Artistic License, as specified in the Perl README file, | ||
| 6 | # with the exception that commercial redistribution, either | ||
| 7 | # electronic or via physical media, as either a standalone package, | ||
| 8 | # or incorporated into a third party product, requires prior | ||
| 9 | # written approval of the author. | ||
| 10 | # | ||
| 11 | # This software is distributed in the hope that it will be useful, | ||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| 14 | # | ||
| 15 | # Presicient Corp. reserves the right to provide support for this software | ||
| 16 | # to individual sites under a separate (possibly fee-based) | ||
| 17 | # agreement. | ||
| 18 | # | ||
| 19 | # History: | ||
| 20 | # | ||
| 21 | # 2005-Jan-27 D. Arnold | ||
| 22 | # Coded. | ||
| 23 | # | ||
| 24 | package SQL::Amazon::ReqFactory; | ||
| 25 | use SQL::Amazon::Request::Request; | ||
| 26 | use SQL::Amazon::Parser; | ||
| 27 | use SQL::Amazon::Parser qw(:pred_node_codes); | ||
| 28 | |||
| 29 | use strict; | ||
| 30 | |||
| 31 | our $VERSION = '0.10'; | ||
| 32 | |||
| 33 | use constant HAS_KEYS => 1; | ||
| 34 | use constant HAS_MATCHES => 2; | ||
| 35 | my %is_search_function = qw( | ||
| 36 | AMZN_MATCH_ANY 1 | ||
| 37 | AMZN_MATCH_ALL 1 | ||
| 38 | AMZN_MATCH_TEXT 1 | ||
| 39 | AMZN_POWER_SEARCH 1 | ||
| 40 | ); | ||
| 41 | my %can_search = qw( | ||
| 42 | CustomerContent SQL::Amazon::Request::CustomerContentSearch | ||
| 43 | Item SQL::Amazon::Request::ItemSearch | ||
| 44 | List SQL::Amazon::Request::ListSearch | ||
| 45 | SellerListing SQL::Amazon::Request::SellerListingSearch | ||
| 46 | ); | ||
| 47 | |||
| 48 | my %can_lookup = qw( | ||
| 49 | BrowseNode SQL::Amazon::Request::BrowseNodeLookup | ||
| 50 | CustomerContent SQL::Amazon::Request::CustomerContentLookup | ||
| 51 | Item SQL::Amazon::Request::ItemLookup | ||
| 52 | List SQL::Amazon::Request::ListLookup | ||
| 53 | Seller SQL::Amazon::Request::SellerLookup | ||
| 54 | SellerListing SQL::Amazon::Request::SellerListingLookup | ||
| 55 | Transaction SQL::Amazon::Request::TransactionLookup | ||
| 56 | ); | ||
| 57 | |||
| 58 | my %can_add = qw( | ||
| 59 | Cart SQL::Amazon::Request::CartAdd | ||
| 60 | ); | ||
| 61 | |||
| 62 | my %can_clear = qw( | ||
| 63 | Cart SQL::Amazon::Request::CartClear | ||
| 64 | ); | ||
| 65 | |||
| 66 | my %can_create = qw( | ||
| 67 | Cart SQL::Amazon::Request::CartCreate | ||
| 68 | ); | ||
| 69 | |||
| 70 | my %can_get = qw( | ||
| 71 | Cart SQL::Amazon::Request::CartGet | ||
| 72 | ); | ||
| 73 | |||
| 74 | my %can_modify = qw( | ||
| 75 | Cart SQL::Amazon::Request::CartModify | ||
| 76 | ); | ||
| 77 | |||
| 78 | our $errstr; | ||
| 79 | sub errstr { return $errstr; } | ||
| 80 | |||
| 81 | sub create_request { | ||
| 82 | my ($class, $reqclass, $table, $predicate, $parser) = @_; | ||
| 83 | |||
| 84 | my $command = $parser->{struct}{command}; | ||
| 85 | $errstr = $command . " unsupported on $table.", | ||
| 86 | return (undef, undef) | ||
| 87 | unless ( | ||
| 88 | (($command eq 'CREATE') && $can_create{$reqclass}) || | ||
| 89 | (($command eq 'DELETE') && $can_clear{$reqclass}) || | ||
| 90 | (($command eq 'INSERT') && $can_add{$reqclass}) || | ||
| 91 | (($command eq 'UPDATE') && $can_modify{$reqclass}) || | ||
| 92 | (($command eq 'SELECT') && | ||
| 93 | ($can_search{$reqclass} || | ||
| 94 | $can_lookup{$reqclass} || | ||
| 95 | $can_get{$reqclass})) | ||
| 96 | ); | ||
| 97 | |||
| 98 | $errstr = 'Only SELECT operation supported in this release.', | ||
| 99 | return (undef, undef) | ||
| 100 | unless ($command eq 'SELECT'); | ||
| 101 | |||
| 102 | my $flags = classify_request_type($predicate, 0); | ||
| 103 | |||
| 104 | $errstr = "$table does not support MATCHES predicate.", | ||
| 105 | return (undef, undef) | ||
| 106 | if (($flags & HAS_MATCHES) && (!$can_search{$reqclass})); | ||
| 107 | my $reqobj; | ||
| 108 | |||
| 109 | $reqclass = (($flags & HAS_MATCHES) || (! ($flags & HAS_KEYS))) ? | ||
| 110 | $can_search{$reqclass} : $can_lookup{$reqclass}; | ||
| 111 | |||
| 112 | ($predicate, $reqobj) = ${reqclass}->new($predicate, $table, $parser); | ||
| 113 | $errstr = ${reqclass}->errstr, | ||
| 114 | return (undef, undef) | ||
| 115 | unless ($predicate || $reqobj); | ||
| 116 | return ($predicate, $reqobj); | ||
| 117 | } | ||
| 118 | sub classify_request_type { | ||
| 119 | my ($expr, $flags) = @_; | ||
| 120 | |||
| 121 | my $conjoins = $expr->[0]; | ||
| 122 | foreach (@$conjoins) { | ||
| 123 | |||
| 124 | $flags |= HAS_KEYS, | ||
| 125 | next | ||
| 126 | if (($_->[SQL_TREE_OP] eq '=') && | ||
| 127 | ($_->[SQL_TREE_ARG1]{type} eq 'column') && | ||
| 128 | ($_->[SQL_TREE_ARG1]{value}=~ | ||
| 129 | /^([A-Z_]\w*\.)?(ASIN|UPC|SKU|EAN)$/i) && | ||
| 130 | (! $_->[SQL_TREE_NEG])); | ||
| 131 | |||
| 132 | next unless ($_->[SQL_TREE_OP] eq 'USER_DEFINED'); | ||
| 133 | my $name; | ||
| 134 | $name = (ref $_ ne 'HASH') ? | ||
| 135 | $_->[SQL_TREE_ARG1]->name : | ||
| 136 | $_->[SQL_TREE_ARG1]{name}; | ||
| 137 | |||
| 138 | $flags |= HAS_MATCHES, | ||
| 139 | next | ||
| 140 | if $is_search_function{$name}; | ||
| 141 | my $value = (ref $_ ne 'HASH') ? | ||
| 142 | $_->[SQL_TREE_ARG1]->args : | ||
| 143 | $_->[SQL_TREE_ARG1]{value}; | ||
| 144 | |||
| 145 | $flags |= HAS_KEYS | ||
| 146 | if (($name eq 'AMZN_IN_ANY') && | ||
| 147 | ($value->[0]{value}=~ | ||
| 148 | /^([A-Z_]\w*\.)?(ASIN|UPC|SKU|EAN)$/i) && | ||
| 149 | (! $_->[SQL_TREE_NEG]) | ||
| 150 | ); | ||
| 151 | } | ||
| 152 | return $flags; | ||
| 153 | } | ||
| 154 | sub cleanup_requests { | ||
| 155 | my ($class, $requests) = @_; | ||
| 156 | |||
| 157 | my @sorted_reqs = (); | ||
| 158 | my @remaining = (); | ||
| 159 | foreach (@$requests) { | ||
| 160 | push(@sorted_reqs, $_) | ||
| 161 | if ($_->[0] eq 'Item'); | ||
| 162 | } | ||
| 163 | foreach (@$requests) { | ||
| 164 | push @remaining, $_ | ||
| 165 | unless (($_->[0] eq 'Item') || | ||
| 166 | (scalar @sorted_reqs)); | ||
| 167 | } | ||
| 168 | push @sorted_reqs, @remaining | ||
| 169 | if scalar @remaining; | ||
| 170 | return \@sorted_reqs; | ||
| 171 | } | ||
| 172 | |||
| 173 | 1; |