11/*
2- * Copyright 2018-2020 ObjectBox Ltd. All rights reserved.
2+ * Copyright 2018-2023 ObjectBox Ltd. All rights reserved.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -164,6 +164,7 @@ int do_action_new(OBX_store* store, int argc, char* argv[]) {
164164 size_t size = 0 ;
165165 OBX_txn * txn = NULL ;
166166 OBX_cursor * cursor = NULL ;
167+ bool success = false;
167168
168169 // grab the task text from the command line
169170 if (parse_text (argc , argv , & text ) <= 0 ) {
@@ -186,7 +187,7 @@ int do_action_new(OBX_store* store, int argc, char* argv[]) {
186187
187188 // Get an ID for our soon-to-be-created task entity
188189 obx_id id = obx_cursor_id_for_put (cursor , 0 );
189- if (! id ) {
190+ if (id == 0 ) {
190191 goto clean_up ;
191192 }
192193
@@ -199,30 +200,29 @@ int do_action_new(OBX_store* store, int argc, char* argv[]) {
199200 if (obx_cursor_put_new (cursor , id , buff , size )) {
200201 goto clean_up ;
201202 }
203+ printf ("New task created with ID %" PRIu64 ": %s\n" , id , text );
204+ success = true;
202205
203206clean_up :
204- if (!obx_last_error_code ()) {
205- printf ("New task: %" PRIu64 " - %s\n" , id , text );
206- } else {
207+ if (!success ) {
207208 printf ("Failed to create the task\n" );
208209 }
210+ obx_err error_code = obx_last_error_code ();
209211
210- if (cursor ) {
211- obx_cursor_close (cursor );
212- }
213-
214- if (txn && !obx_last_error_code ()) {
215- obx_txn_success (txn );
216- }
212+ obx_cursor_close (cursor ); // cursor may be NULL
217213
218214 if (txn ) {
219- obx_txn_close (txn );
215+ if (success && error_code == OBX_SUCCESS ) {
216+ obx_txn_success (txn );
217+ } else {
218+ obx_txn_close (txn );
219+ }
220220 }
221221
222222 free (text );
223223 free (buff );
224224
225- return obx_last_error_code () ;
225+ return error_code ;
226226}
227227
228228//--------------------------------------------------------------------------------------------------------------------
@@ -395,7 +395,7 @@ int parse_text(int argc, char** argv, char** outText) {
395395 return -1 ;
396396 }
397397
398- * outText = (char * ) malloc (sizeof (char ) * (size_t )(size + 1 ));
398+ * outText = (char * ) malloc (sizeof (char ) * (size_t ) (size + 1 ));
399399 if (!* outText ) {
400400 printf ("Could not process task text\n" );
401401 return -1 ;
@@ -446,14 +446,14 @@ int task_build(void** out_buff, size_t* out_size, obx_id id, const char* text, u
446446 return rc ;
447447}
448448
449- uint64_t timestamp_now () { return (uint64_t )(time (NULL ) * 1000 ); }
449+ uint64_t timestamp_now () { return (uint64_t ) (time (NULL ) * 1000 ); }
450450
451451void date_to_str (char * buff , uint64_t timestamp ) {
452452 if (!timestamp ) {
453453 // empty string
454454 buff [0 ] = '\0' ;
455455 } else {
456- time_t time = (time_t )(timestamp / 1000 );
456+ time_t time = (time_t ) (timestamp / 1000 );
457457 struct tm * tm_info = localtime (& time );
458458 strftime (buff , DATE_BUFFER_LENGTH , DATE_FORMAT_STRING , tm_info );
459459 }
0 commit comments