技术开发 频道

使用XForms和Ruby on Rails开发小型门诊管理系统



Rails 和 DB2

现在需要考虑如何将 XForms 前端和 DB2 pureXML 后端连接在一起了。Ruby on Rails 为快速便捷地创建 Web 应用程序提供了一种强大的方法。而且处理数据库也很方便,但是 Rails 能用于 DB2 尤其是 DB2 pureXML 吗?当然可以,而且和 Rails 的其他方面一样,惊人的简单。

为 Ruby 安装 DB2 驱动程序

首先需要启动 Rails 中的 DB2 支持。如果已经安装了 Rails,还需要安装 Ruby Gems。否则应该安装它,因为这个包管理系统非常适合为 Rails 增加新的功能,如 DB2 支持。安装之后在命令提示符下输入:>gem install ibm_db。

从而下载 DB2 Ruby 驱动程序。安装完成后,所有的 Ruby 应用程序(不仅仅是 Rails 应用程序)都有了 DB2 支持。如果使用的是 Windows,就会提示选择什么版本的驱动程序,如清单 5 所示。


清单 5. 选择 DB2 驱动程序
               
>gem install ibm_db
Bulk updating Gem source index for:
http://gems.rubyforge.org
Select which gem to install for your platform (i386-mswin32)
 1. ibm_db 0.6.0 (mswin32)
 2. ibm_db 0.6.0 (ruby)
 3. ibm_db 0.4.6 (ruby)
 4. ibm_db 0.4.6 (mswin32)
 5. Skip this gem
 6. Cancel installation
 

需要选择最新版本的驱动程序,因此选择 #1 或 #2。如果使用 Windows 安装程序安装了 Ruby,需要选择 #1。现在就可以创建使用 DB2 pureXML 的 Rails 应用程序了。

创建 Rails 应用程序

Rails 开发人员应该熟悉下面的代码。我们将创建清单 6 所示的应用程序。
清单 6. 创建 Rails 应用程序   
>rails xmlmd
 create
 create app/controllers
 create app/helpers
 create app/models
 create app/views/layouts
 create config/environments
 create components
 create db
 create doc
 create lib
 create lib/tasks
 create log
 create public/images
 create public/javascripts
 create public/stylesheets
 create script/performance
 create script/process
 create test/fixtures
 create test/functional
 create test/integration
 create test/mocks/development
 create test/mocks/test
 create test/unit
 create vendor
 create vendor/plugins
 create tmp/sessions
 create tmp/sockets
 create tmp/cache
 create tmp/pids
 create Rakefile
 create README
 create app/controllers/application.rb
 create app/helpers/application_helper.rb
 create test/test_helper.rb
 create config/database.yml
 create config/routes.rb
 create public/.htaccess
 create config/boot.rb
 create config/environment.rb
 create config/environments/production.rb
 create config/environments/development.rb
 create config/environments/test.rb
 create script/about
 create script/breakpointer
 create script/console
 create script/destroy
 create script/generate
 create script/performance/benchmarker
 create script/performance/profiler
 create script/process/reaper
 create script/process/spawner
 create script/process/inspector
 create script/runner
 create script/server
 create script/plugin
 create public/dispatch.rb
 create public/dispatch.cgi
 create public/dispatch.fcgi
 create public/404.html
 create public/500.html
 create public/index.html
 create public/favicon.ico
 create public/robots.txt
 create public/images/rails.png
 create public/javascripts/prototype.js
 create public/javascripts/effects.js
 create public/javascripts/dragdrop.js
 create public/javascripts/controls.js
 create public/javascripts/application.js
 create doc/README_FOR_APP
 create log/server.log
 create log/production.log
 create log/development.log
 create log/test.log
 

这样就完成了 Web 应用程序的框架。还需要编辑 config/database.yml 以便连接到 DB2,如清单 7 所示。


清单 7. 编辑 config/database.yml
               
# IBM DB2 Database configuration file
# Install the IBM DB2 driver and get assistance from:
#
http://www.alphaworks.ibm.com/tech/db2onrails
development:
 adapter: ibm_db
 database: health
 username: michael
 password: your_password_here
 schema: xmlmd
# == remote TCP/IP connection (required when no local database catalog entry available)
# host: bigserver // fully qualified hostname or IP address
# port: 50000 // data server TCP/IP port number
 

# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
 adapter: mysql
 database: xmlmd_test
 username: root
 password:
 host: localhost

production:
 adapter: mysql
 database: xmlmd_production
 username: root
 password:
 host: localhost
 

请注意该例中只修改了开发设置,而没有改变测试和产品设置。此外把 adapter 设为 ibm_db。这就是刚刚安装的驱动程序。Rails 不知道这个驱动程序,因为它不是 Rails 默认安装的驱动程序。所幸的是很容易修改。只需要编辑一个文件。找到 Ruby 安装目录打开文件 /lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record.rb。找到 RAILS_CONNECTION_ADAPTERS 这一行,在列表的后面加上 ibm_db,如清单 8 所示。


清单 8. 为 Rails 增加 DB2 支持
               
RAILS_CONNECTION_ADAPTERS = %w( mysql postgresql sqlite firebird sqlserver db2 oracle
sybase openbase frontbase ibm_db )
 

现在可以生成使用 XML 的模型了。首先按照常规方式使用 Rails 生成脚本建立一个模型,如清单 9 所示。
清单 9. 生成 Rails 模型
               
>ruby script\generate model doctor
 exists app/models/
 exists test/unit/
 exists test/fixtures/
 create app/models/doctor.rb
 create test/unit/doctor_test.rb
 create test/fixtures/doctors.yml
 create db/migrate
 create db/migrate/001_create_doctors.rb
 

生成的迁移脚本和预期的一样。我们将利用它生成使用 XML 的另一个表,如清单 10 所示。
清单 10. Rails 迁移脚本:001_create_doctors.rb
               
class CreateDoctors < ActiveRecord::Migration
 def self.up
 create_table :doctors do |t|
 t.column :profile, :xml, :null => false
 end
 end

 def self.down
 drop_table :doctors
 end
end
 

这是非常典型的 Rails 迁移脚本。仅定义了一列 “profile”。惟一特别的一点是将列的类型定义为 xml。我们已经使用 Rails 创建了连接到数据库的 Ruby 脚本,并建立了包含 XML 列的表。剩下的就是测试脚本了。

0
相关文章