Skip to content

Commit 9833d4a

Browse files
o. Replace string literals with constants
o. Add null check for json schema
1 parent 9f3a6ed commit 9833d4a

File tree

1 file changed

+80
-61
lines changed

1 file changed

+80
-61
lines changed

src/main/java/com/oracle/nosql/spring/data/core/NosqlTemplateBase.java

Lines changed: 80 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ protected boolean doCreateTable(
187187
}
188188

189189
protected void doCheckExistingTable(NosqlEntityInformation<?, ?> entityInformation) {
190+
final String columnsField = "fields";
191+
final String columnNameField = "name";
192+
final String columnTypeField = "type";
193+
final String shardField = "shardKey";
194+
final String primaryField = "primaryKey";
195+
final String ttlField = "ttl";
196+
final String identityField = "identity";
197+
190198
TableResult tableResult = doGetTable(entityInformation);
191199
/*If table already exist in the database compare and throw error if
192200
mismatch*/
@@ -201,13 +209,6 @@ protected void doCheckExistingTable(NosqlEntityInformation<?, ?> entityInformati
201209
Map<String, FieldValue.Type> nonShardKeys = entityInformation.
202210
getNonShardKeys();
203211

204-
ArrayValue columns = jsonSchema.get("fields").asArray();
205-
//check number of columns are same
206-
if (columns.size() != shardKeys.size() + nonShardKeys.size() + 1) {
207-
throw new IllegalArgumentException(
208-
"Number of columns are not same");
209-
}
210-
211212
//lower case maps
212213
Map<String, FieldValue.Type> caseShardKey = new LinkedHashMap<>();
213214
shardKeys.forEach((k, v) -> caseShardKey.put(k.toLowerCase(), v));
@@ -216,74 +217,92 @@ protected void doCheckExistingTable(NosqlEntityInformation<?, ?> entityInformati
216217
new LinkedHashMap<>();
217218
nonShardKeys.forEach((k, v) -> caseNonShardKey.put(k.toLowerCase(), v));
218219

219-
//check column names and types are same
220-
for (int i = 0; i < columns.size(); i++) {
221-
MapValue column = columns.get(i).asMap();
222-
String columnName = column.getString("name").toLowerCase();
223-
String columnType = column.getString("type").toLowerCase();
224-
String msg;
225-
if (i < caseShardKey.size()) {
226-
if (!caseShardKey.containsKey(columnName)) {
227-
msg = String.format("column '%s' mismatch", columnName);
228-
throw new IllegalArgumentException(msg);
229-
}
230-
if (!columnType.equalsIgnoreCase(caseShardKey.get(columnName).name())) {
231-
msg = String.format("type mismatch for " +
232-
"column '%s'", columnName);
233-
throw new IllegalArgumentException(msg);
234-
}
235-
} else if (i < caseShardKey.size() + caseNonShardKey.size()) {
236-
if (!caseNonShardKey.containsKey(columnName)) {
237-
msg = String.format("column '%s' mismatch", columnName);
238-
throw new IllegalArgumentException(msg);
239-
}
240-
if (!columnType.equalsIgnoreCase(caseNonShardKey.get(columnName).name())) {
241-
msg = String.format("type mismatch for " +
242-
"column '%s'", columnName);
243-
throw new IllegalArgumentException(msg);
244-
}
245-
} else {
246-
if (!columnName.equalsIgnoreCase(JSON_COLUMN)) {
247-
msg = String.format("%s column not present",
248-
JSON_COLUMN);
249-
throw new IllegalArgumentException(msg);
250-
}
251-
if (!columnType.equalsIgnoreCase("JSON")) {
252-
msg = String.format("%s column type is not JSON",
253-
JSON_COLUMN);
254-
throw new IllegalArgumentException(msg);
220+
221+
ArrayValue columns = jsonSchema.get(columnsField).asArray();
222+
if (columns != null) {
223+
//check number of columns are same
224+
if (columns.size() != shardKeys.size() + nonShardKeys.size() + 1) {
225+
throw new IllegalArgumentException(
226+
"Number of columns are not same");
227+
}
228+
229+
//check column names and types are same
230+
for (int i = 0; i < columns.size(); i++) {
231+
MapValue column = columns.get(i).asMap();
232+
String columnName =
233+
column.getString(columnNameField).toLowerCase();
234+
String columnType =
235+
column.getString(columnTypeField).toLowerCase();
236+
String msg;
237+
if (i < caseShardKey.size()) {
238+
if (!caseShardKey.containsKey(columnName)) {
239+
msg = String.format("column '%s' mismatch",
240+
columnName);
241+
throw new IllegalArgumentException(msg);
242+
}
243+
if (!columnType.equalsIgnoreCase(caseShardKey.get(columnName).name())) {
244+
msg = String.format("type mismatch for " +
245+
"column '%s'", columnName);
246+
throw new IllegalArgumentException(msg);
247+
}
248+
} else if (i < caseShardKey.size() + caseNonShardKey.size()) {
249+
if (!caseNonShardKey.containsKey(columnName)) {
250+
msg = String.format("column '%s' mismatch",
251+
columnName);
252+
throw new IllegalArgumentException(msg);
253+
}
254+
if (!columnType.equalsIgnoreCase(caseNonShardKey.get(columnName).name())) {
255+
msg = String.format("type mismatch for " +
256+
"column '%s'", columnName);
257+
throw new IllegalArgumentException(msg);
258+
}
259+
} else {
260+
if (!columnName.equalsIgnoreCase(JSON_COLUMN)) {
261+
msg = String.format("%s column not present",
262+
JSON_COLUMN);
263+
throw new IllegalArgumentException(msg);
264+
}
265+
if (!columnType.equalsIgnoreCase("JSON")) {
266+
msg = String.format("%s column type is not JSON",
267+
JSON_COLUMN);
268+
throw new IllegalArgumentException(msg);
269+
}
255270
}
256271
}
257272
}
258273

259274
//check order of the shard keys are same
260-
ArrayValue shards = jsonSchema.get("shardKey").asArray();
261-
if (shards.size() != shardKeys.size()) {
262-
throw new IllegalArgumentException("number of shard keys do " +
263-
"not match");
264-
265-
}
266275
int i = 0;
267-
for (String key : shardKeys.keySet()) {
268-
if (!key.equalsIgnoreCase(shards.get(i).getString())) {
269-
throw new IllegalArgumentException("Order of shard keys " +
276+
ArrayValue shards = jsonSchema.get(shardField).asArray();
277+
if (shards != null) {
278+
if (shards.size() != shardKeys.size()) {
279+
throw new IllegalArgumentException("number of shard keys " +
270280
"do not match");
281+
282+
}
283+
for (String key : shardKeys.keySet()) {
284+
if (!key.equalsIgnoreCase(shards.get(i).getString())) {
285+
throw new IllegalArgumentException("Order of shard " +
286+
"keys do not match");
287+
}
288+
i++;
271289
}
272-
i++;
273290
}
274291

275292
//check order of non shard keys are same
276-
ArrayValue primaryKeys = jsonSchema.get("primaryKey").asArray();
277-
for (String key : nonShardKeys.keySet()) {
278-
if (!key.equalsIgnoreCase(primaryKeys.get(i).getString())) {
279-
throw new IllegalArgumentException("Order of non-shard " +
280-
"keys do not match");
293+
ArrayValue primaryKeys = jsonSchema.get(primaryField).asArray();
294+
if (primaryKeys != null) {
295+
for (String key : nonShardKeys.keySet()) {
296+
if (!key.equalsIgnoreCase(primaryKeys.get(i).getString())) {
297+
throw new IllegalArgumentException("Order of " +
298+
"non-shard keys do not match");
299+
}
300+
i++;
281301
}
282-
i++;
283302
}
284303

285304
//check identity same
286-
FieldValue identity = jsonSchema.get("identity");
305+
FieldValue identity = jsonSchema.get(identityField);
287306
if (identity != null && !entityInformation.isAutoGeneratedId()) {
288307
throw new IllegalArgumentException("Identity information " +
289308
"mismatch");
@@ -295,7 +314,7 @@ protected void doCheckExistingTable(NosqlEntityInformation<?, ?> entityInformati
295314
}
296315

297316
//TTL warning
298-
FieldValue ttlValue = jsonSchema.get("ttl");
317+
FieldValue ttlValue = jsonSchema.get(ttlField);
299318
TimeToLive ttl = entityInformation.getTtl();
300319
//TTL is present in database but not in the entity
301320
if (ttlValue != null && ttl != null &&

0 commit comments

Comments
 (0)