20
20
* #L%
21
21
*/
22
22
23
+ import com .datastax .astra .client .collections .commands .FindOneAndDeleteOptions ;
24
+ import com .datastax .astra .client .collections .commands .UpdateOneOptions ;
25
+ import com .datastax .astra .client .collections .commands .UpdateResult ;
26
+ import com .datastax .astra .client .collections .documents .Update ;
23
27
import com .datastax .astra .client .core .options .DataAPIOptions ;
24
28
import com .datastax .astra .client .collections .documents .Document ;
25
29
import com .datastax .astra .client .core .commands .Command ;
31
35
import com .datastax .astra .client .tables .commands .EstimatedCountRowsOptions ;
32
36
import com .datastax .astra .client .tables .commands .TableDeleteManyOptions ;
33
37
import com .datastax .astra .client .tables .commands .TableDeleteOneOptions ;
38
+ import com .datastax .astra .client .tables .commands .TableFindOneAndDeleteOptions ;
34
39
import com .datastax .astra .client .tables .commands .TableFindOneOptions ;
35
40
import com .datastax .astra .client .tables .commands .TableInsertManyOptions ;
36
41
import com .datastax .astra .client .tables .commands .TableInsertManyResult ;
37
42
import com .datastax .astra .client .tables .commands .TableInsertOneOptions ;
38
43
import com .datastax .astra .client .tables .commands .TableInsertOneResult ;
44
+ import com .datastax .astra .client .tables .commands .TableUpdateOneOptions ;
39
45
import com .datastax .astra .client .tables .commands .ddl .AlterTableOperation ;
40
46
import com .datastax .astra .client .tables .commands .ddl .AlterTableOptions ;
41
47
import com .datastax .astra .client .tables .commands .ddl .CreateIndexOptions ;
46
52
import com .datastax .astra .client .tables .mapping .EntityTable ;
47
53
import com .datastax .astra .client .tables .mapping .EntityBeanDefinition ;
48
54
import com .datastax .astra .client .tables .row .Row ;
55
+ import com .datastax .astra .client .tables .row .TableUpdate ;
49
56
import com .datastax .astra .internal .api .DataAPIData ;
50
57
import com .datastax .astra .internal .api .DataAPIResponse ;
51
58
import com .datastax .astra .internal .api .DataAPIStatus ;
@@ -545,24 +552,6 @@ public CompletableFuture<Optional<T>> findOneASync(Filter filter, TableFindOneOp
545
552
return CompletableFuture .supplyAsync (() -> findOne (filter , findOneOptions ));
546
553
}
547
554
548
- // -------------------------
549
- // --- findOneAndDelete ----
550
- // -------------------------
551
-
552
- // FIXME
553
-
554
- // -------------------------
555
- // --- findOneAndReplace ---
556
- // -------------------------
557
-
558
- // FIXME
559
-
560
- // -------------------------
561
- // --- findOneAndUpdate ----
562
- // -------------------------
563
-
564
- // FIXME
565
-
566
555
// -------------------------
567
556
// --- find ----
568
557
// -------------------------
@@ -573,7 +562,70 @@ public CompletableFuture<Optional<T>> findOneASync(Filter filter, TableFindOneOp
573
562
// --- updateOne ----
574
563
// -------------------------
575
564
576
- // FIXME
565
+ /**
566
+ * Update a single row in the table according to the specified arguments.
567
+ *
568
+ * @param filter
569
+ * a row describing the query filter, which may not be null.
570
+ * @param update
571
+ * a row describing the update, which may not be null. The update to apply must include at least one update operator.
572
+ * @return
573
+ * the result of the update one operation
574
+ */
575
+ public UpdateResult updateOne (Filter filter , TableUpdate update ) {
576
+ return updateOne (filter , update , new TableUpdateOneOptions ());
577
+ }
578
+
579
+ /**
580
+ * Update a single document in the collection according to the specified arguments.
581
+ *
582
+ * @param filter
583
+ * a document describing the query filter, which may not be null.
584
+ * @param update
585
+ * a document describing the update, which may not be null. The update to apply must include at least one update operator.
586
+ * @param updateOptions
587
+ * the options to apply to the update operation
588
+ * @return
589
+ * the result of the update one operation
590
+ */
591
+ public UpdateResult updateOne (Filter filter , TableUpdate update , TableUpdateOneOptions updateOptions ) {
592
+ notNull (update , ARG_UPDATE );
593
+ notNull (updateOptions , ARG_OPTIONS );
594
+ Command cmd = Command
595
+ .create ("updateOne" )
596
+ .withFilter (filter )
597
+ .withUpdate (update )
598
+ .withSort (updateOptions .getSort ())
599
+ .withOptions (new Document ()
600
+ .appendIfNotNull (INPUT_UPSERT , updateOptions .getUpsert ())
601
+ );
602
+ return getUpdateResult (runCommand (cmd , updateOptions ));
603
+ }
604
+
605
+ /**
606
+ * Update all documents in the collection according to the specified arguments.
607
+ *
608
+ * @param apiResponse
609
+ * response for the API
610
+ * @return
611
+ * the result of the update many operation
612
+ */
613
+ private static UpdateResult getUpdateResult (DataAPIResponse apiResponse ) {
614
+ UpdateResult result = new UpdateResult ();
615
+ DataAPIStatus status = apiResponse .getStatus ();
616
+ if (status != null ) {
617
+ if (status .containsKey (RESULT_MATCHED_COUNT )) {
618
+ result .setMatchedCount (status .getInteger (RESULT_MATCHED_COUNT ));
619
+ }
620
+ if (status .containsKey (RESULT_MODIFIED_COUNT )) {
621
+ result .setModifiedCount (status .getInteger (RESULT_MODIFIED_COUNT ));
622
+ }
623
+ if (status .containsKey (RESULT_UPSERTED_ID )) {
624
+ result .setMatchedCount (status .getInteger (RESULT_UPSERTED_ID ));
625
+ }
626
+ }
627
+ return result ;
628
+ }
577
629
578
630
// -------------------------
579
631
// --- deleteOne ----
0 commit comments