27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/bind_param.rb', line 27
def set_value(value)
self[:value][:is_null] = FFI::MemoryPointer.new(SQLAnywhere::Bool, 1, true)
if self[:direction] == :input
case value
when String
self[:value][:length] = FFI::MemoryPointer.new(SQLAnywhere::SIZE_T, 1, false)
length = value.bytesize
SQLAnywhere.write_size_t(self[:value][:length], length)
self[:value][:buffer] = FFI::MemoryPointer.new(:char, length + 1, false)
self[:value][:buffer].put_string(0, value)
self[:value][:type] = :string
when Fixnum
self[:value][:buffer] = FFI::MemoryPointer.new(:long, 1, false)
self[:value][:type] = :val64
byte_array = [value].pack('q')
byte_array.each_byte.each_with_index do |b, offset|
self[:value][:buffer].put_char(offset, b)
end
when Bignum
self[:value][:buffer] = FFI::MemoryPointer.new(:long_long, 1, false)
self[:value][:type] = :val64
byte_array = [value].pack('Q')
byte_array.each_byte.each_with_index do |b, offset|
self[:value][:buffer].put_char(offset, b)
end
when Float
self[:value][:buffer] = FFI::MemoryPointer.new(:double, 1, false)
self[:value][:buffer].write_double(value)
self[:value][:type] = :double
when nil
self[:value][:is_null].write_int(1)
self[:value][:buffer] = FFI::MemoryPointer.new(:int, 1, false)
self[:value][:type] = :val32
else
raise "Cannot convert type (#{value.class}). Must be STRING, FIXNUM, BIGNUM, FLOAT, or NIL"
end
else
self[:value][:buffer] = FFI::MemoryPointer.new(:char,
case self[:value][:type]
when :string
self[:value][:buffer_size]
when :double
FFI::Type::FLOAT.size when :val64, :uval64
FFI::Type::LONG_LONG.size
when :val32, :uval32
FFI::Type::INT.size
when :val16, :uval16
FFI::Type::SHORT.size
when :val8, :uval8
FFI::Type::CHAR.size
else
raise "Type unknown (#{self[:value][:type]})"
end
)
end
nil
end
|