Skip to content

Commit 0393656

Browse files
committed
Fix
1 parent 600c743 commit 0393656

File tree

6 files changed

+65
-60
lines changed

6 files changed

+65
-60
lines changed

core/src/main/java/com/scalar/db/common/CoreError.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,16 @@ public enum CoreError implements ScalarDbError {
377377
""),
378378
MULTI_STORAGE_STORAGE_NOT_FOUND(
379379
Category.USER_ERROR, "0084", "Storage not found. Storage: %s", "", ""),
380-
JDBC_NAMESPACE_NAME_NOT_ACCEPTABLE(
381-
Category.USER_ERROR, "0085", "The namespace name is not acceptable. Namespace: %s", "", ""),
382-
JDBC_TABLE_NAME_NOT_ACCEPTABLE(
383-
Category.USER_ERROR, "0086", "The table name is not acceptable. Table: %s", "", ""),
384-
JDBC_IMPORT_NOT_SUPPORTED(
380+
JDBC_SQLITE_NAMESPACE_NAME_NOT_ACCEPTABLE(
385381
Category.USER_ERROR,
386-
"0087",
387-
"Importing tables is not allowed in the RDB engine. RDB engine: %s",
382+
"0085",
383+
"The namespace name is not acceptable in SQLite. Namespace: %s",
388384
"",
389385
""),
386+
JDBC_SQLITE_TABLE_NAME_NOT_ACCEPTABLE(
387+
Category.USER_ERROR, "0086", "The table name is not acceptable in SQLite. Table: %s", "", ""),
388+
JDBC_SQLITE_IMPORT_NOT_SUPPORTED(
389+
Category.USER_ERROR, "0087", "Importing tables is not allowed in SQLite", "", ""),
390390
JDBC_IMPORT_TABLE_WITHOUT_PRIMARY_KEY(
391391
Category.USER_ERROR, "0088", "The %s table must have a primary key", "", ""),
392392
JDBC_RDB_ENGINE_NOT_SUPPORTED(

core/src/main/java/com/scalar/db/storage/jdbc/JdbcAdmin.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,8 @@ static void execute(
147147
@Override
148148
public void createNamespace(String namespace, Map<String, String> options)
149149
throws ExecutionException {
150-
if (!rdbEngine.isValidNamespaceOrTableName(namespace)) {
151-
throw new IllegalArgumentException(
152-
CoreError.JDBC_NAMESPACE_NAME_NOT_ACCEPTABLE.buildMessage(namespace));
153-
}
150+
rdbEngine.throwIfInvalidNamespaceName(namespace);
151+
154152
try (Connection connection = dataSource.getConnection()) {
155153
execute(connection, rdbEngine.createSchemaSqls(namespace));
156154
createNamespacesTableIfNotExists(connection);
@@ -182,10 +180,8 @@ void createTableInternal(
182180
TableMetadata metadata,
183181
boolean ifNotExists)
184182
throws SQLException {
185-
if (!rdbEngine.isValidNamespaceOrTableName(table)) {
186-
throw new IllegalArgumentException(
187-
CoreError.JDBC_TABLE_NAME_NOT_ACCEPTABLE.buildMessage(table));
188-
}
183+
rdbEngine.throwIfInvalidTableName(table);
184+
189185
String createTableStatement = "CREATE TABLE " + encloseFullTableName(schema, table) + "(";
190186
// Order the columns for their creation by (partition keys >> clustering keys >> other columns)
191187
LinkedHashSet<String> sortedColumnNames =
@@ -572,11 +568,6 @@ TableMetadata getImportTableMetadata(
572568
TableMetadata.Builder builder = TableMetadata.newBuilder();
573569
boolean primaryKeyExists = false;
574570

575-
if (!rdbEngine.isImportable()) {
576-
throw new UnsupportedOperationException(
577-
CoreError.JDBC_IMPORT_NOT_SUPPORTED.buildMessage(rdbEngine.getClass().getName()));
578-
}
579-
580571
try (Connection connection = dataSource.getConnection()) {
581572
rdbEngine.setConnectionToReadOnly(connection, true);
582573

@@ -634,6 +625,8 @@ public void importTable(
634625
Map<String, String> options,
635626
Map<String, DataType> overrideColumnsType)
636627
throws ExecutionException {
628+
rdbEngine.throwIfImportNotSupported();
629+
637630
try (Connection connection = dataSource.getConnection()) {
638631
TableMetadata tableMetadata = getImportTableMetadata(namespace, table, overrideColumnsType);
639632
createNamespacesTableIfNotExists(connection);
@@ -852,10 +845,8 @@ private boolean tableExistsInternal(Connection connection, String namespace, Str
852845
@Override
853846
public void repairNamespace(String namespace, Map<String, String> options)
854847
throws ExecutionException {
855-
if (!rdbEngine.isValidNamespaceOrTableName(namespace)) {
856-
throw new IllegalArgumentException(
857-
CoreError.JDBC_NAMESPACE_NAME_NOT_ACCEPTABLE.buildMessage(namespace));
858-
}
848+
rdbEngine.throwIfInvalidNamespaceName(namespace);
849+
859850
try (Connection connection = dataSource.getConnection()) {
860851
createSchemaIfNotExists(connection, namespace);
861852
createNamespacesTableIfNotExists(connection);
@@ -869,6 +860,8 @@ public void repairNamespace(String namespace, Map<String, String> options)
869860
public void repairTable(
870861
String namespace, String table, TableMetadata metadata, Map<String, String> options)
871862
throws ExecutionException {
863+
rdbEngine.throwIfInvalidNamespaceName(table);
864+
872865
try (Connection connection = dataSource.getConnection()) {
873866
createTableInternal(connection, namespace, table, metadata, true);
874867
addTableMetadata(connection, namespace, table, metadata, true, true);

core/src/main/java/com/scalar/db/storage/jdbc/RdbEngineSqlite.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,19 @@ public DataType getDataTypeForScalarDbInternal(
167167
}
168168

169169
@Override
170-
public boolean isValidNamespaceOrTableName(String namespaceOrTableName) {
171-
return !namespaceOrTableName.contains(NAMESPACE_SEPARATOR);
170+
public void throwIfInvalidNamespaceName(String namespaceName) {
171+
if (namespaceName.contains(NAMESPACE_SEPARATOR)) {
172+
throw new IllegalArgumentException(
173+
CoreError.JDBC_SQLITE_NAMESPACE_NAME_NOT_ACCEPTABLE.buildMessage(namespaceName));
174+
}
175+
}
176+
177+
@Override
178+
public void throwIfInvalidTableName(String tableName) {
179+
if (tableName.contains(NAMESPACE_SEPARATOR)) {
180+
throw new IllegalArgumentException(
181+
CoreError.JDBC_SQLITE_TABLE_NAME_NOT_ACCEPTABLE.buildMessage(tableName));
182+
}
172183
}
173184

174185
@Override
@@ -310,8 +321,9 @@ public Driver getDriver() {
310321
}
311322

312323
@Override
313-
public boolean isImportable() {
314-
return false;
324+
public void throwIfImportNotSupported() {
325+
throw new UnsupportedOperationException(
326+
CoreError.JDBC_SQLITE_IMPORT_NOT_SUPPORTED.buildMessage());
315327
}
316328

317329
@Override

core/src/main/java/com/scalar/db/storage/jdbc/RdbEngineStrategy.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ DataType getDataTypeForScalarDb(
7373

7474
String[] createSchemaIfNotExistsSqls(String fullSchema);
7575

76-
default boolean isValidNamespaceOrTableName(String tableName) {
77-
return true;
78-
}
76+
default void throwIfInvalidNamespaceName(String namespaceName) {}
77+
78+
default void throwIfInvalidTableName(String tableName) {}
7979

8080
String createTableInternalPrimaryKeyClause(
8181
boolean hasDescClusteringOrder, TableMetadata metadata);
@@ -165,9 +165,7 @@ default String encloseFullTableName(String schema, String table) {
165165

166166
Driver getDriver();
167167

168-
default boolean isImportable() {
169-
return true;
170-
}
168+
default void throwIfImportNotSupported() {}
171169

172170
/**
173171
* Return properly-preprocessed like pattern for each underlying database.

core/src/test/java/com/scalar/db/storage/jdbc/JdbcAdminTest.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,20 +3644,16 @@ private void getNamespaceNames_forX_ShouldReturnNamespaceNames(
36443644
}
36453645

36463646
@ParameterizedTest
3647-
@EnumSource(RdbEngine.class)
3648-
public void getImportTableMetadata_ForX_ShouldWorkProperly(RdbEngine rdbEngine)
3647+
@EnumSource(
3648+
value = RdbEngine.class,
3649+
mode = Mode.EXCLUDE,
3650+
names = {
3651+
"SQLITE",
3652+
})
3653+
public void getImportTableMetadata_ForXBesidesSqlite_ShouldWorkProperly(RdbEngine rdbEngine)
36493654
throws SQLException, ExecutionException {
3650-
if (rdbEngine.equals(RdbEngine.SQLITE)) {
3651-
getImportTableMetadata_ForSQLite_ShouldThrowUnsupportedOperationException(rdbEngine);
3652-
} else {
3653-
getImportTableMetadata_ForOtherThanSQLite_ShouldWorkProperly(
3654-
rdbEngine, prepareSqlForTableCheck(rdbEngine, NAMESPACE, TABLE));
3655-
}
3656-
}
3655+
String expectedCheckTableExistStatement = prepareSqlForTableCheck(rdbEngine, NAMESPACE, TABLE);
36573656

3658-
private void getImportTableMetadata_ForOtherThanSQLite_ShouldWorkProperly(
3659-
RdbEngine rdbEngine, String expectedCheckTableExistStatement)
3660-
throws SQLException, ExecutionException {
36613657
// Arrange
36623658
Statement checkTableExistStatement = mock(Statement.class);
36633659
DatabaseMetaData metadata = mock(DatabaseMetaData.class);
@@ -3749,16 +3745,6 @@ public Boolean answer(InvocationOnMock invocation) {
37493745
eq(DataType.FLOAT));
37503746
}
37513747

3752-
private void getImportTableMetadata_ForSQLite_ShouldThrowUnsupportedOperationException(
3753-
RdbEngine rdbEngine) {
3754-
// Arrange
3755-
JdbcAdmin admin = createJdbcAdminFor(rdbEngine);
3756-
3757-
// Act Assert
3758-
assertThatThrownBy(() -> admin.getImportTableMetadata(NAMESPACE, TABLE, Collections.emptyMap()))
3759-
.isInstanceOf(UnsupportedOperationException.class);
3760-
}
3761-
37623748
@Test
37633749
public void getImportTableMetadata_PrimaryKeyNotExistsForX_ShouldThrowExecutionException()
37643750
throws SQLException {

core/src/test/java/com/scalar/db/storage/jdbc/RdbEngineSqliteTest.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.scalar.db.storage.jdbc;
22

3+
import static org.assertj.core.api.Assertions.assertThatCode;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
35
import static org.assertj.core.api.AssertionsForClassTypes.catchThrowable;
46
import static org.junit.jupiter.api.Assertions.assertFalse;
57
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -121,12 +123,26 @@ void isConflict_False() {
121123
}
122124

123125
@Test
124-
void isValidNamespaceOrTableName_True() {
125-
assertTrue(rdbEngine.isValidNamespaceOrTableName("a_b"));
126+
void throwIfInvalidNamespaceName_ShouldNotThrowAnyException() {
127+
assertThatCode(() -> rdbEngine.throwIfInvalidNamespaceName("a-b")).doesNotThrowAnyException();
126128
}
127129

128130
@Test
129-
void isValidNamespaceOrTableName_False_WhenContainsNamespaceSeparator() {
130-
assertFalse(rdbEngine.isValidNamespaceOrTableName("a$b"));
131+
void
132+
throwIfInvalidNamespaceName_WhenContainsNamespaceSeparator_ShouldThrowIllegalArgumentException() {
133+
assertThatThrownBy(() -> rdbEngine.throwIfInvalidNamespaceName("a$b"))
134+
.isInstanceOf(IllegalArgumentException.class);
135+
}
136+
137+
@Test
138+
void throwIfInvalidTableName_ShouldNotThrowAnyException() {
139+
assertThatCode(() -> rdbEngine.throwIfInvalidTableName("a-b")).doesNotThrowAnyException();
140+
}
141+
142+
@Test
143+
void
144+
throwIfInvalidTableName_WhenContainsNamespaceSeparator_ShouldThrowIllegalArgumentException() {
145+
assertThatThrownBy(() -> rdbEngine.throwIfInvalidTableName("a$b"))
146+
.isInstanceOf(IllegalArgumentException.class);
131147
}
132148
}

0 commit comments

Comments
 (0)