-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathpartiql_single.rb
More file actions
86 lines (79 loc) · 3.35 KB
/
partiql_single.rb
File metadata and controls
86 lines (79 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
require 'aws-sdk-dynamodb'
require 'json'
require 'open-uri'
require 'zip'
require_relative '../scaffold'
# snippet-start:[ruby.example_code.ruby.DynamoDBPartiQLSingle.full]
# snippet-start:[ruby.example_code.ruby.DynamoDBPartiQLSingle.decl]
class DynamoDBPartiQLSingle
attr_reader :dynamo_resource, :table
def initialize(table_name)
client = Aws::DynamoDB::Client.new(region: 'us-east-1')
@dynamodb = Aws::DynamoDB::Resource.new(client: client)
@table = @dynamodb.table(table_name)
end
# snippet-end:[ruby.example_code.ruby.DynamoDBPartiQLSingle.decl]
# snippet-start:[ruby.example_code.dynamodb.partiql.single_select]
# Gets a single record from a table using PartiQL.
# Note: To perform more fine-grained selects,
# use the Client.query instance method instead.
#
# @param title [String] The title of the movie to search.
# @return [Aws::DynamoDB::Types::ExecuteStatementOutput]
def select_item_by_title(title)
request = {
statement: "SELECT * FROM \"#{@table.name}\" WHERE title=?",
parameters: [title]
}
@dynamodb.client.execute_statement(request)
end
# snippet-end:[ruby.example_code.dynamodb.partiql.single_select]
# snippet-start:[ruby.example_code.dynamodb.partiql.single_update]
# Updates a single record from a table using PartiQL.
#
# @param title [String] The title of the movie to update.
# @param year [Integer] The year the movie was released.
# @param rating [Float] The new rating to assign the title.
# @return [Aws::DynamoDB::Types::ExecuteStatementOutput]
def update_rating_by_title(title, year, rating)
request = {
statement: "UPDATE \"#{@table.name}\" SET info.rating=? WHERE title=? and year=?",
parameters: [{ "N": rating }, title, year]
}
@dynamodb.client.execute_statement(request)
end
# snippet-end:[ruby.example_code.dynamodb.partiql.single_update]
# snippet-start:[ruby.example_code.dynamodb.partiql.single_delete]
# Deletes a single record from a table using PartiQL.
#
# @param title [String] The title of the movie to update.
# @param year [Integer] The year the movie was released.
# @return [Aws::DynamoDB::Types::ExecuteStatementOutput]
def delete_item_by_title(title, year)
request = {
statement: "DELETE FROM \"#{@table.name}\" WHERE title=? and year=?",
parameters: [title, year]
}
@dynamodb.client.execute_statement(request)
end
# snippet-end:[ruby.example_code.dynamodb.partiql.single_delete]
# snippet-start:[ruby.example_code.dynamodb.partiql.single_insert]
# Adds a single record to a table using PartiQL.
#
# @param title [String] The title of the movie to update.
# @param year [Integer] The year the movie was released.
# @param plot [String] The plot of the movie.
# @param rating [Float] The new rating to assign the title.
# @return [Aws::DynamoDB::Types::ExecuteStatementOutput]
def insert_item(title, year, plot, rating)
request = {
statement: "INSERT INTO \"#{@table.name}\" VALUE {'title': ?, 'year': ?, 'info': ?}",
parameters: [title, year, { 'plot': plot, 'rating': rating }]
}
@dynamodb.client.execute_statement(request)
end
# snippet-end:[ruby.example_code.dynamodb.partiql.single_insert]
end
# snippet-end:[ruby.example_code.ruby.DynamoDBPartiQLSingle.full]