femto-sqlite3 1.3.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/.gemtest +0 -0
  2. data/API_CHANGES.rdoc +50 -0
  3. data/CHANGELOG.rdoc +245 -0
  4. data/ChangeLog.cvs +88 -0
  5. data/LICENSE +27 -0
  6. data/Manifest.txt +50 -0
  7. data/README.rdoc +99 -0
  8. data/Rakefile +10 -0
  9. data/ext/sqlite3/backup.c +168 -0
  10. data/ext/sqlite3/backup.h +15 -0
  11. data/ext/sqlite3/database.c +822 -0
  12. data/ext/sqlite3/database.h +15 -0
  13. data/ext/sqlite3/exception.c +94 -0
  14. data/ext/sqlite3/exception.h +8 -0
  15. data/ext/sqlite3/extconf.rb +51 -0
  16. data/ext/sqlite3/sqlite3.c +40 -0
  17. data/ext/sqlite3/sqlite3_ruby.h +53 -0
  18. data/ext/sqlite3/statement.c +438 -0
  19. data/ext/sqlite3/statement.h +16 -0
  20. data/faq/faq.rb +145 -0
  21. data/faq/faq.yml +426 -0
  22. data/lib/sqlite3.rb +10 -0
  23. data/lib/sqlite3/constants.rb +49 -0
  24. data/lib/sqlite3/database.rb +579 -0
  25. data/lib/sqlite3/errors.rb +44 -0
  26. data/lib/sqlite3/pragmas.rb +280 -0
  27. data/lib/sqlite3/resultset.rb +195 -0
  28. data/lib/sqlite3/statement.rb +144 -0
  29. data/lib/sqlite3/translator.rb +118 -0
  30. data/lib/sqlite3/value.rb +57 -0
  31. data/lib/sqlite3/version.rb +25 -0
  32. data/setup.rb +1333 -0
  33. data/tasks/faq.rake +9 -0
  34. data/tasks/gem.rake +32 -0
  35. data/tasks/native.rake +37 -0
  36. data/tasks/vendor_sqlite3.rake +48 -0
  37. data/test/helper.rb +14 -0
  38. data/test/test_backup.rb +33 -0
  39. data/test/test_collation.rb +82 -0
  40. data/test/test_database.rb +350 -0
  41. data/test/test_database_readonly.rb +29 -0
  42. data/test/test_deprecated.rb +44 -0
  43. data/test/test_encoding.rb +121 -0
  44. data/test/test_integration.rb +555 -0
  45. data/test/test_integration_open_close.rb +30 -0
  46. data/test/test_integration_pending.rb +115 -0
  47. data/test/test_integration_resultset.rb +159 -0
  48. data/test/test_integration_statement.rb +194 -0
  49. data/test/test_result_set.rb +37 -0
  50. data/test/test_sqlite3.rb +9 -0
  51. data/test/test_statement.rb +249 -0
  52. data/test/test_statement_execute.rb +35 -0
  53. metadata +200 -0
@@ -0,0 +1,15 @@
1
+ #ifndef SQLITE3_DATABASE_RUBY
2
+ #define SQLITE3_DATABASE_RUBY
3
+
4
+ #include <sqlite3_ruby.h>
5
+
6
+ struct _sqlite3Ruby {
7
+ sqlite3 *db;
8
+ };
9
+
10
+ typedef struct _sqlite3Ruby sqlite3Ruby;
11
+ typedef sqlite3Ruby * sqlite3RubyPtr;
12
+
13
+ void init_sqlite3_database();
14
+
15
+ #endif
@@ -0,0 +1,94 @@
1
+ #include <sqlite3_ruby.h>
2
+
3
+ void rb_sqlite3_raise(sqlite3 * db, int status)
4
+ {
5
+ VALUE klass = Qnil;
6
+
7
+ switch(status) {
8
+ case SQLITE_OK:
9
+ return;
10
+ break;
11
+ case SQLITE_ERROR:
12
+ klass = rb_path2class("SQLite3::SQLException");
13
+ break;
14
+ case SQLITE_INTERNAL:
15
+ klass = rb_path2class("SQLite3::InternalException");
16
+ break;
17
+ case SQLITE_PERM:
18
+ klass = rb_path2class("SQLite3::PermissionException");
19
+ break;
20
+ case SQLITE_ABORT:
21
+ klass = rb_path2class("SQLite3::AbortException");
22
+ break;
23
+ case SQLITE_BUSY:
24
+ klass = rb_path2class("SQLite3::BusyException");
25
+ break;
26
+ case SQLITE_LOCKED:
27
+ klass = rb_path2class("SQLite3::LockedException");
28
+ break;
29
+ case SQLITE_NOMEM:
30
+ klass = rb_path2class("SQLite3::MemoryException");
31
+ break;
32
+ case SQLITE_READONLY:
33
+ klass = rb_path2class("SQLite3::ReadOnlyException");
34
+ break;
35
+ case SQLITE_INTERRUPT:
36
+ klass = rb_path2class("SQLite3::InterruptException");
37
+ break;
38
+ case SQLITE_IOERR:
39
+ klass = rb_path2class("SQLite3::IOException");
40
+ break;
41
+ case SQLITE_CORRUPT:
42
+ klass = rb_path2class("SQLite3::CorruptException");
43
+ break;
44
+ case SQLITE_NOTFOUND:
45
+ klass = rb_path2class("SQLite3::NotFoundException");
46
+ break;
47
+ case SQLITE_FULL:
48
+ klass = rb_path2class("SQLite3::FullException");
49
+ break;
50
+ case SQLITE_CANTOPEN:
51
+ klass = rb_path2class("SQLite3::CantOpenException");
52
+ break;
53
+ case SQLITE_PROTOCOL:
54
+ klass = rb_path2class("SQLite3::ProtocolException");
55
+ break;
56
+ case SQLITE_EMPTY:
57
+ klass = rb_path2class("SQLite3::EmptyException");
58
+ break;
59
+ case SQLITE_SCHEMA:
60
+ klass = rb_path2class("SQLite3::SchemaChangedException");
61
+ break;
62
+ case SQLITE_TOOBIG:
63
+ klass = rb_path2class("SQLite3::TooBigException");
64
+ break;
65
+ case SQLITE_CONSTRAINT:
66
+ klass = rb_path2class("SQLite3::ConstraintException");
67
+ break;
68
+ case SQLITE_MISMATCH:
69
+ klass = rb_path2class("SQLite3::MismatchException");
70
+ break;
71
+ case SQLITE_MISUSE:
72
+ klass = rb_path2class("SQLite3::MisuseException");
73
+ break;
74
+ case SQLITE_NOLFS:
75
+ klass = rb_path2class("SQLite3::UnsupportedException");
76
+ break;
77
+ case SQLITE_AUTH:
78
+ klass = rb_path2class("SQLite3::AuthorizationException");
79
+ break;
80
+ case SQLITE_FORMAT:
81
+ klass = rb_path2class("SQLite3::FormatException");
82
+ break;
83
+ case SQLITE_RANGE:
84
+ klass = rb_path2class("SQLite3::RangeException");
85
+ break;
86
+ case SQLITE_NOTADB:
87
+ klass = rb_path2class("SQLite3::NotADatabaseException");
88
+ break;
89
+ default:
90
+ klass = rb_eRuntimeError;
91
+ }
92
+
93
+ rb_raise(klass, "%s", sqlite3_errmsg(db));
94
+ }
@@ -0,0 +1,8 @@
1
+ #ifndef SQLITE3_EXCEPTION_RUBY
2
+ #define SQLITE3_EXCEPTION_RUBY
3
+
4
+ #define CHECK(_db, _status) rb_sqlite3_raise(_db, _status);
5
+
6
+ void rb_sqlite3_raise(sqlite3 * db, int status);
7
+
8
+ #endif
@@ -0,0 +1,51 @@
1
+ ENV['RC_ARCHS'] = '' if RUBY_PLATFORM =~ /darwin/
2
+
3
+ require 'mkmf'
4
+
5
+ # :stopdoc:
6
+
7
+ RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC'] if ENV['CC']
8
+
9
+ # --with-sqlite3-{dir,include,lib}
10
+ dir_config("sqlite3")
11
+
12
+ # prioritize local builds
13
+ if enable_config("local", false)
14
+ $LDFLAGS = ENV.fetch("LDFLAGS", "")
15
+ end
16
+
17
+ if RbConfig::CONFIG["host_os"] =~ /mswin/
18
+ $CFLAGS << ' -W3'
19
+ end
20
+
21
+ def asplode missing
22
+ if RUBY_PLATFORM =~ /mingw|mswin/
23
+ abort "#{missing} is missing. Install SQLite3 from " +
24
+ "http://www.sqlite.org/ first."
25
+ else
26
+ abort <<-error
27
+ #{missing} is missing. Try 'port install sqlite3 +universal'
28
+ or 'yum install sqlite-devel' and check your shared library search path (the
29
+ location where your sqlite3 shared library is located).
30
+ error
31
+ end
32
+ end
33
+
34
+ asplode('sqlite3.h') unless find_header 'sqlite3.h'
35
+ asplode('sqlite3') unless find_library 'sqlite3', 'sqlite3_libversion_number'
36
+
37
+ # Functions defined in 1.9 but not 1.8
38
+ have_func('rb_proc_arity')
39
+
40
+ # These functions may not be defined
41
+ have_func('sqlite3_initialize')
42
+ have_func('sqlite3_backup_init')
43
+ have_func('sqlite3_column_database_name')
44
+ have_func('sqlite3_enable_load_extension')
45
+ have_func('sqlite3_load_extension')
46
+ have_func('sqlite3_open_v2')
47
+ have_func('sqlite3_prepare_v2')
48
+ have_type('sqlite3_int64', 'sqlite3.h')
49
+ have_type('sqlite3_uint64', 'sqlite3.h')
50
+
51
+ create_makefile('sqlite3/sqlite3_native')
@@ -0,0 +1,40 @@
1
+ #include <sqlite3_ruby.h>
2
+
3
+ VALUE mSqlite3;
4
+ VALUE cSqlite3Blob;
5
+
6
+ static VALUE libversion(VALUE UNUSED(klass))
7
+ {
8
+ return INT2NUM(sqlite3_libversion_number());
9
+ }
10
+
11
+ void Init_sqlite3_native()
12
+ {
13
+ /*
14
+ * SQLite3 is a wrapper around the popular database
15
+ * sqlite[http://sqlite.org].
16
+ *
17
+ * For an example of usage, see SQLite3::Database.
18
+ */
19
+ mSqlite3 = rb_define_module("SQLite3");
20
+
21
+ /* A class for differentiating between strings and blobs, when binding them
22
+ * into statements.
23
+ */
24
+ cSqlite3Blob = rb_define_class_under(mSqlite3, "Blob", rb_cString);
25
+
26
+ /* Initialize the sqlite3 library */
27
+ #ifdef HAVE_SQLITE3_INITIALIZE
28
+ sqlite3_initialize();
29
+ #endif
30
+
31
+ init_sqlite3_database();
32
+ init_sqlite3_statement();
33
+ #ifdef HAVE_SQLITE3_BACKUP_INIT
34
+ init_sqlite3_backup();
35
+ #endif
36
+
37
+ rb_define_singleton_method(mSqlite3, "libversion", libversion, 0);
38
+ rb_define_const(mSqlite3, "SQLITE_VERSION", rb_str_new2(SQLITE_VERSION));
39
+ rb_define_const(mSqlite3, "SQLITE_VERSION_NUMBER", INT2FIX(SQLITE_VERSION_NUMBER));
40
+ }
@@ -0,0 +1,53 @@
1
+ #ifndef SQLITE3_RUBY
2
+ #define SQLITE3_RUBY
3
+
4
+ #include <ruby.h>
5
+
6
+ #ifdef UNUSED
7
+ #elif defined(__GNUC__)
8
+ # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
9
+ #elif defined(__LCLINT__)
10
+ # define UNUSED(x) /*@unused@*/ x
11
+ #else
12
+ # define UNUSED(x) x
13
+ #endif
14
+
15
+ #ifndef RBIGNUM_LEN
16
+ #define RBIGNUM_LEN(x) RBIGNUM(x)->len
17
+ #endif
18
+
19
+ #ifdef HAVE_RUBY_ENCODING_H
20
+ #include <ruby/encoding.h>
21
+
22
+ #define UTF8_P(_obj) (rb_enc_get_index(_obj) == rb_utf8_encindex())
23
+ #define UTF16_LE_P(_obj) (rb_enc_get_index(_obj) == rb_enc_find_index("UTF-16LE"))
24
+ #define UTF16_BE_P(_obj) (rb_enc_get_index(_obj) == rb_enc_find_index("UTF-16BE"))
25
+ #define SQLITE3_UTF8_STR_NEW2(_obj) \
26
+ (rb_enc_associate_index(rb_str_new2(_obj), rb_utf8_encindex()))
27
+
28
+ #else
29
+
30
+ #define SQLITE3_UTF8_STR_NEW2(_obj) (rb_str_new2(_obj))
31
+
32
+ #endif
33
+
34
+
35
+ #include <sqlite3.h>
36
+
37
+ #ifndef HAVE_TYPE_SQLITE3_INT64
38
+ typedef sqlite_int64 sqlite3_int64;
39
+ #endif
40
+
41
+ #ifndef HAVE_TYPE_SQLITE3_UINT64
42
+ typedef sqlite_uint64 sqlite3_uint64;
43
+ #endif
44
+
45
+ extern VALUE mSqlite3;
46
+ extern VALUE cSqlite3Blob;
47
+
48
+ #include <database.h>
49
+ #include <statement.h>
50
+ #include <exception.h>
51
+ #include <backup.h>
52
+
53
+ #endif
@@ -0,0 +1,438 @@
1
+ #include <sqlite3_ruby.h>
2
+
3
+ #define REQUIRE_OPEN_STMT(_ctxt) \
4
+ if(!_ctxt->st) \
5
+ rb_raise(rb_path2class("SQLite3::Exception"), "cannot use a closed statement");
6
+
7
+ VALUE cSqlite3Statement;
8
+
9
+ static void deallocate(void * ctx)
10
+ {
11
+ sqlite3StmtRubyPtr c = (sqlite3StmtRubyPtr)ctx;
12
+ xfree(c);
13
+ }
14
+
15
+ static VALUE allocate(VALUE klass)
16
+ {
17
+ sqlite3StmtRubyPtr ctx = xcalloc((size_t)1, sizeof(sqlite3StmtRuby));
18
+ ctx->st = NULL;
19
+ ctx->done_p = 0;
20
+
21
+ return Data_Wrap_Struct(klass, NULL, deallocate, ctx);
22
+ }
23
+
24
+ /* call-seq: SQLite3::Statement.new(db, sql)
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)
32
+ {
33
+ sqlite3RubyPtr db_ctx;
34
+ sqlite3StmtRubyPtr ctx;
35
+ const char *tail = NULL;
36
+ int status;
37
+
38
+ StringValue(sql);
39
+
40
+ Data_Get_Struct(db, sqlite3Ruby, db_ctx);
41
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
42
+
43
+ if(!db_ctx->db)
44
+ rb_raise(rb_eArgError, "prepare called on a closed database");
45
+
46
+ #ifdef HAVE_RUBY_ENCODING_H
47
+ if(!UTF8_P(sql)) {
48
+ sql = rb_str_export_to_enc(sql, rb_utf8_encoding());
49
+ }
50
+ #endif
51
+
52
+ #ifdef HAVE_SQLITE3_PREPARE_V2
53
+ status = sqlite3_prepare_v2(
54
+ #else
55
+ status = sqlite3_prepare(
56
+ #endif
57
+ db_ctx->db,
58
+ (const char *)StringValuePtr(sql),
59
+ (int)RSTRING_LEN(sql),
60
+ &ctx->st,
61
+ &tail
62
+ );
63
+
64
+ CHECK(db_ctx->db, status);
65
+
66
+ rb_iv_set(self, "@connection", db);
67
+ rb_iv_set(self, "@remainder", rb_str_new2(tail));
68
+ rb_iv_set(self, "@columns", Qnil);
69
+ rb_iv_set(self, "@types", Qnil);
70
+
71
+ return self;
72
+ }
73
+
74
+ /* call-seq: stmt.close
75
+ *
76
+ * Closes the statement by finalizing the underlying statement
77
+ * handle. The statement must not be used after being closed.
78
+ */
79
+ static VALUE sqlite3_rb_close(VALUE self)
80
+ {
81
+ sqlite3StmtRubyPtr ctx;
82
+ sqlite3 * db;
83
+
84
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
85
+
86
+ REQUIRE_OPEN_STMT(ctx);
87
+
88
+ sqlite3_finalize(ctx->st);
89
+ ctx->st = NULL;
90
+
91
+ return self;
92
+ }
93
+
94
+ /* call-seq: stmt.closed?
95
+ *
96
+ * Returns true if the statement has been closed.
97
+ */
98
+ static VALUE closed_p(VALUE self)
99
+ {
100
+ sqlite3StmtRubyPtr ctx;
101
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
102
+
103
+ if(!ctx->st) return Qtrue;
104
+
105
+ return Qfalse;
106
+ }
107
+
108
+ static VALUE step(VALUE self)
109
+ {
110
+ sqlite3StmtRubyPtr ctx;
111
+ sqlite3_stmt *stmt;
112
+ int value, length;
113
+ VALUE list;
114
+ #ifdef HAVE_RUBY_ENCODING_H
115
+ rb_encoding * internal_encoding;
116
+ int enc_index;
117
+ #endif
118
+
119
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
120
+
121
+ REQUIRE_OPEN_STMT(ctx);
122
+
123
+ if(ctx->done_p) return Qnil;
124
+
125
+ #ifdef HAVE_RUBY_ENCODING_H
126
+ {
127
+ 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);
130
+ internal_encoding = rb_default_internal_encoding();
131
+ }
132
+ #endif
133
+
134
+ stmt = ctx->st;
135
+
136
+ value = sqlite3_step(stmt);
137
+ length = sqlite3_column_count(stmt);
138
+ list = rb_ary_new2((long)length);
139
+
140
+ switch(value) {
141
+ case SQLITE_ROW:
142
+ {
143
+ int i;
144
+ for(i = 0; i < length; i++) {
145
+ switch(sqlite3_column_type(stmt, i)) {
146
+ case SQLITE_INTEGER:
147
+ rb_ary_push(list, LL2NUM(sqlite3_column_int64(stmt, i)));
148
+ break;
149
+ case SQLITE_FLOAT:
150
+ rb_ary_push(list, rb_float_new(sqlite3_column_double(stmt, i)));
151
+ break;
152
+ case SQLITE_TEXT:
153
+ {
154
+ VALUE str = rb_tainted_str_new(
155
+ (const char *)sqlite3_column_text(stmt, i),
156
+ (long)sqlite3_column_bytes(stmt, i)
157
+ );
158
+ #ifdef HAVE_RUBY_ENCODING_H
159
+ rb_enc_associate_index(str, enc_index);
160
+ if(internal_encoding)
161
+ str = rb_str_export_to_enc(str, internal_encoding);
162
+ #endif
163
+ rb_ary_push(list, str);
164
+ }
165
+ break;
166
+ case SQLITE_BLOB:
167
+ {
168
+ VALUE str = rb_tainted_str_new(
169
+ (const char *)sqlite3_column_blob(stmt, i),
170
+ (long)sqlite3_column_bytes(stmt, i)
171
+ );
172
+ rb_ary_push(list, str);
173
+ }
174
+ break;
175
+ case SQLITE_NULL:
176
+ rb_ary_push(list, Qnil);
177
+ break;
178
+ default:
179
+ rb_raise(rb_eRuntimeError, "bad type");
180
+ }
181
+ }
182
+ }
183
+ break;
184
+ case SQLITE_DONE:
185
+ ctx->done_p = 1;
186
+ return Qnil;
187
+ break;
188
+ default:
189
+ CHECK(sqlite3_db_handle(ctx->st), value);
190
+ }
191
+
192
+ return list;
193
+ }
194
+
195
+ /* call-seq: stmt.bind_param(key, value)
196
+ *
197
+ * Binds value to the named (or positional) placeholder. If +param+ is a
198
+ * Fixnum, it is treated as an index for a positional placeholder.
199
+ * Otherwise it is used as the name of the placeholder to bind to.
200
+ *
201
+ * See also #bind_params.
202
+ */
203
+ static VALUE bind_param(VALUE self, VALUE key, VALUE value)
204
+ {
205
+ sqlite3StmtRubyPtr ctx;
206
+ int status;
207
+ int index;
208
+
209
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
210
+ REQUIRE_OPEN_STMT(ctx);
211
+
212
+ switch(TYPE(key)) {
213
+ case T_SYMBOL:
214
+ key = rb_funcall(key, rb_intern("to_s"), 0);
215
+ case T_STRING:
216
+ if(RSTRING_PTR(key)[0] != ':') key = rb_str_plus(rb_str_new2(":"), key);
217
+ index = sqlite3_bind_parameter_index(ctx->st, StringValuePtr(key));
218
+ break;
219
+ default:
220
+ index = (int)NUM2INT(key);
221
+ }
222
+
223
+ if(index == 0)
224
+ rb_raise(rb_path2class("SQLite3::Exception"), "no such bind parameter");
225
+
226
+ switch(TYPE(value)) {
227
+ case T_STRING:
228
+ if(CLASS_OF(value) == cSqlite3Blob
229
+ #ifdef HAVE_RUBY_ENCODING_H
230
+ || rb_enc_get_index(value) == rb_ascii8bit_encindex()
231
+ #endif
232
+ ) {
233
+ status = sqlite3_bind_blob(
234
+ ctx->st,
235
+ index,
236
+ (const char *)StringValuePtr(value),
237
+ (int)RSTRING_LEN(value),
238
+ SQLITE_TRANSIENT
239
+ );
240
+ } else {
241
+ #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);
247
+ }
248
+ #endif
249
+
250
+ status = sqlite3_bind_text(
251
+ ctx->st,
252
+ index,
253
+ (const char *)StringValuePtr(value),
254
+ (int)RSTRING_LEN(value),
255
+ SQLITE_TRANSIENT
256
+ );
257
+ }
258
+ 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));
262
+ break;
263
+ }
264
+ case T_FLOAT:
265
+ status = sqlite3_bind_double(ctx->st, index, NUM2DBL(value));
266
+ break;
267
+ case T_FIXNUM:
268
+ status = sqlite3_bind_int64(ctx->st, index, (sqlite3_int64)FIX2LONG(value));
269
+ break;
270
+ case T_NIL:
271
+ status = sqlite3_bind_null(ctx->st, index);
272
+ break;
273
+ default:
274
+ rb_raise(rb_eRuntimeError, "can't prepare %s",
275
+ rb_class2name(CLASS_OF(value)));
276
+ break;
277
+ }
278
+
279
+ CHECK(sqlite3_db_handle(ctx->st), status);
280
+
281
+ return self;
282
+ }
283
+
284
+ /* call-seq: stmt.reset!
285
+ *
286
+ * Resets the statement. This is typically done internally, though it might
287
+ * occassionally be necessary to manually reset the statement.
288
+ */
289
+ static VALUE reset_bang(VALUE self)
290
+ {
291
+ sqlite3StmtRubyPtr ctx;
292
+ int status;
293
+
294
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
295
+ REQUIRE_OPEN_STMT(ctx);
296
+
297
+ status = sqlite3_reset(ctx->st);
298
+
299
+ ctx->done_p = 0;
300
+
301
+ return self;
302
+ }
303
+
304
+ /* call-seq: stmt.clear_bindings!
305
+ *
306
+ * Resets the statement. This is typically done internally, though it might
307
+ * occassionally be necessary to manually reset the statement.
308
+ */
309
+ static VALUE clear_bindings(VALUE self)
310
+ {
311
+ sqlite3StmtRubyPtr ctx;
312
+ int status;
313
+
314
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
315
+ REQUIRE_OPEN_STMT(ctx);
316
+
317
+ status = sqlite3_clear_bindings(ctx->st);
318
+
319
+ ctx->done_p = 0;
320
+
321
+ return self;
322
+ }
323
+
324
+ /* call-seq: stmt.done?
325
+ *
326
+ * returns true if all rows have been returned.
327
+ */
328
+ static VALUE done_p(VALUE self)
329
+ {
330
+ sqlite3StmtRubyPtr ctx;
331
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
332
+
333
+ if(ctx->done_p) return Qtrue;
334
+ return Qfalse;
335
+ }
336
+
337
+ /* call-seq: stmt.column_count
338
+ *
339
+ * Returns the number of columns to be returned for this statement
340
+ */
341
+ static VALUE column_count(VALUE self)
342
+ {
343
+ sqlite3StmtRubyPtr ctx;
344
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
345
+ REQUIRE_OPEN_STMT(ctx);
346
+
347
+ return INT2NUM((long)sqlite3_column_count(ctx->st));
348
+ }
349
+
350
+ /* call-seq: stmt.column_name(index)
351
+ *
352
+ * Get the column name at +index+. 0 based.
353
+ */
354
+ static VALUE column_name(VALUE self, VALUE index)
355
+ {
356
+ sqlite3StmtRubyPtr ctx;
357
+ const char * name;
358
+
359
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
360
+ REQUIRE_OPEN_STMT(ctx);
361
+
362
+ name = sqlite3_column_name(ctx->st, (int)NUM2INT(index));
363
+
364
+ if(name) return SQLITE3_UTF8_STR_NEW2(name);
365
+ return Qnil;
366
+ }
367
+
368
+ /* call-seq: stmt.column_decltype(index)
369
+ *
370
+ * Get the column type at +index+. 0 based.
371
+ */
372
+ static VALUE column_decltype(VALUE self, VALUE index)
373
+ {
374
+ sqlite3StmtRubyPtr ctx;
375
+ const char * name;
376
+
377
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
378
+ REQUIRE_OPEN_STMT(ctx);
379
+
380
+ name = sqlite3_column_decltype(ctx->st, (int)NUM2INT(index));
381
+
382
+ if(name) return rb_str_new2(name);
383
+ return Qnil;
384
+ }
385
+
386
+ /* call-seq: stmt.bind_parameter_count
387
+ *
388
+ * Return the number of bind parameters
389
+ */
390
+ static VALUE bind_parameter_count(VALUE self)
391
+ {
392
+ sqlite3StmtRubyPtr ctx;
393
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
394
+ REQUIRE_OPEN_STMT(ctx);
395
+
396
+ return INT2NUM((long)sqlite3_bind_parameter_count(ctx->st));
397
+ }
398
+
399
+ #ifdef HAVE_SQLITE3_COLUMN_DATABASE_NAME
400
+
401
+ /* call-seq: stmt.database_name(column_index)
402
+ *
403
+ * Return the database name for the column at +column_index+
404
+ */
405
+ static VALUE database_name(VALUE self, VALUE index)
406
+ {
407
+ sqlite3StmtRubyPtr ctx;
408
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
409
+ REQUIRE_OPEN_STMT(ctx);
410
+
411
+ return SQLITE3_UTF8_STR_NEW2(
412
+ sqlite3_column_database_name(ctx->st, NUM2INT(index)));
413
+ }
414
+
415
+ #endif
416
+
417
+ void init_sqlite3_statement()
418
+ {
419
+ cSqlite3Statement = rb_define_class_under(mSqlite3, "Statement", rb_cObject);
420
+
421
+ rb_define_alloc_func(cSqlite3Statement, allocate);
422
+ rb_define_method(cSqlite3Statement, "initialize", initialize, 2);
423
+ rb_define_method(cSqlite3Statement, "close", sqlite3_rb_close, 0);
424
+ rb_define_method(cSqlite3Statement, "closed?", closed_p, 0);
425
+ rb_define_method(cSqlite3Statement, "bind_param", bind_param, 2);
426
+ rb_define_method(cSqlite3Statement, "reset!", reset_bang, 0);
427
+ rb_define_method(cSqlite3Statement, "clear_bindings!", clear_bindings, 0);
428
+ rb_define_method(cSqlite3Statement, "step", step, 0);
429
+ rb_define_method(cSqlite3Statement, "done?", done_p, 0);
430
+ rb_define_method(cSqlite3Statement, "column_count", column_count, 0);
431
+ rb_define_method(cSqlite3Statement, "column_name", column_name, 1);
432
+ rb_define_method(cSqlite3Statement, "column_decltype", column_decltype, 1);
433
+ rb_define_method(cSqlite3Statement, "bind_parameter_count", bind_parameter_count, 0);
434
+
435
+ #ifdef HAVE_SQLITE3_COLUMN_DATABASE_NAME
436
+ rb_define_method(cSqlite3Statement, "database_name", database_name, 1);
437
+ #endif
438
+ }