Search code examples
kotlinormcollatekotlin-exposed

Exposed Kotlin. Problem with cyrillic encoding


I tryed to fix a problem with encodings. So, I sent from 'Postman', from web browser request to server, where I search data in database by keys in request. Request can be like this:

http://localhost:8080/books.getBooksByGenre/Документальное/0/10

(in browser). Server receive string, like

http://localhost:8080/books.getBooksByGenre/%D0%94%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%D0%BB%D1%8C%D0%BD%D0%BE%D0%B5/0/10

then, takes params from url: genreName: 'Документальное' start: 0 count: 10. Then, this data sends to dao:

override fun findGenreByName(genreName: String): DatabaseGenre {
    return transaction(db) { getGenreByName(genreName) }
}

private fun getGenreByName(genreName: String): DatabaseGenre {
    return try {
        val foundGenre = GenreEntity.find { Genres.genre eq genreName }.single()
        DatabaseGenre(foundGenre.id.value, foundGenre.genre, foundGenre.link)
    } catch (e: Exception) {
        throw NothingFoundInDatabaseException("no one genre found by '$genreName'")
    } catch (e: NoSuchElementException) {
        val m = "Duplicates of genre with name '$genreName'"
        throw DuplicatedDataInDatabaseException(m)
    }
}

In log I see, that sql-query for finding genres is correct, but I receive an exception:

java.util.NoSuchElementException: Collection is empty.

The sql-query, as I said, is correct:

SELECT genres.id, genres.genre, genres.link FROM genres WHERE genres.genre = 'Документальное'

Structure of genres table:

genres id: int(10) genre: varchar(100) link: varchar(100

I tryied, to select all genres, and this query executed almost correctly. So, I decided, to check this query with english word, and this query correctly executed:

SELECT genres.id, genres.genre, genres.link FROM genres WHERE genres.genre = 'simpleGenre'

I have not exceptions with this query.

So, what I've done wrong and how to fix problem with collations?

UPD: As I said at github (issue), I've tryied this query it mysql cli and I receive correct answer.

Also, I've tryed to decode url params (with java URLDecoder class). It doesn't helps too.


Solution

  • Thanks, @madhead. I tryied an advance of @madhead, and it works. So, from this time my DB connection URL looks like this:

    val connect = Database.connect(
            url = "jdbc:mysql://localhost:3306/my_database_name?characterEncoding=utf8&useUnicode=true",
            driver = "com.mysql.jdbc.Driver",
            user = user_name,
            password = password
    )