-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DB: Add NSW stops and product class tables (#627)
### TL;DR Added NSW stops database tables and related queries to support stop location functionality. ### What changed? - Created new tables `NswStops` and `NswStopProductClass` to store stop information and their associated product classes - Added SQL queries for inserting and selecting stops based on name and product class filters - Implemented new Sandook interface methods for managing NSW stops data - Added migration script for version 2 to create the new tables - Added documentation for generating Kotlin files from SQL tables ### How to test? 1. Run `./gradlew generateCommonMainKrailSandookInterface` to generate updated Kotlin files 2. Verify the new tables are created by: - Inserting a stop using `insertNswStop` - Adding product classes with `insertNswStopProductClass` - Querying stops using the new select methods 3. Test different stop search scenarios: - Partial name matching - Filtering by included product classes - Filtering by excluded product classes ### Why make this change? To support the storage and retrieval of NSW transport stop locations and their associated transport modes (product classes), enabling location-based features and stop searching functionality in the KRAIL app.
- Loading branch information
1 parent
a39051f
commit a318e1d
Showing
9 changed files
with
276 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
## Sandook | ||
|
||
Sandook is the db for KRAIL app. | ||
Sandook is the db for KRAIL app. | ||
|
||
## Migrations | ||
|
||
Add migration in .sqm files in `migrations` folder. | ||
|
||
### Android | ||
### Android | ||
|
||
- Update in SandookCallback | ||
|
||
### iOS | ||
### iOS | ||
|
||
- Create a new class for after version migration for `SandookMigration` | ||
- Update in `IosSandookDriverFactory` and add `SandookMigrationAfterX` to `getMigrationCallbacks()` | ||
method. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Sandook | ||
|
||
### Generate Kotlin files for SQL tables | ||
|
||
Run `./gradlew generateCommonMainKrailSandookInterface` to generate the | ||
Kotlin files for the sql tables created in common module. The Kotlin files will be generated in the | ||
following dir: `build/generated/sqldelight/code/KrailSandook/commonMain` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- Create NSW Stops Table -- | ||
CREATE TABLE IF NOT EXISTS NswStops ( | ||
stopId TEXT PRIMARY KEY, | ||
stopName TEXT NOT NULL, | ||
stopLat REAL NOT NULL, | ||
stopLon REAL NOT NULL | ||
); | ||
|
||
-- Create NSW Stop Product Class Table -- | ||
CREATE TABLE IF NOT EXISTS NswStopProductClass ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
stopId TEXT NOT NULL, | ||
productClass INTEGER NOT NULL, | ||
FOREIGN KEY (stopId) REFERENCES NswStops(stopId) | ||
); |
65 changes: 65 additions & 0 deletions
65
sandook/src/commonMain/sqldelight/xyz/ksharma/krail/sandook/NswStops.sq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
-- Create NSW Stops Table -- | ||
CREATE TABLE IF NOT EXISTS NswStops ( | ||
stopId TEXT PRIMARY KEY, | ||
stopName TEXT NOT NULL, | ||
stopLat REAL NOT NULL, | ||
stopLon REAL NOT NULL | ||
); | ||
|
||
-- Create NSW Stop Product Class Table -- | ||
-- Foregin key ensures that any stopId value inserted into NswStopProductClass must exist in | ||
-- NswStops, helping maintain data integrity between the two tables. | ||
CREATE TABLE IF NOT EXISTS NswStopProductClass ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
stopId TEXT NOT NULL, | ||
productClass INTEGER NOT NULL, | ||
FOREIGN KEY (stopId) REFERENCES NswStops(stopId) | ||
); | ||
|
||
-- Insert data into NswStops table (without productClass) -- | ||
insertStop: | ||
INSERT OR IGNORE INTO NswStops(stopId, stopName, stopLat, stopLon) | ||
VALUES (?, ?, ?, ?); | ||
|
||
-- Insert each productClass value for a given stop into NswStopProductClass -- | ||
insertStopProductClass: | ||
INSERT INTO NswStopProductClass(stopId, productClass) | ||
VALUES (?, ?); | ||
|
||
-- Select stops with partial match on stopName -- | ||
selectStopsByPartialName: | ||
SELECT * FROM NswStops | ||
WHERE stopName LIKE '%' || ? || '%'; | ||
|
||
-- Select stops with partial match on stopName and specific productClass values -- | ||
selectStopsByNameAndProductClass: | ||
SELECT DISTINCT s.* | ||
FROM NswStops AS s | ||
JOIN NswStopProductClass AS p ON s.stopId = p.stopId | ||
WHERE s.stopName LIKE '%' || ? || '%' | ||
AND p.productClass IN ?; | ||
|
||
selectStopsByNameExcludingProductClass: | ||
SELECT DISTINCT s.* | ||
FROM NswStops AS s | ||
WHERE s.stopName LIKE '%' || ? || '%' | ||
AND s.stopId NOT IN ( | ||
SELECT p.stopId | ||
FROM NswStopProductClass AS p | ||
WHERE p.productClass IN ? | ||
); | ||
|
||
selectStopsByNameExcludingProductClassOrExactStopId: | ||
SELECT DISTINCT s.* | ||
FROM NswStops AS s | ||
WHERE ( | ||
-- Exact match scenario: returns a stop if its stopId matches the given parameter | ||
s.stopId = ? | ||
-- Partial match scenario: returns stops whose stopName contains the given parameter | ||
OR s.stopName LIKE '%' || ? || '%') | ||
AND s.stopId NOT IN ( | ||
-- Exclusion scenario: filters out any stopIds linked to product classes in the specified list | ||
SELECT p.stopId | ||
FROM NswStopProductClass AS p | ||
WHERE p.productClass IN ? | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
sandook/src/iosMain/kotlin/xyz/ksharma/krail/sandook/migrations/SandookMigrationAfter2.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package xyz.ksharma.krail.sandook.migrations | ||
|
||
import app.cash.sqldelight.db.SqlDriver | ||
import xyz.ksharma.krail.core.log.log | ||
|
||
internal object SandookMigrationAfter2 : SandookMigration { | ||
|
||
override fun migrate(sqlDriver: SqlDriver) { | ||
log("Upgrading database from version 2 to 3") | ||
sqlDriver.execute( | ||
identifier = null, | ||
sql = """ | ||
CREATE TABLE IF NOT EXISTS NswStops ( | ||
stopId TEXT PRIMARY KEY, | ||
stopName TEXT NOT NULL, | ||
stopLat REAL NOT NULL, | ||
stopLon REAL NOT NULL | ||
); | ||
CREATE TABLE IF NOT EXISTS NswStopProductClass ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
stopId TEXT NOT NULL, | ||
productClass INTEGER NOT NULL, | ||
FOREIGN KEY (stopId) REFERENCES NswStops(stopId) | ||
); | ||
""".trimIndent(), | ||
parameters = 0, | ||
) | ||
} | ||
} |