Skip to content

Commit

Permalink
mGTFS > shared insert with prepared stmt compat
Browse files Browse the repository at this point in the history
  • Loading branch information
mmathieum committed Dec 10, 2024
1 parent 60edaed commit 692f2aa
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 100 deletions.
11 changes: 1 addition & 10 deletions src/main/java/org/mtransit/commons/gtfs/sql/AgencySQL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,12 @@ object AgencySQL : CommonSQL<Agency>(), TableSQL {
)
}

fun insert(agency: Agency, statement: Statement): Boolean {
return statement.executeUpdate(
getSQLInsertOrReplace(
statement,
agency
)
) > 0
}

fun select(agencyId: AgencyId? = null, statement: Statement): List<Agency> {
val sql = buildString {
append("SELECT ")
append("* ")
append("FROM $T_AGENCY ")
append("LEFT JOIN $T_AGENCY_IDS ON $T_AGENCY.$T_AGENCY_K_ID_INT = $T_AGENCY_IDS.$T_AGENCY_K_ID_INT ")
append("JOIN $T_AGENCY_IDS ON $T_AGENCY.$T_AGENCY_K_ID_INT = $T_AGENCY_IDS.$T_AGENCY_K_ID_INT ")
agencyId?.let {
append("WHERE $T_AGENCY_IDS.$T_AGENCY_IDS_K_ID = '$it'")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ object CalendarDateSQL : CommonSQL<CalendarDate>(), TableSQL {
)
}

fun insert(calendarDate: CalendarDate, statement: Statement): Boolean {
return statement.executeUpdate(
getSQLInsertOrReplace(
statement,
calendarDate
)
) > 0
}

fun select(serviceId: ServiceId? = null, statement: Statement): List<CalendarDate> {
val sql = buildString {
append("SELECT ")
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/org/mtransit/commons/gtfs/sql/CommonSQL.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.mtransit.commons.gtfs.sql

import org.mtransit.commons.sql.SQLInsertBuilder
import org.mtransit.commons.sql.SQLUtils
import org.mtransit.commons.sql.SQLUtils.quotesEscape
import org.mtransit.commons.sql.executeQueryMT
import org.mtransit.commons.sql.executeUpdateMT
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.Statement

Expand All @@ -23,6 +25,7 @@ abstract class CommonSQL<MainType>() : TableSQL {
SQLInsertBuilder.compile(sql, id.quotesEscape())
}

@Suppress("unused")
open fun getSQLSelectIdIntFromId(id: String) = getIdsTable()?.let {
SQLTableDef.makeIdsTableSelect(it, id)
}
Expand Down Expand Up @@ -66,6 +69,48 @@ abstract class CommonSQL<MainType>() : TableSQL {

fun getMainTableSQLInsert() = getMainTable()?.getSQLInsertTableQuery()

fun getMainTableInsertPreparedStatement(allowUpdate: Boolean = false) = getMainTable()?.let {
buildString {
append((if (allowUpdate) SQLUtils.INSERT_OR_REPLACE_INTO else SQLUtils.INSERT_INTO))
append(it.tableName)
append(SQLUtils.VALUES_P1)
it.columns.forEachIndexed { i, columnDef ->
if (i > 0) {
append(SQLUtils.COLUMN_SEPARATOR)
}
append("?")
}
append(SQLUtils.P2)
}
}

fun insertIntoMainTable(mainObject: MainType, statement: Statement, preparedStatement: PreparedStatement?): Boolean {
preparedStatement?.apply {
val mainTable = getMainTable() ?: return false
val columnsValues = toInsertColumns(statement, mainObject)
val columnsDef = mainTable.columns
columnsValues.forEachIndexed { i, columnValue ->
if (columnValue == null) {
setNull(i + 1, java.sql.Types.NULL)
return@forEachIndexed
}
when (columnsDef[i].columnType) {
SQLUtils.INT -> setInt(i + 1, columnValue as Int)
SQLUtils.TXT -> setString(i + 1, columnValue as String)
else -> TODO("Unexpected column type for ${columnsDef[i]}!")
}
}
addBatch()
return true
}
return statement.executeUpdateMT(
getSQLInsertOrReplace(
statement,
mainObject
)
) > 0
}

fun getMainTableSQLDrop() = getMainTable()?.getSQLDropIfExistsQuery()

open fun count(statement: Statement): Int {
Expand Down
11 changes: 1 addition & 10 deletions src/main/java/org/mtransit/commons/gtfs/sql/FrequencySQL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ object FrequencySQL : CommonSQL<Frequency>(), TableSQL {
)
}

fun insert(frequency: Frequency, statement: Statement): Boolean {
return statement.executeUpdate(
getSQLInsertOrReplace(
statement,
frequency
)
) > 0
}

override fun fromResultSet(rs: ResultSet) = with(rs) {
Frequency(
tripId = getString(TripSQL.T_TRIP_IDS_K_ID),
Expand All @@ -62,7 +53,7 @@ object FrequencySQL : CommonSQL<Frequency>(), TableSQL {
append("SELECT ")
append("* ")
append("FROM $T_FREQUENCY ")
append("LEFT JOIN ${TripSQL.T_TRIP_IDS} ON $T_FREQUENCY.$T_FREQUENCY_K_TRIP_ID_INT = ${TripSQL.T_TRIP_IDS}.${TripSQL.T_TRIP_IDS_K_ID_INT} ")
append("JOIN ${TripSQL.T_TRIP_IDS} ON $T_FREQUENCY.$T_FREQUENCY_K_TRIP_ID_INT = ${TripSQL.T_TRIP_IDS}.${TripSQL.T_TRIP_IDS_K_ID_INT} ")
tripId?.let {
append("WHERE ${TripSQL.T_TRIP_IDS}.${TripSQL.T_TRIP_IDS_K_ID} = '$it'")
}
Expand Down
13 changes: 2 additions & 11 deletions src/main/java/org/mtransit/commons/gtfs/sql/RouteSQL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,6 @@ object RouteSQL : CommonSQL<Route>(), TableSQL {
)
}

fun insert(route: Route, statement: Statement): Boolean {
return statement.executeUpdate(
getSQLInsertOrReplace(
statement,
route,
)
) > 0
}

fun select(
routeIds: Collection<RouteId>? = null,
agencyId: AgencyId? = null,
Expand All @@ -100,8 +91,8 @@ object RouteSQL : CommonSQL<Route>(), TableSQL {
append("SELECT ")
append("* ")
append("FROM $T_ROUTE ")
append("LEFT JOIN $T_ROUTE_IDS ON $T_ROUTE.$T_ROUTE_K_ID_INT = $T_ROUTE_IDS.$T_ROUTE_K_ID_INT ")
append("LEFT JOIN ${AgencySQL.T_AGENCY_IDS} ON $T_ROUTE.$T_ROUTE_K_AGENCY_ID_INT = ${AgencySQL.T_AGENCY_IDS}.${AgencySQL.T_AGENCY_K_ID_INT} ")
append("JOIN $T_ROUTE_IDS ON $T_ROUTE.$T_ROUTE_K_ID_INT = $T_ROUTE_IDS.$T_ROUTE_K_ID_INT ")
append("JOIN ${AgencySQL.T_AGENCY_IDS} ON $T_ROUTE.$T_ROUTE_K_AGENCY_ID_INT = ${AgencySQL.T_AGENCY_IDS}.${AgencySQL.T_AGENCY_K_ID_INT} ")
routeIds?.let {
append("WHERE $T_ROUTE_IDS.$T_ROUTE_IDS_K_ID IN (${it.joinToString { "'$it'" }}) ")
}
Expand Down
13 changes: 2 additions & 11 deletions src/main/java/org/mtransit/commons/gtfs/sql/StopSQL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,15 @@ object StopSQL : CommonSQL<Stop>(), TableSQL {
)
}

fun insert(stop: Stop, statement: Statement): Boolean {
return statement.executeUpdate(
getSQLInsertOrReplace(
statement,
stop
)
) > 0
}

fun select(stopId: StopId? = null, statement: Statement): List<Stop> {
val sql = buildString {
append("SELECT ")
append("*, ")
append("${getJoinAlias(T_STOP_K_ID_INT)}.$T_STOP_IDS_K_ID AS ${getAlias(T_STOP_K_ID_INT, T_STOP_IDS_K_ID)}, ")
append("${getJoinAlias(T_STOP_K_PARENT_STATION_ID_INT)}.$T_STOP_IDS_K_ID AS ${getAlias(T_STOP_K_PARENT_STATION_ID_INT, T_STOP_IDS_K_ID)} ")
append("FROM $T_STOP ")
append("LEFT JOIN $T_STOP_IDS AS ${getJoinAlias(T_STOP_K_ID_INT)} ON $T_STOP.$T_STOP_K_ID_INT =${getJoinAlias(T_STOP_K_ID_INT)}.$T_STOP_K_ID_INT ")
append("LEFT JOIN $T_STOP_IDS AS ${getJoinAlias(T_STOP_K_PARENT_STATION_ID_INT)} ON $T_STOP.$T_STOP_K_PARENT_STATION_ID_INT = ${getJoinAlias(T_STOP_K_PARENT_STATION_ID_INT)}.$T_STOP_K_ID_INT ")
append("JOIN $T_STOP_IDS AS ${getJoinAlias(T_STOP_K_ID_INT)} ON $T_STOP.$T_STOP_K_ID_INT =${getJoinAlias(T_STOP_K_ID_INT)}.$T_STOP_K_ID_INT ")
append("JOIN $T_STOP_IDS AS ${getJoinAlias(T_STOP_K_PARENT_STATION_ID_INT)} ON $T_STOP.$T_STOP_K_PARENT_STATION_ID_INT = ${getJoinAlias(T_STOP_K_PARENT_STATION_ID_INT)}.$T_STOP_K_ID_INT ")
stopId?.let {
append("WHERE ${getJoinAlias(T_STOP_K_ID_INT)}.$T_STOP_IDS_K_ID = '$it'")
}
Expand Down
52 changes: 3 additions & 49 deletions src/main/java/org/mtransit/commons/gtfs/sql/TripSQL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.mtransit.commons.sql.SQLUtils.quotesEscape
import org.mtransit.commons.sql.executeQueryMT
import org.mtransit.commons.sql.executeUpdateMT
import org.mtransit.commons.sql.toSQL
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.Statement
import kotlin.use
Expand Down Expand Up @@ -60,22 +59,6 @@ object TripSQL : CommonSQL<Trip>(), TableSQL {
insertAllowReplace = false,
)

// TODO move to upper class
fun getInsertPreparedStatement(allowUpdate: Boolean = false): String {
return buildString {
append((if (allowUpdate) SQLUtils.INSERT_OR_REPLACE_INTO else SQLUtils.INSERT_INTO))
append(T_TRIP)
append(SQLUtils.VALUES_P1)
getMainTable().columns.forEachIndexed { i, columnDef ->
if (i > 0) {
append(SQLUtils.COLUMN_SEPARATOR)
}
append("?")
}
append(SQLUtils.P2)
}
}

override fun toInsertColumns(statement: Statement, trip: Trip) = with(trip) {
arrayOf<Any?>(
getOrInsertIdInt(statement, trip.tripId),
Expand All @@ -91,35 +74,6 @@ object TripSQL : CommonSQL<Trip>(), TableSQL {
)
}

// TODO move to uper class
fun insert(trip: Trip, statement: Statement, preparedStatement: PreparedStatement? = null): Boolean {
if (preparedStatement != null) {
val columnsValues = toInsertColumns(statement, trip)
with(preparedStatement) {
val columnsDef = getMainTable().columns
columnsValues.forEachIndexed { i, columnValue ->
if (columnValue == null) {
setNull(i+1, java.sql.Types.NULL)
return@forEachIndexed
}
when (columnsDef[i].columnType) {
SQLUtils.INT -> setInt(i+1, columnValue as Int)
SQLUtils.TXT -> setString(i+1, columnValue as String)
else -> TODO("Unexpected column type for ${columnsDef[i]}!")
}
}
addBatch()
}
return true
}
return statement.executeUpdateMT(
getSQLInsertOrReplace(
statement,
trip
)
) > 0
}

fun select(
statement: Statement,
tripIds: Collection<TripId>? = null,
Expand All @@ -130,9 +84,9 @@ object TripSQL : CommonSQL<Trip>(), TableSQL {
append("SELECT ")
append("* ")
append("FROM $T_TRIP ")
append("LEFT JOIN $T_TRIP_IDS ON $T_TRIP.$T_TRIP_K_ID_INT = $T_TRIP_IDS.$T_TRIP_IDS_K_ID_INT ")
append("LEFT JOIN ${RouteSQL.T_ROUTE_IDS} ON $T_TRIP.${T_TRIP_K_ROUTE_ID_INT} = ${RouteSQL.T_ROUTE_IDS}.${RouteSQL.T_ROUTE_K_ID_INT} ")
append("LEFT JOIN ${CalendarDateSQL.T_SERVICE_IDS} ON $T_TRIP.${T_TRIP_K_SERVICE_ID_INT} = ${CalendarDateSQL.T_SERVICE_IDS}.${CalendarDateSQL.T_SERVICE_IDS_K_ID_INT} ")
append("JOIN $T_TRIP_IDS ON $T_TRIP.$T_TRIP_K_ID_INT = $T_TRIP_IDS.$T_TRIP_IDS_K_ID_INT ")
append("JOIN ${RouteSQL.T_ROUTE_IDS} ON $T_TRIP.${T_TRIP_K_ROUTE_ID_INT} = ${RouteSQL.T_ROUTE_IDS}.${RouteSQL.T_ROUTE_K_ID_INT} ")
append("JOIN ${CalendarDateSQL.T_SERVICE_IDS} ON $T_TRIP.${T_TRIP_K_SERVICE_ID_INT} = ${CalendarDateSQL.T_SERVICE_IDS}.${CalendarDateSQL.T_SERVICE_IDS_K_ID_INT} ")
tripIds?.let {
append("WHERE $T_TRIP_IDS.$T_TRIP_IDS_K_ID IN (${it.joinToString { "'$it'" }}) ")
}
Expand Down

0 comments on commit 692f2aa

Please sign in to comment.