如何在Vapor/Fluent中访问SQLRow对象中的数据?

huangapple go评论95阅读模式
英文:

How Do I Access Data in SQLRow object in Vapor/Fluent?

问题

我正在在Vapor应用程序中的Fluent上下文中运行原始SQL查询。

我对感兴趣的对象获取的输出如下所示:

  1. (lldb) po words
  2. _PostgresSQLRow
  3. randomAccessView : PostgresRandomAccessRow
  4. columns : 1 个元素
  5. 0 :
  6. - 名称 : "count"
  7. - tableOID : 0
  8. - 列属性编号 : 0
  9. 数据类型 : BIGINT
  10. - 原始值 : 20
  11. - 数据类型大小 : 8
  12. - 数据类型修改器 : -1
  13. - 格式 : 二进制
  14. cells : 1 个元素
  15. 0 : Optional<ByteBuffer>
  16. some : ByteBuffer { readerIndex: 0, writerIndex: 8, readableBytes: 8, capacity: 8, storageCapacity: 1024, slice: _ByteBufferSlice { 59..<67 }, storage: 0x000000010404bc00 (1024 bytes) }
  17. _storage : <_Storage: 0x600001705900>
  18. - _readerIndex : 0
  19. - _writerIndex : 8
  20. _slice : _ByteBufferSlice { 59..<67 }
  21. - upperBound : 67
  22. _begin : 59
  23. _backing : 2 个元素
  24. - .0 : 0
  25. - .1 : 59
  26. lookupTable : 1 个元素
  27. 0 : 2 个元素
  28. - : "count"

具体来说,我想要底部的lookupTable中的 "count" 值。

查询只是一个用于测试的虚拟查询,但它看起来像这样:

  1. guard let postgres = db as? SQLDatabase else { return nil }
  2. return try await postgres.raw("SELECT count(*) FROM words").first().map { row in

最终,这将扩展为包括一堆动态正则表达式查询,但这只是为了获得基本技巧返回值。那么,数据已经存在,但我如何从中提取计数值?

英文:

I am running raw sql queries in a Vapor app in a Fluent context.

The output I am getting for the object I'm interested in looks like:

  1. (lldb) po words
  2. _PostgresSQLRow
  3. randomAccessView : PostgresRandomAccessRow
  4. columns : 1 element
  5. 0 : Column
  6. - name : &quot;count&quot;
  7. - tableOID : 0
  8. - columnAttributeNumber : 0
  9. dataType : BIGINT
  10. - rawValue : 20
  11. - dataTypeSize : 8
  12. - dataTypeModifier : -1
  13. - format : binary
  14. cells : 1 element
  15. 0 : Optional&lt;ByteBuffer&gt;
  16. some : ByteBuffer { readerIndex: 0, writerIndex: 8, readableBytes: 8, capacity: 8, storageCapacity: 1024, slice: _ByteBufferSlice { 59..&lt;67 }, storage: 0x000000010404bc00 (1024 bytes) }
  17. _storage : &lt;_Storage: 0x600001705900&gt;
  18. - _readerIndex : 0
  19. - _writerIndex : 8
  20. _slice : _ByteBufferSlice { 59..&lt;67 }
  21. - upperBound : 67
  22. _begin : 59
  23. _backing : 2 elements
  24. - .0 : 0
  25. - .1 : 59
  26. lookupTable : 1 element
  27. 0 : 2 elements
  28. - key : &quot;count&quot;

Specifically, I want the "count" value in the lookupTable at the bottom.

The query is just a dummy to test with, but it looks like this:

  1. guard let postgres = db as? SQLDatabase else { return nil }
  2. return try await postgres.raw(&quot;SELECT count(*) FROM words&quot;).first().map { row in

This will eventually be expanded to include a bunch of dynamic regex queries, but this is just to get the basic technique returning values.

So, the data is there, but how do I extract the count value from it?

答案1

得分: 2

首先,您需要创建一个用于解码结果的结构体:

  1. struct CountResult: Decodable {
  2. let wordscount: Int
  3. }

然后,修改您的代码:

  1. let count = try await postgres.raw("SELECT count(*) AS wordscount FROM words").first(decoding: CountResult.self)

列名的别名使生活更加方便。

英文:

First, you need to create a structure into which you can decode the result:

  1. struct CountResult: Decodable {
  2. let wordscount: Int
  3. }

Then, modify your code:

  1. let count = try await postgres.raw(&quot;SELECT count(*) AS wordscount FROM words&quot;).first(decoding: CountResult.self)

The alias of the column name makes life easier.

huangapple
  • 本文由 发表于 2023年7月11日 12:26:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定