Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Is there streaming support? #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
unoexperto opened this issue Nov 27, 2021 · 3 comments
Open

Is there streaming support? #272

unoexperto opened this issue Nov 27, 2021 · 3 comments

Comments

@unoexperto
Copy link

I apologize for asking in an issue but I wasn't sure how to contact you @oshai.

Is there streaming support? Looking at ResultSet I see that it's expected to load all query results into a list. My use-case is to pull data in bulk so it could be "select * from table" on a table with billions of rows. I'm happy to contribute but perhaps it's already implemented.

@github-actions
Copy link

Thank you for reporting an issue. See the wiki for documentation and gitter for questions.

@oshai
Copy link
Contributor

oshai commented Nov 27, 2021

Hi, streaming is not supported at the moment. Contribution is welcome of course.

@jillesvangurp
Copy link

jillesvangurp commented Dec 19, 2023

I implemented this (postgres specific) for scrolling through large result sets. Might be interesting to add some variation of this.

    suspend fun queryFlow(query: String, params: List<Any>, fetchSize: Int = 100) : Flow<T> {
        // use channelFlow for thread safety
        return channelFlow {
            val producer = this
            connection.inTransaction {c ->
                val cursorId = "cursor_${Random.nextULong()}"
                try {
                    c.sendPreparedStatement(
                        """
                            DECLARE $cursorId CURSOR FOR 
                            $query
                        """.trimIndent(), params
                    )

                    var resp: QueryResult? = null
                    while (resp == null || resp.rows.isNotEmpty()) {
                        resp = c.sendQuery("FETCH $fetchSize FROM $cursorId;")
                        resp.rows.forEach { row ->
                            val doc = json.decodeFromString(serializationStrategy,row.getString("json")?: error("empty json"))
                            producer.send(doc)
                        }
                    }

                } finally {
                    c.sendQuery("CLOSE $cursorId;")
                }
            }
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants