Class: Mysql::C::Bind

Inherits:
FFI::Struct
  • Object
show all
Defined in:
lib/ffi-mysql/stmt.rb

Instance Method Summary collapse

Instance Method Details

#prepare_result(field) ⇒ Object



39
40
41
42
43
44
# File 'lib/ffi-mysql/stmt.rb', line 39

def prepare_result( field )
    self[:buffer_type] = field.type
    self[:is_null] = @is_null = FFI::MemoryPointer.new(:int)
    self[:length] = @buflen = FFI::MemoryPointer.new(:ulong)
    self[:is_unsigned] = (field.flags & Field::UNSIGNED_FLAG) != 0 ? 1 : 0
end

#set_bind_result(arg, field) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ffi-mysql/stmt.rb', line 67

def set_bind_result( arg, field )
    if arg.nil? or arg == NilClass
        self[:buffer_type] = field.type
    elsif arg == String
        self[:buffer_type] = Field::TYPE_STRING
    elsif arg == Numeric or arg == Integer or arg == Fixnum
        self[:buffer_type] = Field::TYPE_LONGLONG
    elsif arg == Float
        self[:buffer_type] = Field::TYPE_DOUBLE
    elsif arg == Mysql::Time or arg == Time
        self[:buffer_type] = Field::TYPE_DATETIME
    else
        raise TypeError, "unrecognized class: #{arg.class}"
    end
end

#set_param(arg) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ffi-mysql/stmt.rb', line 83

def set_param( arg )
    case arg
    when nil
        self[:buffer_type] = Field::TYPE_NULL
    when Integer
        if arg.size == 4
            self[:buffer_type] = Field::TYPE_LONG
            self[:buffer] = @buf = FFI::MemoryPointer.new( :long )
            @buf.write_long(arg)
        else
            self[:buffer_type] = Field::TYPE_LONGLONG
            self[:buffer] = @buf = FFI::MemoryPointer.new( :long_long )
            @buf.write_long_long(arg)
        end
    when Float
        self[:buffer_type] = Field::TYPE_DOUBLE
        self[:buffer] = @buf = FFI::MemoryPointer.new( :double )
        @buf.write_double(arg)
    when String
        self[:buffer_type] = Field::TYPE_STRING
        self[:buffer] = @buf = FFI::MemoryPointer::from_string(arg)
        self[:buffer_length] = arg.bytesize
        self[:length] = @buflen = FFI::MemoryPointer.new( :ulong )
        @buflen.write_long( arg.bytesize )
    when ::Time, Mysql::Time
        self[:buffer_type] = Field::TYPE_DATETIME
        self[:buffer] = @buf = FFI::MemoryPointer.new( C::Time, 1, true )
        self[:buffer_length] = C::Time.size
        self[:length] = @buflen = FFI::MemoryPointer.new( :ulong )
        @buflen.write_long(C::Time.size)
        time = C::Time.new(@buf)
        time[:second_part] = 0
        time[:neg] = 0
        if arg.kind_of? Mysql::Time
            time[:second] = arg.second
            time[:minute] = arg.minute
            time[:hour] = arg.hour
            time[:day] = arg.day
            time[:month] = arg.month
            time[:year] = arg.year
        else
            time[:second] = arg.sec
            time[:minute] = arg.min
            time[:hour] = arg.hour
            time[:day] = arg.day
            time[:month] = arg.month
            time[:year] = arg.year
        end
    else
        raise TypeError, "unsupported type: #{arg.class}"
    end
end

#set_result(field) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ffi-mysql/stmt.rb', line 46

def set_result( field )
    case self[:buffer_type]
    when Field::TYPE_NULL
        nil
    when Field::TYPE_TINY, Field::TYPE_SHORT, Field::TYPE_YEAR, Field::TYPE_INT24,
         Field::TYPE_LONG, Field::TYPE_LONGLONG, Field::TYPE_FLOAT, Field::TYPE_DOUBLE
        self[:buffer] = @buf = FFI::MemoryPointer.new(8, 1, true)
        self[:buffer_length] = 8
    when Field::TYPE_DECIMAL, Field::TYPE_STRING, Field::TYPE_VAR_STRING, Field::TYPE_TINY_BLOB,
         Field::TYPE_BLOB, Field::TYPE_MEDIUM_BLOB, Field::TYPE_LONG_BLOB, Field::TYPE_NEWDECIMAL,
         Field::TYPE_BIT
        self[:buffer] = @buf = FFI::MemoryPointer.new( field.max_length, 1, true )
        self[:buffer_length] = field.max_length
    when Field::TYPE_TIME, Field::TYPE_DATE, Field::TYPE_DATETIME, Field::TYPE_TIMESTAMP
        self[:buffer] = @buf = FFI::MemoryPointer.new( C::Time.size, 1, true )
        self[:buffer_length] = C::Time.size
    else
        raise TypeError, "unknown buffer_type: #{self[:buffer_type]}"
    end
end

#valueObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ffi-mysql/stmt.rb', line 136

def value
    if @is_null.read_int != 0
        nil
    else
        case self[:buffer_type]
        when Field::TYPE_TINY
            self[:is_unsigned] == 0 ? @buf.get_int8(0) : @buf.get_uint8(0)
        when Field::TYPE_SHORT, Field::TYPE_YEAR
            self[:is_unsigned] == 0 ? @buf.get_int16(0) : @buf.get_uint16(0)
        when Field::TYPE_INT24, Field::TYPE_LONG
            self[:is_unsigned] == 0 ? @buf.get_int32(0) : @buf.get_uint32(0)
        when Field::TYPE_LONGLONG
            self[:is_unsigned] == 0 ? @buf.get_int64(0) : @buf.get_uint64(0)
        when Field::TYPE_FLOAT
            @buf.get_float(0)
        when Field::TYPE_DOUBLE
            @buf.get_double(0)
        when Field::TYPE_TIME, Field::TYPE_DATE, Field::TYPE_DATETIME, Field::TYPE_TIMESTAMP
            time = C::Time.new(@buf)
            Time.new( time[:year], time[:month], time[:day],
                      time[:hour], time[:minute], time[:second],
                      time[:neg] != 0, time[:second_part] )
        when Field::TYPE_DECIMAL, Field::TYPE_STRING, Field::TYPE_VAR_STRING,
             Field::TYPE_TINY_BLOB, Field::TYPE_BLOB, Field::TYPE_MEDIUM_BLOB,
             Field::TYPE_LONG_BLOB, Field::TYPE_NEWDECIMAL, Field::TYPE_BIT
            @buf.read_string( @buflen.get_ulong(0) )
        else
            raise TypeError, "unknown buffer type: #{self[:buffer_type]}"
        end
    end
end