sqlite3 1.3.8-x64-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/API_CHANGES.rdoc +50 -0
- data/CHANGELOG.rdoc +265 -0
- data/ChangeLog.cvs +88 -0
- data/Gemfile +15 -0
- data/LICENSE +27 -0
- data/Manifest.txt +52 -0
- data/README.rdoc +95 -0
- data/Rakefile +10 -0
- data/ext/sqlite3/backup.c +168 -0
- data/ext/sqlite3/backup.h +15 -0
- data/ext/sqlite3/database.c +822 -0
- data/ext/sqlite3/database.h +15 -0
- data/ext/sqlite3/exception.c +94 -0
- data/ext/sqlite3/exception.h +8 -0
- data/ext/sqlite3/extconf.rb +51 -0
- data/ext/sqlite3/sqlite3.c +40 -0
- data/ext/sqlite3/sqlite3_ruby.h +53 -0
- data/ext/sqlite3/statement.c +439 -0
- data/ext/sqlite3/statement.h +16 -0
- data/faq/faq.rb +145 -0
- data/faq/faq.yml +426 -0
- data/lib/sqlite3.rb +10 -0
- data/lib/sqlite3/2.0/sqlite3_native.so +0 -0
- data/lib/sqlite3/constants.rb +49 -0
- data/lib/sqlite3/database.rb +579 -0
- data/lib/sqlite3/errors.rb +44 -0
- data/lib/sqlite3/pragmas.rb +280 -0
- data/lib/sqlite3/resultset.rb +195 -0
- data/lib/sqlite3/statement.rb +144 -0
- data/lib/sqlite3/translator.rb +118 -0
- data/lib/sqlite3/value.rb +57 -0
- data/lib/sqlite3/version.rb +25 -0
- data/setup.rb +1333 -0
- data/tasks/faq.rake +9 -0
- data/tasks/gem.rake +37 -0
- data/tasks/native.rake +45 -0
- data/tasks/vendor_sqlite3.rake +87 -0
- data/test/helper.rb +18 -0
- data/test/test_backup.rb +33 -0
- data/test/test_collation.rb +82 -0
- data/test/test_database.rb +350 -0
- data/test/test_database_readonly.rb +29 -0
- data/test/test_deprecated.rb +44 -0
- data/test/test_encoding.rb +121 -0
- data/test/test_integration.rb +555 -0
- data/test/test_integration_open_close.rb +30 -0
- data/test/test_integration_pending.rb +115 -0
- data/test/test_integration_resultset.rb +159 -0
- data/test/test_integration_statement.rb +194 -0
- data/test/test_result_set.rb +37 -0
- data/test/test_sqlite3.rb +9 -0
- data/test/test_statement.rb +256 -0
- data/test/test_statement_execute.rb +35 -0
- metadata +246 -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,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,439 @@
|
|
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
|
+
|
83
|
+
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
84
|
+
|
85
|
+
REQUIRE_OPEN_STMT(ctx);
|
86
|
+
|
87
|
+
sqlite3_finalize(ctx->st);
|
88
|
+
ctx->st = NULL;
|
89
|
+
|
90
|
+
return self;
|
91
|
+
}
|
92
|
+
|
93
|
+
/* call-seq: stmt.closed?
|
94
|
+
*
|
95
|
+
* Returns true if the statement has been closed.
|
96
|
+
*/
|
97
|
+
static VALUE closed_p(VALUE self)
|
98
|
+
{
|
99
|
+
sqlite3StmtRubyPtr ctx;
|
100
|
+
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
101
|
+
|
102
|
+
if(!ctx->st) return Qtrue;
|
103
|
+
|
104
|
+
return Qfalse;
|
105
|
+
}
|
106
|
+
|
107
|
+
static VALUE step(VALUE self)
|
108
|
+
{
|
109
|
+
sqlite3StmtRubyPtr ctx;
|
110
|
+
sqlite3_stmt *stmt;
|
111
|
+
int value, length;
|
112
|
+
VALUE list;
|
113
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
114
|
+
rb_encoding * internal_encoding;
|
115
|
+
int enc_index;
|
116
|
+
#endif
|
117
|
+
|
118
|
+
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
119
|
+
|
120
|
+
REQUIRE_OPEN_STMT(ctx);
|
121
|
+
|
122
|
+
if(ctx->done_p) return Qnil;
|
123
|
+
|
124
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
125
|
+
{
|
126
|
+
VALUE db = rb_iv_get(self, "@connection");
|
127
|
+
VALUE encoding = rb_funcall(db, rb_intern("encoding"), 0);
|
128
|
+
enc_index = NIL_P(encoding) ? rb_utf8_encindex() : rb_to_encoding_index(encoding);
|
129
|
+
internal_encoding = rb_default_internal_encoding();
|
130
|
+
}
|
131
|
+
#endif
|
132
|
+
|
133
|
+
stmt = ctx->st;
|
134
|
+
|
135
|
+
value = sqlite3_step(stmt);
|
136
|
+
length = sqlite3_column_count(stmt);
|
137
|
+
list = rb_ary_new2((long)length);
|
138
|
+
|
139
|
+
switch(value) {
|
140
|
+
case SQLITE_ROW:
|
141
|
+
{
|
142
|
+
int i;
|
143
|
+
for(i = 0; i < length; i++) {
|
144
|
+
switch(sqlite3_column_type(stmt, i)) {
|
145
|
+
case SQLITE_INTEGER:
|
146
|
+
rb_ary_push(list, LL2NUM(sqlite3_column_int64(stmt, i)));
|
147
|
+
break;
|
148
|
+
case SQLITE_FLOAT:
|
149
|
+
rb_ary_push(list, rb_float_new(sqlite3_column_double(stmt, i)));
|
150
|
+
break;
|
151
|
+
case SQLITE_TEXT:
|
152
|
+
{
|
153
|
+
VALUE str = rb_tainted_str_new(
|
154
|
+
(const char *)sqlite3_column_text(stmt, i),
|
155
|
+
(long)sqlite3_column_bytes(stmt, i)
|
156
|
+
);
|
157
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
158
|
+
rb_enc_associate_index(str, enc_index);
|
159
|
+
if(internal_encoding)
|
160
|
+
str = rb_str_export_to_enc(str, internal_encoding);
|
161
|
+
#endif
|
162
|
+
rb_ary_push(list, str);
|
163
|
+
}
|
164
|
+
break;
|
165
|
+
case SQLITE_BLOB:
|
166
|
+
{
|
167
|
+
VALUE str = rb_tainted_str_new(
|
168
|
+
(const char *)sqlite3_column_blob(stmt, i),
|
169
|
+
(long)sqlite3_column_bytes(stmt, i)
|
170
|
+
);
|
171
|
+
rb_ary_push(list, str);
|
172
|
+
}
|
173
|
+
break;
|
174
|
+
case SQLITE_NULL:
|
175
|
+
rb_ary_push(list, Qnil);
|
176
|
+
break;
|
177
|
+
default:
|
178
|
+
rb_raise(rb_eRuntimeError, "bad type");
|
179
|
+
}
|
180
|
+
}
|
181
|
+
}
|
182
|
+
break;
|
183
|
+
case SQLITE_DONE:
|
184
|
+
ctx->done_p = 1;
|
185
|
+
return Qnil;
|
186
|
+
break;
|
187
|
+
default:
|
188
|
+
sqlite3_reset(stmt);
|
189
|
+
ctx->done_p = 0;
|
190
|
+
CHECK(sqlite3_db_handle(ctx->st), value);
|
191
|
+
}
|
192
|
+
|
193
|
+
return list;
|
194
|
+
}
|
195
|
+
|
196
|
+
/* call-seq: stmt.bind_param(key, value)
|
197
|
+
*
|
198
|
+
* Binds value to the named (or positional) placeholder. If +param+ is a
|
199
|
+
* Fixnum, it is treated as an index for a positional placeholder.
|
200
|
+
* Otherwise it is used as the name of the placeholder to bind to.
|
201
|
+
*
|
202
|
+
* See also #bind_params.
|
203
|
+
*/
|
204
|
+
static VALUE bind_param(VALUE self, VALUE key, VALUE value)
|
205
|
+
{
|
206
|
+
sqlite3StmtRubyPtr ctx;
|
207
|
+
int status;
|
208
|
+
int index;
|
209
|
+
|
210
|
+
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
211
|
+
REQUIRE_OPEN_STMT(ctx);
|
212
|
+
|
213
|
+
switch(TYPE(key)) {
|
214
|
+
case T_SYMBOL:
|
215
|
+
key = rb_funcall(key, rb_intern("to_s"), 0);
|
216
|
+
case T_STRING:
|
217
|
+
if(RSTRING_PTR(key)[0] != ':') key = rb_str_plus(rb_str_new2(":"), key);
|
218
|
+
index = sqlite3_bind_parameter_index(ctx->st, StringValuePtr(key));
|
219
|
+
break;
|
220
|
+
default:
|
221
|
+
index = (int)NUM2INT(key);
|
222
|
+
}
|
223
|
+
|
224
|
+
if(index == 0)
|
225
|
+
rb_raise(rb_path2class("SQLite3::Exception"), "no such bind parameter");
|
226
|
+
|
227
|
+
switch(TYPE(value)) {
|
228
|
+
case T_STRING:
|
229
|
+
if(CLASS_OF(value) == cSqlite3Blob
|
230
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
231
|
+
|| rb_enc_get_index(value) == rb_ascii8bit_encindex()
|
232
|
+
#endif
|
233
|
+
) {
|
234
|
+
status = sqlite3_bind_blob(
|
235
|
+
ctx->st,
|
236
|
+
index,
|
237
|
+
(const char *)StringValuePtr(value),
|
238
|
+
(int)RSTRING_LEN(value),
|
239
|
+
SQLITE_TRANSIENT
|
240
|
+
);
|
241
|
+
} else {
|
242
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
243
|
+
if(!UTF8_P(value)) {
|
244
|
+
VALUE db = rb_iv_get(self, "@connection");
|
245
|
+
VALUE encoding = rb_funcall(db, rb_intern("encoding"), 0);
|
246
|
+
rb_encoding * enc = rb_to_encoding(encoding);
|
247
|
+
value = rb_str_export_to_enc(value, enc);
|
248
|
+
}
|
249
|
+
#endif
|
250
|
+
|
251
|
+
status = sqlite3_bind_text(
|
252
|
+
ctx->st,
|
253
|
+
index,
|
254
|
+
(const char *)StringValuePtr(value),
|
255
|
+
(int)RSTRING_LEN(value),
|
256
|
+
SQLITE_TRANSIENT
|
257
|
+
);
|
258
|
+
}
|
259
|
+
break;
|
260
|
+
case T_BIGNUM:
|
261
|
+
if (RBIGNUM_LEN(value) * SIZEOF_BDIGITS <= 8) {
|
262
|
+
status = sqlite3_bind_int64(ctx->st, index, (sqlite3_int64)NUM2LL(value));
|
263
|
+
break;
|
264
|
+
}
|
265
|
+
case T_FLOAT:
|
266
|
+
status = sqlite3_bind_double(ctx->st, index, NUM2DBL(value));
|
267
|
+
break;
|
268
|
+
case T_FIXNUM:
|
269
|
+
status = sqlite3_bind_int64(ctx->st, index, (sqlite3_int64)FIX2LONG(value));
|
270
|
+
break;
|
271
|
+
case T_NIL:
|
272
|
+
status = sqlite3_bind_null(ctx->st, index);
|
273
|
+
break;
|
274
|
+
default:
|
275
|
+
rb_raise(rb_eRuntimeError, "can't prepare %s",
|
276
|
+
rb_class2name(CLASS_OF(value)));
|
277
|
+
break;
|
278
|
+
}
|
279
|
+
|
280
|
+
CHECK(sqlite3_db_handle(ctx->st), status);
|
281
|
+
|
282
|
+
return self;
|
283
|
+
}
|
284
|
+
|
285
|
+
/* call-seq: stmt.reset!
|
286
|
+
*
|
287
|
+
* Resets the statement. This is typically done internally, though it might
|
288
|
+
* occassionally be necessary to manually reset the statement.
|
289
|
+
*/
|
290
|
+
static VALUE reset_bang(VALUE self)
|
291
|
+
{
|
292
|
+
sqlite3StmtRubyPtr ctx;
|
293
|
+
int status;
|
294
|
+
|
295
|
+
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
296
|
+
REQUIRE_OPEN_STMT(ctx);
|
297
|
+
|
298
|
+
status = sqlite3_reset(ctx->st);
|
299
|
+
|
300
|
+
ctx->done_p = 0;
|
301
|
+
|
302
|
+
return self;
|
303
|
+
}
|
304
|
+
|
305
|
+
/* call-seq: stmt.clear_bindings!
|
306
|
+
*
|
307
|
+
* Resets the statement. This is typically done internally, though it might
|
308
|
+
* occassionally be necessary to manually reset the statement.
|
309
|
+
*/
|
310
|
+
static VALUE clear_bindings(VALUE self)
|
311
|
+
{
|
312
|
+
sqlite3StmtRubyPtr ctx;
|
313
|
+
int status;
|
314
|
+
|
315
|
+
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
316
|
+
REQUIRE_OPEN_STMT(ctx);
|
317
|
+
|
318
|
+
status = sqlite3_clear_bindings(ctx->st);
|
319
|
+
|
320
|
+
ctx->done_p = 0;
|
321
|
+
|
322
|
+
return self;
|
323
|
+
}
|
324
|
+
|
325
|
+
/* call-seq: stmt.done?
|
326
|
+
*
|
327
|
+
* returns true if all rows have been returned.
|
328
|
+
*/
|
329
|
+
static VALUE done_p(VALUE self)
|
330
|
+
{
|
331
|
+
sqlite3StmtRubyPtr ctx;
|
332
|
+
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
333
|
+
|
334
|
+
if(ctx->done_p) return Qtrue;
|
335
|
+
return Qfalse;
|
336
|
+
}
|
337
|
+
|
338
|
+
/* call-seq: stmt.column_count
|
339
|
+
*
|
340
|
+
* Returns the number of columns to be returned for this statement
|
341
|
+
*/
|
342
|
+
static VALUE column_count(VALUE self)
|
343
|
+
{
|
344
|
+
sqlite3StmtRubyPtr ctx;
|
345
|
+
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
346
|
+
REQUIRE_OPEN_STMT(ctx);
|
347
|
+
|
348
|
+
return INT2NUM((long)sqlite3_column_count(ctx->st));
|
349
|
+
}
|
350
|
+
|
351
|
+
/* call-seq: stmt.column_name(index)
|
352
|
+
*
|
353
|
+
* Get the column name at +index+. 0 based.
|
354
|
+
*/
|
355
|
+
static VALUE column_name(VALUE self, VALUE index)
|
356
|
+
{
|
357
|
+
sqlite3StmtRubyPtr ctx;
|
358
|
+
const char * name;
|
359
|
+
|
360
|
+
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
361
|
+
REQUIRE_OPEN_STMT(ctx);
|
362
|
+
|
363
|
+
name = sqlite3_column_name(ctx->st, (int)NUM2INT(index));
|
364
|
+
|
365
|
+
if(name) return SQLITE3_UTF8_STR_NEW2(name);
|
366
|
+
return Qnil;
|
367
|
+
}
|
368
|
+
|
369
|
+
/* call-seq: stmt.column_decltype(index)
|
370
|
+
*
|
371
|
+
* Get the column type at +index+. 0 based.
|
372
|
+
*/
|
373
|
+
static VALUE column_decltype(VALUE self, VALUE index)
|
374
|
+
{
|
375
|
+
sqlite3StmtRubyPtr ctx;
|
376
|
+
const char * name;
|
377
|
+
|
378
|
+
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
379
|
+
REQUIRE_OPEN_STMT(ctx);
|
380
|
+
|
381
|
+
name = sqlite3_column_decltype(ctx->st, (int)NUM2INT(index));
|
382
|
+
|
383
|
+
if(name) return rb_str_new2(name);
|
384
|
+
return Qnil;
|
385
|
+
}
|
386
|
+
|
387
|
+
/* call-seq: stmt.bind_parameter_count
|
388
|
+
*
|
389
|
+
* Return the number of bind parameters
|
390
|
+
*/
|
391
|
+
static VALUE bind_parameter_count(VALUE self)
|
392
|
+
{
|
393
|
+
sqlite3StmtRubyPtr ctx;
|
394
|
+
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
395
|
+
REQUIRE_OPEN_STMT(ctx);
|
396
|
+
|
397
|
+
return INT2NUM((long)sqlite3_bind_parameter_count(ctx->st));
|
398
|
+
}
|
399
|
+
|
400
|
+
#ifdef HAVE_SQLITE3_COLUMN_DATABASE_NAME
|
401
|
+
|
402
|
+
/* call-seq: stmt.database_name(column_index)
|
403
|
+
*
|
404
|
+
* Return the database name for the column at +column_index+
|
405
|
+
*/
|
406
|
+
static VALUE database_name(VALUE self, VALUE index)
|
407
|
+
{
|
408
|
+
sqlite3StmtRubyPtr ctx;
|
409
|
+
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
410
|
+
REQUIRE_OPEN_STMT(ctx);
|
411
|
+
|
412
|
+
return SQLITE3_UTF8_STR_NEW2(
|
413
|
+
sqlite3_column_database_name(ctx->st, NUM2INT(index)));
|
414
|
+
}
|
415
|
+
|
416
|
+
#endif
|
417
|
+
|
418
|
+
void init_sqlite3_statement()
|
419
|
+
{
|
420
|
+
cSqlite3Statement = rb_define_class_under(mSqlite3, "Statement", rb_cObject);
|
421
|
+
|
422
|
+
rb_define_alloc_func(cSqlite3Statement, allocate);
|
423
|
+
rb_define_method(cSqlite3Statement, "initialize", initialize, 2);
|
424
|
+
rb_define_method(cSqlite3Statement, "close", sqlite3_rb_close, 0);
|
425
|
+
rb_define_method(cSqlite3Statement, "closed?", closed_p, 0);
|
426
|
+
rb_define_method(cSqlite3Statement, "bind_param", bind_param, 2);
|
427
|
+
rb_define_method(cSqlite3Statement, "reset!", reset_bang, 0);
|
428
|
+
rb_define_method(cSqlite3Statement, "clear_bindings!", clear_bindings, 0);
|
429
|
+
rb_define_method(cSqlite3Statement, "step", step, 0);
|
430
|
+
rb_define_method(cSqlite3Statement, "done?", done_p, 0);
|
431
|
+
rb_define_method(cSqlite3Statement, "column_count", column_count, 0);
|
432
|
+
rb_define_method(cSqlite3Statement, "column_name", column_name, 1);
|
433
|
+
rb_define_method(cSqlite3Statement, "column_decltype", column_decltype, 1);
|
434
|
+
rb_define_method(cSqlite3Statement, "bind_parameter_count", bind_parameter_count, 0);
|
435
|
+
|
436
|
+
#ifdef HAVE_SQLITE3_COLUMN_DATABASE_NAME
|
437
|
+
rb_define_method(cSqlite3Statement, "database_name", database_name, 1);
|
438
|
+
#endif
|
439
|
+
}
|