Skip to content

Commit f879a4f

Browse files
Allow use of _id for "insertOne" (#1338)
1 parent f2f7e3a commit f879a4f

File tree

7 files changed

+31
-32
lines changed

7 files changed

+31
-32
lines changed

src/main/java/io/stargate/sgv2/jsonapi/service/cqldriver/executor/QueryExecutor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ protected Uni<Optional<TableMetadata>> getSchema(
265265
.failure(ErrorCode.NAMESPACE_DOES_NOT_EXIST.toApiException("%s", namespace));
266266
}
267267
// else get the table
268-
// TODO: this should probably use CqlIdentifier.fromCql() if we want to be case sensitive
268+
// TODO: this should probably use CqlIdentifier.fromCql() (or .fromInternal())
269+
// if we want to support case-sensitive names
269270
return Uni.createFrom().item(keyspaceMetadata.getTable("\"" + collectionName + "\""));
270271
}
271272

src/main/java/io/stargate/sgv2/jsonapi/service/operation/filters/table/codecs/MissingJSONCodecException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public MissingJSONCodecException(
2222
TableMetadata table, ColumnMetadata column, Class<?> javaType, Object value) {
2323
super(
2424
String.format(
25-
"No JSONCodec found for table %s column %s with java type %s and value %s",
26-
table.getName(), column.getName(), javaType, value));
25+
"No JSONCodec found for table '%s' column '%s' column type %s with java type %s and value %s",
26+
table.getName(), column.getName(), column.getType(), javaType, value));
2727
this.table = table;
2828
this.column = column;
2929
this.javaType = javaType;

src/main/java/io/stargate/sgv2/jsonapi/service/operation/tables/InsertTableOperation.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package io.stargate.sgv2.jsonapi.service.operation.tables;
22

3-
import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.*;
4-
53
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
4+
import com.datastax.oss.driver.api.querybuilder.QueryBuilder;
65
import com.datastax.oss.driver.api.querybuilder.insert.InsertInto;
76
import com.datastax.oss.driver.api.querybuilder.insert.RegularInsert;
87
import io.smallrye.mutiny.Multi;
@@ -94,7 +93,7 @@ private SimpleStatement buildInsertStatement(
9493
QueryExecutor queryExecutor, TableInsertAttempt insertAttempt) {
9594

9695
InsertInto insertInto =
97-
insertInto(
96+
QueryBuilder.insertInto(
9897
commandContext.schemaObject().tableMetadata.getKeyspace(),
9998
commandContext.schemaObject().tableMetadata.getName());
10099

src/main/java/io/stargate/sgv2/jsonapi/service/operation/tables/TableInsertAttempt.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
import com.fasterxml.jackson.databind.JsonNode;
44
import io.stargate.sgv2.jsonapi.service.cqldriver.executor.TableSchemaObject;
55
import io.stargate.sgv2.jsonapi.service.operation.InsertAttempt;
6-
import io.stargate.sgv2.jsonapi.service.operation.query.InsertValuesCQLClause;
76
import io.stargate.sgv2.jsonapi.service.shredding.DocRowIdentifer;
87
import io.stargate.sgv2.jsonapi.service.shredding.tables.RowId;
98
import io.stargate.sgv2.jsonapi.service.shredding.tables.RowShredder;
109
import io.stargate.sgv2.jsonapi.service.shredding.tables.WriteableTableRow;
10+
import java.util.ArrayList;
1111
import java.util.List;
1212
import java.util.Objects;
1313
import java.util.Optional;
14-
import java.util.stream.IntStream;
1514

1615
public class TableInsertAttempt implements InsertAttempt {
1716

@@ -41,25 +40,23 @@ public static List<TableInsertAttempt> create(
4140
Objects.requireNonNull(tableSchemaObject, "tableSchemaObject cannot be null");
4241
Objects.requireNonNull(documents, "documents cannot be null");
4342

44-
// TODO, just use a for loop, instead of IntStream
45-
return IntStream.range(0, documents.size())
46-
.mapToObj(
47-
i -> {
48-
WriteableTableRow row;
49-
try {
50-
row = shredder.shred(tableSchemaObject, documents.get(i));
51-
} catch (Exception e) {
52-
// TODO: need a shredding base excpetion to catch
53-
// TODO: we need to get the row id, so we can return it in the response
54-
return (TableInsertAttempt)
55-
new TableInsertAttempt(tableSchemaObject, i, null, null).maybeAddFailure(e);
56-
}
57-
return new TableInsertAttempt(tableSchemaObject, i, row.id(), row);
58-
})
59-
.toList();
43+
final List<TableInsertAttempt> attempts = new ArrayList<>(documents.size());
44+
for (int i = 0; i < documents.size(); i++) {
45+
try {
46+
WriteableTableRow row = shredder.shred(tableSchemaObject, documents.get(i));
47+
attempts.add(new TableInsertAttempt(tableSchemaObject, i, row.id(), row));
48+
} catch (Exception e) {
49+
// TODO: need a shredding base exception to catch
50+
// TODO: we need to get the row id, so we can return it in the response
51+
attempts.add(
52+
(TableInsertAttempt)
53+
new TableInsertAttempt(tableSchemaObject, i, null, null).maybeAddFailure(e));
54+
}
55+
}
56+
return attempts;
6057
}
6158

62-
public InsertValuesCQLClause getInsertValuesCQLClause() {
59+
public TableInsertValuesCQLClause getInsertValuesCQLClause() {
6360
return new TableInsertValuesCQLClause(tableSchemaObject, row);
6461
}
6562

src/main/java/io/stargate/sgv2/jsonapi/service/resolver/matcher/FilterMatchRules.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public FilterMatchRule<T> addMatchRule(
5353
}
5454

5555
/**
56-
* Applies all the rules to to return an Operation or throw.
56+
* Applies all the rules to return an Operation or throw.
5757
*
5858
* @param commandContext
5959
* @param command

src/main/java/io/stargate/sgv2/jsonapi/service/resolver/update/TableUpdateResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private static List<ColumnAssignment> resolveSet(TableSchemaObject table, Object
105105
entry ->
106106
new ColumnAssignment(
107107
table.tableMetadata,
108-
CqlIdentifier.fromCql(entry.getKey()),
108+
CqlIdentifier.fromInternal(entry.getKey()),
109109
RowShredder.shredValue(entry.getValue())))
110110
.toList();
111111
}
@@ -132,7 +132,7 @@ private static List<ColumnAssignment> resolveUnset(
132132
.map(
133133
entry ->
134134
new ColumnAssignment(
135-
table.tableMetadata, CqlIdentifier.fromCql(entry.getKey()), null))
135+
table.tableMetadata, CqlIdentifier.fromInternal(entry.getKey()), null))
136136
.toList();
137137
}
138138
}

src/main/java/io/stargate/sgv2/jsonapi/service/shredding/tables/RowShredder.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ public WriteableTableRow shred(TableSchemaObject table, JsonNode document) {
4747
.fields()
4848
.forEachRemaining(
4949
entry -> {
50-
// using fromCQL so it is case sensitive
51-
columnValues.put(CqlIdentifier.fromCql(entry.getKey()), shredValue(entry.getValue()));
50+
// using fromInternal to preserve case-sensitivity
51+
columnValues.put(
52+
CqlIdentifier.fromInternal(entry.getKey()), shredValue(entry.getValue()));
5253
});
5354

5455
// the document should have been validated that all the fields present exist in the table
@@ -58,8 +59,9 @@ public WriteableTableRow shred(TableSchemaObject table, JsonNode document) {
5859
.map(ColumnMetadata::getName)
5960
.map(
6061
colIdentifier -> {
61-
if (columnValues.containsKey(colIdentifier)) {
62-
return columnValues.get(colIdentifier);
62+
Object value = columnValues.get(colIdentifier);
63+
if (value != null) {
64+
return value;
6365
}
6466
throw new UnvalidatedClauseException(
6567
String.format(

0 commit comments

Comments
 (0)