sqldelight和数据类,在科特林(Kotlin)中有一到许多关系

发布于 2025-01-18 04:35:37 字数 1064 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉溺在你眼里的海 2025-01-25 04:35:37

您可以像 Sven 建议的那样创建查询,然后使用查询上的映射器将连接的表记录映射到您的authorEntity 对象中。

这就是使用映射器的方式。您只需将其调整为您在问题中提到的连接列即可填充authorEntity。

override fun get(id: Long): Flow<Query<Friend>>? {
   return try {
      return queries.getFriendById(
          id = id,
          mapper = { friendId, username, firstname, lastname, phone, picture, accepted ->
          Friend(
              Profile(friendId, username, firstname, lastname, phone, picture),
              accepted
          )
      }).asFlow()
  } catch (e: NullPointerException) {
      null
  }
}

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.

override fun get(id: Long): Flow<Query<Friend>>? {
   return try {
      return queries.getFriendById(
          id = id,
          mapper = { friendId, username, firstname, lastname, phone, picture, accepted ->
          Friend(
              Profile(friendId, username, firstname, lastname, phone, picture),
              accepted
          )
      }).asFlow()
  } catch (e: NullPointerException) {
      null
  }
}
甜警司 2025-01-25 04:35:37

on join的条款是链接两个表的条​​件。您无需在子句中重复中的条件。使用在其中进一步缩小查询的范围,例如,如果您要搜索特定的作者名称。

SELECT * FROM author
JOIN book ON book.authorId = author.id
WHERE author.name LIKE 'John%';

如果要查询所有作者,只需完全删除的。

另外,您无需创建数据类并自己进行映射。 SQLDELIGHT已经为您的查询创建数据类。

The ON clause of the JOIN is the condition which links both tables. You don't need to repeat the condition in the WHERE clause. Use WHERE to further narrow down the query, for instance if you're searching for a specific author name.

SELECT * FROM author
JOIN book ON book.authorId = author.id
WHERE author.name LIKE 'John%';

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文