我正在从事一个项目,在这个项目中,我是PostgreSQL环境中的数据库设计师/管理员。领导者决定将Rails用于应用程序逻辑,并招募了一名Rails程序员。
这位rails程序员说,他通常会编写所有的应用程序代码,不喜欢别人给他传递存储过程带来的缺乏控制权,而且他从来没有在Rails中这样做过。
数据库利用了大量的继承/EERM,因此存储过程和触发器将使他的工作变得容易得多,除了使用procs带来的性能优势之外。
我有四个问题:
1)如何从Rails调用无返回值的pl/pgSQL存储过程
2)如何使用单个返回值(1行/1列)从Rails调用pl/pgSQL存储过程
3)如何从Rails调用一个单行多列返回值的pl/pgSQL存储过程。
4)如何使用OUT参数从Rails调用pl/pgSQL存储过程?
谢谢!
发布于 2014-03-19 17:35:55
您好,我正在使用这段代码来处理PgSQL存储过程,除了最后一个问题之外,这段代码涵盖了所有内容
class SpActiveRecord < ActiveRecord::Base
self.abstract_class = true
#without return value
def self.execute_sp(sql, *bindings)
perform_sp(:execute, sql, *bindings)
end
#select many return values
def self.fetch_sp(sql, *bindings)
perform_sp(:select_all, sql, *bindings)
end
#select single return value
def self.fetch_sp_val(sql, *bindings)
perform_sp(:select_value, sql, *bindings)
end
protected
def self.perform_sp(method, sql, *bindings)
if bindings.any?
sql = self.send(:sanitize_sql_array, bindings.unshift(sql))
end
self.connection.send(method, sql)
end
end示例
class Invoice < SpActiveRecord
def create_or_update
raise ReadOnlyRecord if readonly? or !new_record?
self.id = fetch_sp_val("SELECT * FROM billing.invoice_generate(?,?,?)",
self.account_id,
self.start_date,
self.end_date)
@new_record = false
true
end
endhttps://stackoverflow.com/questions/17119763
复制相似问题