sqldelight和数据类,在科特林(Kotlin)中有一到许多关系
我正在尝试使用 Kotlin Multiplatform 创建一个小型应用程序,在其中我可以保存书名及其作者,但我正在努力解决数据类以及如何将它们映射在一起,以便我获得作者及其所有书籍和发布日期。
CREATE TABLE book(
id INTEGER NOT NULL,
name TEXT NOT NULL,
publishDate INTEGER NOT NULL,
authorId INTEGER NOT NULL
)
CREATE TABLE author(
id INTEGER NOT NULL,
name TEXT NOT NULL
)
这是我的数据类:
@Serializable
data class bookEntity(
id: Int,
name: String,
authorId: Int
)
@Serializable
data class authorEntity(
id: Int,
authorName: String
books: List<bookEntity>
)
和我的查询:
selectAuthors:
SELECT * FROM author
JOIN book ON book.authorId = author.id
WHERE book.authorId = author.id
我尝试了以下映射,但它不起作用:
private fun mapAuthor(
id: Int,
authorName: String,
bookId: String,
name: String,
publishDate: Long
): Author(
return Author (
id = id,
authorName = authorName,
book = List<BookEntity>(
id = bookId,
name = name,
publishDate = publishDate
)
)
)
我如何使用这样的列表? 感谢您的每一次帮助!
I am trying to create a small app using Kotlin Multiplatform where i can save book titles and their authors but i'm struggling with the data classes and how to map them together so that i get the author with all of their books and the publish date.
CREATE TABLE book(
id INTEGER NOT NULL,
name TEXT NOT NULL,
publishDate INTEGER NOT NULL,
authorId INTEGER NOT NULL
)
CREATE TABLE author(
id INTEGER NOT NULL,
name TEXT NOT NULL
)
Here are my data classes:
@Serializable
data class bookEntity(
id: Int,
name: String,
authorId: Int
)
@Serializable
data class authorEntity(
id: Int,
authorName: String
books: List<bookEntity>
)
and my Query:
selectAuthors:
SELECT * FROM author
JOIN book ON book.authorId = author.id
WHERE book.authorId = author.id
i tried the following mapping but it didn't work:
private fun mapAuthor(
id: Int,
authorName: String,
bookId: String,
name: String,
publishDate: Long
): Author(
return Author (
id = id,
authorName = authorName,
book = List<BookEntity>(
id = bookId,
name = name,
publishDate = publishDate
)
)
)
How can i work with lists like this?
Every help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以像 Sven 建议的那样创建查询,然后使用查询上的映射器将连接的表记录映射到您的authorEntity 对象中。
这就是使用映射器的方式。您只需将其调整为您在问题中提到的连接列即可填充authorEntity。
You can create the query like Sven suggested and then map the joined table records into your authorEntity object using a mapper on the query.
This is how you use the mapper. You just need to adjust this to the joined columns you mentioned in your question to fill the authorEntity.
的
的条款是链接两个表的条件。您无需在子句中重复on
join中的条件。使用
在其中进一步缩小查询的范围,例如,如果您要搜索特定的作者名称。如果要查询所有作者,只需完全删除的。
另外,您无需创建数据类并自己进行映射。 SQLDELIGHT已经为您的查询创建数据类。
The
ON
clause of theJOIN
is the condition which links both tables. You don't need to repeat the condition in theWHERE
clause. UseWHERE
to further narrow down the query, for instance if you're searching for a specific author name.If you want to query all authors, just remove the
WHERE
completely.Also, you don't need to create data classes and do the mapping yourself. SQLDelight already creates data classes for your queries.