如何检查我在Rails中准备好的语句是否正在执行?

问题描述 投票:0回答:1

我在Rails(postgres db)中有此准备好的语句:

    conn = ActiveRecord::Base.connection.raw_connection

    conn.prepare('query_fetch_dest_values',
       "SELECT q.entity AS source_entity,
          q.financial_institution AS source_financial_institution,
          q.account_number AS source_account_number,
          a2.entity AS destination_entity,
          a2.financial_institution AS destination_financial_institution,
          a2.account_number AS destination_account_number
        FROM
        (SELECT *
         FROM forecast_entry_and_cash_positions
         INNER JOIN accounts a ON forecast_entry_and_cash_positions.source_account_id = a.account_number 
         WHERE (input_type IN ($1) AND
            a.financial_institution IN ($2) AND
            forecast_entry_and_cash_positions.source_account_id IN ($3))
         )q LEFT JOIN accounts a2 ON q.dest_account_id = a2.account_number")

    search_destination_values = 
      conn.exec_prepared('query_fetch_dest_values',
        [decode_input_type(params[:search_input_type]),
          params[:search_source_financial_institution],
          params[:search_source_account_number]
        ])

    puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    puts search_destination_values.inspect
  1. 以上SQL无法在我的控制台上打印。那么如何知道我的查询是否正在执行?
  2. 在上面的代码中显示的inspect中,这就是在控制台上打印的内容:
#<PG::Result:0x00007f9d32c528b8 status=PGRES_TUPLES_OK ntuples=0 nfields=6 cmd_tuples=0>

我从这个博客了解到-https://blog.kiprosh.com/understanding-pg-result-object/上述输出中的nTuples=0cmd_tuples=0表示SQL返回的记录数为0。

我在pgadmin中的查询返回3条记录-为什么我在这里没有得到任何输出?

  1. [本文档https://rubydoc.info/gems/pg/PG/Connection#exec_prepared-instance_method和类似这样的stackoverflow博客-> Prepared Statement on Postgresql in Rails显示了当过滤条件为被过滤列的=(等于)条件时,如何传递过滤器的动态值。

但是在我的情况下,我有3个IN过滤器。该文档没有显示为IN子句传递动态过滤器的示例。在上面的代码片段中,为我的3个IN子句传递动态过滤器的方式正确吗? (同样,由于我的查询没有在控制台上打印,所以我无法验证我的过滤器是否正确添加到SQL中。)>

我在Rails(postgres db)中有此准备好的语句:conn = ActiveRecord :: Base.connection.raw_connection conn.prepare('query_fetch_dest_values',“ SELECT q.entity AS source_entity,...

ruby-on-rails postgresql prepared-statement
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.