File tree Expand file tree Collapse file tree
compiler/extensions/java/freemarker
test/extensions/language/sql_databases/java/sql_databases/db_with_relocation Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 2121import org .junit .jupiter .api .Test ;
2222
2323import zserio .runtime .SqlDatabase ;
24+ import zserio .runtime .ZserioError ;
2425import zserio .runtime .validation .ValidationReport ;
2526
2627import 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
You can’t perform that action at this time.
0 commit comments