Skip to content
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

Is it possible to get an entity from a ResultRow that contains aliased table? #404

Closed
ezhaka opened this issue Oct 12, 2018 · 9 comments
Closed
Assignees

Comments

@ezhaka
Copy link

ezhaka commented Oct 12, 2018

Hi!

Consider a simple example:

val subQueryAlias = Table2.select { /* tricky subquery goes here */ }

val query = Join(Table1).join(subQueryAlias) { Table1.id eq subQueryAlias[Table2.id] }

query.map { resultRow -> 
    // bang! it's impossible to do because Table2 is under the alias in the resultRow
    Table2Entity.wrapRow(resultRow) 
}

Is there a way to get my entity from ResultRow?

@ezhaka ezhaka changed the title Is it possible to get entity from a ResultRow that contains aliased table? Is it possible to get an entity from a ResultRow that contains aliased table? Oct 12, 2018
@Tapac Tapac self-assigned this Oct 16, 2018
@albertPu
Copy link

@Tapac
Can I get the entities from the query results and serialize them directly into json?Each time, the foreach loop assembles the data, and some cases can be serialized directly

@albertPu
Copy link

@Tapac
.Net LinqToDB framework for querying data is very convenient, if you can achieve that effect kotlin's query is too convenient

@Tapac
Copy link
Contributor

Tapac commented Oct 17, 2018

@albertPu , I do not quite understand what do you mean and how this relates to a current issue.
You can't serialize entity to json without custom serializer, because of all entitie's fields are delegates, not a real fields.

@yanghengyh
Copy link

@Tapac How can I define the serialization program myself?

@rabolfazl
Copy link

Please add this feature :)

Tapac added a commit that referenced this issue Jun 8, 2019
@Tapac Tapac closed this as completed Jun 8, 2019
@gabrielgatu
Copy link

For those still searching: this has been implemented so now it's possible to deserialize ResultRow into a model. In my case, this is what I've had to write:

RevenueTable.innerJoin(ProductTable)
        .slice(RevenueTable.columns)
        .select {
            ...
        }.let { RevenueModel.wrapRows(it) }.toList()

Hope this can help other people in my same situation.

@gunslingor
Copy link

I'm not following the solution.... what needs to happen? I don't see the wrapRows function anywhere, nor Table2Entityh. Somehow there has to be an efficient way to get entities back instead of resultRows without manually doing this for every table right?

object Outlets : Table() {
    val outlet = varchar("outlet", 255)
    val url = varchar("url", 2083)

    fun getAll(){
        dbConnect()
        val result = mutableMapOf<String, Outlet>()
        transaction { selectAll() }.forEach {
            result.put(it[outlet],
                Outlet(
                    it[outlet],
                    it[url]
                )
            )
        }
        return result
    }
}

class Outlet(
    val outlet: String,
    val url: String? = null
)

@joaoeudes7
Copy link

Any solution to get value from alias in ResultRow?

@Tapac
Copy link
Contributor

Tapac commented Feb 13, 2021

@joaoeudes7 , please check that test, it contains an example.

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

No branches or pull requests

8 participants