OCI8::CLOB
CLOB オブジェクトを読み書きするための I/O クラスです。 CLOB のデータをテーブルへ挿入したり、select 文で取り出したりできます。
con = OCI8.new('scott', 'tiger')
# テーブルの作成
con.exec("CREATE TABLE novels (title VARCHAR2(30), contents CLOB)")
# CLOB オブジェクトの挿入
con.exec("INSERT INTO novels(title, contents) values (:1, :2)",
'a long tale', OCI8::CLOB.new(con, 'long long text ...'))
# CLOB オブジェクトを select
con.exec("select contents from novels where title = :1",
'a long tale') do |row|
# row[0] is a CLOB object.
contents = row[0].read
end
クラスメソッド
OCI8::CLOB.new(con, val)
(new in 1.0.0-rc2)
テンポラリLOBの作成。ここでは"テンポラリ(一時的な)"はデータベースに保存されてない状態を意味する。データをテンポラリLOBに追加してから、テーブルへ挿入することができます。
インスタンスメソッド
available?
CLOBが使用可能かどうかをチェックする。
con.exec("CREATE TABLE novels (title VARCHAR2(30), contents CLOB)")
con.exec("INSERT INTO novels VALUES ('null-data', NULL)")
con.exec("INSERT INTO novels VALUES ('empty-data', EMPTY_CLOB())")
con.exec("SELECT title, contents FROM novels") do |title, contents|
case title
when 'null-data'
puts "#{title} => #{contents.available?.to_s}"
# => false
when 'empty-data'
puts "#{title} => #{contents.available?.to_s}"
# => true
end
end
read(size = nil)
CLOB から文字データを取得する。文字数は最大で引数で指定された size までです。size が nil のときは、CLOB の最後まで読み込みます。
注意点: size はバイト単位ではなく、文字単位です。戻り値にマルチバイト文字が含まれている場合、戻り値のバイト数は size よりも長くなることがあります。
# read CLOB data.
con.exec("SELECT title, contents FROM novels") do |title, contents|
File.open(title, 'w') do |f|
f.write(contents.read)
end
end
write(string)
write the given string to CLOB. The return value is the number of written characters. If old data is longer than new data, resize by OCI8::CLOB#size.
# write to CLOB.
con.exec("SELECT title, contents FROM novels") do |title, clob|
File.open(title, 'r') do |f|
contents = f.read
num_chars = clob.write(contents)
clob.size = num_chars
end
end
note: OCI8::CLOB#size is counted by characters. If the contents contain multibyte characters, the following code set incorrect clob size.
# bad example
con.exec("SELECT title, contents FROM novels") do |title, clob|
File.open(title, 'r') do |f|
contents = f.read
clob.write(contents)
clob.size = contents.length # boo!
end
end
size
return the number of characters in the CLOB.
size=(len)
set the number of characters in the CLOB.
pos
return the current offset of the CLOB in characters.
pos = number
set the current offset of the CLOB in characters.
eof?
true if the current offset is at the end of the CLOB.
chunk_size
return the chunk size of CLOB.
truncate(len)
synonym for size=.
tell
synonym for pos.
seek(pos)
synonym for pos=.
rewind
set the current offset to zero.
Keyword(s):
References:[SideMenu] [ruby-oci8 API]