Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/data/gtfs/GtfsDatabase.kt
Show All 37 Lines | entities = [ | ||||
], | ], | ||||
version = GtfsDatabase.VERSION, | version = GtfsDatabase.VERSION, | ||||
) | ) | ||||
@TypeConverters(Converters::class) | @TypeConverters(Converters::class) | ||||
abstract class GtfsDatabase : RoomDatabase() { | abstract class GtfsDatabase : RoomDatabase() { | ||||
abstract fun gtfsDao() : GtfsDBDao | abstract fun gtfsDao() : GtfsDBDao | ||||
companion object{ | companion object{ | ||||
@Volatile | @Volatile | ||||
private var INSTANCE: GtfsDatabase? =null | private var INSTANCE: GtfsDatabase? =null | ||||
const val DB_NAME="gtfs_database" | |||||
fun getGtfsDatabase(context: Context): GtfsDatabase{ | fun getGtfsDatabase(context: Context): GtfsDatabase{ | ||||
return INSTANCE ?: synchronized(this){ | return INSTANCE ?: synchronized(this){ | ||||
val instance = Room.databaseBuilder(context.applicationContext, | val instance = Room.databaseBuilder(context.applicationContext, | ||||
GtfsDatabase::class.java, | GtfsDatabase::class.java, | ||||
"gtfs_database") | DB_NAME) | ||||
.addMigrations(MIGRATION_1_2) | .addMigrations(MIGRATION_1_2) | ||||
.build() | .build() | ||||
INSTANCE = instance | INSTANCE = instance | ||||
instance | instance | ||||
} | } | ||||
} | } | ||||
const val VERSION = 2 | const val VERSION = 2 | ||||
Show All 9 Lines | companion object{ | ||||
//recreate routes | //recreate routes | ||||
it.execSQL("DROP TABLE IF EXISTS `routes_table`") | it.execSQL("DROP TABLE IF EXISTS `routes_table`") | ||||
it.execSQL("CREATE TABLE IF NOT EXISTS `routes_table` (`route_id` TEXT NOT NULL, `agency_id` TEXT NOT NULL, `route_short_name` TEXT NOT NULL, `route_long_name` TEXT NOT NULL, `route_desc` TEXT NOT NULL, `route_mode` TEXT NOT NULL, `route_color` TEXT NOT NULL, `route_text_color` TEXT NOT NULL, PRIMARY KEY(`route_id`))") | it.execSQL("CREATE TABLE IF NOT EXISTS `routes_table` (`route_id` TEXT NOT NULL, `agency_id` TEXT NOT NULL, `route_short_name` TEXT NOT NULL, `route_long_name` TEXT NOT NULL, `route_desc` TEXT NOT NULL, `route_mode` TEXT NOT NULL, `route_color` TEXT NOT NULL, `route_text_color` TEXT NOT NULL, PRIMARY KEY(`route_id`))") | ||||
//create patterns and stops | //create patterns and stops | ||||
it.execSQL("CREATE TABLE IF NOT EXISTS `mato_patterns` (`pattern_name` TEXT NOT NULL, `pattern_code` TEXT NOT NULL, `pattern_hash` TEXT NOT NULL, `pattern_direction_id` INTEGER NOT NULL, `pattern_route_id` TEXT NOT NULL, `pattern_headsign` TEXT, `pattern_polyline` TEXT NOT NULL, `pattern_polylength` INTEGER NOT NULL, PRIMARY KEY(`pattern_code`), FOREIGN KEY(`pattern_route_id`) REFERENCES `routes_table`(`route_id`) ON UPDATE NO ACTION ON DELETE CASCADE )") | it.execSQL("CREATE TABLE IF NOT EXISTS `mato_patterns` (`pattern_name` TEXT NOT NULL, `pattern_code` TEXT NOT NULL, `pattern_hash` TEXT NOT NULL, `pattern_direction_id` INTEGER NOT NULL, `pattern_route_id` TEXT NOT NULL, `pattern_headsign` TEXT, `pattern_polyline` TEXT NOT NULL, `pattern_polylength` INTEGER NOT NULL, PRIMARY KEY(`pattern_code`), FOREIGN KEY(`pattern_route_id`) REFERENCES `routes_table`(`route_id`) ON UPDATE NO ACTION ON DELETE CASCADE )") | ||||
it.execSQL("CREATE TABLE IF NOT EXISTS `patterns_stops` (`pattern_gtfs_id` TEXT NOT NULL, `stop_gtfs_id` TEXT NOT NULL, `stop_order` INTEGER NOT NULL, PRIMARY KEY(`pattern_gtfs_id`, `stop_gtfs_id`, `stop_order`), FOREIGN KEY(`pattern_gtfs_id`) REFERENCES `mato_patterns`(`pattern_code`) ON UPDATE NO ACTION ON DELETE CASCADE )") | it.execSQL("CREATE TABLE IF NOT EXISTS `patterns_stops` (`pattern_gtfs_id` TEXT NOT NULL, `stop_gtfs_id` TEXT NOT NULL, `stop_order` INTEGER NOT NULL, PRIMARY KEY(`pattern_gtfs_id`, `stop_gtfs_id`, `stop_order`), FOREIGN KEY(`pattern_gtfs_id`) REFERENCES `mato_patterns`(`pattern_code`) ON UPDATE NO ACTION ON DELETE CASCADE )") | ||||
} | } | ||||
} | } | ||||
} | } | ||||
No newline at end of file | No newline at end of file |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register