Code Monkey home page Code Monkey logo

Comments (4)

thomasnield avatar thomasnield commented on June 22, 2024

To support also insert operations:

fun <T: Any> SelectOperation.toPipeline(mapper: (ResultSet) -> T) = Pipeline(this, mapper = mapper)
fun <T: Any> InsertOperation.toPipeline(mapper: (ResultSet) -> T) = Pipeline(insertOperation = this, mapper = mapper)

class Pipeline<T: Any>(val selectOperation: SelectOperation? = null,
                       val insertOperation: InsertOperation? = null,
                  val mapper: (ResultSet) -> T
) {
    fun toObservable(): Observable<T> = selectOperation?.toObservable(mapper) ?: insertOperation?.toObservable(mapper) ?: throw Exception("Operation must be provided")
    fun toFlowable(): Flowable<T> = selectOperation?.toFlowable(mapper) ?: insertOperation?.toFlowable(mapper) ?: throw Exception("Operation must be provided")
    fun toSingle(): Single<T> = selectOperation?.toSingle(mapper) ?: insertOperation?.toSingle(mapper) ?: throw Exception("Operation must be provided")
    fun toMaybe(): Maybe<T> = selectOperation?.toMaybe(mapper) ?: insertOperation?.toMaybe(mapper) ?: throw Exception("Operation must be provided")
    fun toSequence(): ResultSetSequence<T> = selectOperation?.toSequence(mapper) ?: insertOperation?.toSequence(mapper) ?: throw Exception("Operation must be provided")
    fun blockingFirst() = selectOperation?.blockingFirst(mapper) ?: insertOperation?.blockingFirst(mapper) ?: throw Exception("Operation must be provided")
    fun blockingFirstOrNull() = selectOperation?.blockingFirstOrNull(mapper) ?: insertOperation?.blockingFirstOrNull(mapper) ?: throw Exception("Operation must be provided")
}

from rxkotlin-jdbc.

ZakTaccardi avatar ZakTaccardi commented on June 22, 2024

I thought this was achieved by Publisher<T>? What advantage does Pipeline bring over Publisher<T>?

from rxkotlin-jdbc.

thomasnield avatar thomasnield commented on June 22, 2024

@ZakTaccardi I'm not really trying to create an interface or contract. What I'm trying to achieve is if I have a JBDC query, I want the option for the API user to stream the results with the fluent pipeline they want, while having all of them share the same defined mapping operation (ResultSet) -> T.

This way, client can call MyItem.all.toObservable() rather than MyItem.all.toObservable(::MyItem), where ::MyItem maps each ResultSet to a MyItem.

from rxkotlin-jdbc.

thomasnield avatar thomasnield commented on June 22, 2024

Sometimes, as a user of an API, I want to return items as a Sequence, other times a Stream, and other times an Observable or Flowable.

from rxkotlin-jdbc.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.