:meta-keywords: cubrid ruby driver, cubrid ruby api, ruby sample :meta-description: CUBRID Ruby driver implements the interface to enable access from applications in Ruby to CUBRID database server and official one is available as a RubyGem package. CUBRID Ruby driver is written based on CCI API. ************* Ruby 드ë¼ì´ë²„ ************* CUBRID Ruby 드ë¼ì´ë²„는 Ruby로 작성한 ì‘ìš© 프로그램ì—서 CUBRID ë°ì´í„°ë² ì´ìŠ¤ë¥¼ ì‚¬ìš©í• ìˆ˜ 있게 하는 드ë¼ì´ë²„로, RubyGem 패키지 형태로 ì œê³µëœë‹¤. CUBRID Ruby 드ë¼ì´ë²„는 CCI API를 기반으로 작성ë˜ì—ˆìœ¼ë¯€ë¡œ, CCI API ë° CCIì— ì ìš©ë˜ëŠ” **CCI_DEFAULT_AUTOCOMMIT** ê³¼ ê°™ì€ ì„¤ì • 파ë¼ë¯¸í„°ì— ì˜í–¥ì„ 받는다. .. FIXME: 별ë„로 Ruby 드ë¼ì´ë²„를 다운로드하거나 Ruby 드ë¼ì´ë²„ì— ëŒ€í•œ ìµœì‹ ì •ë³´ë¥¼ 확ì¸í•˜ë ¤ë©´ http://www.cubrid.org/wiki_apis/entry/cubrid-ruby-driver\ ì— ì ‘ì†í•œë‹¤. Ruby 설치 ë° ì„¤ì • ================= **기본 환경** * Ruby 1.8.7 ì´ìƒ * CUBRID gem * ActiveRecord gem **Linux** **gem** ì„ ì‚¬ìš©í•˜ì—¬ CUBRID Connector를 ì„¤ì¹˜í• ìˆ˜ 있다. 다ìŒê³¼ ê°™ì´ **sudo** ëª…ë ¹ì–´ì— **-E** ì˜µì…˜ì„ ì‚¬ìš©í•˜ì—¬ **sudo** ëª…ë ¹ì–´ê°€ CUBRID ë°ì´í„°ë² ì´ìФ 설치 경로 환경 변수를 변경하지 않ë„ë¡ í•´ì•¼ 한다. :: sudo -E gem install cubrid **Windows** ë‹¤ìŒ ëª…ë ¹ì–´ë¥¼ 실행하여 ìµœì‹ ë²„ì „ì˜ CUBRID Ruby 드ë¼ì´ë²„를 설치한다. :: gem install cubrid .. FIXME: .. note:: .. FIXME: Gem Installerê°€ 설치ë˜ì–´ 있지 않다면 http://www.cubrid.org/wiki_apis/entry/cubrid-ruby-driver-installation-instructions 를 ì°¸ê³ í•œë‹¤. Ruby ì˜ˆì œ 프로그램 ================== 여기ì—서는 Ruby로 CUBRID ë°ì´í„°ë² ì´ìŠ¤ì— ëŒ€í•œ ìž‘ì—…ì„ ìˆ˜í–‰í•˜ëŠ” ì˜ˆì œ í”„ë¡œê·¸ëž¨ì„ ìž‘ì„±í•œë‹¤. ì˜ˆì œë¡œ 다ìŒê³¼ ê°™ì´ í…Œì´ë¸”ì„ ìƒì„±í•œë‹¤. .. code-block:: sql CREATE TABLE countries( id INTEGER AUTO_INCREMENT, code CHARACTER VARYING(3) NOT NULL UNIQUE, name CHARACTER VARYING(40) NOT NULL UNIQUE, record_date DATETIME DEFAULT SYSDATETIME NOT NULL, CONSTRAINT pk_countries_id PRIMARY KEY(id) ); CREATE TABLE cities( id INTEGER AUTO_INCREMENT NOT NULL UNIQUE, name CHARACTER VARYING(40) NOT NULL, country_id INTEGER NOT NULL, record_date DATETIME DEFAULT SYSDATETIME NOT NULL, FOREIGN KEY (country_id) REFERENCES countries(id) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT pk_cities_id PRIMARY KEY(id) ); **ë¼ì´ë¸ŒëŸ¬ë¦¬ 로드** ì˜ˆì œ 프로그램으로 *tutorial.rb* ë¼ëŠ” 파ì¼ì„ ìƒì„±í•˜ê³ 다ìŒê³¼ ê°™ì€ ê¸°ë³¸ ì„¤ì •ì„ ìž‘ì„±í•œë‹¤. .. code-block:: ruby require 'rubygems' require 'active_record' require 'pp' **ë°ì´í„°ë² ì´ìФ ì—°ê²°** 다ìŒê³¼ ê°™ì´ íŒŒë¼ë¯¸í„°ë¥¼ ì •ì˜í•˜ì—¬ ë°ì´í„°ë² ì´ìФ ì—°ê²°ì„ ìƒì„±í•œë‹¤. .. code-block:: ruby ActiveRecord::Base.establish_connection( :adapter => "cubrid", :host => "localhost", :database => "demodb" , :user => "dba" ) **ë°ì´í„°ë² ì´ìŠ¤ì— ê°ì²´ 삽입** í…Œì´ë¸”ì„ ì¡°ìž‘í•˜ê¸° ì „ì— í…Œì´ë¸”ì„ ActiveRecordì˜ í´ëž˜ìŠ¤ì™€ 매핑해야 한다. .. code-block:: ruby class Country < ActiveRecord::Base end class City < ActiveRecord::Base end Country.create(:code => 'ROU', :name => 'Romania') Country.create(:code => 'HUN', :name => 'Hungary') Country.create(:code => 'DEU', :name => 'Germany') Country.create(:code => 'FRA', :name => 'France') Country.create(:code => 'ITA', :name => 'Italy', :record_date => Time.now) Country.create(:code => 'SPN', :name => 'Spain') **ë°ì´í„°ë² ì´ìФì—서 ë ˆì½”ë“œ 조회** 다ìŒê³¼ ê°™ì´ ë°ì´í„°ë² ì´ìФì—서 ë ˆì½”ë“œë¥¼ 조회한다. .. code-block:: ruby romania = Country.find(1) pp(romania) romania = Country.where(:code => 'ROU') pp(romania) Country.find_each do |country| pp(country) end **ë°ì´í„°ë² ì´ìФ ë ˆì½”ë“œ ê°±ì‹ ** 여기ì—서는 다ìŒê³¼ ê°™ì´ *Spain* ì˜ *code* 를 *'SPN'* ì—서 *'ESP'* 로 변경한다. .. code-block:: ruby Country.transaction do spain = Country.where(:code => 'SPN')[0] spain.code = 'ESP' spain.save end **ë°ì´í„°ë² ì´ìФ ë ˆì½”ë“œ ì‚ì œ** ë°ì´í„°ë² ì´ìŠ¤ì˜ ë ˆì½”ë“œë¥¼ ì‚ì œí•˜ëŠ” 코드는 다ìŒê³¼ 같다. .. code-block:: ruby Country.transaction do spain = Country.where(:code => 'ESP')[0] spain.destroy end **ì—°ê´€(association)ì„ ì´ìš©í•œ 작업** êµê°€ì— ë„시를 추가하는 방법 중 하나는 *Country* 를 조회하여 *Country* ì˜ *code* 를 새로운 *City* ê°ì²´ì— í• ë‹¹í•˜ëŠ” 것ì´ë‹¤. .. code-block:: ruby romania = Country.where(:code => 'ROU')[0] City.create(:country_id => romania.id, :name => 'Bucharest'); ë” ì¢‹ì€ ë°©ë²•ì€ ë‹¤ìŒê³¼ ê°™ì´ ActiveRecordì— ê´€ê³„ë¥¼ ì•Œë¦¬ê³ ì´ë¥¼ Country í´ëž˜ìŠ¤ì— ì„ ì–¸í•˜ëŠ” 것ì´ë‹¤. .. code-block:: ruby class Country < ActiveRecord::Base has_many :cities, :dependent => :destroy end class City < ActiveRecord::Base end 위 ì½”ë“œì— ë”°ë¼ í•œ êµê°€ëŠ” 여러 ê°œì˜ ë„시를 가질 수 있다. ì´ì œ 다ìŒê³¼ ê°™ì´ ê°„ë‹¨í•˜ê²Œ êµê°€ì— 새 ë„시를 ì¶”ê°€í• ìˆ˜ 있다. ì´ ë°©ë²•ì„ ì‚¬ìš©í•˜ë©´ ë„ì‹œì— ì ‘ê·¼í• ë•Œ 참조ë˜ëŠ” êµê°€ì˜ ëª¨ë“ ë„ì‹œë“¤ì„ ì–»ì„ ìˆ˜ 있으므로 ìœ ìš©í•˜ê²Œ ì‚¬ìš©í• ìˆ˜ 있다. .. code-block:: ruby italy = Country.where(:code => 'ITA')[0] italy.cities.create(:name => 'Milano'); italy.cities.create(:name => 'Napoli'); pp (romania.cities) pp (italy.cities) ë˜í•œ 다ìŒê³¼ ê°™ì€ ì½”ë“œë¡œ êµê°€ë¥¼ ì‚ì œí•˜ë©´ ê·¸ êµê°€ì˜ ëª¨ë“ ë„시가 ì‚ì œëœë‹¤. .. code-block:: ruby romania.destroy **ActiveRecord** 는 ì¼ëŒ€ì¼ì´ë‚˜ 다대다(many-to-many)와 ê°™ì€ ê´€ê³„ë„ ì§€ì›í•œë‹¤. **메타ë°ì´í„° 관리** ActiveRecord를 사용하면 코드를 ìˆ˜ì •í•˜ì§€ ì•Šì•„ë„ ë‹¤ë¥¸ ë°ì´í„°ë² ì´ìŠ¤ë¥¼ ì‚¬ìš©í• ìˆ˜ 있다. **ë°ì´í„°ë² ì´ìФ 구조 ì •ì˜** **ActiveRecord::Schema.define** ì„ ì‚¬ìš©í•˜ì—¬ 새 í…Œì´ë¸”ì„ ì •ì˜í• 수 있다. 예를 들면 다ìŒê³¼ ê°™ì´ ì¼ëŒ€ë‹¤(one-to-many)로 대ì‘ë˜ëŠ” ì±…ì— ëŒ€í•œ í…Œì´ë¸”(*books*)ê³¼ ì €ìžì— 대한 í…Œì´ë¸”(*authors*)ì„ ìƒì„±í• 수 있다. .. code-block:: ruby ActiveRecord::Schema.define do create_table :books do |table| table.column :title, :string, :null => false table.column :price, :float, :null => false table.column :author_id, :integer, :null => false end create_table :authors do |table| table.column :name, :string, :null => false table.column :address, :string table.column :phone, :string end add_index :books, :author_id end CUBRIDì—서 ì§€ì›í•˜ëŠ” 칼럼 íƒ€ìž…ì€ **:string**, **:text**, **:integer**, **:float**, **:decimal**, **:datetime**, **:timestamp**, **:time**, **:boolean**, **:bit**, **:smallint**, **:bigint**, **:char** ì´ë‹¤. 현재 **:binary** 는 ì§€ì›í•˜ì§€ 않는다. **í…Œì´ë¸” 칼럼 관리** **ActiveRecord::Migration** ì˜ ê¸°ëŠ¥ì„ ì‚¬ìš©í•˜ì—¬ í…Œì´ë¸”ì˜ ì¹¼ëŸ¼ì„ ì¶”ê°€í•˜ê±°ë‚˜ ì—…ë°ì´íЏ, ì‚ì œí• ìˆ˜ 있다. .. code-block:: ruby ActiveRecord::Schema.define do create_table :todos do |table| table.column :title, :string table.column :description, :string end change_column :todos, :description, :string, :null => false add_column :todos, :created, :datetime, :default => Time.now rename_column :todos, :created, :record_date remove_column :todos, :record_date end **ë°ì´í„°ë² ì´ìФ 스키마 ë¤í”„** **ActiveRecord::SchemaDumper.dump** 를 사용하여 현재 사용 ì¤‘ì¸ ìŠ¤í‚¤ë§ˆì˜ ì •ë³´ë¥¼ ë¤í”„í• ìˆ˜ 있다. ë¤í”„ëœ ìŠ¤í‚¤ë§ˆ ì •ë³´ëŠ” 플랫í¼ê³¼ ìƒê´€ì—†ì´ ì‚¬ìš©í• ìˆ˜ 있는 형ì‹ìœ¼ë¡œ ì €ìž¥ë˜ë©° Ruby ActiveRecordì—ì„œë„ ì‚¬ìš©í• ìˆ˜ 있다. 단, **:bigint**, **:bit** 등과 ê°™ì´ íŠ¹ì • ë°ì´í„°ë² ì´ìФì—서 사용ë˜ëŠ” 커스텀 칼럼 íƒ€ìž…ì„ ì‚¬ìš©í•œë‹¤ë©´ ì œëŒ€ë¡œ ë™ìž‘하지 ì•Šì„ ìˆ˜ 있다. **서버 용량 ì •ë³´ íšë“** 현재 ì—°ê²°ì—서 다ìŒê³¼ ê°™ì´ ë°ì´í„°ë² ì´ìФ ì •ë³´ë¥¼ íšë“í• ìˆ˜ 있다. .. code-block:: ruby puts "Maximum column length : " + ActiveRecord::Base.connection.column_name_length.to_s puts "SQL statement maximum length : " + ActiveRecord::Base.connection.sql_query_length.to_s puts "Quoting : '''test''' : " + ActiveRecord::Base.connection.quote("'''test'''") **ë°ì´í„°ë² ì´ìФ ìƒì„±** CUBRIDì—서는 ë°ì´í„°ë² ì´ìФ ìƒì„±ì„ **cubrid create** ìœ í‹¸ë¦¬í‹° ëª…ë ¹ì–´ë¡œë§Œ 처리하기 때문ì—, 프로그램 ë‚´ì—서는 ë°ì´í„°ë² ì´ìŠ¤ë¥¼ ìƒì„±í• 수 없다. .. code-block:: ruby ActiveRecord::Schema.define do create_database('not_supported') end Ruby API ======== http://ftp.cubrid.org/CUBRID_Docs/Drivers/Ruby/\ 를 ì°¸ê³ í•œë‹¤.