Ruby-aci类说明¶
STACI类¶
常量¶
类方法¶
.charset_id2name¶
.charset_id2name(charset_id) => String
如果id有效,则返回指定的字符集ID的字符集名称。
require 'staci'
cname=STACI.charset_id2name(854)
puts "charset name : #{cname}"
执行结果:
charset name : ZHS32GB18030
.charset_name2id¶
.charset_name2id(charset_name) => Integer
如果有效,则返回指定的字符集名称的字符集ID。
require 'staci'
cid=STACI.charset_name2id('ZHS32GB18030')
puts "charset id : #{cid}"
执行结果:
charset id : 854
实例方法¶
STACI的实例其实就是创建的连接对象,常规定义为conn。
autocommit¶
#autocommit=(autocommit_mode) => Object
设置自动提交事务状态。
直接调用autocommit方法返回当前事务模式:
irb(main):026:0> conn.autocommit => false
执行autocommit时设置事务状态:
irb(main):027:0> conn.autocommit=true => true
describe_table¶
#describe_table(table_name, table_only = false) => STACI::Metadata::Table or STACI::Metadata::View
获得表或视图的信息。
describe_synonym¶
#describe_synonym(synonym_name, check_public_also = true) STACI::Metadata::Synonym
获得同义词的信息
构造方法¶
Cursor类¶
该类的实例对应于数据库术语中的游标,对应Perl/DBI的语句句柄$sth。不要通过调用“new”方法来创建实例。请通过调用STACI#exec或STACI#parse来创建它。
实例方法¶
[](key)¶
#[](key) => Object
获取绑定变量的值。当绑定变量由#bind_param显式绑定时,下标键必须与传递给#bind_param的参数键相同。当它们被STACI#exec或#exec隐式绑定时,下标键是从一开始的位置。返回绑定对应的值。
举例:
按参数名称显示绑定
cursor = conn.parse("BEGIN :out := 'BAR'; END;")
cursor.bind_param(:out, 'FOO') # bind by name
p cursor[:out] # => 'FOO' - The subscript must be :out.
cursor.exec()
p cursor[:out] # => 'BAR'
按参数位置显示绑定
cursor = conn.parse("BEGIN :out := 'BAR'; END;")
cursor.bind_param(1, 'FOO') # bind by position
p cursor[1] # => 'FOO' - The subscript must be 1.
cursor.exec()
p cursor[1] # => 'BAR'
按参数位置隐式绑定
cursor = conn.exec("BEGIN :out := 'BAR'; END;", 'FOO')
# 1st bind variable is bound as String with width 3. Its initial value is 'FOO'
# After execute, the value become 'BAR'.
p cursor[1] # => 'BAR'
[]=(key, val)¶
#[]=(key, val) => Object
更改绑定变量值。当绑定变量由#bind_param显式绑定时,下标键必须与传递给#bind_param的参数键相同。当它们被STACI#exec或#exec隐式绑定时,下标键是从一开始的位置。
举例如下:
# Inserts three rows whose values are 'FOO', 'BAR' and 'BAZ.'
cursor = conn.parse("INSERT INTO test(col1) VALUES(:1)")
begin
cursor.bind_params(1, nil, String, 3)
['FOO', 'BAR', 'BAZ'].each do |column_value|
cursor[1] = column_value # Change the bind value
cursor.exec # and insert it.
end
ensure
cursor.close()
end
# This makes same result with the following but is more efficient.
#
# ['FOO', 'BAR', 'BAZ'].each do |column_value|
# conn.exec("INSERT INTO test(col1) VALUES(:1)", column_value)
# end
#
bind_param¶
#bind_param(key, param, type = nil, length = nil) => Object
显式绑定变量。当key为number时,它按位置绑定,位置从1开始。当key为string时,它按占位符的名称绑定。
举例:
cursor = conn.parse("SELECT * FROM emp WHERE ename = :ename")
cursor.bind_param(1, 'SMITH') # bind by position
# ...or...
cursor.bind_param(':ename', 'SMITH') # bind by name
要绑定为数字,请将数字intself设置为val。如果其初始值为NULL,请将nil设置为type,将Integer、Float或OraNumber设置为val。
举例如下:
cursor.bind_param(1, 1234) # 绑定一个整形,初始值为1234
cursor.bind_param(1, 1234.0) # 绑定一个浮点数,初始值为1234.0.
cursor.bind_param(1, nil, Integer) # 绑定一个整形,初始值为NULL.
cursor.bind_param(1, nil, Float) #绑定一个浮点数,初始值为 NULL.
cursor.bind_param(1, OraNumber(1234)) # 绑定一个 OraNumber,初始值为1234.
cursor.bind_param(1, nil, OraNumber) #绑定一个 OraNumber,初始值为 NULL.
在绑定字符串的情况下,将字符串本身设置为val。当绑定变量用作输出时,设置长度足以存储或设置长度的字符串。
举例如下:
cursor = conn.parse("BEGIN :out := :in || '_OUT'; END;")
cursor.bind_param(':in', 'DATA') # 绑定4个长度的字符串.
cursor.bind_param(':out', nil, String, 7) # 绑定7个长度的字符串.
cursor.exec()
p cursor[':out'] # => 'DATA_OU'
# Though the length of :out is 8 bytes in PL/SQL block, it is
# bound as 7 bytes. So result is cut off at 7 byte.
如果将字符串绑定为RAW,请将STACI::RAW设置为type,举例如下:
cursor = conn.parse("INSERT INTO raw_table(raw_column) VALUE (:1)")
cursor.bind_param(1, 'RAW_STRING', STACI::RAW)
cursor.exec()
cursor.close()
bind_param_array¶
#bind_param_array(key, var_array, type = nil, max_item_length = nil) => Object
显式绑定数组.当key为number时,它按位置绑定,位置从1开始。当key为string时,它按占位符的名称绑定。max_array_size参数必须设置在调用bind_param_array之前。
举例如下:
cursor = conn.parse("INSERT INTO test_table VALUES (:str)")
cursor.max_array_size = 3
cursor.bind_param_array(1, ['happy', 'new', 'year'], String, 30)
cursor.exec_array
column_metadata¶
#column_metadata => Array of STACI::Metadata::Column
获取select语句的STACI::Metadata::Column 的数组。
举例:
cursor = conn.exec('select * from tab')
puts ' Name Type'
puts ' ----------------------------------------- ----------------------------'
cursor.column_metadata.each do |colinfo|
puts format(' %-41s %s',
colinfo.name,
colinfo.type_string)
end
define¶
#define(pos, type, length = nil) => Object
明确指出获取值的日期类型。在parse 和exec中运行此方法。pos从1开始。当类型为String时使用lentgh。
举例:
cursor = conn.parse("SELECT ename, hiredate FROM emp")
cursor.define(1, String, 20) # 第一列用一个20个长度字符串获取.
cursor.define(2, Time) # 第二列用一个Time类型.
cursor.exec()
exec¶
#exec(*bindvars) => Object
执行分配给游标的SQL语句。返回值的类型取决于sql语句的类型:select,insert, update delete, create, alter, drop 或PL/SQL。对于select语句,它返回查询结果的列数。对于insert、update或delete语句,它返回已处理的行数。对于create、alter、drop和PL/SQL语句,它返回true。与STACI#exec相反,即使PL/SQL也返回true。显式使用STACI::Cursor#[]获取绑定变量。
fetch¶
#fetch => Array
获取获取的数据作为数组。这仅适用于select语句。
举例如下:
conn = STACI.new('scott', 'tiger')
cursor = conn.exec('SELECT * FROM emp')
while r = cursor.fetch()
puts r.join(',')
end
cursor.close
conn.logoff
max_array_size¶
#max_array_size=(size) => Object
为 bind_param_array方法设置最大数组大小。
设置max_array_size时会清零掉cursor对象中的绑定对象。
max_array_size应于用户实际传入的绑定数组保存一致。
备注:所有绑定数组的大小都必须相同
rowid¶
#rowid => String
获取上次插入、更新或删除的行的rowid。这不能用于select语句。
举例如下:
cursor = conn.parse('INSERT INTO foo_table values(:1, :2)', 1, 2)
cursor.execcursor.rowid # => "AAAFlLAAEAAAAG9AAA", the inserted row's rowid
#statement => String
获得只是SQL的语句字符串。
举例如下:
cursor = conn.parse("select * from country where country_code = 'ja'")
cursor.statement # => "select * from country where country_code = 'ja'"
type¶
#type => Object
获得执行SQL的语句类型,支持如下:
- STACI::STMT_SELECT
- STACI::STMT_UPDATE
- STACI::STMT_DELETE
- STACI::STMT_INSERT
- STACI::STMT_CREATE
- STACI::STMT_DROP
- STACI::STMT_ALTER
- STACI::STMT_BEGIN
- STACI::STMT_DECLARE
- STACI::STMT_MERGE
ConnectionPool类¶
连接池是由多个会话使用一组(池)可重用的物理连接来平衡负载。
注意,这与通常所说的连接池不同。通常,连接池在池中缓存连接。当应用程序需要新连接时,会从池中获取连接。这样可以减少建立连接的时间。然而,ruby-aci中的连接池与上述不同。
数据库的连接分为两层:一层是物理连接,如TCP/IP。另一种是应用程序使用的逻辑连接。ruby-aci中的连接池在池中缓存物理连接,而不是逻辑连接。当应用程序需要新连接时,通过物理连接创建逻辑连接,与通常称为连接池不同,物理连接需要时间来建立连接。当应用程序通过逻辑连接发送查询时,将从池中获取物理连接。查询完成后,物理连接自动返回到池。只要按顺序使用逻辑连接,就只使用一个物理连接。当应用程序一次发送查询时,它需要多个物理连接,因为一个物理连接不能一次传输多个查询。
举例如下:
# Create a connection pool.
# The number of initial physical connections: 1
# The number of maximum physical connections: 5
# the connection increment parameter: 2
# username and password are required to establish an implicit primary session.
cpool = STACI::ConnectionPool.new(1, 5, 2, username, password, database)
# The number of physical connections: 1
# The number of logical connections: 0
# Create a session.# Pass the connection pool to the third argument.
conn1 = STACI.new(username, password, cpool)
# One logical connection was created.
# The number of physical connections: 1
# The number of logical connections: 1
# Create another session.
conn2 = STACI.new(username, password, cpool)
# Another logical connection was created.
# The number of physical connections: 1
# The number of logical connections: 2
# Use conn1 and conn2 sequentially
conn1.exec('...')
conn2.exec('...')
# Both logical connections use one physical connection.
# The number of physical connections: 1
# The number of logical connections: 2
thr1 = Thread.new do
conn1.exec('...')
end
thr2 = Thread.new do
conn2.exec('...')
end
thr1.join
thr2.join
# Logical connections cannot use one physical connection at a time.
# So that the number of physical connections is incremented.
# The number of physical connections: 3
# The number of logical connections: 2
conn1.logoff
# One logical connection was closed.
# The number of physical connections: 3
# The number of logical connections: 1
conn2.logoff
# All logical connections were closed.
# The number of physical connections: 3
# The number of logical connections: 0
Metadata类¶
STACI具有获取数据库对象信息的方法,如表、视图、过程、函数等。获取的数据称为元数据,并作为STACI::metadata::Base的子类的实例进行检索。
举例如下:
conn = STACI.new('username/passord')
table = conn.describe_table('scott.emp')
table.columns.each do |col|
puts "#{col.name} #{col.data_type_string}"
end
注解
Metadata类不需要开发者去实例化,而是通过其他方法返回Metadata类的实例。conn实例下有多个方法生成Metadata类的实例。
BindType模块¶
类方法¶
Base子类¶
类方法
create
.create(con, val, param, max_array_size) => Object
不显示调用。
实例方法
get
#get => Object
不显示调用。
set
#set(val) => Object
不显示调用。
DateTime子类¶
–STACI::BindType::DateTime++这是一个助手类,用于选择或绑定数据库数据类型,例如日期、时间戳、带时区的时间戳和带本地时区的时间戳。检索到的值是DateTime。
如何选择数据时间值。
默认情况下,选择DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE and TIMESTAMP WITH LOCAL TIME ZONE。通过显式调用STACI::Cursor#define来更改行为,如下所示:
cursor = conn.parse("SELECT hiredate FROM emp")
cursor.define(1, nil, DateTime)
cursor.exec()
否则,您可以更改所有查询的默认映射。
# Changes the mapping for DATE
STACI::BindType::Mapping[STACI::SQLT_DAT] = STACI::BindType::DateTime
# Changes the mapping for TIMESTAMP
STACI::BindType::Mapping[STACI::SQLT_TIMESTAMP] = STACI::BindType::DateTime
# Changes the mapping for TIMESTAMP WITH TIME ZONE
STACI::BindType::Mapping[STACI::SQLT_TIMESTAMP_TZ] = STACI::BindType::DateTime
# Changes the mapping for TIMESTAMP WITH LOCAL TIME ZONE
STACI::BindType::Mapping[STACI::SQLT_TIMESTAMP_LTZ] = STACI::BindType::DateTime
默认时区注释
如果检索到的值的数据类型是DATE, TIMESTAMP or TIMESTAMP WITH LOCAL TIME ZONE,则其时区由会话时区确定。
默认情况下,会话时区与本地计算机相同。它由以下SQL更改。
ALTER SESSION SET TIME_ZONE='-05:00'
实例方法
get
#get => Object
set
#set(val) =>Object
Float子类¶
所有方法都继承自BindType::Base
Integer子类¶
所有方法都继承自BindType::Base
IntervalDS子类¶
这是一个助手类,用于选择或绑定数据类型的时间间隔。检索到的值是作为浮点的两个TypeStamp之间的秒数。
注意,如果STACI::BindType::IntervalDS,则它是作为有理数的天数。单位为:天。
如何将间隔日绑定到秒
不能隐式地将绑定变量绑定为间隔日到秒。它必须由STACI::Cursor#bind_param显式绑定。
# output bind variable
cursor = conn.parse(<<-EOS)
BEGIN
:interval := (:ts1 - :ts2) DAY TO SECOND(9);
END;
EOS
cursor.bind_param(:interval, nil, :interval_ds)
cursor.bind_param(:ts1, DateTime.parse('1969-11-19 06:54:35 00:00'))
cursor.bind_param(:ts2, DateTime.parse('1969-07-20 20:17:40 00:00'))
cursor.execcursor[:interval] # => 10492615.0 seconds
cursor.close
# input bind variable
cursor = conn.parse(<<-EOS)
BEGIN
:ts1 := :ts2 + :interval;
END;
EOS
cursor.bind_param(:ts1, nil, DateTime)
cursor.bind_param(:ts2, DateTime.parse('1969-07-20 20:17:40 00:00'))
cursor.bind_param(:interval, 10492615.0, :interval_ds)
cursor.execcursor[:ts1].strftime('%Y-%m-%d %H:%M:%S') # => 1969-11-19 06:54:35
cursor.close
属性
@@hour
@@minute
@@sec
@@fsec
@@unit
类方法
unit
.unit => :second or :day
检索间隔单位。
unit=
.unit=(val) => Object
更改间隔单位:第二个是默认值。
实例方法
get
#get => Object
不显示调用
set
#set(val) => Object
不显示调用
IntervalYM子类¶
–STACI::BindType::IntervalYM++
这是一个助手类,用于选择或绑定数据库数据类型间隔(从年到月)。检索到的值是两个时间戳之间的月数。
该值可应用于DateTime#>>以转换月份。如果已加载ActiveSupport,则可以将其应用于时间#months_。
如何选择年到月的间隔:
IntervalYM选择为整数,比如:
conn.exec("select (current_timestamp - hiredate) year to month from emp") do |hired_months|
puts "hired_months = #{hired_months}"
end
如何将间隔年绑定到月
不能隐式地将绑定变量绑定为年到月的间隔。它必须由OCI8::Cursor#bind_param显式绑定。
# output bind variablecursor = conn.parse(<<-EOS)
BEGIN
:interval := (:ts1 - :ts2) YEAR TO MONTH;
END;
EOS
cursor.bind_param(:interval, nil, :interval_ym)
cursor.bind_param(:ts1, DateTime.parse('1969-11-19 06:54:35 00:00'))
cursor.bind_param(:ts2, DateTime.parse('1969-07-20 20:17:40 00:00'))
cursor.execcursor[:interval] # => 4 (months)
cursor.close
# input bind variable
cursor = conn.parse(<<-EOS)
BEGIN
:ts1 := :ts2 + :interval;
END;
EOS
cursor.bind_param(:ts1, nil, DateTime)
cursor.bind_param(:ts2, Date.parse('1969-11-19'))
cursor.bind_param(:interval, 4, :interval_ym)
cursor.execcursor[:ts1].strftime('%Y-%m-%d') # => 1970-03-19
cursor.close
实例方法
get
#get => Object
不显示调用
set
#set(val) => Object
不显示调用
Object子类¶
实例方法
get
#get => Object
不显示调用
get_orig
#get_orig => Object
不显示调用
set
#set(val) => Object
不显示调用
OraDate子类¶
这是一个助手类,用于将OraDate绑定为数据库的日期数据类型。
OraNumber子类¶
这是一个助手类,用于将OraNumber绑定为数据库的number类型。
String子类¶
属性
@@minimum_bind_length
最大绑定长度
类方法
create
.create(con, val, param, max_array_size) => Object
minimum_bind_length?
.minimum_bind_length?=> Object
minimum_bind_length
.minimum_bind_length=(val) => Object
Time子类¶
–STACI::BindType::Time++这是一个助手类,用于选择或绑定数据库数据类型,例如DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE and TIMESTAMP WITH LOCAL TIME ZONE。检索到的值是时间。
如何查询Time值
DATE,TIMESTAMP,TIMESTAMP WITH TIME ZONE 和 TIMESTAMP WITH LOCAL TIME ZONE这些类型可以作为Time值的查询对象,可以通过显式调用STACI::Cursor#define将其作为时间进行选择,如下所示:
cursor = conn.parse("SELECT hiredate FROM emp")
cursor.define(1, nil, Time)
cursor.exec()
实例方法
get
#get => Object
不显示调用
set
#set(val) => Object
不显示调用
Util模块¶
属性
@@datetime_fsec_base
@@default_timezone
类方法
default_timezone
.default_timezone => Object
default_timezone
.default_timezone=(tz) => Object
Math模块¶
STACI::Math模块包含用于基本三角函数和超越函数的模块函数。其精度与OraNumber相同。
属性:¶
PI¶
圆周率
举例如下:
irb(main):008:0> STACI::Math::PI
=> #<OraNumber:3.1415926535897932384626433832795028842>
类方法:¶
Util模块¶
dll_path¶
.dll_path => String
返回加载的aci库的路径。
比如:
STACI::Util.dll_path
执行结果:
irb(main):004:0> STACI::Util.dll_path
=> "/opt/ShenTong/bin/libaci.so"
ACIException类¶
ruby-aci引发的所有异常的超类。以下异常定义为ACIException的子类。当调用接口函数返回错误状态时,会引发这些异常。
- ACIBreak
- ACIError
- ACISuccessWithInfo
- ACINoData(他是 ACIException 的子类)
- ACIInvalidHandle
Break类¶
ACIException的子类,当SQL执行被STACI#break取消时引发。
ACIError类¶
ACIException的子类,以下异常被定义为ACIERROR的子类:
- ACISccessWithInfo
- ACINoData(在ruby-aci 2.0之前,它一直是ACIException的子类,而不是ACIError)
当底层调用接口出现数据库错误代码(如ORA-00001)时引发。
实例方法¶
initialize¶
#initialize(message, error_code = nil, sql_stmt = nil, parse_error_offset = nil) => ACIError
按参数生成一个ACIError的一个实例,比如
ACIError.new("ORA-00001: unique constraint (%s.%s) violated", 1, 'insert into table_name values (1)', )
# => #<ACIError: ORA-00001: unique constraint (%s.%s) violated>
#<ACIError: ORA-00923: FROM keyword not found where expected>
"select sysdate"
923
14
#initialize(error_code, *params) => ACIError
创建一个新的ACIERROR对象,其中包含与指定的数据库错误代码相对应的错误消息。
ACINoData类¶
ACIError子类。在ruby-aci 2.0之前,它一直是ACIException的子类。在执行PL/SQL时返回NO_DATA_FOUND会报这个异常。
ACISuccessWithInfo类¶
ACIError子类。
ACIInvalidHandle类¶
ACIException的子类,当传递给底层调用接口的句柄无效时引发。
ACIHandle类¶
用于实现ACI处理数据类型和ACI描述符数据类型的常见行为的抽象基类。
LOB类¶
STACI::LOB是一个I/O对象。LOB内容通过关联连接从数据库读取和写入。当连接关闭时,关联的LOB也会关闭。这是大对象数据类型的抽象基类;BFILE、BLOB、CLOB。
CLOB类¶
处理CLOB数据的类。
实例方法¶
initialize¶
#initialize(conn, contents = nil) => STACI::CLOB constructor
当contents 不为nil时创建临时CLOB。否则,它将创建一个未初始化的lob,该lob在内部用于获取CLOB列数据。
举例如下:
# Inserts a file name and its contents as CLOB.
file_name="/opt/1.txt"
clob = STACI::CLOB.new(conn, File.read(file_name))
conn.exec('insert into file_contents values (:1, :2)', file_name, clob)
BLOB类¶
处理BLOB数据的类。
实例方法¶
initialize¶
#initialize(conn, contents = nil) => STACI::BLOB constructor
当contents 不为nil时创建临时BLOB。否则,它将创建一个未初始化的lob,该lob在内部用于获取BLOB列数据。
举例如下:
# Inserts a file name and its contents as BLOB.
blob = STACI::BLOB.new(conn, File.read(file_name, :mode => 'rb'))
conn.exec('insert into file_contents values (:1, :2)', file_name, blob)
BFILE类¶
处理BFILE数据的类,它是只读的LOB类型,你不能更新内容。
你可以在数据库端创建BFILE的文件:
CREATE DIRECTORY file_storage_dir AS '/opt/file_storage';
/opt/file_storage目录下生成一个文件:
echo 'Hello World!' > /opt/file_storage/hello_world.txt
用ruby-aci读取bfile数据如下:
require 'staci'
# The user must have 'READ ON DIRECTORY file_storage_dir' privilege.
conn = STACI.new('username/password')
# The second argument is usually an uppercase string unless the directory
# object is explicitly created as *double-quoted* lowercase characters.
bfile = STACI::BFILE.new(conn, 'FILE_STORAGE_DIR', 'hello_world.txt')
bfile.read # => "Hello World!\n"
OraDate类¶
OraDate是与数据库DATE数据类型兼容的ruby类。范围在公元前4712年到公元9999年之间。
类方法¶
now¶
.now=> OraDate
返回初始化为当前本地时间的OraDate对象。
比如:
OraDate.now.to_s
执行结果:
irb(main):018:0> OraDate.now.to_s
=> "2022/07/25 18:00:07"
构造方法¶
initialize¶
#initialize(year = 1, month = 1, day = 1, hour = 0, min = 0, sec = 0) => Object
返回初始化为指定日期和时间的OraDate对象。
比如:
OraDate.new # => 0001-01-01 00:00:00
OraDate.new(2012) # => 2012-01-01 00:00:00
OraDate.new(2012, 3, 4) # => 2012-03-04 00:00:00
OraNumber类¶
OraNumber是ACI NUMBER数据类型的ruby表示。没有精度和比例指示器。
类方法¶
构造方法¶
initialize¶
#initialize(expr = nil, fmt = nil, nlsparam = nil) => Object
从expr 表达式中创建OraNumber的值。表达式可以是数值或字符串值。如果是字符串值,则将可选的fmt和nlsparam用作SQL函数TO_NUMBER。
比如:
# Numeric expr
OraNumber.new(123456.789) # -> 123456.789
# String expr
OraNumber.new('123456.789') # => #<OraNumber:123456.789>
# String expr with fmt
OraNumber.new('123,456.789', '999,999,999.999') # => #<OraNumber:123456.789>
# String expr with fmt and nlsparam
OraNumber.new('123.456,789', '999G999G999D999', "NLS_NUMERIC_CHARACTERS = ',.'") # => #<OraNumber:123456.789>
实例方法¶
*¶
#*(other) => Numeric
返回self和other的乘积。当other的类是Integer时,它返回一个OraNumber值。否则,它将返回与其他类相同的值。
比如:
OraNumber(2) \* 3 # => #<OraNumber:6>
OraNumber(2) \* 1.5 # => 3.0 (Float)
**¶
#**(other) => OraNumber
指定数值的指数,比如:
OraNumber(2) \*\* 2 # => #<OraNumber:4>
OraNumber(2) \*\* 2.5 # => #<OraNumber:5.65685424949238019520675489683879231435>
+¶
#+(other) => Numeric
返回self和other的总和。当other的类是Integer时,它返回一个OraNumber值。否则,它将返回与其他类相同的值。
比如:
OraNumber(2) + 3 # => #<OraNumber:5>
OraNumber(2) + 1.5 # => 3.5 (Float)
-¶
#-(other) => Numeric
返回self和other的差。当other的类是Integer时,它返回一个OraNumber值。否则,它将返回与其他类相同的值。
比如:
OraNumber(2) - 3 # => #<OraNumber:-1>
OraNumber(2) - 1.5 # => 0.5 (Float)
/¶
#/(other) => Numeric
返回self除以other的结果。当other的类是Integer时,它返回一个OraNumber值。否则,它将返回与其他类相同的值。
比如:
OraNumber(2) / 3 # =>#<OraNumber:0.6666666666666666666666666666666666666667>
OraNumber(2) / 1.5 # => 1.3333333333333333 (Float)
<=>¶
#<=>(other) => -1, 0 or +1
根据self是小于、等于还是大于other,返回-1、0或+1。这是可比测试的基础。
比如:
OraNumber(5) <=> 3 # => 1
OraNumber(4) <=> 4 # => 0
OraNumber(2) <=> 2.5 # => 1
ceil¶
#ceil => Integer
返回大于或等于self的最小整数。
比如:
OraNumber(11.1).ceil # => 12
OraNumber(25.8).ceil # => 26
OraNumber(-25.8).ceil # => -25
dump¶
#dump => String
返回内部表示,其格式与SQL函数转储的返回值相同。
比如:
OraNumber.new(100).dump #=> "Typ=2 Len=2: 194,2"
OraNumber.new(123).dump #=> "Typ=2 Len=3: 194,2,24"
OraNumber.new(0.1).dump #=> "Typ=2 Len=2: 192,11"
floor¶
#floor=> Integer
返回小于或等于self的最大整数。
比如:
OraNumber(11.1).floor # => 11
OraNumber(25.8).floor # => 25
OraNumber(-25.8).floor # => -26
has_fractional_part¶
#has_fractional_part => Boolean
如果self有小数部分,则返回true。
比如:
OraNumber(10).has_fractional_part # => false
OraNumber(10.1).has_fractional_part # => true
initialize_copy¶
#initialize_copy(obj) => Object
将self替换为obj。Object#clone和Object#dup调用此方法来复制ruby解释器未知的数据。
round¶
#round => Integer
将自身舍入到最近的整数。
比如:
OraNumber(1.49).round # => 1
OraNumber(1.5).round # => 2
OraNumber(-1.49).round # => -1
OraNumber(-1.5).round # => -2
round(decplace)¶
#round(decplace) => OraNumber
将onum舍入到指定的小数点位。
比如:
OraNumber(123.456).round(2) # => #<OraNumber:123.46>
OraNumber(123.456).round(1) # => #<OraNumber:123.5>
OraNumber(123.456).round(0) # => #<OraNumber:123>
OraNumber(123.456).round(-1) # => #<OraNumber:120>
round_prec¶
#round_prec(digits) => OraNumber
将自身舍入到指定的小数位数。
比如:
OraNumber(1.234).round_prec(2) # => #<OraNumber:1.2>
OraNumber(12.34).round_prec(2) # => #<OraNumber:12>
OraNumber(123.4).round_prec(2) # => #<OraNumber:120>
shift¶
#shift(ndigits) => OraNumber
返回按ndigit自移位的值。
比如:
OraNumber(123).shift(3) # => #<OraNumber:123000>
OraNumber(123).shift(-3) # => #<OraNumber:0.123>
to_char¶
#to_char(fmt = nil, nlsparam = nil) => String
返回包含self表示形式的字符串。fmt和nlsparam用作SQL函数,用于TO_CHAR(number)。
比如:
OraNumber(123456.789).to_char('FM999,999,999.999') # => "123,456.789"
OraNumber(123456.789).to_char('FM999G999G999D999', "NLS_NUMERIC_CHARACTERS = ',.'") # => "123.456,789"
truncate¶
#truncate(decplace = 0) => OraNumber
将自身截断到指定的小数点后十位。未指定deplace时返回整数。
比如:
OraNumber(123.456).truncate # => #<OraNumber:123>
OraNumber(123.456).truncate(1) # => #<OraNumber:123.4>
OraNumber(123.456).truncate(2) # => #<OraNumber:123.45>
OraNumber(123.456).truncate(-1) # => #<OraNumber:120>
OraNumber(-123.456).truncate # => #<OraNumber:-123>
OraNumber(-123.456).truncate(1) # => #<OraNumber:-123.4>
OraNumber(-123.456).truncate(2) # => #<OraNumber:-123.45>
OraNumber(-123.456).truncate(-1) # => #<OraNumber:-120>
String类¶
实例方法¶
to_onum¶
to_onum(format = nil, nls_params = nil) => OraNumber
将string类型转换为OraNumber类型,可选format 和nls_params_参数用作SQL函数TO_NUMBER 工作。
比如:
'123456.789'.to_onum # => #<OraNumber:123456.789>
'123,456.789'.to_onum('999,999,999.999') # => #<OraNumber:123456.789>
'123.456,789'.to_onum('999G999G999D999', "NLS_NUMERIC_CHARACTERS = ',.'") # => #<OraNumber:123456.789>