forked from Dynamoid/dynamoid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Dynamoid#655 from Dynamoid/migrate-to-expression-a…
…ttributes-for-query-and-scan Migrate to expression attributes for Query and Scan
- Loading branch information
Showing
16 changed files
with
679 additions
and
528 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
lib/dynamoid/adapter_plugin/aws_sdk_v3/filter_expression_convertor.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# frozen_string_literal: true | ||
|
||
module Dynamoid | ||
# @private | ||
module AdapterPlugin | ||
class AwsSdkV3 | ||
class FilterExpressionConvertor | ||
attr_reader :expression, :name_placeholders, :value_placeholders | ||
|
||
def initialize(conditions, name_placeholders, value_placeholders, name_placeholder_sequence, value_placeholder_sequence) | ||
@conditions = conditions | ||
@name_placeholders = name_placeholders.dup | ||
@value_placeholders = value_placeholders.dup | ||
@name_placeholder_sequence = name_placeholder_sequence | ||
@value_placeholder_sequence = value_placeholder_sequence | ||
|
||
build | ||
end | ||
|
||
private | ||
|
||
def build | ||
clauses = @conditions.map do |name, attribute_conditions| | ||
attribute_conditions.map do |operator, value| | ||
name_or_placeholder = name_or_placeholder_for(name) | ||
|
||
case operator | ||
when :eq | ||
"#{name_or_placeholder} = #{value_placeholder_for(value)}" | ||
when :ne | ||
"#{name_or_placeholder} <> #{value_placeholder_for(value)}" | ||
when :gt | ||
"#{name_or_placeholder} > #{value_placeholder_for(value)}" | ||
when :lt | ||
"#{name_or_placeholder} < #{value_placeholder_for(value)}" | ||
when :gte | ||
"#{name_or_placeholder} >= #{value_placeholder_for(value)}" | ||
when :lte | ||
"#{name_or_placeholder} <= #{value_placeholder_for(value)}" | ||
when :between | ||
"#{name_or_placeholder} BETWEEN #{value_placeholder_for(value[0])} AND #{value_placeholder_for(value[1])}" | ||
when :begins_with | ||
"begins_with (#{name_or_placeholder}, #{value_placeholder_for(value)})" | ||
when :in | ||
list = value.map(&method(:value_placeholder_for)).join(' , ') | ||
"#{name_or_placeholder} IN (#{list})" | ||
when :contains | ||
"contains (#{name_or_placeholder}, #{value_placeholder_for(value)})" | ||
when :not_contains | ||
"NOT contains (#{name_or_placeholder}, #{value_placeholder_for(value)})" | ||
when :null | ||
"attribute_not_exists (#{name_or_placeholder})" | ||
when :not_null | ||
"attribute_exists (#{name_or_placeholder})" | ||
end | ||
end | ||
end.flatten | ||
|
||
@expression = clauses.join(' AND ') | ||
end | ||
|
||
def name_or_placeholder_for(name) | ||
return name unless name.upcase.in?(Dynamoid::AdapterPlugin::AwsSdkV3::RESERVED_WORDS) | ||
|
||
placeholder = @name_placeholder_sequence.call | ||
@name_placeholders[placeholder] = name | ||
placeholder | ||
end | ||
|
||
def value_placeholder_for(value) | ||
placeholder = @value_placeholder_sequence.call | ||
@value_placeholders[placeholder] = value | ||
placeholder | ||
end | ||
end | ||
end | ||
end | ||
end |
38 changes: 38 additions & 0 deletions
38
lib/dynamoid/adapter_plugin/aws_sdk_v3/projection_expression_convertor.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module Dynamoid | ||
# @private | ||
module AdapterPlugin | ||
class AwsSdkV3 | ||
class ProjectionExpressionConvertor | ||
attr_reader :expression, :name_placeholders | ||
|
||
def initialize(names, name_placeholders, name_placeholder_sequence) | ||
@names = names | ||
@name_placeholders = name_placeholders.dup | ||
@name_placeholder_sequence = name_placeholder_sequence | ||
|
||
build | ||
end | ||
|
||
private | ||
|
||
def build | ||
return if @names.nil? || @names.empty? | ||
|
||
clauses = @names.map do |name| | ||
if name.upcase.in?(Dynamoid::AdapterPlugin::AwsSdkV3::RESERVED_WORDS) | ||
placeholder = @name_placeholder_sequence.call | ||
@name_placeholders[placeholder] = name | ||
placeholder | ||
else | ||
name.to_s | ||
end | ||
end | ||
|
||
@expression = clauses.join(' , ') | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.