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