Kotlin Exposed query + native query

56 views Asked by At

Can I combine an existing exposed query and a native query? Like:

`val query = PersonTable.select { PersonTable.name eq "Napoleon" }

query + "and surname = 'Bonapart'"`

Other than filtering at the code level, nothing comes up

1

There are 1 answers

0
Ricardo Gellman On

Yes, using and & or but have in mind that it may result in fetching more data than necessary from the database, so it's often better to try to express your conditions using Exposed queries if possible.

val query = PersonTable.select { PersonTable.name eq "Napoleon" }
val combinedQuery = query.and { PersonTable.surname eq "Bonapart" }

If you want to mix native SQL queries with Exposed queries, you might need to execute them separately and then combine the results at the code level.

val exposedQuery = PersonTable.select { PersonTable.name eq "Napoleon" }.toList()
val nativeQuery = transaction {
    exec("SELECT * FROM person WHERE surname = 'Bonapart'")
}

// Combine the results in code
val combinedResults = exposedQuery.filter { 
  /* filter based on the native query results */ 
}