Mat*_*sen 7 ruby stored-procedures ruby-on-rails plpgsql rails-postgresql
我正在开发一个项目,我在PostgreSQL环境中担任数据库设计师/管理员.领导者决定将Rails用于应用程序逻辑并招募一名Rails程序员.
Rails程序员说他通常会编写所有应用程序代码,并且不喜欢让某人通过他存储过程而导致缺乏控制,并且他从未在rails中完成过.
数据库使用了很多继承/ EERM,因此存储过程和触发器将使他的工作变得更容易,除了使用procs的性能优势.
我有四个问题:
1)如何从没有返回值的Rails调用pl/pgSQL存储过程
2)如何使用单个返回值(1行/ 1列)从Rails调用pl/pgSQL存储过程
3)如何从Rails调用pl/pgSQL存储过程,其中1行多列返回值.
4)如何使用OUT参数从Rails调用pl/pgSQL存储过程?
谢谢!
您好我正在使用此代码来处理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
Run Code Online (Sandbox Code Playgroud)
例
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
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3580 次 |
| 最近记录: |