Skip to content

Commit

Permalink
fix: support abnormal table name
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Aug 28, 2024
1 parent 5e359cc commit 11457f0
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 13 deletions.
8 changes: 8 additions & 0 deletions src/main/java/typewriter/h2/H2.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ public class H2 extends Dialect {
private H2() {
}

/**
* {@inheritDoc}
*/
@Override
public String quote() {
return "\"";
}

/**
* {@inheritDoc}
*/
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/typewriter/rdb/RDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public RDB(Class<M> type, String name, Dialect dialect, String url) {
Map<String, String> rows = new HashMap();
try (Connection connection = provider.get()) {
DatabaseMetaData meta = connection.getMetaData();
try (ResultSet columns = meta.getColumns(null, null, tableName.replaceAll(dialect.quote(), ""), null)) {
try (ResultSet columns = meta.getColumns(null, null, this.name, null)) {
while (columns.next()) {
rows.put(columns.getString("COLUMN_NAME"), columns.getString("TYPE_NAME"));
}
Expand Down Expand Up @@ -130,8 +130,8 @@ private RDB(Model<M> model, String name, Dialect dialect, WiseSupplier<Connectio
}

this.model = model;
this.name = name;
this.tableName = dialect.quote() + name.replaceAll("[$\\.\s]+", "_") + dialect.quote();
this.name = name.replaceAll("['\"\\s]+", "_");
this.tableName = dialect.quote() + this.name + dialect.quote();
this.dialect = dialect;
this.provider = provider;
}
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/typewriter/api/AbnormalTableNameTestSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2024 The TYPEWRITER Development Team
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*/
package typewriter.api;

import org.junit.jupiter.api.Test;

import typewriter.api.model.DerivableModel;

public interface AbnormalTableNameTestSet extends Testable {

@Test
default void dot() {
createEmptyDB(Abnormal.class, "dot...");
}

@Test
default void doller() {
createEmptyDB(Abnormal.class, "$doller$");
}

@Test
default void whiteSpace() {
createEmptyDB(Abnormal.class, "white space");
}

@Test
default void reservedWord() {
createEmptyDB(Abnormal.class, "SELECT");
}

@Test
default void doubleQuote() {
createEmptyDB(Abnormal.class, "\"quoted\"");
}

@Test
default void singleQuote() {
createEmptyDB(Abnormal.class, "someone's");
}

class Abnormal extends DerivableModel {
public int item;
}
}
13 changes: 12 additions & 1 deletion src/test/java/typewriter/api/Testable.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ public interface Testable {
* @param type
* @return
*/
<M extends IdentifiableModel, Q extends QueryExecutor<M, Signal<M>, ?, Q>> Q createEmptyDB(Class<M> type);
default <M extends IdentifiableModel, Q extends QueryExecutor<M, Signal<M>, ?, Q>> Q createEmptyDB(Class<M> type) {
return createEmptyDB(type, type.getName());
}

/**
* Create the {@link QueryExecutor} for test.
*
* @param <M>
* @param type
* @return
*/
<M extends IdentifiableModel, Q extends QueryExecutor<M, Signal<M>, ?, Q>> Q createEmptyDB(Class<M> type, String name);

/**
* Create the {@link QueryExecutor} for test with initial models.
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/typewriter/duck/AbnormalTableNameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (C) 2024 The TYPEWRITER Development Team
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*/
package typewriter.duck;

import typewriter.api.AbnormalTableNameTestSet;

public class AbnormalTableNameTest extends DuckTestBase implements AbnormalTableNameTestSet {
}
4 changes: 2 additions & 2 deletions src/test/java/typewriter/duck/DuckTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void release() {
* {@inheritDoc}
*/
@Override
public <M extends IdentifiableModel, Q extends QueryExecutor<M, Signal<M>, ?, Q>> Q createEmptyDB(Class<M> type) {
return (Q) new RDB(type, type.getName(), RDB.DuckDB, db);
public <M extends IdentifiableModel, Q extends QueryExecutor<M, Signal<M>, ?, Q>> Q createEmptyDB(Class<M> type, String name) {
return (Q) new RDB(type, name, RDB.DuckDB, db);
}
}
15 changes: 15 additions & 0 deletions src/test/java/typewriter/h2/AbnormalTableNameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (C) 2024 The TYPEWRITER Development Team
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*/
package typewriter.h2;

import typewriter.api.AbnormalTableNameTestSet;

public class AbnormalTableNameTest extends H2TestBase implements AbnormalTableNameTestSet {
}
4 changes: 2 additions & 2 deletions src/test/java/typewriter/h2/H2TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void release() {
* {@inheritDoc}
*/
@Override
public <M extends IdentifiableModel, Q extends QueryExecutor<M, Signal<M>, ?, Q>> Q createEmptyDB(Class<M> type) {
return (Q) new RDB(type, type.getName(), RDB.H2, db);
public <M extends IdentifiableModel, Q extends QueryExecutor<M, Signal<M>, ?, Q>> Q createEmptyDB(Class<M> type, String name) {
return (Q) new RDB(type, name, RDB.H2, db);
}
}
15 changes: 15 additions & 0 deletions src/test/java/typewriter/maria/AbnormalTableNameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (C) 2024 The TYPEWRITER Development Team
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*/
package typewriter.maria;

import typewriter.api.AbnormalTableNameTestSet;

public class AbnormalTableNameTest extends MariaDBTestBase implements AbnormalTableNameTestSet {
}
4 changes: 2 additions & 2 deletions src/test/java/typewriter/maria/MariaDBTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void release() throws ManagedProcessException {
* {@inheritDoc}
*/
@Override
public <M extends IdentifiableModel, Q extends QueryExecutor<M, Signal<M>, ?, Q>> Q createEmptyDB(Class<M> type) {
return (Q) new RDB(type, type.getName(), RDB.MariaDB, url);
public <M extends IdentifiableModel, Q extends QueryExecutor<M, Signal<M>, ?, Q>> Q createEmptyDB(Class<M> type, String name) {
return (Q) new RDB(type, name, RDB.MariaDB, url);
}
}
15 changes: 15 additions & 0 deletions src/test/java/typewriter/mongo/AbnormalTableNameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (C) 2024 The TYPEWRITER Development Team
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*/
package typewriter.mongo;

import typewriter.api.AbnormalTableNameTestSet;

public class AbnormalTableNameTest extends MongoTestBase implements AbnormalTableNameTestSet {
}
2 changes: 1 addition & 1 deletion src/test/java/typewriter/mongo/MongoTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void teardown() {
* {@inheritDoc}
*/
@Override
public <M extends IdentifiableModel, Q extends QueryExecutor<M, Signal<M>, ?, Q>> Q createEmptyDB(Class<M> type) {
public <M extends IdentifiableModel, Q extends QueryExecutor<M, Signal<M>, ?, Q>> Q createEmptyDB(Class<M> type, String name) {
return (Q) new Mongo(type, client);
}

Expand Down
15 changes: 15 additions & 0 deletions src/test/java/typewriter/sqlite/AbnormalTableNameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (C) 2024 The TYPEWRITER Development Team
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*/
package typewriter.sqlite;

import typewriter.api.AbnormalTableNameTestSet;

public class AbnormalTableNameTest extends SQLiteTestBase implements AbnormalTableNameTestSet {
}
4 changes: 2 additions & 2 deletions src/test/java/typewriter/sqlite/SQLiteTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void release() {
* {@inheritDoc}
*/
@Override
public <M extends IdentifiableModel, Q extends QueryExecutor<M, Signal<M>, ?, Q>> Q createEmptyDB(Class<M> type) {
return (Q) new RDB(type, type.getName(), RDB.SQLite, db);
public <M extends IdentifiableModel, Q extends QueryExecutor<M, Signal<M>, ?, Q>> Q createEmptyDB(Class<M> type, String name) {
return (Q) new RDB(type, name, RDB.SQLite, db);
}
}

0 comments on commit 11457f0

Please sign in to comment.