Class: SQLite3::Driver::Native::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/sqlite3/driver/native/driver.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDriver

Returns a new instance of Driver.



7
8
9
10
11
12
# File 'lib/sqlite3/driver/native/driver.rb', line 7

def initialize
  @callback_data = Hash.new
  @authorizer = Hash.new
  @busy_handler = Hash.new
  @trace = Hash.new
end

Class Method Details

.api_delegate(name) ⇒ Object



161
162
163
# File 'lib/sqlite3/driver/native/driver.rb', line 161

def self.api_delegate( name )
  eval "def #{name} (*args) API.sqlite3_#{name}( *args ) ; end"
end

Instance Method Details

#bind_text(stmt, index, value, utf16 = false) ⇒ Object



80
81
82
83
# File 'lib/sqlite3/driver/native/driver.rb', line 80

def bind_text( stmt, index, value, utf16=false )
  API.send( ( utf16 ? :sqlite3_bind_text16 : :sqlite3_bind_text ),
    stmt, index, value.to_s )
end

#busy_handler(db, data = nil, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sqlite3/driver/native/driver.rb', line 18

def busy_handler( db, data=nil, &block )
  if block
    cb = API::CallbackData.new
    cb.proc = block
    cb.data = data
    result = API.sqlite3_busy_handler( db, API::Sqlite3_ruby_busy_handler, cb )
    # Reference the Callback object so that 
    # it is not deleted by the GC
    @busy_handler[db] = cb
  else
    # Unreference the callback *after* having removed it
    # from sqlite
    result = API.sqlite3_busy_handler( db, nil, nil )
    @busy_handler.delete(db)
  end

  result
end

#column_decltype(stmt, index, utf16 = false) ⇒ Object



90
91
92
93
94
# File 'lib/sqlite3/driver/native/driver.rb', line 90

def column_decltype( stmt, index, utf16=false )
  API.send(
    ( utf16 ? :sqlite3_column_decltype16 : :sqlite3_column_decltype ),
    stmt, index )
end

#column_name(stmt, index, utf16 = false) ⇒ Object



85
86
87
88
# File 'lib/sqlite3/driver/native/driver.rb', line 85

def column_name( stmt, index, utf16=false )
  API.send( ( utf16 ? :sqlite3_column_name16 : :sqlite3_column_name ),
    stmt, index )
end

#column_text(stmt, index, utf16 = false) ⇒ Object



96
97
98
99
# File 'lib/sqlite3/driver/native/driver.rb', line 96

def column_text( stmt, index, utf16=false )
  API.send( ( utf16 ? :sqlite3_column_text16 : :sqlite3_column_text ),
    stmt, index )
end

#complete?(sql, utf16 = false) ⇒ Boolean

Returns:



14
15
16
# File 'lib/sqlite3/driver/native/driver.rb', line 14

def complete?( sql, utf16=false )
  API.send( utf16 ? :sqlite3_complete16 : :sqlite3_complete, sql ) != 0
end

#create_function(db, name, args, text, cookie, func, step, final) ⇒ Object



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
# File 'lib/sqlite3/driver/native/driver.rb', line 101

def create_function( db, name, args, text, cookie, func, step, final )
  if func || ( step && final )
    cb = API::CallbackData.new
    cb.proc = cb.proc2 = nil
    cb.data = cookie
  end

  if func
    cb.proc = func

    func = API::Sqlite3_ruby_function_step
    step = final = nil
  elsif step && final
    cb.proc = step
    cb.proc2 = final

    func = nil
    step = API::Sqlite3_ruby_function_step
    final = API::Sqlite3_ruby_function_final
  end

  result = API.sqlite3_create_function( db, name, args, text, cb, func, step, final )

  # see comments in busy_handler
  if cb
    @callback_data[ name ] = cb
  else
    @callback_data.delete( name )
  end

  return result
end

#errmsg(db, utf16 = false) ⇒ Object



71
72
73
# File 'lib/sqlite3/driver/native/driver.rb', line 71

def errmsg( db, utf16=false )
  API.send( utf16 ? :sqlite3_errmsg16 : :sqlite3_errmsg, db )
end

#open(filename, utf16 = false) ⇒ Object



67
68
69
# File 'lib/sqlite3/driver/native/driver.rb', line 67

def open( filename, utf16=false )
  API.send( utf16 ? :sqlite3_open16 : :sqlite3_open, filename )
end

#prepare(db, sql, utf16 = false) ⇒ Object



75
76
77
78
# File 'lib/sqlite3/driver/native/driver.rb', line 75

def prepare( db, sql, utf16=false )
  API.send( ( utf16 ? :sqlite3_prepare16 : :sqlite3_prepare ),
    db, sql )
end

#result_error(context, value, utf16 = false) ⇒ Object



156
157
158
159
# File 'lib/sqlite3/driver/native/driver.rb', line 156

def result_error( context, value, utf16=false )
  API.send( ( utf16 ? :sqlite3_result_error16 : :sqlite3_result_error ),
    context, value )
end

#result_text(context, result, utf16 = false) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/sqlite3/driver/native/driver.rb', line 145

def result_text( context, result, utf16=false )
  method = case utf16
    when nil, false then :sqlite3_result_text
    when :le then :sqlite3_result_text16le
    when :be then :sqlite3_result_text16be
    else :sqlite3_result_text16
  end

  API.send( method, context, result.to_s )
end

#set_authorizer(db, data = nil, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sqlite3/driver/native/driver.rb', line 37

def set_authorizer( db, data=nil, &block )
  if block
    cb = API::CallbackData.new
    cb.proc = block
    cb.data = data
    result = API.sqlite3_set_authorizer( db, API::Sqlite3_ruby_authorizer, cb )
    @authorizer[db] = cb # see comments in busy_handler
  else
    result = API.sqlite3_set_authorizer( db, nil, nil )
    @authorizer.delete(db) # see comments in busy_handler
  end

  result
end

#trace(db, data = nil, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sqlite3/driver/native/driver.rb', line 52

def trace( db, data=nil, &block )
  if block
    cb = API::CallbackData.new
    cb.proc = block
    cb.data = data
    result = API.sqlite3_trace( db, API::Sqlite3_ruby_trace, cb )
    @trace[db] = cb # see comments in busy_handler
  else
    result = API.sqlite3_trace( db, nil, nil )
    @trace.delete(db) # see comments in busy_handler
  end

  result
end

#value_text(value, utf16 = false) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/sqlite3/driver/native/driver.rb', line 134

def value_text( value, utf16=false )
  method = case utf16
    when nil, false then :sqlite3_value_text
    when :le then :sqlite3_value_text16le
    when :be then :sqlite3_value_text16be
    else :sqlite3_value_text16
  end

  API.send( method, value )
end