diff --git a/src/main/java/com/conveyal/gtfs/PatternBuilder.java b/src/main/java/com/conveyal/gtfs/PatternBuilder.java index f8dcf0dcf..9b3c879ef 100644 --- a/src/main/java/com/conveyal/gtfs/PatternBuilder.java +++ b/src/main/java/com/conveyal/gtfs/PatternBuilder.java @@ -41,9 +41,9 @@ public PatternBuilder(Feed feed) throws SQLException { } public void create(Map patterns, Set patternIdsLoadedFromFile) { - String patternsTableName = feed.getTableName("patterns"); - String tripsTableName = feed.getTableName("trips"); - String patternStopsTableName = feed.getTableName("pattern_stops"); + String patternsTableName = feed.getTableNameWithSchemaPrefix("patterns"); + String tripsTableName = feed.getTableNameWithSchemaPrefix("trips"); + String patternStopsTableName = feed.getTableNameWithSchemaPrefix("pattern_stops"); Table patternsTable = new Table(patternsTableName, Pattern.class, Requirement.EDITOR, Table.PATTERNS.fields); Table patternStopsTable = new Table(patternStopsTableName, PatternStop.class, Requirement.EDITOR, Table.PATTERN_STOP.fields); diff --git a/src/main/java/com/conveyal/gtfs/loader/Feed.java b/src/main/java/com/conveyal/gtfs/loader/Feed.java index a4844129f..1430b5751 100644 --- a/src/main/java/com/conveyal/gtfs/loader/Feed.java +++ b/src/main/java/com/conveyal/gtfs/loader/Feed.java @@ -28,7 +28,7 @@ public class Feed { // The unique database schema name for this particular feed, including the separator character (dot). // This may be the empty string if the feed is stored in the root ("public") schema. - private final String tablePrefix; + private final String databaseSchemaPrefix; public final TableReader agencies; public final TableReader calendars; @@ -44,23 +44,23 @@ public class Feed { /** * Create a feed that reads tables over a JDBC connection. The connection should already be set to the right * schema within the database. - * @param tablePrefix the unique prefix for the table (may be null for no prefix) + * @param databaseSchemaPrefix the unique prefix for the table (may be null for no prefix) */ - public Feed (DataSource dataSource, String tablePrefix) { + public Feed (DataSource dataSource, String databaseSchemaPrefix) { this.dataSource = dataSource; // Ensure separator dot is present - if (tablePrefix != null && !tablePrefix.endsWith(".")) tablePrefix += "."; - this.tablePrefix = tablePrefix == null ? "" : tablePrefix; - agencies = new JDBCTableReader(Table.AGENCY, dataSource, tablePrefix, EntityPopulator.AGENCY); - fareAttributes = new JDBCTableReader(Table.FARE_ATTRIBUTES, dataSource, tablePrefix, EntityPopulator.FARE_ATTRIBUTE); - frequencies = new JDBCTableReader(Table.FREQUENCIES, dataSource, tablePrefix, EntityPopulator.FREQUENCY); - calendars = new JDBCTableReader(Table.CALENDAR, dataSource, tablePrefix, EntityPopulator.CALENDAR); - calendarDates = new JDBCTableReader(Table.CALENDAR_DATES, dataSource, tablePrefix, EntityPopulator.CALENDAR_DATE); - routes = new JDBCTableReader(Table.ROUTES, dataSource, tablePrefix, EntityPopulator.ROUTE); - stops = new JDBCTableReader(Table.STOPS, dataSource, tablePrefix, EntityPopulator.STOP); - trips = new JDBCTableReader(Table.TRIPS, dataSource, tablePrefix, EntityPopulator.TRIP); - stopTimes = new JDBCTableReader(Table.STOP_TIMES, dataSource, tablePrefix, EntityPopulator.STOP_TIME); - patterns = new JDBCTableReader(Table.PATTERNS, dataSource, tablePrefix, EntityPopulator.PATTERN); + if (databaseSchemaPrefix != null && !databaseSchemaPrefix.endsWith(".")) databaseSchemaPrefix += "."; + this.databaseSchemaPrefix = databaseSchemaPrefix == null ? "" : databaseSchemaPrefix; + agencies = new JDBCTableReader(Table.AGENCY, dataSource, databaseSchemaPrefix, EntityPopulator.AGENCY); + fareAttributes = new JDBCTableReader(Table.FARE_ATTRIBUTES, dataSource, databaseSchemaPrefix, EntityPopulator.FARE_ATTRIBUTE); + frequencies = new JDBCTableReader(Table.FREQUENCIES, dataSource, databaseSchemaPrefix, EntityPopulator.FREQUENCY); + calendars = new JDBCTableReader(Table.CALENDAR, dataSource, databaseSchemaPrefix, EntityPopulator.CALENDAR); + calendarDates = new JDBCTableReader(Table.CALENDAR_DATES, dataSource, databaseSchemaPrefix, EntityPopulator.CALENDAR_DATE); + routes = new JDBCTableReader(Table.ROUTES, dataSource, databaseSchemaPrefix, EntityPopulator.ROUTE); + stops = new JDBCTableReader(Table.STOPS, dataSource, databaseSchemaPrefix, EntityPopulator.STOP); + trips = new JDBCTableReader(Table.TRIPS, dataSource, databaseSchemaPrefix, EntityPopulator.TRIP); + stopTimes = new JDBCTableReader(Table.STOP_TIMES, dataSource, databaseSchemaPrefix, EntityPopulator.STOP_TIME); + patterns = new JDBCTableReader(Table.PATTERNS, dataSource, databaseSchemaPrefix, EntityPopulator.PATTERN); } /** @@ -80,7 +80,7 @@ public ValidationResult validate (FeedValidatorCreator... additionalValidators) // Reconnect to the existing error tables. SQLErrorStorage errorStorage; try { - errorStorage = new SQLErrorStorage(dataSource.getConnection(), tablePrefix, false); + errorStorage = new SQLErrorStorage(dataSource.getConnection(), databaseSchemaPrefix, false); } catch (SQLException | InvalidNamespaceException ex) { throw new StorageException(ex); } @@ -157,7 +157,7 @@ public Connection getConnection() throws SQLException { /** * @return the table name prefixed with this feed's database schema. */ - public String getTableName(String tableName) { - return tablePrefix + tableName; + public String getTableNameWithSchemaPrefix(String tableName) { + return databaseSchemaPrefix + tableName; } } diff --git a/src/main/java/com/conveyal/gtfs/loader/JdbcGtfsExporter.java b/src/main/java/com/conveyal/gtfs/loader/JdbcGtfsExporter.java index ad1c72ddb..9e84f7301 100644 --- a/src/main/java/com/conveyal/gtfs/loader/JdbcGtfsExporter.java +++ b/src/main/java/com/conveyal/gtfs/loader/JdbcGtfsExporter.java @@ -408,7 +408,7 @@ private TableLoadResult export (Table table, String filterSql) { } // Create entry for table - String textFileName = Table.getTableFileName(table.name); + String textFileName = Table.getTableFileNameWithExtension(table.name); ZipEntry zipEntry = new ZipEntry(textFileName); zipOutputStream.putNextEntry(zipEntry); diff --git a/src/main/java/com/conveyal/gtfs/loader/JdbcGtfsLoader.java b/src/main/java/com/conveyal/gtfs/loader/JdbcGtfsLoader.java index a666b3c3f..37f55ba96 100644 --- a/src/main/java/com/conveyal/gtfs/loader/JdbcGtfsLoader.java +++ b/src/main/java/com/conveyal/gtfs/loader/JdbcGtfsLoader.java @@ -323,7 +323,7 @@ private int getTableSize(Table table) { private int loadInternal(Table table) throws Exception { CsvReader csvReader = table.getCsvReader(zip, errorStorage); if (csvReader == null) { - LOG.info("File {} not found in gtfs zip file.", Table.getTableFileName(table.name)); + LOG.info("File {} not found in gtfs zip file.", Table.getTableFileNameWithExtension(table.name)); // This GTFS table could not be opened in the zip, even in a subdirectory. if (table.isRequired()) errorStorage.storeError(NewGTFSError.forTable(table, MISSING_TABLE)); return 0; diff --git a/src/main/java/com/conveyal/gtfs/loader/Table.java b/src/main/java/com/conveyal/gtfs/loader/Table.java index 587d3aeb6..a052db565 100644 --- a/src/main/java/com/conveyal/gtfs/loader/Table.java +++ b/src/main/java/com/conveyal/gtfs/loader/Table.java @@ -622,13 +622,21 @@ public String generateInsertSql (String namespace, boolean setDefaultId) { ); } + public static String getTableFileNameWithExtension(String tableName) { + return getTableFileName(tableName, ".txt"); + } + + public static String getTableFileName(String tableName) { + return getTableFileName(tableName, ""); + } + /** * Proprietary table file names are prefix with "datatools_" to distinguish them from GTFS spec files. */ - public static String getTableFileName(String tableName) { + private static String getTableFileName(String tableName, String fileExtension) { return (tableName.equals("patterns")) - ? String.format("%s%s.txt", PROPRIETARY_FILE_PREFIX, tableName) - : tableName + ".txt"; + ? String.format("%s%s%s", PROPRIETARY_FILE_PREFIX, tableName, fileExtension) + : String.format("%s%s", tableName, fileExtension); } /** @@ -638,7 +646,7 @@ public static String getTableFileName(String tableName) { * It then creates a CSV reader for that table if it's found. */ public CsvReader getCsvReader(ZipFile zipFile, SQLErrorStorage sqlErrorStorage) { - final String tableFileName = getTableFileName(this.name); + final String tableFileName = getTableFileNameWithExtension(this.name); ZipEntry entry = zipFile.getEntry(tableFileName); if (entry == null) { // Table was not found, check if it is in a subdirectory. diff --git a/src/main/java/com/conveyal/gtfs/model/Pattern.java b/src/main/java/com/conveyal/gtfs/model/Pattern.java index ef20785d1..559468208 100644 --- a/src/main/java/com/conveyal/gtfs/model/Pattern.java +++ b/src/main/java/com/conveyal/gtfs/model/Pattern.java @@ -113,7 +113,7 @@ public Pattern () {} public static class Loader extends Entity.Loader { public Loader(GTFSFeed feed) { - super(feed, Table.PROPRIETARY_FILE_PREFIX + "patterns"); + super(feed, Table.getTableFileName("patterns")); } @Override @@ -141,7 +141,7 @@ public void loadOneRow() throws IOException { public static class Writer extends Entity.Writer { public Writer (GTFSFeed feed) { - super(feed, Table.PROPRIETARY_FILE_PREFIX + "patterns"); + super(feed, Table.getTableFileName("patterns")); } @Override diff --git a/src/main/java/com/conveyal/gtfs/validator/ServiceValidator.java b/src/main/java/com/conveyal/gtfs/validator/ServiceValidator.java index 8403c18c9..c2ede5888 100644 --- a/src/main/java/com/conveyal/gtfs/validator/ServiceValidator.java +++ b/src/main/java/com/conveyal/gtfs/validator/ServiceValidator.java @@ -268,7 +268,7 @@ select durations.service_id, duration_seconds, days_active from ( // Except that some service IDs may have no trips on them, or may not be referenced in any calendar or // calendar exception, which would keep them from appearing in either of those tables. So we just create // this somewhat redundant materialized view to serve as a master list of all services. - String servicesTableName = feed.getTableName("services"); + String servicesTableName = feed.getTableNameWithSchemaPrefix("services"); String sql = String.format("create table %s (service_id varchar, n_days_active integer, duration_seconds integer, n_trips integer)", servicesTableName); LOG.info(sql); statement.execute(sql); @@ -285,7 +285,7 @@ select durations.service_id, duration_seconds, days_active from ( serviceTracker.executeRemaining(); // Create a table that shows on which dates each service is active. - String serviceDatesTableName = feed.getTableName("service_dates"); + String serviceDatesTableName = feed.getTableNameWithSchemaPrefix("service_dates"); sql = String.format("create table %s (service_date varchar, service_id varchar)", serviceDatesTableName); LOG.info(sql); statement.execute(sql); @@ -318,7 +318,7 @@ select durations.service_id, duration_seconds, days_active from ( // where dates.service_id = durations.service_id // group by service_date, route_type order by service_date, route_type; - String serviceDurationsTableName = feed.getTableName("service_durations"); + String serviceDurationsTableName = feed.getTableNameWithSchemaPrefix("service_durations"); sql = String.format("create table %s (service_id varchar, route_type integer, " + "duration_seconds integer, primary key (service_id, route_type))", serviceDurationsTableName); LOG.info(sql); diff --git a/src/test/java/com/conveyal/gtfs/GTFSTest.java b/src/test/java/com/conveyal/gtfs/GTFSTest.java index 7fbc27698..1aad4b97e 100644 --- a/src/test/java/com/conveyal/gtfs/GTFSTest.java +++ b/src/test/java/com/conveyal/gtfs/GTFSTest.java @@ -1016,7 +1016,7 @@ private void assertThatExportedGtfsMeetsExpectations( if (persistenceExpectation.appliesToEditorDatabaseOnly) continue; // No need to check that errors were exported because it is an internal table only. if ("errors".equals(persistenceExpectation.tableName)) continue; - final String tableFileName = Table.getTableFileName(persistenceExpectation.tableName); + final String tableFileName = Table.getTableFileNameWithExtension(persistenceExpectation.tableName); LOG.info(String.format("reading table: %s", tableFileName)); ZipEntry entry = gtfsZipfile.getEntry(tableFileName);