Skip to content

Commit

Permalink
added delete_item cmd to dynamodb module
Browse files Browse the repository at this point in the history
  • Loading branch information
jerily committed Aug 21, 2024
1 parent b2eece2 commit 74cf602
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 16 deletions.
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ cmake --install . --config=Release

### Download the latest release
```bash
wget https://github.com/jerily/aws-sdk-tcl/archive/refs/tags/v1.0.9.tar.gz
tar -xzf v1.0.9.tar.gz
export TCL_AWS_DIR=$(pwd)/aws-sdk-tcl-1.0.9
wget https://github.com/jerily/aws-sdk-tcl/archive/refs/tags/v1.0.10.tar.gz
tar -xzf v1.0.10.tar.gz
export TCL_AWS_DIR=$(pwd)/aws-sdk-tcl-1.0.10
```

### Build for TCL:
Expand Down
2 changes: 1 addition & 1 deletion src/aws-sdk-tcl-dynamodb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.22.1)
project(aws-sdk-tcl-dynamodb VERSION 1.0.9 LANGUAGES CXX C)
project(aws-sdk-tcl-dynamodb VERSION 1.0.10 LANGUAGES CXX C)
message(project: ${PROJECT_NAME})

set(TARGET ${PROJECT_NAME})
Expand Down
58 changes: 58 additions & 0 deletions src/aws-sdk-tcl-dynamodb/examples/delete-item.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package require awsdynamodb

set table "MyTableWithItems"

# To use it with real AWS S3, you can use the following configuration:
# set config_dict [dict create region "us-east-1" aws_access_key_id "your_access_key_id" aws_secret_access_key "your_secret_access_key"]

# To use it with localstack, you can use the following configuration:
set config_dict [dict create region us-east-1 endpoint "http://localhost:4566"]

# creates a DynamoDB client
::aws::dynamodb::create $config_dict client

# create a new table with an id attribute of type N (number) and HASH key type
$client create_table $table [dict create id [list N HASH]]

# an item with a nested structure, it includes the following attributes:
# - id: number
# - name: string
# - age: number
# - scopes: list of strings
# - address: map with the following attributes:
# - street: string
# - city: string
# - state: string
# - zip: number
set item_dict [dict create \
id [list N 1] \
name [list S "test"] \
age [list N 20] \
scopes [list L [list [list S "read"] [list S "write"] [list S "admin"]]] \
address [list M [list \
street [list S "1234 Main Street"] \
city [list S "Seattle"] \
state [list S "WA"] \
zip [list N 98101] \
]] \
]

# put the item into the table
$client put_item $table $item_dict

# put a second item into the table
$client put_item $table [dict create id [list N 2] name [list S "test2"]]

# specify the key of the item to get
set key_dict [dict create id [list N "1"]]

# delete the item from the table
puts deleted=[$client delete_item $table $key_dict]

# get the item from the table
set item [$client get_item $table $key_dict]
if { $item eq {} } {
puts "Item not found"
} else {
puts "Item: $item"
}
1 change: 1 addition & 0 deletions src/aws-sdk-tcl-dynamodb/examples/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ This directory contains examples of using the AWS DynamoDB service with the AWS
* [Delete a Table](delete-table.tcl) - Delete a table in DynamoDB.
* [List Tables](list-tables.tcl) - List tables in DynamoDB.
* [Put and Get Item](put-and-get-item.tcl) - Put an item in a table in DynamoDB.
* [Delete Item](delete-item.tcl) - Delete an item from a table in DynamoDB.
* [Query](query.tcl) - Query a table in DynamoDB.
* [Global Secondary Index](global-secondary-index.tcl) - Create a global secondary index on a table in DynamoDB.
69 changes: 65 additions & 4 deletions src/aws-sdk-tcl-dynamodb/library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <aws/dynamodb/DynamoDBClient.h>
#include <aws/dynamodb/model/PutItemRequest.h>
#include <aws/dynamodb/model/GetItemRequest.h>
#include <aws/dynamodb/model/DeleteItemRequest.h>
#include <aws/dynamodb/model/QueryRequest.h>
#include <aws/dynamodb/model/ScanRequest.h>
#include <aws/dynamodb/model/CreateTableRequest.h>
Expand Down Expand Up @@ -371,6 +372,53 @@ int aws_sdk_tcl_dynamodb_GetItem(Tcl_Interp *interp, const char *handle, const c
}
}

int aws_sdk_tcl_dynamodb_DeleteItem(Tcl_Interp *interp, const char *handle, const char *tableName, Tcl_Obj *dictPtr) {
DBG(fprintf(stderr, "aws_sdk_tcl_dynamodb_DeleteItem: handle=%s tableName=%s dict=%s\n", handle, tableName,
Tcl_GetString(dictPtr)));
Aws::DynamoDB::DynamoDBClient *client = aws_sdk_tcl_dynamodb_GetInternalFromName(handle);
if (!client) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("handle not found", -1));
return TCL_ERROR;
}

Aws::DynamoDB::Model::DeleteItemRequest deleteItemRequest;
deleteItemRequest.SetTableName(tableName);

Tcl_DictSearch search;
Tcl_Obj *key, *spec;
int done;
if (Tcl_DictObjFirst(interp, dictPtr, &search,
&key, &spec, &done) != TCL_OK) {
return TCL_ERROR;
}
for (; !done; Tcl_DictObjNext(&search, &key, &spec, &done)) {
Tcl_Size length;
Tcl_ListObjLength(interp, spec, &length);
if (length != 2) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("Invalid attribute value", -1));
return TCL_ERROR;
}
Aws::String attribute_key = Tcl_GetString(key);
DBG(fprintf(stderr, "key=%s spec=%s\n", attribute_key.c_str(), Tcl_GetString(spec)));
auto value = set_attribute_value(interp, spec);
DBG(fprintf(stderr, "done\n"));
deleteItemRequest.AddKey(attribute_key, *value);
}
Tcl_DictObjDone(&search);

DBG(fprintf(stderr, "deleteItemRequest ready\n"));

const Aws::DynamoDB::Model::DeleteItemOutcome outcome = client->DeleteItem(
deleteItemRequest);
if (outcome.IsSuccess()) {
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(1));
return TCL_OK;
} else {
Tcl_SetObjResult(interp, Tcl_NewStringObj(outcome.GetError().GetMessage().c_str(), -1));
return TCL_ERROR;
}
}

int aws_sdk_tcl_dynamodb_QueryItems(
Tcl_Interp *interp,
const char *handle,
Expand Down Expand Up @@ -917,10 +965,10 @@ int aws_sdk_tcl_dynamodb_ClientObjCmd(ClientData clientData, Tcl_Interp *interp,
"destroy",
"put_item",
"get_item",
"delete_item",
"query_items",
"scan",
"update_item",
"delete_item",
"create_table",
"delete_table",
"list_tables",
Expand All @@ -931,10 +979,10 @@ int aws_sdk_tcl_dynamodb_ClientObjCmd(ClientData clientData, Tcl_Interp *interp,
m_destroy,
m_putItem,
m_getItem,
m_deleteItem,
m_queryItems,
m_scan,
m_updateItem,
m_deleteItem,
m_createTable,
m_deleteTable,
m_listTables
Expand Down Expand Up @@ -970,6 +1018,14 @@ int aws_sdk_tcl_dynamodb_ClientObjCmd(ClientData clientData, Tcl_Interp *interp,
Tcl_GetString(objv[2]),
objv[3]
);
case m_deleteItem:
CheckArgs(4, 4, 1, "delete_item table key_dict");
return aws_sdk_tcl_dynamodb_DeleteItem(
interp,
handle,
Tcl_GetString(objv[2]),
objv[3]
);
case m_queryItems:
CheckArgs(4, 8, 1,
"get_item table query_dict ?projection_expression? ?scan_forward? ?limit? ?index_name?");
Expand All @@ -993,8 +1049,6 @@ int aws_sdk_tcl_dynamodb_ClientObjCmd(ClientData clientData, Tcl_Interp *interp,
);
case m_updateItem:
break;
case m_deleteItem:
break;
case m_createTable:
CheckArgs(4, 6, 1,
"create_table table key_schema_dict ?provisioned_throughput_dict? ?global_secondary_indexes_list?");
Expand Down Expand Up @@ -1120,6 +1174,12 @@ static int aws_sdk_tcl_dynamodb_GetItemCmd(ClientData clientData, Tcl_Interp *in
return aws_sdk_tcl_dynamodb_GetItem(interp, Tcl_GetString(objv[1]), Tcl_GetString(objv[2]), objv[3]);
}

static int aws_sdk_tcl_dynamodb_DeleteItemCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
DBG(fprintf(stderr, "DeleteItemCmd\n"));
CheckArgs(4, 4, 1, "handle_name table key_dict");
return aws_sdk_tcl_dynamodb_DeleteItem(interp, Tcl_GetString(objv[1]), Tcl_GetString(objv[2]), objv[3]);
}

static int
aws_sdk_tcl_dynamodb_QueryItemsCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
DBG(fprintf(stderr, "QueryItemsCmd\n"));
Expand Down Expand Up @@ -1225,6 +1285,7 @@ int Aws_sdk_tcl_dynamodb_Init(Tcl_Interp *interp) {
Tcl_CreateObjCommand(interp, "::aws::dynamodb::destroy", aws_sdk_tcl_dynamodb_DestroyCmd, nullptr, nullptr);
Tcl_CreateObjCommand(interp, "::aws::dynamodb::put_item", aws_sdk_tcl_dynamodb_PutItemCmd, nullptr, nullptr);
Tcl_CreateObjCommand(interp, "::aws::dynamodb::get_item", aws_sdk_tcl_dynamodb_GetItemCmd, nullptr, nullptr);
Tcl_CreateObjCommand(interp, "::aws::dynamodb::delete_item", aws_sdk_tcl_dynamodb_DeleteItemCmd, nullptr, nullptr);
Tcl_CreateObjCommand(interp, "::aws::dynamodb::query_items", aws_sdk_tcl_dynamodb_QueryItemsCmd, nullptr, nullptr);
Tcl_CreateObjCommand(interp, "::aws::dynamodb::scan", aws_sdk_tcl_dynamodb_ScanCmd, nullptr, nullptr);
Tcl_CreateObjCommand(interp, "::aws::dynamodb::create_table", aws_sdk_tcl_dynamodb_CreateTableCmd, nullptr,
Expand Down
2 changes: 2 additions & 0 deletions src/aws-sdk-tcl-dynamodb/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ See the [examples](examples) directory for examples of using the AWS DynamoDB se
- puts an item into a table
* **::aws::dynamodb::get_item** *handle table key_dict*
- gets an item from a table
* **::aws::dynamodb::delete_item** *handle table key_dict*
- deletes an item from a table
* **::aws::dynamodb::query_items** *handle table query_dict ?projection_expression? ?scan_forward? ?limit? ?index_name?*
- queries items from a table
* **::aws::dynamodb::scan** *handle table ?projection_expression?*
Expand Down
2 changes: 1 addition & 1 deletion src/aws-sdk-tcl-iam/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.22.1)
project(aws-sdk-tcl-iam VERSION 1.0.9 LANGUAGES CXX C)
project(aws-sdk-tcl-iam VERSION 1.0.10 LANGUAGES CXX C)
message(project: ${PROJECT_NAME})

set(TARGET ${PROJECT_NAME})
Expand Down
2 changes: 1 addition & 1 deletion src/aws-sdk-tcl-kms/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.22.1)
project(aws-sdk-tcl-kms VERSION 1.0.9 LANGUAGES CXX C)
project(aws-sdk-tcl-kms VERSION 1.0.10 LANGUAGES CXX C)
message(project: ${PROJECT_NAME})

set(TARGET ${PROJECT_NAME})
Expand Down
2 changes: 1 addition & 1 deletion src/aws-sdk-tcl-lambda/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.22.1)
project(aws-sdk-tcl-lambda VERSION 1.0.9 LANGUAGES CXX C)
project(aws-sdk-tcl-lambda VERSION 1.0.10 LANGUAGES CXX C)
message(project: ${PROJECT_NAME})

set(TARGET ${PROJECT_NAME})
Expand Down
2 changes: 1 addition & 1 deletion src/aws-sdk-tcl-s3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.22.1)
project(aws-sdk-tcl-s3 VERSION 1.0.9 LANGUAGES CXX C)
project(aws-sdk-tcl-s3 VERSION 1.0.10 LANGUAGES CXX C)
message(project: ${PROJECT_NAME})

set(TARGET ${PROJECT_NAME})
Expand Down
2 changes: 1 addition & 1 deletion src/aws-sdk-tcl-sqs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.22.1)
project(aws-sdk-tcl-sqs VERSION 1.0.9 LANGUAGES CXX C)
project(aws-sdk-tcl-sqs VERSION 1.0.10 LANGUAGES CXX C)
message(project: ${PROJECT_NAME})

set(TARGET ${PROJECT_NAME})
Expand Down
2 changes: 1 addition & 1 deletion src/aws-sdk-tcl-ssm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.22.1)
project(aws-sdk-tcl-ssm VERSION 1.0.9 LANGUAGES CXX C)
project(aws-sdk-tcl-ssm VERSION 1.0.10 LANGUAGES CXX C)
message(project: ${PROJECT_NAME})

set(TARGET ${PROJECT_NAME})
Expand Down
4 changes: 2 additions & 2 deletions ttrek.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aws-sdk-tcl",
"version": "1.0.9",
"version": "1.0.10",
"scripts": {

},
Expand All @@ -15,7 +15,7 @@
"default": [
{
"cmd": "download",
"url": "https://github.com/jerily/aws-sdk-tcl/archive/refs/tags/v1.0.9.tar.gz"
"url": "https://github.com/jerily/aws-sdk-tcl/archive/refs/tags/v1.0.10.tar.gz"
},
{
"cmd": "unpack"
Expand Down

0 comments on commit 74cf602

Please sign in to comment.