| D:\>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 |
|
| # 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: xmldb username: user password: secret schema: teamroom application: TeamRoom account: devuser workstation: devbox # == 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 |
| 连接属性 | 描述 | 是否必需 |
| Adapter | Ruby 适配器名称,对于 DB2 为 ‘ibm_db’ | 是 |
| Database | Rails 项目所连接到的数据库 | 是 |
| Username | 用于连接到 DB2 数据库的用户 ID | 是 |
| Password | 指定的用户 ID 的密码 | 是 |
| Schema | 命名对象的集合。模式提供了在数据库中在逻辑上组织对象的方式。在这个例子中,我们将 Rails Team Room 项目的所有数据库对象组织在 ‘teamroom’ 数据库模式之下。这样便允许多个 Rails 项目共享一个数据库 | 可以将默认模式设置为当前会话用户的授权 ID(见注 4) |
| Application | 当使用 DB2 Connect 时,用于标识客户机应用程序名称的一个字符串被发送至主机数据库服务器。在 DB2 Connect 上,发出 ‘db2 list applications’ 后将显示 ‘application’ 名称,而不是 Ruby 可执行文件。 | 可选 |
| Account | 当使用 DB2 Connect 时,用于标识客户机帐户的一个字符串被发送至主机数据库服务器 | 可选 |
| Workstation | 当使用 DB2 Connect 时,用于标识客户机工作站名称的一个字符串被发送至主机数据库服务器 | 可选 |
| Host | 数据库所在远程服务器的主机名 | 可选(见注 5) |
| Port | 该参数包含数据库服务器用于等待来自远程客户机的通信的 TCP/IP 端口的名称 | 可选(见注 5) |
| 注 4:总是显式指定模式 强烈建议显式地指定模式,以便在逻辑上组织数据库对象。否则,如果使用相同的授权 ID 连接到由多个 Rails 项目共享的同一个数据库,就可能无意中致使多个 Rails 项目写入到同一个 <AuthID>.schema_info 表中。这将导致不可预测的结果。Schema_info 表用于跟踪迁移版本。下面的 Team Room 例子对此作了进一步的解释。 注 5:只有当没有可用的 DB2 编目信息,且没有在用于 DB2 CLI 的 db2cli.ini 配置文件中注册数据源时,与远程 TCP/IP 连接相关的可选连接属性 host 和 port 才是必需的。在使用 IBM Driver for ODBC 和 CLI,而不是本地安装的完整的 DB2 Client 的情况下,就可能出现这种类型的设置。 |
| 列名 | 数据类型 | 描述 |
| ID | Integer | 主键 |
| Name | VARCHAR | 文档名称 |
| Size | Integer | 文件大小 |
| Data | BLOB | 以二进制模式存储的文件,最大 2 M |
| Content_type | VARCHAR | 文档类型,包括 .doc、.ppt、.pdf、.sw、.exe、.a、.so、 .gif、.jpeg、.mov、.avi、.mpeg、.bmp 等 |
| D:\rails\teamroom>ruby script/generate migration create_docs_store create db/migrate create db/migrate/001_create_docs_store.rb |
| class CreateDocsStore < ActiveRecord::Migration def self.up create_table :documents do |t| t.column :name, :string, :null => false t.column :size, :integer, :null => false t.column :content_type, :string, :null => false t.column :data, :binary, :limit => 2.megabytes end end def self.down drop_table :documents end end |
| D:\rails\teamroom>rake db:migrate (in D:/rails/teamroom) == CreateDocsStore: migrating ================================================= -- create_table(:documents) -> 0.2010s == CreateDocsStore: migrated (0.2010s) ======================================== |
| D:\>db2 list tables for schema teamroom Table/View Schema Type Creation time ------------------------------- --------------- ----- -------------------------- DOCUMENTS TEAMROOM T 2007-04-21-21.00.18.131001 SCHEMA_INFO TEAMROOM T 2007-04-21-21.00.17.740001 2 record(s) selected. |
| D:\>db2 describe table teamroom.documents Column Type Type name schema name Length Scale Nulls ------------------------------ --------- ------------------ -------- ----- ------ ID SYSIBM INTEGER 4 0 No NAME SYSIBM VARCHAR 255 0 No SIZE SYSIBM INTEGER 4 0 No DATA SYSIBM BLOB 2097152 0 Yes CONTENT_TYPE SYSIBM VARCHAR 255 0 No 5 record(s) selected. |
| D:\>db2 select * from schema_info VERSION ----------- 1 1 record(s) selected. |
| 列名 | 数据类型 | 描述 |
| ID | Integer | 主键 |
| Name | VARCHAR | 文档的名称 |
| Size | Integer | 文件大小 |
| Data | BLOB | 以二进制模式存储的文件,最大 2 M |
| Content_type | VARCHAR | 文档类型,包括:.doc、.ppt、.pdf、.sw、.exe、.a、.so、.gif、.jpeg、.mov、.avi、.mpeg、.bmp 等 |
| Created_at | TIMESTAMP | 文件被上传的时间(见注 9) |
| Updated_at | TIMESTAMP | 文档最近更新时间戳(见注 9) |
| Platform | VARCHAR | 特定于文件平台的信息 |
| D:\rails\teamroom>ruby script/generate migration add_docs_attributes exists db/migrate create db/migrate/002_add_docs_attributes.rb |
| class AddDocsAttributes < ActiveRecord::Migration def self.up add_column :documents, :created_at, :timestamp add_column :documents, :updated_at, :timestamp add_column :documents, :platform, :string, :limit => 10 end def self.down remove_column :documents, :created_at remove_column :documents, :updated_at remove_column :documents, :platform end end |
| D:\rails\teamroom>rake db:migrate (in D:/rails/teamroom) == AddDocsAttributes: migrating =============================================== -- add_column(:documents, :created_at, :timestamp) -> 0.0500s -- add_column(:documents, :updated_at, :timestamp) -> 0.0100s -- add_column(:documents, :platform, :string, {:limit=>10}) -> 0.0000s == AddDocsAttributes: migrated (0.0600s) ====================================== |
| class CreateUsersTable < ActiveRecord::Migration def self.up create_table :users do |t| t.column :usertype, :string, :limit => 5, :null => false t.column :firstname, :string, :limit => 30 t.column :lastname, :string, :limit => 30 t.column :extension, :string, :limit => 4 end add_column :documents, :user_id, :integer end def self.down drop_table :users remove_column :documents, :user_id end end |
| D:\rails\teamroom>rake db:migrate (in D:/rails/teamroom) == CreateUsersTable: migrating ================================================ -- create_table(:users) -> 0.1400s -- add_column(:documents, :user_id, :integer) -> 0.0000s == CreateUsersTable: migrated (0.1400s) ======================================= |
| D:\rails\teamroom>ruby script/generate scaffold document exists app/controllers/ exists app/helpers/ create app/views/documents create app/views/layouts/ create test/functional/ dependency model create app/models/ exists test/unit/ exists test/fixtures/ create app/models/document.rb create test/unit/document_test.rb create test/fixtures/documents.yml create app/views/documents/_form.rhtml create app/views/documents/list.rhtml create app/views/documents/show.rhtml create app/views/documents/new.rhtml create app/views/documents/edit.rhtml create app/controllers/documents_controller.rb create test/functional/documents_controller_test.rb create app/helpers/documents_helper.rb create app/views/layouts/documents.rhtml create public/stylesheets/scaffold.css |
| D:\rails\teamroom>ruby script/server => Booting WEBrick... => Rails application started on http://0.0.0.0:3000 => Ctrl-C to shutdown server; call with --help for options [2007-04-26 16:54:57] INFO WEBrick 1.3.1 [2007-04-26 16:54:57] INFO ruby 1.8.5 (2006-12-25) [i386-mswin32] [2007-04-26 16:54:57] INFO WEBrick::HTTPServer#start: pid=444 port=3000 |
| class Document < ActiveRecord::Base belongs_to :user end |
| class User < ActiveRecord::Base has_many :document end |
| class CreateSubjectsTable < ActiveRecord::Migration def self.up create_table :subjects do |t| t.column :name, :string, :limit => 20 t.column :size, :integer t.column :description, :text t.column :tag, :string, :limit => 10 end add_column :documents, :subject_id, :integer end def self.down drop_table :subjects remove_column :documents, :subject_id end end |
| class Subject < ActiveRecord::Base has_many :document end |
| class Document < ActiveRecord::Base belongs_to :user belongs_to :subject <... code to assist with document uploading ...> <... ...> end end |
| class CreateSubscriptionsTable < ActiveRecord::Migration def self.up create_table :subscriptions do |t| t.column :name, :string, :limit => 20 t.column :description, :text t.column :user_id, :integer end add_column :subjects, :subscription_id, :integer end def self.down drop_table :subscriptions remove_column :subjects, :subscription_id end end |
| class AddEmailToUser < ActiveRecord::Migration def self.up add_column :users, :email, :string, :limit => 30 end def self.down remove_column :users, :email end end |
| class AddXmlDocColumn < ActiveRecord::Migration def self.up add_column :documents, :xmldata, :xml end # Currently, a column that is part of a table containing an XML column # cannot be dropped. To remove the column, the table must be dropped # and recreated without the previous XML column. def self.down drop_table :documents create_table :documents do |t| t.column :name, :string, :null => false t.column :size, :integer, :null => false t.column :data, :binary, :limit => 2.megabytes t.column :content_type, :string, :null => false t.column :created_at, :timestamp t.column :updated_at, :timestamp t.column :platform, :string, :limit => 10 t.column :user_id, :integer t.column :subject_id, :integer end end end |
| def zips @id = params[:id] @xmldata = Document.find_by_sql "select xmlquery(\ '<zipcodes>\ {for $i in $t/marketinfo/sales/customer/address\ where $i/city = \"Toronto\"\ return <zip>{$i/zip/text()}</zip>} \ </zipcodes>'\ passing c.xmldata as \"t\")\ from documents c where id = #{@id}" p @xmldata[0] redirect_to :action => 'list' end |
| Document.find_by_sql "select xmlquery('<zipcodes>\ {$t/marketinfo/sales/customer/address/zip[../city = \"Toronto\"]}\ </zipcodes>' passing c.xmldata as \"t\")\ from documents c where id = #{@id}" |
| D:\rails\teamroom>rake db:migrate VERSION=0 (in D:/rails/teamroom) == AddXmlDocColumn: reverting ================================================= -- drop_table(:documents) -> 0.0150s -- create_table(:documents) -> 0.1880s == AddXmlDocColumn: reverted (0.2030s) ======================================== == AddEmailToUser: reverting ================================================== -- remove_column(:users, :email) -> 0.1250s == AddEmailToUser: reverted (0.1250s) ========================================= == CreateSubscriptionsTable: reverting ======================================== -- drop_table(:subscriptions) -> 0.0000s -- remove_column(:subjects, :subscription_id) -> 0.1560s == CreateSubscriptionsTable: reverted (0.1560s) =============================== == CreateSubjectsTable: reverting ============================================= -- drop_table(:subjects) -> 0.0000s -- remove_column(:documents, :subject_id) -> 0.1570s == CreateSubjectsTable: reverted (0.1570s) ==================================== == CreateUsersTable: reverting ================================================ -- drop_table(:users) -> 0.0000s -- remove_column(:documents, :user_id) -> 0.1400s == CreateUsersTable: reverted (0.1400s) ======================================= == AddDocsAttributes: reverting =============================================== -- remove_column(:documents, :created_at) -> 0.1250s -- remove_column(:documents, :updated_at) -> 0.1870s -- remove_column(:documents, :platform) -> 0.1260s == AddDocsAttributes: reverted (0.4380s) ====================================== == CreateDocsStore: reverting ================================================= -- drop_table(:documents) -> 0.0000s == CreateDocsStore: reverted (0.0000s) ======================================== |