Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/data/gtfs/GtfsDatabase.kt
Show All 12 Lines | BusTO - Data components | ||||
GNU General Public License for more details. | GNU General Public License for more details. | ||||
You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
package it.reyboz.bustorino.data.gtfs | package it.reyboz.bustorino.data.gtfs | ||||
import android.content.Context | import android.content.Context | ||||
import android.util.Log | |||||
import androidx.room.* | import androidx.room.* | ||||
import androidx.room.migration.Migration | |||||
@Database( | @Database( | ||||
entities = [ | entities = [ | ||||
GtfsFeed::class, | |||||
GtfsAgency::class, | |||||
GtfsServiceDate::class, | GtfsServiceDate::class, | ||||
GtfsStop::class, | GtfsStop::class, | ||||
GtfsService::class, | GtfsService::class, | ||||
GtfsRoute::class, | GtfsRoute::class, | ||||
GtfsStopTime::class, | GtfsStopTime::class, | ||||
GtfsTrip::class, | GtfsTrip::class, | ||||
GtfsShape::class], | GtfsShape::class, | ||||
MatoPattern::class, | |||||
PatternStop::class | |||||
], | |||||
version = GtfsDatabase.VERSION, | version = GtfsDatabase.VERSION, | ||||
exportSchema = false, | |||||
) | ) | ||||
@TypeConverters(Converters::class) | @TypeConverters(Converters::class) | ||||
public abstract class GtfsDatabase : RoomDatabase() { | abstract class GtfsDatabase : RoomDatabase() { | ||||
abstract fun gtfsDao() : StaticGtfsDao | abstract fun gtfsDao() : GtfsDBDao | ||||
companion object{ | companion object{ | ||||
@Volatile | @Volatile | ||||
private var INSTANCE: GtfsDatabase? =null | private var INSTANCE: GtfsDatabase? =null | ||||
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").build() | "gtfs_database") | ||||
.addMigrations(MIGRATION_1_2) | |||||
.build() | |||||
INSTANCE = instance | INSTANCE = instance | ||||
instance | instance | ||||
} | } | ||||
} | } | ||||
const val VERSION = 1 | const val VERSION = 2 | ||||
const val FOREIGNKEY_ONDELETE = ForeignKey.CASCADE | const val FOREIGNKEY_ONDELETE = ForeignKey.CASCADE | ||||
val MIGRATION_1_2 = Migration(1,2) { | |||||
Log.d("BusTO-Database", "Upgrading from version 1 to version 2 the Room Database") | |||||
//create table for feeds | |||||
it.execSQL("CREATE TABLE IF NOT EXISTS `gtfs_feeds` (`feed_id` TEXT NOT NULL, PRIMARY KEY(`feed_id`))") | |||||
//create table gtfs_agencies | |||||
it.execSQL("CREATE TABLE IF NOT EXISTS `gtfs_agencies` (`gtfs_id` TEXT NOT NULL, `ag_name` TEXT NOT NULL, `ag_url` TEXT NOT NULL, `fare_url` TEXT, `phone` TEXT, `feed_id` TEXT, PRIMARY KEY(`gtfs_id`))") | |||||
//recreate routes | |||||
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`))") | |||||
//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 `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