sqlite3 1.3.7 → 1.3.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -79,7 +79,6 @@ static VALUE initialize(VALUE self, VALUE db, VALUE sql)
79
79
  static VALUE sqlite3_rb_close(VALUE self)
80
80
  {
81
81
  sqlite3StmtRubyPtr ctx;
82
- sqlite3 * db;
83
82
 
84
83
  Data_Get_Struct(self, sqlite3StmtRuby, ctx);
85
84
 
@@ -113,7 +112,6 @@ static VALUE step(VALUE self)
113
112
  VALUE list;
114
113
  #ifdef HAVE_RUBY_ENCODING_H
115
114
  rb_encoding * internal_encoding;
116
- int enc_index;
117
115
  #endif
118
116
 
119
117
  Data_Get_Struct(self, sqlite3StmtRuby, ctx);
@@ -125,8 +123,7 @@ static VALUE step(VALUE self)
125
123
  #ifdef HAVE_RUBY_ENCODING_H
126
124
  {
127
125
  VALUE db = rb_iv_get(self, "@connection");
128
- VALUE encoding = rb_funcall(db, rb_intern("encoding"), 0);
129
- enc_index = NIL_P(encoding) ? rb_utf8_encindex() : rb_to_encoding_index(encoding);
126
+ rb_funcall(db, rb_intern("encoding"), 0);
130
127
  internal_encoding = rb_default_internal_encoding();
131
128
  }
132
129
  #endif
@@ -156,7 +153,7 @@ static VALUE step(VALUE self)
156
153
  (long)sqlite3_column_bytes(stmt, i)
157
154
  );
158
155
  #ifdef HAVE_RUBY_ENCODING_H
159
- rb_enc_associate_index(str, enc_index);
156
+ rb_enc_associate_index(str, rb_utf8_encindex());
160
157
  if(internal_encoding)
161
158
  str = rb_str_export_to_enc(str, internal_encoding);
162
159
  #endif
@@ -186,6 +183,8 @@ static VALUE step(VALUE self)
186
183
  return Qnil;
187
184
  break;
188
185
  default:
186
+ sqlite3_reset(stmt);
187
+ ctx->done_p = 0;
189
188
  CHECK(sqlite3_db_handle(ctx->st), value);
190
189
  }
191
190
 
@@ -238,15 +237,22 @@ static VALUE bind_param(VALUE self, VALUE key, VALUE value)
238
237
  SQLITE_TRANSIENT
239
238
  );
240
239
  } else {
240
+
241
+
241
242
  #ifdef HAVE_RUBY_ENCODING_H
242
- if(!UTF8_P(value)) {
243
- VALUE db = rb_iv_get(self, "@connection");
244
- VALUE encoding = rb_funcall(db, rb_intern("encoding"), 0);
245
- rb_encoding * enc = rb_to_encoding(encoding);
246
- value = rb_str_export_to_enc(value, enc);
243
+ if (UTF16_LE_P(value) || UTF16_BE_P(value)) {
244
+ status = sqlite3_bind_text16(
245
+ ctx->st,
246
+ index,
247
+ (const char *)StringValuePtr(value),
248
+ (int)RSTRING_LEN(value),
249
+ SQLITE_TRANSIENT
250
+ );
251
+ } else {
252
+ if (!UTF8_P(value) || !USASCII_P(value)) {
253
+ value = rb_str_encode(value, rb_enc_from_encoding(rb_utf8_encoding()), 0, Qnil);
247
254
  }
248
255
  #endif
249
-
250
256
  status = sqlite3_bind_text(
251
257
  ctx->st,
252
258
  index,
@@ -254,13 +260,18 @@ static VALUE bind_param(VALUE self, VALUE key, VALUE value)
254
260
  (int)RSTRING_LEN(value),
255
261
  SQLITE_TRANSIENT
256
262
  );
263
+ #ifdef HAVE_RUBY_ENCODING_H
264
+ }
265
+ #endif
257
266
  }
258
267
  break;
259
- case T_BIGNUM:
260
- if (RBIGNUM_LEN(value) * SIZEOF_BDIGITS <= 8) {
261
- status = sqlite3_bind_int64(ctx->st, index, (sqlite3_int64)NUM2LL(value));
268
+ case T_BIGNUM: {
269
+ sqlite3_int64 num64;
270
+ if (bignum_to_int64(value, &num64)) {
271
+ status = sqlite3_bind_int64(ctx->st, index, num64);
262
272
  break;
263
273
  }
274
+ }
264
275
  case T_FLOAT:
265
276
  status = sqlite3_bind_double(ctx->st, index, NUM2DBL(value));
266
277
  break;
@@ -289,12 +300,11 @@ static VALUE bind_param(VALUE self, VALUE key, VALUE value)
289
300
  static VALUE reset_bang(VALUE self)
290
301
  {
291
302
  sqlite3StmtRubyPtr ctx;
292
- int status;
293
303
 
294
304
  Data_Get_Struct(self, sqlite3StmtRuby, ctx);
295
305
  REQUIRE_OPEN_STMT(ctx);
296
306
 
297
- status = sqlite3_reset(ctx->st);
307
+ sqlite3_reset(ctx->st);
298
308
 
299
309
  ctx->done_p = 0;
300
310
 
@@ -309,12 +319,11 @@ static VALUE reset_bang(VALUE self)
309
319
  static VALUE clear_bindings(VALUE self)
310
320
  {
311
321
  sqlite3StmtRubyPtr ctx;
312
- int status;
313
322
 
314
323
  Data_Get_Struct(self, sqlite3StmtRuby, ctx);
315
324
  REQUIRE_OPEN_STMT(ctx);
316
325
 
317
- status = sqlite3_clear_bindings(ctx->st);
326
+ sqlite3_clear_bindings(ctx->st);
318
327
 
319
328
  ctx->done_p = 0;
320
329
 
data/faq/faq.yml CHANGED
@@ -331,7 +331,7 @@
331
331
  p obj == h
332
332
  </pre>
333
333
 
334
- - "How do insert binary data into the database?": >-
334
+ - "How do I insert binary data into the database?": >-
335
335
  Use blobs. Blobs are new features of SQLite3. You have to use bind
336
336
  variables to make it work:
337
337
 
@@ -94,10 +94,17 @@ in version 2.0.0.
94
94
  begin
95
95
  yield stmt
96
96
  ensure
97
- stmt.close
97
+ stmt.close unless stmt.closed?
98
98
  end
99
99
  end
100
100
 
101
+ # Returns the filename for the database named +db_name+. +db_name+ defaults
102
+ # to "main". Main return `nil` or an empty string if the database is
103
+ # temporary or in-memory.
104
+ def filename db_name = 'main'
105
+ db_filename db_name
106
+ end
107
+
101
108
  # Executes the given SQL statement. If additional parameters are given,
102
109
  # they are treated as bind variables, and are bound to the placeholders in
103
110
  # the query.
@@ -113,10 +120,6 @@ in version 2.0.0.
113
120
  # See also #execute2, #query, and #execute_batch for additional ways of
114
121
  # executing statements.
115
122
  def execute sql, bind_vars = [], *args, &block
116
- # FIXME: This is a terrible hack and should be removed but is required
117
- # for older versions of rails
118
- hack = Object.const_defined?(:ActiveRecord) && sql =~ /^PRAGMA index_list/
119
-
120
123
  if bind_vars.nil? || !args.empty?
121
124
  if args.empty?
122
125
  bind_vars = []
@@ -147,12 +150,7 @@ Support for bind parameters as *args will be removed in 2.0.0.
147
150
  else
148
151
  if @results_as_hash
149
152
  stmt.map { |row|
150
- h = type_translation ? row : ordered_map_for(columns, row)
151
-
152
- # FIXME UGH TERRIBLE HACK!
153
- h['unique'] = h['unique'].to_s if hack
154
-
155
- h
153
+ type_translation ? row : ordered_map_for(columns, row)
156
154
  }
157
155
  else
158
156
  stmt.to_a
@@ -221,15 +219,18 @@ Support for this behavior will be removed in version 2.0.0.
221
219
  sql = sql.strip
222
220
  until sql.empty? do
223
221
  prepare( sql ) do |stmt|
224
- # FIXME: this should probably use sqlite3's api for batch execution
225
- # This implementation requires stepping over the results.
226
- if bind_vars.length == stmt.bind_parameter_count
227
- stmt.bind_params(bind_vars)
222
+ unless stmt.closed?
223
+ # FIXME: this should probably use sqlite3's api for batch execution
224
+ # This implementation requires stepping over the results.
225
+ if bind_vars.length == stmt.bind_parameter_count
226
+ stmt.bind_params(bind_vars)
227
+ end
228
+ stmt.step
228
229
  end
229
- stmt.step
230
230
  sql = stmt.remainder.strip
231
231
  end
232
232
  end
233
+ # FIXME: we should not return `nil` as a success return value
233
234
  nil
234
235
  end
235
236
 
@@ -392,6 +393,9 @@ Support for this will be removed in version 2.0.0.
392
393
 
393
394
  def finalize
394
395
  super(@ctx)
396
+ result = @ctx.result
397
+ @ctx = FunctionProxy.new
398
+ result
395
399
  end
396
400
  })
397
401
  proxy.ctx = FunctionProxy.new
@@ -428,6 +432,7 @@ Support for this will be removed in version 2.0.0.
428
432
  #
429
433
  # class LengthsAggregateHandler
430
434
  # def self.arity; 1; end
435
+ # def self.name; 'lengths'; end
431
436
  #
432
437
  # def initialize
433
438
  # @total = 0
@@ -446,21 +451,28 @@ Support for this will be removed in version 2.0.0.
446
451
  # puts db.get_first_value( "select lengths(name) from A" )
447
452
  def create_aggregate_handler( handler )
448
453
  proxy = Class.new do
449
- def initialize handler
450
- @handler = handler
451
- @fp = FunctionProxy.new
454
+ def initialize klass
455
+ @klass = klass
456
+ @fp = FunctionProxy.new
452
457
  end
453
458
 
454
459
  def step( *args )
455
- @handler.step(@fp, *args)
460
+ instance.step(@fp, *args)
456
461
  end
457
462
 
458
463
  def finalize
459
- @handler.finalize @fp
464
+ instance.finalize @fp
465
+ @instance = nil
460
466
  @fp.result
461
467
  end
468
+
469
+ private
470
+
471
+ def instance
472
+ @instance ||= @klass.new
473
+ end
462
474
  end
463
- define_aggregator(handler.name, proxy.new(handler.new))
475
+ define_aggregator(handler.name, proxy.new(handler))
464
476
  self
465
477
  end
466
478