Skip to content

Commit 51b5ec6

Browse files
committed
query events by bounds
1 parent a5b8c7c commit 51b5ec6

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

app/src/main/kotlin/org/btcmap/db/table/event/EventQueries.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,31 @@ class EventQueries(private val conn: SQLiteConnection) {
4444
}
4545
}
4646

47+
fun selectByBounds(
48+
minLat: Double,
49+
maxLat: Double,
50+
minLon: Double,
51+
maxLon: Double,
52+
): List<Event> {
53+
conn.prepare(
54+
"""
55+
SELECT ${FullProjection.COLUMNS}
56+
FROM $TABLE
57+
WHERE $LAT >= ?1 AND $LAT <= ?2 AND $LON >= ?3 AND $LON <= ?4;
58+
"""
59+
).use {
60+
it.bindDouble(1, minLat)
61+
it.bindDouble(2, maxLat)
62+
it.bindDouble(3, minLon)
63+
it.bindDouble(4, maxLon)
64+
val rows = mutableListOf<Event>()
65+
while (it.step()) {
66+
rows.add(FullProjection.fromStatement(it))
67+
}
68+
return rows
69+
}
70+
}
71+
4772
fun selectById(id: Long): Event? {
4873
conn.prepare(
4974
"""

app/src/test/kotlin/org/btcmap/db/table/event/EventQueriesTest.kt

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,84 @@ class EventQueriesTest {
7979
Assert.assertTrue(results.isEmpty())
8080
}
8181

82+
@Test
83+
fun insert_and_selectByBounds() {
84+
val db = createDatabase()
85+
val event1 = Event(
86+
id = 1L,
87+
areaId = null,
88+
lat = 40.7128,
89+
lon = -74.0060,
90+
name = "NYC Event",
91+
website = "https://example.com".toHttpUrl(),
92+
startsAt = ZonedDateTime.parse("2024-06-01T18:00:00Z"),
93+
endsAt = null,
94+
cronSchedule = null,
95+
)
96+
val event2 = Event(
97+
id = 2L,
98+
areaId = null,
99+
lat = 51.5074,
100+
lon = -0.1278,
101+
name = "London Event",
102+
website = "https://example.com".toHttpUrl(),
103+
startsAt = ZonedDateTime.parse("2024-07-01T18:00:00Z"),
104+
endsAt = null,
105+
cronSchedule = null,
106+
)
107+
val event3 = Event(
108+
id = 3L,
109+
areaId = null,
110+
lat = 34.0522,
111+
lon = -118.2437,
112+
name = "LA Event",
113+
website = "https://example.com".toHttpUrl(),
114+
startsAt = ZonedDateTime.parse("2024-08-01T18:00:00Z"),
115+
endsAt = null,
116+
cronSchedule = null,
117+
)
118+
119+
db.event.insert(listOf(event1, event2, event3))
120+
121+
val results = db.event.selectByBounds(
122+
minLat = 40.0,
123+
maxLat = 52.0,
124+
minLon = -75.0,
125+
maxLon = 0.0,
126+
)
127+
128+
Assert.assertEquals(2, results.size)
129+
Assert.assertEquals("NYC Event", results[0].name)
130+
Assert.assertEquals("London Event", results[1].name)
131+
}
132+
133+
@Test
134+
fun selectByBounds_returnsEmptyListWhenNoMatch() {
135+
val db = createDatabase()
136+
val event = Event(
137+
id = 1L,
138+
areaId = null,
139+
lat = 40.7128,
140+
lon = -74.0060,
141+
name = "NYC Event",
142+
website = "https://example.com".toHttpUrl(),
143+
startsAt = ZonedDateTime.parse("2024-06-01T18:00:00Z"),
144+
endsAt = null,
145+
cronSchedule = null,
146+
)
147+
148+
db.event.insert(listOf(event))
149+
150+
val results = db.event.selectByBounds(
151+
minLat = 50.0,
152+
maxLat = 60.0,
153+
minLon = -80.0,
154+
maxLon = -70.0,
155+
)
156+
157+
Assert.assertTrue(results.isEmpty())
158+
}
159+
82160
@Test
83161
fun deleteAll_removesAllEvents() {
84162
val db = createDatabase()

0 commit comments

Comments
 (0)