英文:
How Do I Access Data in SQLRow object in Vapor/Fluent?
问题
我正在在Vapor应用程序中的Fluent上下文中运行原始SQL查询。
我对感兴趣的对象获取的输出如下所示:
(lldb) po words
▿ _PostgresSQLRow
▿ randomAccessView : PostgresRandomAccessRow
▿ columns : 1 个元素
▿ 0 : 列
- 名称 : "count"
- tableOID : 0
- 列属性编号 : 0
▿ 数据类型 : BIGINT
- 原始值 : 20
- 数据类型大小 : 8
- 数据类型修改器 : -1
- 格式 : 二进制
▿ cells : 1 个元素
▿ 0 : Optional<ByteBuffer>
▿ some : ByteBuffer { readerIndex: 0, writerIndex: 8, readableBytes: 8, capacity: 8, storageCapacity: 1024, slice: _ByteBufferSlice { 59..<67 }, storage: 0x000000010404bc00 (1024 bytes) }
▿ _storage : <_Storage: 0x600001705900>
- _readerIndex : 0
- _writerIndex : 8
▿ _slice : _ByteBufferSlice { 59..<67 }
- upperBound : 67
▿ _begin : 59
▿ _backing : 2 个元素
- .0 : 0
- .1 : 59
▿ lookupTable : 1 个元素
▿ 0 : 2 个元素
- 键 : "count"
具体来说,我想要底部的lookupTable中的 "count" 值。
查询只是一个用于测试的虚拟查询,但它看起来像这样:
guard let postgres = db as? SQLDatabase else { return nil }
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:
(lldb) po words
▿ _PostgresSQLRow
▿ randomAccessView : PostgresRandomAccessRow
▿ columns : 1 element
▿ 0 : Column
- name : "count"
- tableOID : 0
- columnAttributeNumber : 0
▿ dataType : BIGINT
- rawValue : 20
- dataTypeSize : 8
- dataTypeModifier : -1
- format : binary
▿ cells : 1 element
▿ 0 : Optional<ByteBuffer>
▿ some : ByteBuffer { readerIndex: 0, writerIndex: 8, readableBytes: 8, capacity: 8, storageCapacity: 1024, slice: _ByteBufferSlice { 59..<67 }, storage: 0x000000010404bc00 (1024 bytes) }
▿ _storage : <_Storage: 0x600001705900>
- _readerIndex : 0
- _writerIndex : 8
▿ _slice : _ByteBufferSlice { 59..<67 }
- upperBound : 67
▿ _begin : 59
▿ _backing : 2 elements
- .0 : 0
- .1 : 59
▿ lookupTable : 1 element
▿ 0 : 2 elements
- key : "count"
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:
guard let postgres = db as? SQLDatabase else { return nil }
return try await postgres.raw("SELECT count(*) FROM words").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
首先,您需要创建一个用于解码结果的结构体:
struct CountResult: Decodable {
let wordscount: Int
}
然后,修改您的代码:
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:
struct CountResult: Decodable {
let wordscount: Int
}
Then, modify your code:
let count = try await postgres.raw("SELECT count(*) AS wordscount FROM words").first(decoding: CountResult.self)
The alias of the column name makes life easier.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论