sqlite3 1.7.3 → 2.5.0

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.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +292 -0
  3. data/CONTRIBUTING.md +33 -7
  4. data/FAQ.md +43 -77
  5. data/INSTALLATION.md +14 -6
  6. data/LICENSE +18 -22
  7. data/README.md +97 -9
  8. data/dependencies.yml +10 -11
  9. data/ext/sqlite3/aggregator.c +142 -145
  10. data/ext/sqlite3/aggregator.h +2 -4
  11. data/ext/sqlite3/backup.c +74 -65
  12. data/ext/sqlite3/backup.h +2 -2
  13. data/ext/sqlite3/database.c +621 -493
  14. data/ext/sqlite3/database.h +13 -4
  15. data/ext/sqlite3/exception.c +116 -92
  16. data/ext/sqlite3/exception.h +5 -1
  17. data/ext/sqlite3/extconf.rb +33 -24
  18. data/ext/sqlite3/sqlite3.c +176 -115
  19. data/ext/sqlite3/sqlite3_ruby.h +2 -2
  20. data/ext/sqlite3/statement.c +553 -300
  21. data/ext/sqlite3/statement.h +4 -3
  22. data/ext/sqlite3/timespec.h +20 -0
  23. data/lib/sqlite3/constants.rb +195 -47
  24. data/lib/sqlite3/database.rb +223 -187
  25. data/lib/sqlite3/errors.rb +54 -1
  26. data/lib/sqlite3/fork_safety.rb +66 -0
  27. data/lib/sqlite3/pragmas.rb +140 -136
  28. data/lib/sqlite3/resultset.rb +14 -97
  29. data/lib/sqlite3/statement.rb +58 -13
  30. data/lib/sqlite3/value.rb +17 -20
  31. data/lib/sqlite3/version.rb +2 -21
  32. data/lib/sqlite3/version_info.rb +17 -0
  33. data/lib/sqlite3.rb +8 -4
  34. data/ports/archives/sqlite-autoconf-3470200.tar.gz +0 -0
  35. metadata +9 -37
  36. data/API_CHANGES.md +0 -49
  37. data/ChangeLog.cvs +0 -88
  38. data/Gemfile +0 -10
  39. data/LICENSE-DEPENDENCIES +0 -20
  40. data/lib/sqlite3/translator.rb +0 -117
  41. data/ports/archives/sqlite-autoconf-3450200.tar.gz +0 -0
  42. data/test/helper.rb +0 -27
  43. data/test/test_backup.rb +0 -33
  44. data/test/test_collation.rb +0 -82
  45. data/test/test_database.rb +0 -668
  46. data/test/test_database_flags.rb +0 -95
  47. data/test/test_database_readonly.rb +0 -36
  48. data/test/test_database_readwrite.rb +0 -41
  49. data/test/test_deprecated.rb +0 -49
  50. data/test/test_encoding.rb +0 -165
  51. data/test/test_integration.rb +0 -507
  52. data/test/test_integration_aggregate.rb +0 -336
  53. data/test/test_integration_open_close.rb +0 -30
  54. data/test/test_integration_pending.rb +0 -115
  55. data/test/test_integration_resultset.rb +0 -142
  56. data/test/test_integration_statement.rb +0 -194
  57. data/test/test_pragmas.rb +0 -22
  58. data/test/test_result_set.rb +0 -47
  59. data/test/test_sqlite3.rb +0 -30
  60. data/test/test_statement.rb +0 -290
  61. data/test/test_statement_execute.rb +0 -39
@@ -1,81 +1,87 @@
1
1
  #include <sqlite3_ruby.h>
2
2
 
3
3
  #define REQUIRE_OPEN_STMT(_ctxt) \
4
- if(!_ctxt->st) \
4
+ if (!_ctxt->st) \
5
5
  rb_raise(rb_path2class("SQLite3::Exception"), "cannot use a closed statement");
6
6
 
7
+ #define REQUIRE_LIVE_DB(_ctxt) \
8
+ if (_ctxt->db->flags & SQLITE3_RB_DATABASE_DISCARDED) \
9
+ rb_raise(rb_path2class("SQLite3::Exception"), "cannot use a statement associated with a discarded database");
10
+
7
11
  VALUE cSqlite3Statement;
8
12
 
9
- static size_t statement_memsize(const void *data)
13
+ static void
14
+ statement_deallocate(void *data)
15
+ {
16
+ sqlite3StmtRubyPtr s = (sqlite3StmtRubyPtr)data;
17
+
18
+ if (s->st) {
19
+ sqlite3_finalize(s->st);
20
+ }
21
+
22
+ xfree(data);
23
+ }
24
+
25
+ static size_t
26
+ statement_memsize(const void *data)
10
27
  {
11
- const sqlite3StmtRubyPtr s = (const sqlite3StmtRubyPtr)data;
12
- // NB: can't account for s->st because the type is incomplete.
13
- return sizeof(*s);
28
+ const sqlite3StmtRubyPtr s = (const sqlite3StmtRubyPtr)data;
29
+ // NB: can't account for s->st because the type is incomplete.
30
+ return sizeof(*s);
14
31
  }
15
32
 
16
33
  static const rb_data_type_t statement_type = {
17
- "SQLite3::Backup",
18
- {
19
- NULL,
20
- RUBY_TYPED_DEFAULT_FREE,
21
- statement_memsize,
22
- },
23
- 0,
24
- 0,
25
- RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
34
+ "SQLite3::Backup",
35
+ {
36
+ NULL,
37
+ statement_deallocate,
38
+ statement_memsize,
39
+ },
40
+ 0,
41
+ 0,
42
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
26
43
  };
27
44
 
28
- static VALUE allocate(VALUE klass)
45
+ static VALUE
46
+ allocate(VALUE klass)
29
47
  {
30
- sqlite3StmtRubyPtr ctx;
31
- return TypedData_Make_Struct(klass, sqlite3StmtRuby, &statement_type, ctx);
48
+ sqlite3StmtRubyPtr ctx;
49
+ return TypedData_Make_Struct(klass, sqlite3StmtRuby, &statement_type, ctx);
32
50
  }
33
51
 
34
- /* call-seq: SQLite3::Statement.new(db, sql)
35
- *
36
- * Create a new statement attached to the given Database instance, and which
37
- * encapsulates the given SQL text. If the text contains more than one
38
- * statement (i.e., separated by semicolons), then the #remainder property
39
- * will be set to the trailing text.
40
- */
41
- static VALUE initialize(VALUE self, VALUE db, VALUE sql)
52
+ static VALUE
53
+ prepare(VALUE self, VALUE db, VALUE sql)
42
54
  {
43
- sqlite3RubyPtr db_ctx = sqlite3_database_unwrap(db);
44
- sqlite3StmtRubyPtr ctx;
45
- const char *tail = NULL;
46
- int status;
47
-
48
- StringValue(sql);
55
+ sqlite3RubyPtr db_ctx = sqlite3_database_unwrap(db);
56
+ sqlite3StmtRubyPtr ctx;
57
+ const char *tail = NULL;
58
+ int status;
49
59
 
50
- TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
60
+ StringValue(sql);
51
61
 
52
- if(!db_ctx->db)
53
- rb_raise(rb_eArgError, "prepare called on a closed database");
62
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
54
63
 
55
- if(!UTF8_P(sql)) {
56
- sql = rb_str_export_to_enc(sql, rb_utf8_encoding());
57
- }
64
+ /* Dereferencing a pointer to the database struct will be faster than accessing it through the
65
+ * instance variable @connection. The struct pointer is guaranteed to be live because instance
66
+ * variable will keep it from being GCed. */
67
+ ctx->db = db_ctx;
58
68
 
59
69
  #ifdef HAVE_SQLITE3_PREPARE_V2
60
- status = sqlite3_prepare_v2(
70
+ status = sqlite3_prepare_v2(
61
71
  #else
62
- status = sqlite3_prepare(
72
+ status = sqlite3_prepare(
63
73
  #endif
64
- db_ctx->db,
65
- (const char *)StringValuePtr(sql),
66
- (int)RSTRING_LEN(sql),
67
- &ctx->st,
68
- &tail
69
- );
74
+ db_ctx->db,
75
+ (const char *)StringValuePtr(sql),
76
+ (int)RSTRING_LEN(sql),
77
+ &ctx->st,
78
+ &tail
79
+ );
70
80
 
71
- CHECK(db_ctx->db, status);
81
+ CHECK_PREPARE(db_ctx->db, status, StringValuePtr(sql));
82
+ timespecclear(&db_ctx->stmt_deadline);
72
83
 
73
- rb_iv_set(self, "@connection", db);
74
- rb_iv_set(self, "@remainder", rb_str_new2(tail));
75
- rb_iv_set(self, "@columns", Qnil);
76
- rb_iv_set(self, "@types", Qnil);
77
-
78
- return self;
84
+ return rb_utf8_str_new_cstr(tail);
79
85
  }
80
86
 
81
87
  /* call-seq: stmt.close
@@ -83,122 +89,125 @@ static VALUE initialize(VALUE self, VALUE db, VALUE sql)
83
89
  * Closes the statement by finalizing the underlying statement
84
90
  * handle. The statement must not be used after being closed.
85
91
  */
86
- static VALUE sqlite3_rb_close(VALUE self)
92
+ static VALUE
93
+ sqlite3_rb_close(VALUE self)
87
94
  {
88
- sqlite3StmtRubyPtr ctx;
95
+ sqlite3StmtRubyPtr ctx;
89
96
 
90
- TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
97
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
91
98
 
92
- REQUIRE_OPEN_STMT(ctx);
99
+ REQUIRE_OPEN_STMT(ctx);
93
100
 
94
- sqlite3_finalize(ctx->st);
95
- ctx->st = NULL;
101
+ sqlite3_finalize(ctx->st);
102
+ ctx->st = NULL;
96
103
 
97
- return self;
104
+ return self;
98
105
  }
99
106
 
100
107
  /* call-seq: stmt.closed?
101
108
  *
102
109
  * Returns true if the statement has been closed.
103
110
  */
104
- static VALUE closed_p(VALUE self)
111
+ static VALUE
112
+ closed_p(VALUE self)
105
113
  {
106
- sqlite3StmtRubyPtr ctx;
107
- TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
114
+ sqlite3StmtRubyPtr ctx;
115
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
108
116
 
109
- if(!ctx->st) return Qtrue;
117
+ if (!ctx->st) { return Qtrue; }
110
118
 
111
- return Qfalse;
119
+ return Qfalse;
112
120
  }
113
121
 
114
- static VALUE step(VALUE self)
122
+ static VALUE
123
+ step(VALUE self)
115
124
  {
116
- sqlite3StmtRubyPtr ctx;
117
- sqlite3_stmt *stmt;
118
- int value, length;
119
- VALUE list;
120
- rb_encoding * internal_encoding;
121
-
122
- TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
123
-
124
- REQUIRE_OPEN_STMT(ctx);
125
-
126
- if(ctx->done_p) return Qnil;
127
-
128
- {
129
- VALUE db = rb_iv_get(self, "@connection");
130
- rb_funcall(db, rb_intern("encoding"), 0);
131
- internal_encoding = rb_default_internal_encoding();
132
- }
133
-
134
- stmt = ctx->st;
135
-
136
- value = sqlite3_step(stmt);
137
- if (rb_errinfo() != Qnil) {
138
- /* some user defined function was invoked as a callback during step and
139
- * it raised an exception that has been suppressed until step returns.
140
- * Now re-raise it. */
141
- VALUE exception = rb_errinfo();
142
- rb_set_errinfo(Qnil);
143
- rb_exc_raise(exception);
144
- }
145
-
146
- length = sqlite3_column_count(stmt);
147
- list = rb_ary_new2((long)length);
148
-
149
- switch(value) {
150
- case SQLITE_ROW:
151
- {
152
- int i;
153
- for(i = 0; i < length; i++) {
154
- switch(sqlite3_column_type(stmt, i)) {
155
- case SQLITE_INTEGER:
156
- rb_ary_push(list, LL2NUM(sqlite3_column_int64(stmt, i)));
157
- break;
158
- case SQLITE_FLOAT:
159
- rb_ary_push(list, rb_float_new(sqlite3_column_double(stmt, i)));
160
- break;
161
- case SQLITE_TEXT:
162
- {
163
- VALUE str = rb_str_new(
164
- (const char *)sqlite3_column_text(stmt, i),
165
- (long)sqlite3_column_bytes(stmt, i)
166
- );
167
- rb_enc_associate_index(str, rb_utf8_encindex());
168
- if(internal_encoding)
169
- str = rb_str_export_to_enc(str, internal_encoding);
170
- rb_ary_push(list, str);
171
- }
172
- break;
173
- case SQLITE_BLOB:
174
- {
175
- VALUE str = rb_str_new(
176
- (const char *)sqlite3_column_blob(stmt, i),
177
- (long)sqlite3_column_bytes(stmt, i)
178
- );
179
- rb_ary_push(list, str);
180
- }
181
- break;
182
- case SQLITE_NULL:
183
- rb_ary_push(list, Qnil);
184
- break;
185
- default:
186
- rb_raise(rb_eRuntimeError, "bad type");
187
- }
125
+ sqlite3StmtRubyPtr ctx;
126
+ sqlite3_stmt *stmt;
127
+ int value, length;
128
+ VALUE list;
129
+ rb_encoding *internal_encoding;
130
+
131
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
132
+
133
+ REQUIRE_LIVE_DB(ctx);
134
+ REQUIRE_OPEN_STMT(ctx);
135
+
136
+ if (ctx->done_p) { return Qnil; }
137
+
138
+ internal_encoding = rb_default_internal_encoding();
139
+
140
+ stmt = ctx->st;
141
+
142
+ value = sqlite3_step(stmt);
143
+ if (rb_errinfo() != Qnil) {
144
+ /* some user defined function was invoked as a callback during step and
145
+ * it raised an exception that has been suppressed until step returns.
146
+ * Now re-raise it. */
147
+ VALUE exception = rb_errinfo();
148
+ rb_set_errinfo(Qnil);
149
+ rb_exc_raise(exception);
150
+ }
151
+
152
+ length = sqlite3_column_count(stmt);
153
+ list = rb_ary_new2((long)length);
154
+
155
+ switch (value) {
156
+ case SQLITE_ROW: {
157
+ int i;
158
+ for (i = 0; i < length; i++) {
159
+ VALUE val;
160
+
161
+ switch (sqlite3_column_type(stmt, i)) {
162
+ case SQLITE_INTEGER:
163
+ val = LL2NUM(sqlite3_column_int64(stmt, i));
164
+ break;
165
+ case SQLITE_FLOAT:
166
+ val = rb_float_new(sqlite3_column_double(stmt, i));
167
+ break;
168
+ case SQLITE_TEXT: {
169
+ val = rb_utf8_str_new(
170
+ (const char *)sqlite3_column_text(stmt, i),
171
+ (long)sqlite3_column_bytes(stmt, i)
172
+ );
173
+ if (internal_encoding) {
174
+ val = rb_str_export_to_enc(val, internal_encoding);
175
+ }
176
+ rb_obj_freeze(val);
177
+ }
178
+ break;
179
+ case SQLITE_BLOB: {
180
+ val = rb_str_new(
181
+ (const char *)sqlite3_column_blob(stmt, i),
182
+ (long)sqlite3_column_bytes(stmt, i)
183
+ );
184
+ rb_obj_freeze(val);
185
+ }
186
+ break;
187
+ case SQLITE_NULL:
188
+ val = Qnil;
189
+ break;
190
+ default:
191
+ rb_raise(rb_eRuntimeError, "bad type");
192
+ }
193
+
194
+ rb_ary_store(list, (long)i, val);
195
+ }
188
196
  }
189
- }
190
- break;
191
- case SQLITE_DONE:
192
- ctx->done_p = 1;
193
- return Qnil;
194
- break;
195
- default:
196
- sqlite3_reset(stmt);
197
- ctx->done_p = 0;
198
- CHECK(sqlite3_db_handle(ctx->st), value);
199
- }
200
-
201
- return list;
197
+ break;
198
+ case SQLITE_DONE:
199
+ ctx->done_p = 1;
200
+ return Qnil;
201
+ break;
202
+ default:
203
+ sqlite3_reset(stmt);
204
+ ctx->done_p = 0;
205
+ CHECK(sqlite3_db_handle(ctx->st), value);
206
+ }
207
+
208
+ rb_obj_freeze(list);
209
+
210
+ return list;
202
211
  }
203
212
 
204
213
  /* call-seq: stmt.bind_param(key, value)
@@ -209,91 +218,95 @@ static VALUE step(VALUE self)
209
218
  *
210
219
  * See also #bind_params.
211
220
  */
212
- static VALUE bind_param(VALUE self, VALUE key, VALUE value)
221
+ static VALUE
222
+ bind_param(VALUE self, VALUE key, VALUE value)
213
223
  {
214
- sqlite3StmtRubyPtr ctx;
215
- int status;
216
- int index;
217
-
218
- TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
219
- REQUIRE_OPEN_STMT(ctx);
220
-
221
- switch(TYPE(key)) {
222
- case T_SYMBOL:
223
- key = rb_funcall(key, rb_intern("to_s"), 0);
224
- case T_STRING:
225
- if(RSTRING_PTR(key)[0] != ':') key = rb_str_plus(rb_str_new2(":"), key);
226
- index = sqlite3_bind_parameter_index(ctx->st, StringValuePtr(key));
227
- break;
228
- default:
229
- index = (int)NUM2INT(key);
230
- }
231
-
232
- if(index == 0)
233
- rb_raise(rb_path2class("SQLite3::Exception"), "no such bind parameter");
234
-
235
- switch(TYPE(value)) {
236
- case T_STRING:
237
- if(CLASS_OF(value) == cSqlite3Blob
238
- || rb_enc_get_index(value) == rb_ascii8bit_encindex()
239
- ) {
240
- status = sqlite3_bind_blob(
241
- ctx->st,
242
- index,
243
- (const char *)StringValuePtr(value),
244
- (int)RSTRING_LEN(value),
245
- SQLITE_TRANSIENT
246
- );
247
- } else {
248
-
249
-
250
- if (UTF16_LE_P(value) || UTF16_BE_P(value)) {
251
- status = sqlite3_bind_text16(
252
- ctx->st,
253
- index,
254
- (const char *)StringValuePtr(value),
255
- (int)RSTRING_LEN(value),
256
- SQLITE_TRANSIENT
257
- );
258
- } else {
259
- if (!UTF8_P(value) || !USASCII_P(value)) {
260
- value = rb_str_encode(value, rb_enc_from_encoding(rb_utf8_encoding()), 0, Qnil);
261
- }
262
- status = sqlite3_bind_text(
263
- ctx->st,
264
- index,
265
- (const char *)StringValuePtr(value),
266
- (int)RSTRING_LEN(value),
267
- SQLITE_TRANSIENT
268
- );
224
+ sqlite3StmtRubyPtr ctx;
225
+ int status;
226
+ int index;
227
+
228
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
229
+
230
+ REQUIRE_LIVE_DB(ctx);
231
+ REQUIRE_OPEN_STMT(ctx);
232
+
233
+ switch (TYPE(key)) {
234
+ case T_SYMBOL:
235
+ key = rb_funcall(key, rb_intern("to_s"), 0);
236
+ case T_STRING:
237
+ if (RSTRING_PTR(key)[0] != ':') { key = rb_str_plus(rb_str_new2(":"), key); }
238
+ index = sqlite3_bind_parameter_index(ctx->st, StringValuePtr(key));
239
+ break;
240
+ default:
241
+ index = (int)NUM2INT(key);
242
+ }
243
+
244
+ if (index == 0) {
245
+ rb_raise(rb_path2class("SQLite3::Exception"), "no such bind parameter");
246
+ }
247
+
248
+ switch (TYPE(value)) {
249
+ case T_STRING:
250
+ if (CLASS_OF(value) == cSqlite3Blob
251
+ || rb_enc_get_index(value) == rb_ascii8bit_encindex()
252
+ ) {
253
+ status = sqlite3_bind_blob(
254
+ ctx->st,
255
+ index,
256
+ (const char *)StringValuePtr(value),
257
+ (int)RSTRING_LEN(value),
258
+ SQLITE_TRANSIENT
259
+ );
260
+ } else {
261
+
262
+
263
+ if (UTF16_LE_P(value) || UTF16_BE_P(value)) {
264
+ status = sqlite3_bind_text16(
265
+ ctx->st,
266
+ index,
267
+ (const char *)StringValuePtr(value),
268
+ (int)RSTRING_LEN(value),
269
+ SQLITE_TRANSIENT
270
+ );
271
+ } else {
272
+ if (!UTF8_P(value) || !USASCII_P(value)) {
273
+ value = rb_str_encode(value, rb_enc_from_encoding(rb_utf8_encoding()), 0, Qnil);
274
+ }
275
+ status = sqlite3_bind_text(
276
+ ctx->st,
277
+ index,
278
+ (const char *)StringValuePtr(value),
279
+ (int)RSTRING_LEN(value),
280
+ SQLITE_TRANSIENT
281
+ );
282
+ }
283
+ }
284
+ break;
285
+ case T_BIGNUM: {
286
+ sqlite3_int64 num64;
287
+ if (bignum_to_int64(value, &num64)) {
288
+ status = sqlite3_bind_int64(ctx->st, index, num64);
289
+ break;
290
+ }
269
291
  }
270
- }
271
- break;
272
- case T_BIGNUM: {
273
- sqlite3_int64 num64;
274
- if (bignum_to_int64(value, &num64)) {
275
- status = sqlite3_bind_int64(ctx->st, index, num64);
276
- break;
277
- }
292
+ case T_FLOAT:
293
+ status = sqlite3_bind_double(ctx->st, index, NUM2DBL(value));
294
+ break;
295
+ case T_FIXNUM:
296
+ status = sqlite3_bind_int64(ctx->st, index, (sqlite3_int64)FIX2LONG(value));
297
+ break;
298
+ case T_NIL:
299
+ status = sqlite3_bind_null(ctx->st, index);
300
+ break;
301
+ default:
302
+ rb_raise(rb_eRuntimeError, "can't prepare %s",
303
+ rb_class2name(CLASS_OF(value)));
304
+ break;
278
305
  }
279
- case T_FLOAT:
280
- status = sqlite3_bind_double(ctx->st, index, NUM2DBL(value));
281
- break;
282
- case T_FIXNUM:
283
- status = sqlite3_bind_int64(ctx->st, index, (sqlite3_int64)FIX2LONG(value));
284
- break;
285
- case T_NIL:
286
- status = sqlite3_bind_null(ctx->st, index);
287
- break;
288
- default:
289
- rb_raise(rb_eRuntimeError, "can't prepare %s",
290
- rb_class2name(CLASS_OF(value)));
291
- break;
292
- }
293
-
294
- CHECK(sqlite3_db_handle(ctx->st), status);
295
-
296
- return self;
306
+
307
+ CHECK(sqlite3_db_handle(ctx->st), status);
308
+
309
+ return self;
297
310
  }
298
311
 
299
312
  /* call-seq: stmt.reset!
@@ -301,18 +314,21 @@ static VALUE bind_param(VALUE self, VALUE key, VALUE value)
301
314
  * Resets the statement. This is typically done internally, though it might
302
315
  * occasionally be necessary to manually reset the statement.
303
316
  */
304
- static VALUE reset_bang(VALUE self)
317
+ static VALUE
318
+ reset_bang(VALUE self)
305
319
  {
306
- sqlite3StmtRubyPtr ctx;
320
+ sqlite3StmtRubyPtr ctx;
321
+
322
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
307
323
 
308
- TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
309
- REQUIRE_OPEN_STMT(ctx);
324
+ REQUIRE_LIVE_DB(ctx);
325
+ REQUIRE_OPEN_STMT(ctx);
310
326
 
311
- sqlite3_reset(ctx->st);
327
+ sqlite3_reset(ctx->st);
312
328
 
313
- ctx->done_p = 0;
329
+ ctx->done_p = 0;
314
330
 
315
- return self;
331
+ return self;
316
332
  }
317
333
 
318
334
  /* call-seq: stmt.clear_bindings!
@@ -320,132 +336,369 @@ static VALUE reset_bang(VALUE self)
320
336
  * Resets the statement. This is typically done internally, though it might
321
337
  * occasionally be necessary to manually reset the statement.
322
338
  */
323
- static VALUE clear_bindings_bang(VALUE self)
339
+ static VALUE
340
+ clear_bindings_bang(VALUE self)
324
341
  {
325
- sqlite3StmtRubyPtr ctx;
342
+ sqlite3StmtRubyPtr ctx;
326
343
 
327
- TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
328
- REQUIRE_OPEN_STMT(ctx);
344
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
329
345
 
330
- sqlite3_clear_bindings(ctx->st);
346
+ REQUIRE_LIVE_DB(ctx);
347
+ REQUIRE_OPEN_STMT(ctx);
331
348
 
332
- ctx->done_p = 0;
349
+ sqlite3_clear_bindings(ctx->st);
333
350
 
334
- return self;
351
+ ctx->done_p = 0;
352
+
353
+ return self;
335
354
  }
336
355
 
337
356
  /* call-seq: stmt.done?
338
357
  *
339
358
  * returns true if all rows have been returned.
340
359
  */
341
- static VALUE done_p(VALUE self)
360
+ static VALUE
361
+ done_p(VALUE self)
342
362
  {
343
- sqlite3StmtRubyPtr ctx;
344
- TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
363
+ sqlite3StmtRubyPtr ctx;
364
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
345
365
 
346
- if(ctx->done_p) return Qtrue;
347
- return Qfalse;
366
+ if (ctx->done_p) { return Qtrue; }
367
+ return Qfalse;
348
368
  }
349
369
 
350
370
  /* call-seq: stmt.column_count
351
371
  *
352
372
  * Returns the number of columns to be returned for this statement
353
373
  */
354
- static VALUE column_count(VALUE self)
374
+ static VALUE
375
+ column_count(VALUE self)
355
376
  {
356
- sqlite3StmtRubyPtr ctx;
357
- TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
358
- REQUIRE_OPEN_STMT(ctx);
377
+ sqlite3StmtRubyPtr ctx;
378
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
379
+
380
+ REQUIRE_LIVE_DB(ctx);
381
+ REQUIRE_OPEN_STMT(ctx);
382
+
383
+ return INT2NUM(sqlite3_column_count(ctx->st));
384
+ }
359
385
 
360
- return INT2NUM(sqlite3_column_count(ctx->st));
386
+ #if HAVE_RB_ENC_INTERNED_STR_CSTR
387
+ static VALUE
388
+ interned_utf8_cstr(const char *str)
389
+ {
390
+ return rb_enc_interned_str_cstr(str, rb_utf8_encoding());
391
+ }
392
+ #else
393
+ static VALUE
394
+ interned_utf8_cstr(const char *str)
395
+ {
396
+ VALUE rb_str = rb_utf8_str_new_cstr(str);
397
+ return rb_funcall(rb_str, rb_intern("-@"), 0);
361
398
  }
399
+ #endif
362
400
 
363
401
  /* call-seq: stmt.column_name(index)
364
402
  *
365
403
  * Get the column name at +index+. 0 based.
366
404
  */
367
- static VALUE column_name(VALUE self, VALUE index)
405
+ static VALUE
406
+ column_name(VALUE self, VALUE index)
368
407
  {
369
- sqlite3StmtRubyPtr ctx;
370
- const char * name;
408
+ sqlite3StmtRubyPtr ctx;
409
+ const char *name;
410
+
411
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
412
+
413
+ REQUIRE_LIVE_DB(ctx);
414
+ REQUIRE_OPEN_STMT(ctx);
371
415
 
372
- TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
373
- REQUIRE_OPEN_STMT(ctx);
416
+ name = sqlite3_column_name(ctx->st, (int)NUM2INT(index));
374
417
 
375
- name = sqlite3_column_name(ctx->st, (int)NUM2INT(index));
418
+ VALUE ret = Qnil;
376
419
 
377
- if(name) return SQLITE3_UTF8_STR_NEW2(name);
378
- return Qnil;
420
+ if (name) {
421
+ ret = interned_utf8_cstr(name);
422
+ }
423
+ return ret;
379
424
  }
380
425
 
381
426
  /* call-seq: stmt.column_decltype(index)
382
427
  *
383
428
  * Get the column type at +index+. 0 based.
384
429
  */
385
- static VALUE column_decltype(VALUE self, VALUE index)
430
+ static VALUE
431
+ column_decltype(VALUE self, VALUE index)
386
432
  {
387
- sqlite3StmtRubyPtr ctx;
388
- const char * name;
433
+ sqlite3StmtRubyPtr ctx;
434
+ const char *name;
435
+
436
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
389
437
 
390
- TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
391
- REQUIRE_OPEN_STMT(ctx);
438
+ REQUIRE_LIVE_DB(ctx);
439
+ REQUIRE_OPEN_STMT(ctx);
392
440
 
393
- name = sqlite3_column_decltype(ctx->st, (int)NUM2INT(index));
441
+ name = sqlite3_column_decltype(ctx->st, (int)NUM2INT(index));
394
442
 
395
- if(name) return rb_str_new2(name);
396
- return Qnil;
443
+ if (name) { return rb_str_new2(name); }
444
+ return Qnil;
397
445
  }
398
446
 
399
447
  /* call-seq: stmt.bind_parameter_count
400
448
  *
401
449
  * Return the number of bind parameters
402
450
  */
403
- static VALUE bind_parameter_count(VALUE self)
451
+ static VALUE
452
+ bind_parameter_count(VALUE self)
453
+ {
454
+ sqlite3StmtRubyPtr ctx;
455
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
456
+
457
+ REQUIRE_LIVE_DB(ctx);
458
+ REQUIRE_OPEN_STMT(ctx);
459
+
460
+ return INT2NUM(sqlite3_bind_parameter_count(ctx->st));
461
+ }
462
+
463
+ enum stmt_stat_sym {
464
+ stmt_stat_sym_fullscan_steps,
465
+ stmt_stat_sym_sorts,
466
+ stmt_stat_sym_autoindexes,
467
+ stmt_stat_sym_vm_steps,
468
+ #ifdef SQLITE_STMTSTATUS_REPREPARE
469
+ stmt_stat_sym_reprepares,
470
+ #endif
471
+ #ifdef SQLITE_STMTSTATUS_RUN
472
+ stmt_stat_sym_runs,
473
+ #endif
474
+ #ifdef SQLITE_STMTSTATUS_FILTER_MISS
475
+ stmt_stat_sym_filter_misses,
476
+ #endif
477
+ #ifdef SQLITE_STMTSTATUS_FILTER_HIT
478
+ stmt_stat_sym_filter_hits,
479
+ #endif
480
+ stmt_stat_sym_last
481
+ };
482
+
483
+ static VALUE stmt_stat_symbols[stmt_stat_sym_last];
484
+
485
+ static void
486
+ setup_stmt_stat_symbols(void)
487
+ {
488
+ if (stmt_stat_symbols[0] == 0) {
489
+ #define S(s) stmt_stat_symbols[stmt_stat_sym_##s] = ID2SYM(rb_intern_const(#s))
490
+ S(fullscan_steps);
491
+ S(sorts);
492
+ S(autoindexes);
493
+ S(vm_steps);
494
+ #ifdef SQLITE_STMTSTATUS_REPREPARE
495
+ S(reprepares);
496
+ #endif
497
+ #ifdef SQLITE_STMTSTATUS_RUN
498
+ S(runs);
499
+ #endif
500
+ #ifdef SQLITE_STMTSTATUS_FILTER_MISS
501
+ S(filter_misses);
502
+ #endif
503
+ #ifdef SQLITE_STMTSTATUS_FILTER_HIT
504
+ S(filter_hits);
505
+ #endif
506
+ #undef S
507
+ }
508
+ }
509
+
510
+ static size_t
511
+ stmt_stat_internal(VALUE hash_or_sym, sqlite3_stmt *stmt)
512
+ {
513
+ VALUE hash = Qnil, key = Qnil;
514
+
515
+ setup_stmt_stat_symbols();
516
+
517
+ if (RB_TYPE_P(hash_or_sym, T_HASH)) {
518
+ hash = hash_or_sym;
519
+ } else if (SYMBOL_P(hash_or_sym)) {
520
+ key = hash_or_sym;
521
+ } else {
522
+ rb_raise(rb_eTypeError, "non-hash or symbol argument");
523
+ }
524
+
525
+ #define SET(name, stat_type) \
526
+ if (key == stmt_stat_symbols[stmt_stat_sym_##name]) \
527
+ return sqlite3_stmt_status(stmt, stat_type, 0); \
528
+ else if (hash != Qnil) \
529
+ rb_hash_aset(hash, stmt_stat_symbols[stmt_stat_sym_##name], SIZET2NUM(sqlite3_stmt_status(stmt, stat_type, 0)));
530
+
531
+ SET(fullscan_steps, SQLITE_STMTSTATUS_FULLSCAN_STEP);
532
+ SET(sorts, SQLITE_STMTSTATUS_SORT);
533
+ SET(autoindexes, SQLITE_STMTSTATUS_AUTOINDEX);
534
+ SET(vm_steps, SQLITE_STMTSTATUS_VM_STEP);
535
+ #ifdef SQLITE_STMTSTATUS_REPREPARE
536
+ SET(reprepares, SQLITE_STMTSTATUS_REPREPARE);
537
+ #endif
538
+ #ifdef SQLITE_STMTSTATUS_RUN
539
+ SET(runs, SQLITE_STMTSTATUS_RUN);
540
+ #endif
541
+ #ifdef SQLITE_STMTSTATUS_FILTER_MISS
542
+ SET(filter_misses, SQLITE_STMTSTATUS_FILTER_MISS);
543
+ #endif
544
+ #ifdef SQLITE_STMTSTATUS_FILTER_HIT
545
+ SET(filter_hits, SQLITE_STMTSTATUS_FILTER_HIT);
546
+ #endif
547
+ #undef SET
548
+
549
+ if (!NIL_P(key)) { /* matched key should return above */
550
+ rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(key));
551
+ }
552
+
553
+ return 0;
554
+ }
555
+
556
+ /* call-seq: stmt.stats_as_hash(hash)
557
+ *
558
+ * Returns a Hash containing information about the statement.
559
+ */
560
+ static VALUE
561
+ stats_as_hash(VALUE self)
404
562
  {
405
- sqlite3StmtRubyPtr ctx;
406
- TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
407
- REQUIRE_OPEN_STMT(ctx);
563
+ sqlite3StmtRubyPtr ctx;
564
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
565
+
566
+ REQUIRE_LIVE_DB(ctx);
567
+ REQUIRE_OPEN_STMT(ctx);
408
568
 
409
- return INT2NUM(sqlite3_bind_parameter_count(ctx->st));
569
+ VALUE arg = rb_hash_new();
570
+
571
+ stmt_stat_internal(arg, ctx->st);
572
+ return arg;
410
573
  }
411
574
 
575
+ /* call-seq: stmt.stmt_stat(hash_or_key)
576
+ *
577
+ * Returns a Hash containing information about the statement.
578
+ */
579
+ static VALUE
580
+ stat_for(VALUE self, VALUE key)
581
+ {
582
+ sqlite3StmtRubyPtr ctx;
583
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
584
+
585
+ REQUIRE_LIVE_DB(ctx);
586
+ REQUIRE_OPEN_STMT(ctx);
587
+
588
+ if (SYMBOL_P(key)) {
589
+ size_t value = stmt_stat_internal(key, ctx->st);
590
+ return SIZET2NUM(value);
591
+ } else {
592
+ rb_raise(rb_eTypeError, "non-symbol given");
593
+ }
594
+ }
595
+
596
+ #ifdef SQLITE_STMTSTATUS_MEMUSED
597
+ /* call-seq: stmt.memory_used
598
+ *
599
+ * Return the approximate number of bytes of heap memory used to store the prepared statement
600
+ */
601
+ static VALUE
602
+ memused(VALUE self)
603
+ {
604
+ sqlite3StmtRubyPtr ctx;
605
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
606
+
607
+ REQUIRE_LIVE_DB(ctx);
608
+ REQUIRE_OPEN_STMT(ctx);
609
+
610
+ return INT2NUM(sqlite3_stmt_status(ctx->st, SQLITE_STMTSTATUS_MEMUSED, 0));
611
+ }
612
+ #endif
613
+
412
614
  #ifdef HAVE_SQLITE3_COLUMN_DATABASE_NAME
413
615
 
414
616
  /* call-seq: stmt.database_name(column_index)
415
617
  *
416
618
  * Return the database name for the column at +column_index+
417
619
  */
418
- static VALUE database_name(VALUE self, VALUE index)
620
+ static VALUE
621
+ database_name(VALUE self, VALUE index)
419
622
  {
420
- sqlite3StmtRubyPtr ctx;
421
- TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
422
- REQUIRE_OPEN_STMT(ctx);
623
+ sqlite3StmtRubyPtr ctx;
624
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
625
+
626
+ REQUIRE_LIVE_DB(ctx);
627
+ REQUIRE_OPEN_STMT(ctx);
423
628
 
424
- return SQLITE3_UTF8_STR_NEW2(
425
- sqlite3_column_database_name(ctx->st, NUM2INT(index)));
629
+ return SQLITE3_UTF8_STR_NEW2(
630
+ sqlite3_column_database_name(ctx->st, NUM2INT(index)));
426
631
  }
427
632
 
428
633
  #endif
429
634
 
430
- void init_sqlite3_statement(void)
635
+ /* call-seq: stmt.sql
636
+ *
637
+ * Returns the SQL statement used to create this prepared statement
638
+ */
639
+ static VALUE
640
+ get_sql(VALUE self)
641
+ {
642
+ sqlite3StmtRubyPtr ctx;
643
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
644
+
645
+ REQUIRE_LIVE_DB(ctx);
646
+ REQUIRE_OPEN_STMT(ctx);
647
+
648
+ return rb_obj_freeze(SQLITE3_UTF8_STR_NEW2(sqlite3_sql(ctx->st)));
649
+ }
650
+
651
+ /* call-seq: stmt.expanded_sql
652
+ *
653
+ * Returns the SQL statement used to create this prepared statement, but
654
+ * with bind parameters substituted in to the statement.
655
+ */
656
+ static VALUE
657
+ get_expanded_sql(VALUE self)
431
658
  {
432
- cSqlite3Statement = rb_define_class_under(mSqlite3, "Statement", rb_cObject);
433
-
434
- rb_define_alloc_func(cSqlite3Statement, allocate);
435
- rb_define_method(cSqlite3Statement, "initialize", initialize, 2);
436
- rb_define_method(cSqlite3Statement, "close", sqlite3_rb_close, 0);
437
- rb_define_method(cSqlite3Statement, "closed?", closed_p, 0);
438
- rb_define_method(cSqlite3Statement, "bind_param", bind_param, 2);
439
- rb_define_method(cSqlite3Statement, "reset!", reset_bang, 0);
440
- rb_define_method(cSqlite3Statement, "clear_bindings!", clear_bindings_bang, 0);
441
- rb_define_method(cSqlite3Statement, "step", step, 0);
442
- rb_define_method(cSqlite3Statement, "done?", done_p, 0);
443
- rb_define_method(cSqlite3Statement, "column_count", column_count, 0);
444
- rb_define_method(cSqlite3Statement, "column_name", column_name, 1);
445
- rb_define_method(cSqlite3Statement, "column_decltype", column_decltype, 1);
446
- rb_define_method(cSqlite3Statement, "bind_parameter_count", bind_parameter_count, 0);
659
+ sqlite3StmtRubyPtr ctx;
660
+ char *expanded_sql;
661
+ VALUE rb_expanded_sql;
662
+
663
+ TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
447
664
 
665
+ REQUIRE_LIVE_DB(ctx);
666
+ REQUIRE_OPEN_STMT(ctx);
667
+
668
+ expanded_sql = sqlite3_expanded_sql(ctx->st);
669
+ rb_expanded_sql = rb_obj_freeze(SQLITE3_UTF8_STR_NEW2(expanded_sql));
670
+ sqlite3_free(expanded_sql);
671
+
672
+ return rb_expanded_sql;
673
+ }
674
+
675
+ void
676
+ init_sqlite3_statement(void)
677
+ {
678
+ cSqlite3Statement = rb_define_class_under(mSqlite3, "Statement", rb_cObject);
679
+
680
+ rb_define_alloc_func(cSqlite3Statement, allocate);
681
+ rb_define_method(cSqlite3Statement, "close", sqlite3_rb_close, 0);
682
+ rb_define_method(cSqlite3Statement, "closed?", closed_p, 0);
683
+ rb_define_method(cSqlite3Statement, "bind_param", bind_param, 2);
684
+ rb_define_method(cSqlite3Statement, "reset!", reset_bang, 0);
685
+ rb_define_method(cSqlite3Statement, "clear_bindings!", clear_bindings_bang, 0);
686
+ rb_define_method(cSqlite3Statement, "step", step, 0);
687
+ rb_define_method(cSqlite3Statement, "done?", done_p, 0);
688
+ rb_define_method(cSqlite3Statement, "column_count", column_count, 0);
689
+ rb_define_method(cSqlite3Statement, "column_name", column_name, 1);
690
+ rb_define_method(cSqlite3Statement, "column_decltype", column_decltype, 1);
691
+ rb_define_method(cSqlite3Statement, "bind_parameter_count", bind_parameter_count, 0);
692
+ rb_define_method(cSqlite3Statement, "sql", get_sql, 0);
693
+ rb_define_method(cSqlite3Statement, "expanded_sql", get_expanded_sql, 0);
448
694
  #ifdef HAVE_SQLITE3_COLUMN_DATABASE_NAME
449
- rb_define_method(cSqlite3Statement, "database_name", database_name, 1);
695
+ rb_define_method(cSqlite3Statement, "database_name", database_name, 1);
696
+ #endif
697
+ #ifdef SQLITE_STMTSTATUS_MEMUSED
698
+ rb_define_method(cSqlite3Statement, "memused", memused, 0);
450
699
  #endif
700
+
701
+ rb_define_private_method(cSqlite3Statement, "prepare", prepare, 2);
702
+ rb_define_private_method(cSqlite3Statement, "stats_as_hash", stats_as_hash, 0);
703
+ rb_define_private_method(cSqlite3Statement, "stat_for", stat_for, 1);
451
704
  }