如何在 Sequel ORM 中将行作为数组(而不是哈希)获取?

发布于 2024-11-02 16:04:38 字数 996 浏览 4 评论 0原文

在 Ruby 的 Sequel ORM 中,Dataset 类有一个 all< /code> 方法生成行哈希数组:每行都是一个以列名作为键的哈希。

例如,给定一个表 T:

a  b   c
--------------
0  22  "Abe"
1  35  "Betty"
2  58  "Chris"

then:

ds = DB['select a, b, c from T']
ah = ds.all # Array of row Hashes

应该生成:

[{"a":0,"b":22,"c":"Abe"},{"a":1,"b":35,"c":"Betty"},{"a":2,"b":58,"c":"Chris"}]

Sequel 中是否有内置方法来生成行数组数组,其中每行都是仅包含每行中的值的数组 按指定的顺序在查询中select_rows 在 ActiveRecord 中如何工作?像这样的:

aa = ds.rows # Array of row Arrays

会产生:

[[0,22,"Abe"],[1,35,"Betty"],[2,58,"Chris"]]

注意:表达式:

aa = ds.map { |h| h.values }

产生一个数组数组,但行中值的顺序不能保证与原始查询中请求的顺序匹配。在此示例中,aa 可能如下所示:

[["Abe",0,22],["Betty",1,35],["Chris",2,58]]

In the Sequel ORM for Ruby, the Dataset class has an all method which produces an Array of row hashes: each row is a Hash with column names as keys.

For example, given a table T:

a  b   c
--------------
0  22  "Abe"
1  35  "Betty"
2  58  "Chris"

then:

ds = DB['select a, b, c from T']
ah = ds.all # Array of row Hashes

should produce:

[{"a":0,"b":22,"c":"Abe"},{"a":1,"b":35,"c":"Betty"},{"a":2,"b":58,"c":"Chris"}]

Is there a way built in to Sequel to instead produce an Array of row Arrays, where each row is an array of only the values in each row in the order specified in the query? Sort of how select_rows works in ActiveRecord? Something like this:

aa = ds.rows # Array of row Arrays

which would produce:

[[0,22,"Abe"],[1,35,"Betty"],[2,58,"Chris"]]

Note: the expression:

aa = ds.map { |h| h.values }

produces an array of arrays, but the order of values in the rows is NOT guaranteed to match the order requested in the original query. In this example, aa might look like:

[["Abe",0,22],["Betty",1,35],["Chris",2,58]]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

心如狂蝶 2024-11-09 16:04:39

你试过这个吗?

ds = DB['select a, b, c from T'].to_a

不确定它是否有效,但试一试。

have you tried this?

ds = DB['select a, b, c from T'].to_a

not sure it it works but give it a shot.

分开我的手 2024-11-09 16:04:38

旧版本的 Sequel(2.0 之前)在某些适配器中能够返回数组而不是哈希值。但它引起了很多问题,没有人使用它,而且我不想维护它,所以它被删除了。如果您确实需要数组,则需要下降到连接级别并使用特定于连接的方法:

DB.synchronize do |conn|
  rows = conn.exec('SQL Here') # Hypothetical example code
end

您需要的实际代码将取决于您正在使用的适配器。

Old versions of Sequel (pre 2.0) had the ability in some adapters to return arrays instead of hashes. But it caused numerous issues, nobody used it, and I didn't want to maintain it, so it was removed. If you really want arrays, you need to drop down to the connection level and use a connection specific method:

DB.synchronize do |conn|
  rows = conn.exec('SQL Here') # Hypothetical example code
end

The actual code you need will depend on the adapter you are using.

眼角的笑意。 2024-11-09 16:04:38

DB[:table].where().select_map(:id)

DB[:table].where().select_map(:id)

删除会话 2024-11-09 16:04:38

如果您只想要一个值数组的数组...

DB['select * from T'].map { |h| h.values }

似乎可以进行

考虑到与查询顺序匹配的列顺序的更新要求,

cols= [:a, :c, :b]
DB[:T].select{cols}.collect{ |h| cols.collect {|c| h[c]}}

更新...不是很漂亮,但保证顺序与选择顺序相同。
似乎没有内置程序可以执行此操作。
您可以提出该功能的请求。

If you want just an array of array of values...

DB['select * from T'].map { |h| h.values }

seems to work

UPDATE given the updated requirement of the column order matching the query order...

cols= [:a, :c, :b]
DB[:T].select{cols}.collect{ |h| cols.collect {|c| h[c]}}

not very pretty but guaranteed order is the same as the select order.
There does not appear to be a builtin to do this.
You could make a request for the feature.

七月上 2024-11-09 16:04:38

我还没有找到一个内置方法来返回行数组的数组,其中行数组中的值按原始查询中的列顺序排序。尽管我怀疑内部方法可能更有效,但以下函数确实有效:

def rows( ds )
  ret = []
  column_keys = ds.columns  # guaranteed to match query order?
  ds.all { |row_hash|
    row_array = []
    column_keys.map { |column_key| row_array << row_hash[column_key] }
    ret << row_array
  }
  ret
end

*此函数取决于 Dataset.columns 返回的数组的顺序。如果此顺序未定义,则此 rows 函数不是很有用。

I haven't yet found a built-in method to return an array of row arrays where the values in the row arrays are ordered by the column order in the original query. The following function does* although I suspect an internal method could be more effecient:

def rows( ds )
  ret = []
  column_keys = ds.columns  # guaranteed to match query order?
  ds.all { |row_hash|
    row_array = []
    column_keys.map { |column_key| row_array << row_hash[column_key] }
    ret << row_array
  }
  ret
end

*This function depends on the order of the array returned by Dataset.columns. If this order is undefined, then this rows function isn't very useful.

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