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

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

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 : &quot;count&quot;
        - tableOID : 0
        - columnAttributeNumber : 0
        ▿ dataType : BIGINT
          - rawValue : 20
        - dataTypeSize : 8
        - dataTypeModifier : -1
        - format : binary
    ▿ cells : 1 element
      ▿ 0 : Optional&lt;ByteBuffer&gt;
        ▿ some : ByteBuffer { readerIndex: 0, writerIndex: 8, readableBytes: 8, capacity: 8, storageCapacity: 1024, slice: _ByteBufferSlice { 59..&lt;67 }, storage: 0x000000010404bc00 (1024 bytes) }
          ▿ _storage : &lt;_Storage: 0x600001705900&gt;
          - _readerIndex : 0
          - _writerIndex : 8
          ▿ _slice : _ByteBufferSlice { 59..&lt;67 }
            - upperBound : 67
            ▿ _begin : 59
              ▿ _backing : 2 elements
                - .0 : 0
                - .1 : 59
    ▿ lookupTable : 1 element
      ▿ 0 : 2 elements
        - 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:

        guard let postgres = db as? SQLDatabase else  { return nil }
        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

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

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(&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:

确定