dawanda-sqlite3 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/.gemtest +0 -0
  2. data/API_CHANGES.rdoc +50 -0
  3. data/CHANGELOG.rdoc +184 -0
  4. data/ChangeLog.cvs +88 -0
  5. data/LICENSE +27 -0
  6. data/Manifest.txt +50 -0
  7. data/README.rdoc +95 -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 +762 -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 +41 -0
  16. data/ext/sqlite3/sqlite3.c +40 -0
  17. data/ext/sqlite3/sqlite3_ruby.h +44 -0
  18. data/ext/sqlite3/statement.c +418 -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 +587 -0
  25. data/lib/sqlite3/errors.rb +44 -0
  26. data/lib/sqlite3/pragmas.rb +280 -0
  27. data/lib/sqlite3/resultset.rb +126 -0
  28. data/lib/sqlite3/statement.rb +148 -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 +31 -0
  35. data/tasks/native.rake +61 -0
  36. data/tasks/vendor_sqlite3.rake +104 -0
  37. data/test/helper.rb +3 -0
  38. data/test/test_backup.rb +33 -0
  39. data/test/test_collation.rb +82 -0
  40. data/test/test_database.rb +319 -0
  41. data/test/test_database_readonly.rb +29 -0
  42. data/test/test_deprecated.rb +37 -0
  43. data/test/test_encoding.rb +119 -0
  44. data/test/test_integration.rb +544 -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 +156 -0
  48. data/test/test_integration_statement.rb +194 -0
  49. data/test/test_sqlite3.rb +9 -0
  50. data/test/test_statement.rb +213 -0
  51. data/test/test_statement_execute.rb +35 -0
  52. metadata +184 -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,41 @@
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
+ sqlite = dir_config('sqlite3', ['/usr/local', '/opt/local', '/sw/local', '/usr'])
10
+
11
+ if RUBY_PLATFORM =~ /mswin/
12
+ $CFLAGS << ' -W3'
13
+ end
14
+
15
+ def asplode missing
16
+ if RUBY_PLATFORM =~ /mswin/
17
+ abort "#{missing} is missing. Install SQLite3 from " +
18
+ "http://www.sqlite.org/ first."
19
+ else
20
+ abort <<-error
21
+ #{missing} is missing. Try 'port install sqlite3 +universal'
22
+ or 'yum install sqlite3-devel' and check your shared library search path (the
23
+ location where your sqlite3 shared library is located).
24
+ error
25
+ end
26
+ end
27
+
28
+ asplode('sqlite3.h') unless find_header 'sqlite3.h'
29
+ asplode('sqlite3') unless find_library 'sqlite3', 'sqlite3_libversion_number'
30
+
31
+ # Functions defined in 1.9 but not 1.8
32
+ have_func('rb_proc_arity')
33
+
34
+ # These functions may not be defined
35
+ have_func('sqlite3_initialize')
36
+ have_func('sqlite3_backup_init')
37
+ have_func('sqlite3_column_database_name')
38
+ have_func('sqlite3_enable_load_extension')
39
+ have_func('sqlite3_load_extension')
40
+
41
+ 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,44 @@
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 SQLITE3_UTF8_STR_NEW2(_obj) \
25
+ (rb_enc_associate_index(rb_str_new2(_obj), rb_utf8_encindex()))
26
+
27
+ #else
28
+
29
+ #define SQLITE3_UTF8_STR_NEW2(_obj) (rb_str_new2(_obj))
30
+
31
+ #endif
32
+
33
+
34
+ #include <sqlite3.h>
35
+
36
+ extern VALUE mSqlite3;
37
+ extern VALUE cSqlite3Blob;
38
+
39
+ #include <database.h>
40
+ #include <statement.h>
41
+ #include <exception.h>
42
+ #include <backup.h>
43
+
44
+ #endif
@@ -0,0 +1,418 @@
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
+ status = sqlite3_prepare_v2(
53
+ db_ctx->db,
54
+ (const char *)StringValuePtr(sql),
55
+ (int)RSTRING_LEN(sql),
56
+ &ctx->st,
57
+ &tail
58
+ );
59
+
60
+ CHECK(db_ctx->db, status);
61
+
62
+ rb_iv_set(self, "@connection", db);
63
+ rb_iv_set(self, "@remainder", rb_str_new2(tail));
64
+ rb_iv_set(self, "@columns", Qnil);
65
+ rb_iv_set(self, "@types", Qnil);
66
+
67
+ return self;
68
+ }
69
+
70
+ /* call-seq: stmt.close
71
+ *
72
+ * Closes the statement by finalizing the underlying statement
73
+ * handle. The statement must not be used after being closed.
74
+ */
75
+ static VALUE sqlite3_rb_close(VALUE self)
76
+ {
77
+ sqlite3StmtRubyPtr ctx;
78
+ sqlite3 * db;
79
+
80
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
81
+
82
+ REQUIRE_OPEN_STMT(ctx);
83
+
84
+ db = sqlite3_db_handle(ctx->st);
85
+ CHECK(db, sqlite3_finalize(ctx->st));
86
+
87
+ ctx->st = NULL;
88
+
89
+ return self;
90
+ }
91
+
92
+ /* call-seq: stmt.closed?
93
+ *
94
+ * Returns true if the statement has been closed.
95
+ */
96
+ static VALUE closed_p(VALUE self)
97
+ {
98
+ sqlite3StmtRubyPtr ctx;
99
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
100
+
101
+ if(!ctx->st) return Qtrue;
102
+
103
+ return Qfalse;
104
+ }
105
+
106
+ static VALUE step(VALUE self)
107
+ {
108
+ sqlite3StmtRubyPtr ctx;
109
+ sqlite3_stmt *stmt;
110
+ int value, length;
111
+ VALUE list;
112
+ #ifdef HAVE_RUBY_ENCODING_H
113
+ rb_encoding * internal_encoding;
114
+ int enc_index;
115
+ #endif
116
+
117
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
118
+
119
+ REQUIRE_OPEN_STMT(ctx);
120
+
121
+ if(ctx->done_p) return Qnil;
122
+
123
+ #ifdef HAVE_RUBY_ENCODING_H
124
+ {
125
+ VALUE db = rb_iv_get(self, "@connection");
126
+ VALUE encoding = rb_funcall(db, rb_intern("encoding"), 0);
127
+ enc_index = NIL_P(encoding) ? rb_utf8_encindex() : rb_to_encoding_index(encoding);
128
+ internal_encoding = rb_default_internal_encoding();
129
+ }
130
+ #endif
131
+
132
+ stmt = ctx->st;
133
+
134
+ value = sqlite3_step(stmt);
135
+ length = sqlite3_column_count(stmt);
136
+ list = rb_ary_new2((long)length);
137
+
138
+ switch(value) {
139
+ case SQLITE_ROW:
140
+ {
141
+ int i;
142
+ for(i = 0; i < length; i++) {
143
+ switch(sqlite3_column_type(stmt, i)) {
144
+ case SQLITE_INTEGER:
145
+ rb_ary_push(list, LL2NUM(sqlite3_column_int64(stmt, i)));
146
+ break;
147
+ case SQLITE_FLOAT:
148
+ rb_ary_push(list, rb_float_new(sqlite3_column_double(stmt, i)));
149
+ break;
150
+ case SQLITE_TEXT:
151
+ {
152
+ VALUE str = rb_tainted_str_new(
153
+ (const char *)sqlite3_column_text(stmt, i),
154
+ (long)sqlite3_column_bytes(stmt, i)
155
+ );
156
+ #ifdef HAVE_RUBY_ENCODING_H
157
+ rb_enc_associate_index(str, enc_index);
158
+ if(internal_encoding)
159
+ str = rb_str_export_to_enc(str, internal_encoding);
160
+ #endif
161
+ rb_ary_push(list, str);
162
+ }
163
+ break;
164
+ case SQLITE_BLOB:
165
+ {
166
+ VALUE str = rb_tainted_str_new(
167
+ (const char *)sqlite3_column_blob(stmt, i),
168
+ (long)sqlite3_column_bytes(stmt, i)
169
+ );
170
+ rb_ary_push(list, str);
171
+ }
172
+ break;
173
+ case SQLITE_NULL:
174
+ rb_ary_push(list, Qnil);
175
+ break;
176
+ default:
177
+ rb_raise(rb_eRuntimeError, "bad type");
178
+ }
179
+ }
180
+ }
181
+ break;
182
+ case SQLITE_DONE:
183
+ ctx->done_p = 1;
184
+ return Qnil;
185
+ break;
186
+ default:
187
+ CHECK(sqlite3_db_handle(ctx->st), value);
188
+ }
189
+
190
+ return list;
191
+ }
192
+
193
+ /* call-seq: stmt.bind_param(key, value)
194
+ *
195
+ * Binds value to the named (or positional) placeholder. If +param+ is a
196
+ * Fixnum, it is treated as an index for a positional placeholder.
197
+ * Otherwise it is used as the name of the placeholder to bind to.
198
+ *
199
+ * See also #bind_params.
200
+ */
201
+ static VALUE bind_param(VALUE self, VALUE key, VALUE value)
202
+ {
203
+ sqlite3StmtRubyPtr ctx;
204
+ int status;
205
+ int index;
206
+
207
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
208
+ REQUIRE_OPEN_STMT(ctx);
209
+
210
+ switch(TYPE(key)) {
211
+ case T_SYMBOL:
212
+ key = rb_funcall(key, rb_intern("to_s"), 0);
213
+ case T_STRING:
214
+ if(RSTRING_PTR(key)[0] != ':') key = rb_str_plus(rb_str_new2(":"), key);
215
+ index = sqlite3_bind_parameter_index(ctx->st, StringValuePtr(key));
216
+ break;
217
+ default:
218
+ index = (int)NUM2INT(key);
219
+ }
220
+
221
+ if(index == 0)
222
+ rb_raise(rb_path2class("SQLite3::Exception"), "no such bind parameter");
223
+
224
+ switch(TYPE(value)) {
225
+ case T_STRING:
226
+ if(CLASS_OF(value) == cSqlite3Blob
227
+ #ifdef HAVE_RUBY_ENCODING_H
228
+ || rb_enc_get_index(value) == rb_ascii8bit_encindex()
229
+ #endif
230
+ ) {
231
+ status = sqlite3_bind_blob(
232
+ ctx->st,
233
+ index,
234
+ (const char *)StringValuePtr(value),
235
+ (int)RSTRING_LEN(value),
236
+ SQLITE_TRANSIENT
237
+ );
238
+ } else {
239
+ #ifdef HAVE_RUBY_ENCODING_H
240
+ if(!UTF8_P(value)) {
241
+ VALUE db = rb_iv_get(self, "@connection");
242
+ VALUE encoding = rb_funcall(db, rb_intern("encoding"), 0);
243
+ rb_encoding * enc = rb_to_encoding(encoding);
244
+ value = rb_str_export_to_enc(value, enc);
245
+ }
246
+ #endif
247
+
248
+ status = sqlite3_bind_text(
249
+ ctx->st,
250
+ index,
251
+ (const char *)StringValuePtr(value),
252
+ (int)RSTRING_LEN(value),
253
+ SQLITE_TRANSIENT
254
+ );
255
+ }
256
+ break;
257
+ case T_BIGNUM:
258
+ #if SIZEOF_LONG < 8
259
+ if (RBIGNUM_LEN(value) * SIZEOF_BDIGITS <= 8) {
260
+ status = sqlite3_bind_int64(ctx->st, index, (sqlite3_int64)NUM2LL(value));
261
+ break;
262
+ }
263
+ #endif
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
+ CHECK(sqlite3_db_handle(ctx->st), status);
299
+
300
+ ctx->done_p = 0;
301
+
302
+ return self;
303
+ }
304
+
305
+ /* call-seq: stmt.done?
306
+ *
307
+ * returns true if all rows have been returned.
308
+ */
309
+ static VALUE done_p(VALUE self)
310
+ {
311
+ sqlite3StmtRubyPtr ctx;
312
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
313
+
314
+ if(ctx->done_p) return Qtrue;
315
+ return Qfalse;
316
+ }
317
+
318
+ /* call-seq: stmt.column_count
319
+ *
320
+ * Returns the number of columns to be returned for this statement
321
+ */
322
+ static VALUE column_count(VALUE self)
323
+ {
324
+ sqlite3StmtRubyPtr ctx;
325
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
326
+ REQUIRE_OPEN_STMT(ctx);
327
+
328
+ return INT2NUM((long)sqlite3_column_count(ctx->st));
329
+ }
330
+
331
+ /* call-seq: stmt.column_name(index)
332
+ *
333
+ * Get the column name at +index+. 0 based.
334
+ */
335
+ static VALUE column_name(VALUE self, VALUE index)
336
+ {
337
+ sqlite3StmtRubyPtr ctx;
338
+ const char * name;
339
+
340
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
341
+ REQUIRE_OPEN_STMT(ctx);
342
+
343
+ name = sqlite3_column_name(ctx->st, (int)NUM2INT(index));
344
+
345
+ if(name) return rb_str_new2(name);
346
+ return Qnil;
347
+ }
348
+
349
+ /* call-seq: stmt.column_decltype(index)
350
+ *
351
+ * Get the column type at +index+. 0 based.
352
+ */
353
+ static VALUE column_decltype(VALUE self, VALUE index)
354
+ {
355
+ sqlite3StmtRubyPtr ctx;
356
+ const char * name;
357
+
358
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
359
+ REQUIRE_OPEN_STMT(ctx);
360
+
361
+ name = sqlite3_column_decltype(ctx->st, (int)NUM2INT(index));
362
+
363
+ if(name) return rb_str_new2(name);
364
+ return Qnil;
365
+ }
366
+
367
+ /* call-seq: stmt.bind_parameter_count
368
+ *
369
+ * Return the number of bind parameters
370
+ */
371
+ static VALUE bind_parameter_count(VALUE self)
372
+ {
373
+ sqlite3StmtRubyPtr ctx;
374
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
375
+ REQUIRE_OPEN_STMT(ctx);
376
+
377
+ return INT2NUM((long)sqlite3_bind_parameter_count(ctx->st));
378
+ }
379
+
380
+ #ifdef HAVE_SQLITE3_COLUMN_DATABASE_NAME
381
+
382
+ /* call-seq: stmt.database_name(column_index)
383
+ *
384
+ * Return the database name for the column at +column_index+
385
+ */
386
+ static VALUE database_name(VALUE self, VALUE index)
387
+ {
388
+ sqlite3StmtRubyPtr ctx;
389
+ Data_Get_Struct(self, sqlite3StmtRuby, ctx);
390
+ REQUIRE_OPEN_STMT(ctx);
391
+
392
+ return SQLITE3_UTF8_STR_NEW2(
393
+ sqlite3_column_database_name(ctx->st, NUM2INT(index)));
394
+ }
395
+
396
+ #endif
397
+
398
+ void init_sqlite3_statement()
399
+ {
400
+ cSqlite3Statement = rb_define_class_under(mSqlite3, "Statement", rb_cObject);
401
+
402
+ rb_define_alloc_func(cSqlite3Statement, allocate);
403
+ rb_define_method(cSqlite3Statement, "initialize", initialize, 2);
404
+ rb_define_method(cSqlite3Statement, "close", sqlite3_rb_close, 0);
405
+ rb_define_method(cSqlite3Statement, "closed?", closed_p, 0);
406
+ rb_define_method(cSqlite3Statement, "bind_param", bind_param, 2);
407
+ rb_define_method(cSqlite3Statement, "reset!", reset_bang, 0);
408
+ rb_define_method(cSqlite3Statement, "step", step, 0);
409
+ rb_define_method(cSqlite3Statement, "done?", done_p, 0);
410
+ rb_define_method(cSqlite3Statement, "column_count", column_count, 0);
411
+ rb_define_method(cSqlite3Statement, "column_name", column_name, 1);
412
+ rb_define_method(cSqlite3Statement, "column_decltype", column_decltype, 1);
413
+ rb_define_method(cSqlite3Statement, "bind_parameter_count", bind_parameter_count, 0);
414
+
415
+ #ifdef HAVE_SQLITE3_COLUMN_DATABASE_NAME
416
+ rb_define_method(cSqlite3Statement, "database_name", database_name, 1);
417
+ #endif
418
+ }