Skip to content

Commit 25b23f7

Browse files
committed
java
1 parent 372def8 commit 25b23f7

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

compiler/extensions/java/freemarker/SqlDatabase.java.ftl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,17 @@ public final class ${name} implements zserio.runtime.SqlDatabase<#if !withWriter
335335
private void attachDatabase(java.lang.String dbFileName, java.lang.String attachedDbName)
336336
throws java.sql.SQLException
337337
{
338-
final java.lang.StringBuilder sqlQuery = new java.lang.StringBuilder("ATTACH DATABASE '");
339-
sqlQuery.append(new java.io.File(dbFileName).toString());
340-
sqlQuery.append("' AS ");
341-
sqlQuery.append(attachedDbName);
342-
executeUpdate(sqlQuery.toString());
343-
338+
for (char chr : attachedDbName.toCharArray())
339+
{
340+
if (chr != '_' && !Character.isLetterOrDigit(chr))
341+
throw new zserio.runtime.ZserioError("${name}.attachDatabase: attached DB name contains invalid characters");
342+
}
343+
final String sqlQuery = "ATTACH DATABASE ? AS " + attachedDbName;
344+
try (java.sql.PreparedStatement stmt = connection.prepareStatement(sqlQuery))
345+
{
346+
stmt.setString(1, new java.io.File(dbFileName).toString());
347+
stmt.executeUpdate();
348+
}
344349
attachedDbList.add(attachedDbName);
345350
}
346351

test/extensions/language/sql_databases/java/sql_databases/db_with_relocation/DbWithRelocationTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.junit.jupiter.api.Test;
2222

2323
import zserio.runtime.SqlDatabase;
24+
import zserio.runtime.ZserioError;
2425
import zserio.runtime.validation.ValidationReport;
2526

2627
import test_utils.FileUtil;
@@ -188,6 +189,18 @@ public void checkAttachedDatabases() throws SQLException
188189
assertFalse(attachedDatabaseNames.contains("main"));
189190
}
190191

192+
@Test
193+
public void checkInvalidDbName()
194+
{
195+
final String dbName = "db_with_relocation_test_invalid_db_name.sqlite";
196+
final Map<String, String> reloc1 = new HashMap<String, String>();
197+
reloc1.put("inval id", "db_with_relocation_test_invalid1.sqlite");
198+
final Map<String, String> reloc2 = new HashMap<String, String>();
199+
reloc2.put("in?valid", "db_with_relocation_test_invalid1.sqlite");
200+
assertThrows(ZserioError.class, () -> { new EuropeDb(dbName, reloc1); });
201+
assertThrows(ZserioError.class, () -> { new EuropeDb(dbName, reloc2); });
202+
}
203+
191204
private static boolean isRelocatedTableInDb(String relocatedTableName, SqlDatabase db) throws SQLException
192205
{
193206
// check if database does contain relocated table

0 commit comments

Comments
 (0)