【问题标题】:escaping Postgresql hstore type with ruby?用 ruby​​ 转义 Postgresql hstore 类型?
【发布时间】:2018-12-07 19:30:10
【问题描述】:

我正在使用带有 rails (pg gem) 的 postgresql 类型 hstore。我需要的是在很多行中大量插入 hstore 值,所以我想制作 SQL 字符串。

但我在 pg gem 中找不到 hstore 引用方法(我认为应该有一个)。

是我漏了什么,还是我自己写这个引用方法?

【问题讨论】:

    标签: ruby postgresql hstore


    【解决方案1】:

    我可能会使用hstore(text, text) and hstore(text[], text[]) functions 而不是尝试构建字符串版本。将它们与array constructor 语法结合起来,您将只处理字符串文字。例如,如果您的 hstore 是单个键和值,则:

    insert into your_table (hstore_column)
    values (hstore('key', 'value'))
    

    将工作并在hstore_column 中为您提供"key"=>"value";如果您的 hstore 有多个对,则:

    insert into your_table (hstore_column)
    values (hstore(array['key1', 'key2', 'key3'], array['value1, 'value2', 'value3']))
    

    将在hstore_column 中为您提供"key1"=>"value1","key2"=>"value2","key3"=>"value3"

    我怀疑调用函数的开销与解析字符串版本的开销会有很大不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      • 2014-08-31
      相关资源
      最近更新 更多