ITeye博客

ruby调用oracle存储过程实例之oracle连接配置

scholltop · 2014-07-24 14:40 · oracle · RUBY · 编程语言 · Rails · Oracle · Markdown

先看一下ruby怎样连接oracle数据库:


class External < ActiveRecord::Base
  self.abstract_class = true
  establish_connection :wifi_rms
end

abstract_class 抽象类:抽象类是不允许实例化的类,因此一般它需要被进行扩展继承。 从establish_connection 进去可见:class ConnectionSpecification类 定义了这个连接查找方法:


 def self.establish_connection(spec = nil)
      case spec
        when nil
          raise AdapterNotSpecified unless defined? RAILS_ENV
          establish_connection(RAILS_ENV)
        when ConnectionSpecification
          @@connection_handler.establish_connection(name, spec)
        when Symbol, String
          if configuration = configurations[spec.to_s]
            establish_connection(configuration)
          else
            raise AdapterNotSpecified, "#{spec} database is not configured"
          end
        else
        ......
        end

这里传入的是database.yml中配置的参数


wifi_rms:
  adapter: oracle
  database: (DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = "your ip")(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = "your service")(SERVER = DEDICATED)))
  username: name
  password: password
  encoding: utf8
  host: host_ip
  service: your_service

这样当你要调用oracle库关联的时候直接继承连接类即可实现类持久化到oracle数据库


class ConvenientReportMonth < External
  #继承External类
  #关联表设置
  set_table_name "CONVENIENT_REPORTS_MONTH"
  #表序列设置
  set_sequence_name "seq_convenient_month_id"
 end

最后要说一下的是: 由于rails生产模式下配置的是mysql数据库


development: &config
  adapter: mysql
  encoding: utf8
  database: db_new
  username: root
  password: password
  socket: /tmp/mysql.sock
  host: your_ip
  port: 3306

因此需要调用mysql库的时候只要集成 class Event < ActiveRecord::Base #ActiveRecord::Base默认连接库为mysql end

至此,实现了rails项目中mysql和oracle双库开发工作环境。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。