connection.parse 未在 rails 4 中接受
connection.parse not accepted in rails 4
我正在尝试将现有的 rails 3 项目转换为 rails 4。
这是代码的一部分,其语法在 Rails 4
中是不可接受的
sql = [
"BEGIN #{Card::MY_PACKAGE}.retrieve_card(",
my_attrs.map { |a| ":#{a}, " }.join,
':errormsg); END;'
].join
connection = self.connection.raw_connection
cursor = connection.parse(sql)
my_attrs.each { |a| cursor.bind_param(a, my_attrs.send(a)) }
我注意到 connection.parse 不再适用于 Rails 4(我正在使用 activerecord-oracle_enhanced-adapter)
我正在尝试完全理解上面代码的作用(我没有写)
rails 4 中的等效语法是什么?
cursor = ActiveRecord::Base.connection.execute(sql) //Something along these lines?
编辑:
我明白了:
`"DEPRECATION WARNING: #connection is deprecated in favour of accessing it via the class."` //when i try to puts self.connection.class
I am trying to fully comp[re]hrend what the above code does( I didn't
write it)
module Card #Creates a namespace called Card
MY_PACKAGE = "hello"
end
str1 = "BEGIN #{Card::MY_PACKAGE}.retrieve_card(" #Looks in a namespace called Card for the constant MY_PACKAGE
puts str1 #=> "BEGIN hello.retrieve_card("
my_attrs = ['dog', 'cat', 'squirrel']
arr = my_attrs.map { |a| ":#{a}, " }
p arr #=> [":dog, ", ":cat, ", ":squirrel, "]
str2 = arr.join('')
p str2 #=> ":dog, :cat, :squirrel,"
str3 = ':errormsg); END;'
p str3 #=> ":errormsg); END;"
array_of_strs = [str1, str2, str3]
sql_statement = array_of_strs.join('')
p sql_statement #=> "BEGIN hello.retrieve_card(:dog, :cat, :squirrel, :errormsg); END;"
加入(分隔符=$,)
Returns 通过将数组的每个元素转换为字符串创建的字符串,由给定的分隔符分隔。如果分隔符为 nil,则使用当前的 $,。如果分隔符和 $, 都为 nil,则使用空字符串。
http://www.ruby-doc.org/core-2.2.0/Array.html#method-i-join
What would be the equivalent syntax in rails 4?
对我来说,看起来 raw_connection 应该能够调用 parse(),请参阅此处的第 98、107 行:
该页面上的 class 继承自:
class OracleEnhancedOCIConnection < OracleEnhancedConnection
...并且 OracleEnhancedConnection class 具有 @raw_connection 的属性 reader,请参阅此处的第 17 行:
是self.connection.raw_connection返回的raw_connection吗?尝试:
puts self.connection.class
puts self.class.connection.raw_connection.class returns: OCI8EnhancedAutoRecover"
这里定义了OCI8EnhancedAutoRecover
class:
...像这样:
class OCI8EnhancedAutoRecover < DelegateClass(OCI8)
OCI8EnhancedAutoRecover
class 定义了一些方法--none 其中是 parse()-- 并将其他方法调用委托给 OCI8 class-- 那是< DelegateClass(OCI8)
是什么意思。
OCI8 class 由以下人员提供:
require "oci8"
...这需要 ruby-oci8 gem
。 gem 在这里定义了 OCI8 class:
https://github.com/kubo/ruby-oci8/blob/master/lib/oci8/oci8.rb
在第 168 行,它定义了一个 parse() 方法:
def parse(sql)
@last_error = nil
parse_internal(sql)
end
所以在 raw_connection 上调用 parse() 应该可以。
我正在尝试将现有的 rails 3 项目转换为 rails 4。 这是代码的一部分,其语法在 Rails 4
中是不可接受的 sql = [
"BEGIN #{Card::MY_PACKAGE}.retrieve_card(",
my_attrs.map { |a| ":#{a}, " }.join,
':errormsg); END;'
].join
connection = self.connection.raw_connection
cursor = connection.parse(sql)
my_attrs.each { |a| cursor.bind_param(a, my_attrs.send(a)) }
我注意到 connection.parse 不再适用于 Rails 4(我正在使用 activerecord-oracle_enhanced-adapter)
我正在尝试完全理解上面代码的作用(我没有写)
rails 4 中的等效语法是什么?
cursor = ActiveRecord::Base.connection.execute(sql) //Something along these lines?
编辑:
我明白了:
`"DEPRECATION WARNING: #connection is deprecated in favour of accessing it via the class."` //when i try to puts self.connection.class
I am trying to fully comp[re]hrend what the above code does( I didn't write it)
module Card #Creates a namespace called Card
MY_PACKAGE = "hello"
end
str1 = "BEGIN #{Card::MY_PACKAGE}.retrieve_card(" #Looks in a namespace called Card for the constant MY_PACKAGE
puts str1 #=> "BEGIN hello.retrieve_card("
my_attrs = ['dog', 'cat', 'squirrel']
arr = my_attrs.map { |a| ":#{a}, " }
p arr #=> [":dog, ", ":cat, ", ":squirrel, "]
str2 = arr.join('')
p str2 #=> ":dog, :cat, :squirrel,"
str3 = ':errormsg); END;'
p str3 #=> ":errormsg); END;"
array_of_strs = [str1, str2, str3]
sql_statement = array_of_strs.join('')
p sql_statement #=> "BEGIN hello.retrieve_card(:dog, :cat, :squirrel, :errormsg); END;"
加入(分隔符=$,)
Returns 通过将数组的每个元素转换为字符串创建的字符串,由给定的分隔符分隔。如果分隔符为 nil,则使用当前的 $,。如果分隔符和 $, 都为 nil,则使用空字符串。
http://www.ruby-doc.org/core-2.2.0/Array.html#method-i-join
What would be the equivalent syntax in rails 4?
对我来说,看起来 raw_connection 应该能够调用 parse(),请参阅此处的第 98、107 行:
该页面上的 class 继承自:
class OracleEnhancedOCIConnection < OracleEnhancedConnection
...并且 OracleEnhancedConnection class 具有 @raw_connection 的属性 reader,请参阅此处的第 17 行:
是self.connection.raw_connection返回的raw_connection吗?尝试:
puts self.connection.class
puts self.class.connection.raw_connection.class returns: OCI8EnhancedAutoRecover"
这里定义了OCI8EnhancedAutoRecover
class:
...像这样:
class OCI8EnhancedAutoRecover < DelegateClass(OCI8)
OCI8EnhancedAutoRecover
class 定义了一些方法--none 其中是 parse()-- 并将其他方法调用委托给 OCI8 class-- 那是< DelegateClass(OCI8)
是什么意思。
OCI8 class 由以下人员提供:
require "oci8"
...这需要 ruby-oci8 gem
。 gem 在这里定义了 OCI8 class:
https://github.com/kubo/ruby-oci8/blob/master/lib/oci8/oci8.rb
在第 168 行,它定义了一个 parse() 方法:
def parse(sql)
@last_error = nil
parse_internal(sql)
end
所以在 raw_connection 上调用 parse() 应该可以。