Update vendored Sqlite3-ruby to 1.3.0
Also, some tweaks to Maruku.
This commit is contained in:
parent
9a80cacc34
commit
4f8759cdf3
55 changed files with 3071 additions and 8028 deletions
693
vendor/plugins/sqlite3-ruby/ext/sqlite3/database.c
vendored
Normal file
693
vendor/plugins/sqlite3-ruby/ext/sqlite3/database.c
vendored
Normal file
|
@ -0,0 +1,693 @@
|
|||
#include <sqlite3_ruby.h>
|
||||
|
||||
#define REQUIRE_OPEN_DB(_ctxt) \
|
||||
if(!_ctxt->db) \
|
||||
rb_raise(rb_path2class("SQLite3::Exception"), "cannot use a closed database");
|
||||
|
||||
VALUE cSqlite3Database;
|
||||
static VALUE sym_utf16, sym_results_as_hash, sym_type_translation;
|
||||
|
||||
static void deallocate(void * ctx)
|
||||
{
|
||||
sqlite3RubyPtr c = (sqlite3RubyPtr)ctx;
|
||||
sqlite3 * db = c->db;
|
||||
sqlite3_stmt * stmt;
|
||||
|
||||
if(db) {
|
||||
while((stmt = sqlite3_next_stmt(db, NULL)) != NULL) {
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
sqlite3_close(db);
|
||||
}
|
||||
xfree(c);
|
||||
}
|
||||
|
||||
static VALUE allocate(VALUE klass)
|
||||
{
|
||||
sqlite3RubyPtr ctx = xcalloc((size_t)1, sizeof(sqlite3Ruby));
|
||||
return Data_Wrap_Struct(klass, NULL, deallocate, ctx);
|
||||
}
|
||||
|
||||
static char *
|
||||
utf16_string_value_ptr(VALUE str)
|
||||
{
|
||||
StringValue(str);
|
||||
rb_str_buf_cat(str, "\x00", 1L);
|
||||
return RSTRING_PTR(str);
|
||||
}
|
||||
|
||||
/* call-seq: SQLite3::Database.new(file, options = {})
|
||||
*
|
||||
* Create a new Database object that opens the given file. If utf16
|
||||
* is +true+, the filename is interpreted as a UTF-16 encoded string.
|
||||
*
|
||||
* By default, the new database will return result rows as arrays
|
||||
* (#results_as_hash) and has type translation disabled (#type_translation=).
|
||||
*/
|
||||
static VALUE initialize(int argc, VALUE *argv, VALUE self)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
VALUE file;
|
||||
VALUE opts;
|
||||
VALUE zvfs;
|
||||
int status;
|
||||
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
|
||||
rb_scan_args(argc, argv, "12", &file, &opts, &zvfs);
|
||||
if(NIL_P(opts)) opts = rb_hash_new();
|
||||
|
||||
#ifdef HAVE_RUBY_ENCODING_H
|
||||
if(UTF16_LE_P(file)) {
|
||||
status = sqlite3_open16(utf16_string_value_ptr(file), &ctx->db);
|
||||
} else {
|
||||
#endif
|
||||
|
||||
if(Qtrue == rb_hash_aref(opts, sym_utf16)) {
|
||||
status = sqlite3_open16(utf16_string_value_ptr(file), &ctx->db);
|
||||
} else {
|
||||
|
||||
#ifdef HAVE_RUBY_ENCODING_H
|
||||
if(!UTF8_P(file)) {
|
||||
file = rb_str_export_to_enc(file, rb_utf8_encoding());
|
||||
}
|
||||
#endif
|
||||
|
||||
status = sqlite3_open_v2(
|
||||
StringValuePtr(file),
|
||||
&ctx->db,
|
||||
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
|
||||
NIL_P(zvfs) ? NULL : StringValuePtr(zvfs)
|
||||
);
|
||||
}
|
||||
|
||||
#ifdef HAVE_RUBY_ENCODING_H
|
||||
}
|
||||
#endif
|
||||
|
||||
CHECK(ctx->db, status)
|
||||
|
||||
rb_iv_set(self, "@tracefunc", Qnil);
|
||||
rb_iv_set(self, "@authorizer", Qnil);
|
||||
rb_iv_set(self, "@encoding", Qnil);
|
||||
rb_iv_set(self, "@busy_handler", Qnil);
|
||||
rb_iv_set(self, "@results_as_hash", rb_hash_aref(opts, sym_results_as_hash));
|
||||
rb_iv_set(self, "@type_translation", rb_hash_aref(opts, sym_type_translation));
|
||||
|
||||
if(rb_block_given_p()) {
|
||||
rb_yield(self);
|
||||
rb_funcall(self, rb_intern("close"), 0);
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/* call-seq: db.close
|
||||
*
|
||||
* Closes this database.
|
||||
*/
|
||||
static VALUE sqlite3_rb_close(VALUE self)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
sqlite3 * db;
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
|
||||
db = ctx->db;
|
||||
CHECK(db, sqlite3_close(ctx->db));
|
||||
|
||||
ctx->db = NULL;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/* call-seq: db.closed?
|
||||
*
|
||||
* Returns +true+ if this database instance has been closed (see #close).
|
||||
*/
|
||||
static VALUE closed_p(VALUE self)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
|
||||
if(!ctx->db) return Qtrue;
|
||||
|
||||
return Qfalse;
|
||||
}
|
||||
|
||||
/* call-seq: total_changes
|
||||
*
|
||||
* Returns the total number of changes made to this database instance
|
||||
* since it was opened.
|
||||
*/
|
||||
static VALUE total_changes(VALUE self)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
REQUIRE_OPEN_DB(ctx);
|
||||
|
||||
return INT2NUM((long)sqlite3_total_changes(ctx->db));
|
||||
}
|
||||
|
||||
static void tracefunc(void * data, const char *sql)
|
||||
{
|
||||
VALUE self = (VALUE)data;
|
||||
VALUE thing = rb_iv_get(self, "@tracefunc");
|
||||
rb_funcall(thing, rb_intern("call"), 1, rb_str_new2(sql));
|
||||
}
|
||||
|
||||
/* call-seq:
|
||||
* trace { |sql| ... }
|
||||
* trace(Class.new { def call sql; end }.new)
|
||||
*
|
||||
* Installs (or removes) a block that will be invoked for every SQL
|
||||
* statement executed. The block receives one parameter: the SQL statement
|
||||
* executed. If the block is +nil+, any existing tracer will be uninstalled.
|
||||
*/
|
||||
static VALUE trace(int argc, VALUE *argv, VALUE self)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
VALUE block;
|
||||
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
REQUIRE_OPEN_DB(ctx);
|
||||
|
||||
rb_scan_args(argc, argv, "01", &block);
|
||||
|
||||
if(NIL_P(block) && rb_block_given_p()) block = rb_block_proc();
|
||||
|
||||
rb_iv_set(self, "@tracefunc", block);
|
||||
|
||||
sqlite3_trace(ctx->db, NIL_P(block) ? NULL : tracefunc, (void *)self);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
static int rb_sqlite3_busy_handler(void * ctx, int count)
|
||||
{
|
||||
VALUE self = (VALUE)(ctx);
|
||||
VALUE handle = rb_iv_get(self, "@busy_handler");
|
||||
VALUE result = rb_funcall(handle, rb_intern("call"), 1, INT2NUM((long)count));
|
||||
|
||||
if(Qfalse == result) return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* call-seq:
|
||||
* busy_handler { |count| ... }
|
||||
* busy_handler(Class.new { def call count; end }.new)
|
||||
*
|
||||
* Register a busy handler with this database instance. When a requested
|
||||
* resource is busy, this handler will be invoked. If the handler returns
|
||||
* +false+, the operation will be aborted; otherwise, the resource will
|
||||
* be requested again.
|
||||
*
|
||||
* The handler will be invoked with the name of the resource that was
|
||||
* busy, and the number of times it has been retried.
|
||||
*
|
||||
* See also the mutually exclusive #busy_timeout.
|
||||
*/
|
||||
static VALUE busy_handler(int argc, VALUE *argv, VALUE self)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
VALUE block;
|
||||
int status;
|
||||
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
REQUIRE_OPEN_DB(ctx);
|
||||
|
||||
rb_scan_args(argc, argv, "01", &block);
|
||||
|
||||
if(NIL_P(block) && rb_block_given_p()) block = rb_block_proc();
|
||||
|
||||
rb_iv_set(self, "@busy_handler", block);
|
||||
|
||||
status = sqlite3_busy_handler(
|
||||
ctx->db, NIL_P(block) ? NULL : rb_sqlite3_busy_handler, (void *)self);
|
||||
|
||||
CHECK(ctx->db, status);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/* call-seq: last_insert_row_id
|
||||
*
|
||||
* Obtains the unique row ID of the last row to be inserted by this Database
|
||||
* instance.
|
||||
*/
|
||||
static VALUE last_insert_row_id(VALUE self)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
REQUIRE_OPEN_DB(ctx);
|
||||
|
||||
return LL2NUM(sqlite3_last_insert_rowid(ctx->db));
|
||||
}
|
||||
|
||||
static VALUE sqlite3val2rb(sqlite3_value * val)
|
||||
{
|
||||
switch(sqlite3_value_type(val)) {
|
||||
case SQLITE_INTEGER:
|
||||
return LL2NUM(sqlite3_value_int64(val));
|
||||
break;
|
||||
case SQLITE_FLOAT:
|
||||
return rb_float_new(sqlite3_value_double(val));
|
||||
break;
|
||||
case SQLITE_TEXT:
|
||||
return rb_tainted_str_new2((const char *)sqlite3_value_text(val));
|
||||
break;
|
||||
case SQLITE_BLOB:
|
||||
return rb_tainted_str_new2((const char *)sqlite3_value_blob(val));
|
||||
break;
|
||||
case SQLITE_NULL:
|
||||
return Qnil;
|
||||
break;
|
||||
default:
|
||||
rb_raise(rb_eRuntimeError, "bad type"); /* FIXME */
|
||||
}
|
||||
}
|
||||
|
||||
static void set_sqlite3_func_result(sqlite3_context * ctx, VALUE result)
|
||||
{
|
||||
switch(TYPE(result)) {
|
||||
case T_NIL:
|
||||
sqlite3_result_null(ctx);
|
||||
break;
|
||||
case T_FIXNUM:
|
||||
sqlite3_result_int64(ctx, (sqlite3_int64)FIX2LONG(result));
|
||||
break;
|
||||
case T_BIGNUM:
|
||||
#if SIZEOF_LONG < 8
|
||||
if (RBIGNUM_LEN(result) * SIZEOF_BDIGITS <= 8) {
|
||||
sqlite3_result_int64(ctx, NUM2LL(result));
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
case T_FLOAT:
|
||||
sqlite3_result_double(ctx, NUM2DBL(result));
|
||||
break;
|
||||
case T_STRING:
|
||||
sqlite3_result_text(
|
||||
ctx,
|
||||
(const char *)StringValuePtr(result),
|
||||
(int)RSTRING_LEN(result),
|
||||
SQLITE_TRANSIENT
|
||||
);
|
||||
break;
|
||||
default:
|
||||
rb_raise(rb_eRuntimeError, "can't return %s",
|
||||
rb_class2name(CLASS_OF(result)));
|
||||
}
|
||||
}
|
||||
|
||||
static void rb_sqlite3_func(sqlite3_context * ctx, int argc, sqlite3_value **argv)
|
||||
{
|
||||
VALUE callable = (VALUE)sqlite3_user_data(ctx);
|
||||
VALUE * params = NULL;
|
||||
VALUE result;
|
||||
int i;
|
||||
|
||||
if (argc > 0) {
|
||||
params = xcalloc((size_t)argc, sizeof(VALUE *));
|
||||
|
||||
for(i = 0; i < argc; i++) {
|
||||
params[i] = sqlite3val2rb(argv[i]);
|
||||
}
|
||||
}
|
||||
|
||||
result = rb_funcall2(callable, rb_intern("call"), argc, params);
|
||||
xfree(params);
|
||||
|
||||
set_sqlite3_func_result(ctx, result);
|
||||
}
|
||||
|
||||
#ifndef HAVE_RB_PROC_ARITY
|
||||
int rb_proc_arity(VALUE self)
|
||||
{
|
||||
return (int)NUM2INT(rb_funcall(self, rb_intern("arity"), 0));
|
||||
}
|
||||
#endif
|
||||
|
||||
/* call-seq: define_function(name) { |args,...| }
|
||||
*
|
||||
* Define a function named +name+ with +args+. The arity of the block
|
||||
* will be used as the arity for the function defined.
|
||||
*/
|
||||
static VALUE define_function(VALUE self, VALUE name)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
VALUE block;
|
||||
int status;
|
||||
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
REQUIRE_OPEN_DB(ctx);
|
||||
|
||||
block = rb_block_proc();
|
||||
|
||||
status = sqlite3_create_function(
|
||||
ctx->db,
|
||||
StringValuePtr(name),
|
||||
rb_proc_arity(block),
|
||||
SQLITE_UTF8,
|
||||
(void *)block,
|
||||
rb_sqlite3_func,
|
||||
NULL,
|
||||
NULL
|
||||
);
|
||||
|
||||
CHECK(ctx->db, status);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
static int sqlite3_obj_method_arity(VALUE obj, ID id)
|
||||
{
|
||||
VALUE method = rb_funcall(obj, rb_intern("method"), 1, ID2SYM(id));
|
||||
VALUE arity = rb_funcall(method, rb_intern("arity"), 0);
|
||||
|
||||
return (int)NUM2INT(arity);
|
||||
}
|
||||
|
||||
static void rb_sqlite3_step(sqlite3_context * ctx, int argc, sqlite3_value **argv)
|
||||
{
|
||||
VALUE callable = (VALUE)sqlite3_user_data(ctx);
|
||||
VALUE * params = NULL;
|
||||
int i;
|
||||
|
||||
if (argc > 0) {
|
||||
params = xcalloc((size_t)argc, sizeof(VALUE *));
|
||||
for(i = 0; i < argc; i++) {
|
||||
params[i] = sqlite3val2rb(argv[i]);
|
||||
}
|
||||
}
|
||||
rb_funcall2(callable, rb_intern("step"), argc, params);
|
||||
xfree(params);
|
||||
}
|
||||
|
||||
static void rb_sqlite3_final(sqlite3_context * ctx)
|
||||
{
|
||||
VALUE callable = (VALUE)sqlite3_user_data(ctx);
|
||||
VALUE result = rb_funcall(callable, rb_intern("finalize"), 0);
|
||||
set_sqlite3_func_result(ctx, result);
|
||||
}
|
||||
|
||||
/* call-seq: define_aggregator(name, aggregator)
|
||||
*
|
||||
* Define an aggregate function named +name+ using the object +aggregator+.
|
||||
* +aggregator+ must respond to +step+ and +finalize+. +step+ will be called
|
||||
* with row information and +finalize+ must return the return value for the
|
||||
* aggregator function.
|
||||
*/
|
||||
static VALUE define_aggregator(VALUE self, VALUE name, VALUE aggregator)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
int arity, status;
|
||||
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
REQUIRE_OPEN_DB(ctx);
|
||||
|
||||
arity = sqlite3_obj_method_arity(aggregator, rb_intern("step"));
|
||||
|
||||
status = sqlite3_create_function(
|
||||
ctx->db,
|
||||
StringValuePtr(name),
|
||||
arity,
|
||||
SQLITE_UTF8,
|
||||
(void *)aggregator,
|
||||
NULL,
|
||||
rb_sqlite3_step,
|
||||
rb_sqlite3_final
|
||||
);
|
||||
|
||||
rb_iv_set(self, "@agregator", aggregator);
|
||||
|
||||
CHECK(ctx->db, status);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/* call-seq: interrupt
|
||||
*
|
||||
* Interrupts the currently executing operation, causing it to abort.
|
||||
*/
|
||||
static VALUE interrupt(VALUE self)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
REQUIRE_OPEN_DB(ctx);
|
||||
|
||||
sqlite3_interrupt(ctx->db);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/* call-seq: errmsg
|
||||
*
|
||||
* Return a string describing the last error to have occurred with this
|
||||
* database.
|
||||
*/
|
||||
static VALUE errmsg(VALUE self)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
REQUIRE_OPEN_DB(ctx);
|
||||
|
||||
return rb_str_new2(sqlite3_errmsg(ctx->db));
|
||||
}
|
||||
|
||||
/* call-seq: errcode
|
||||
*
|
||||
* Return an integer representing the last error to have occurred with this
|
||||
* database.
|
||||
*/
|
||||
static VALUE errcode_(VALUE self)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
REQUIRE_OPEN_DB(ctx);
|
||||
|
||||
return INT2NUM((long)sqlite3_errcode(ctx->db));
|
||||
}
|
||||
|
||||
/* call-seq: complete?(sql)
|
||||
*
|
||||
* Return +true+ if the string is a valid (ie, parsable) SQL statement, and
|
||||
* +false+ otherwise.
|
||||
*/
|
||||
static VALUE complete_p(VALUE UNUSED(self), VALUE sql)
|
||||
{
|
||||
if(sqlite3_complete(StringValuePtr(sql)))
|
||||
return Qtrue;
|
||||
|
||||
return Qfalse;
|
||||
}
|
||||
|
||||
/* call-seq: changes
|
||||
*
|
||||
* Returns the number of changes made to this database instance by the last
|
||||
* operation performed. Note that a "delete from table" without a where
|
||||
* clause will not affect this value.
|
||||
*/
|
||||
static VALUE changes(VALUE self)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
REQUIRE_OPEN_DB(ctx);
|
||||
|
||||
return INT2NUM(sqlite3_changes(ctx->db));
|
||||
}
|
||||
|
||||
static int rb_sqlite3_auth(
|
||||
void *ctx,
|
||||
int _action,
|
||||
const char * _a,
|
||||
const char * _b,
|
||||
const char * _c,
|
||||
const char * _d)
|
||||
{
|
||||
VALUE self = (VALUE)ctx;
|
||||
VALUE action = INT2NUM(_action);
|
||||
VALUE a = _a ? rb_str_new2(_a) : Qnil;
|
||||
VALUE b = _b ? rb_str_new2(_b) : Qnil;
|
||||
VALUE c = _c ? rb_str_new2(_c) : Qnil;
|
||||
VALUE d = _d ? rb_str_new2(_d) : Qnil;
|
||||
VALUE callback = rb_iv_get(self, "@authorizer");
|
||||
VALUE result = rb_funcall(callback, rb_intern("call"), 5, action, a, b, c, d);
|
||||
|
||||
if(T_FIXNUM == TYPE(result)) return (int)NUM2INT(result);
|
||||
if(Qtrue == result) return SQLITE_OK;
|
||||
if(Qfalse == result) return SQLITE_DENY;
|
||||
|
||||
return SQLITE_IGNORE;
|
||||
}
|
||||
|
||||
/* call-seq: set_authorizer = auth
|
||||
*
|
||||
* Set the authorizer for this database. +auth+ must respond to +call+, and
|
||||
* +call+ must take 5 arguments.
|
||||
*
|
||||
* Installs (or removes) a block that will be invoked for every access
|
||||
* to the database. If the block returns 0 (or +true+), the statement
|
||||
* is allowed to proceed. Returning 1 or false causes an authorization error to
|
||||
* occur, and returning 2 or nil causes the access to be silently denied.
|
||||
*/
|
||||
static VALUE set_authorizer(VALUE self, VALUE authorizer)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
int status;
|
||||
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
REQUIRE_OPEN_DB(ctx);
|
||||
|
||||
status = sqlite3_set_authorizer(
|
||||
ctx->db, NIL_P(authorizer) ? NULL : rb_sqlite3_auth, (void *)self
|
||||
);
|
||||
|
||||
CHECK(ctx->db, status);
|
||||
|
||||
rb_iv_set(self, "@authorizer", authorizer);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/* call-seq: db.busy_timeout = ms
|
||||
*
|
||||
* Indicates that if a request for a resource terminates because that
|
||||
* resource is busy, SQLite should sleep and retry for up to the indicated
|
||||
* number of milliseconds. By default, SQLite does not retry
|
||||
* busy resources. To restore the default behavior, send 0 as the
|
||||
* +ms+ parameter.
|
||||
*
|
||||
* See also the mutually exclusive #busy_handler.
|
||||
*/
|
||||
static VALUE set_busy_timeout(VALUE self, VALUE timeout)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
REQUIRE_OPEN_DB(ctx);
|
||||
|
||||
CHECK(ctx->db, sqlite3_busy_timeout(ctx->db, (int)NUM2INT(timeout)));
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/* call-seq: db.load_extension(file)
|
||||
*
|
||||
* Loads an SQLite extension library from the named file. Extension
|
||||
* loading must be enabled using db.enable_load_extension(1) prior
|
||||
* to calling this API.
|
||||
*/
|
||||
static VALUE load_extension(VALUE self, VALUE file)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
int status;
|
||||
char *errMsg;
|
||||
VALUE errexp;
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
REQUIRE_OPEN_DB(ctx);
|
||||
|
||||
status = sqlite3_load_extension(ctx->db, RSTRING_PTR(file), 0, &errMsg);
|
||||
if (status != SQLITE_OK)
|
||||
{
|
||||
errexp = rb_exc_new2(rb_eRuntimeError, errMsg);
|
||||
sqlite3_free(errMsg);
|
||||
rb_exc_raise(errexp);
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/* call-seq: db.enable_load_extension(onoff)
|
||||
*
|
||||
* Enable or disable extension loading.
|
||||
*/
|
||||
static VALUE enable_load_extension(VALUE self, VALUE onoff)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
REQUIRE_OPEN_DB(ctx);
|
||||
|
||||
CHECK(ctx->db, sqlite3_enable_load_extension(ctx->db, (int)NUM2INT(onoff)));
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
#ifdef HAVE_RUBY_ENCODING_H
|
||||
static int enc_cb(void * _self, int UNUSED(columns), char **data, char **UNUSED(names))
|
||||
{
|
||||
VALUE self = (VALUE)_self;
|
||||
|
||||
int index = rb_enc_find_index(data[0]);
|
||||
rb_encoding * e = rb_enc_from_index(index);
|
||||
rb_iv_set(self, "@encoding", rb_enc_from_encoding(e));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* call-seq: db.encoding
|
||||
*
|
||||
* Fetch the encoding set on this database
|
||||
*/
|
||||
static VALUE db_encoding(VALUE self)
|
||||
{
|
||||
sqlite3RubyPtr ctx;
|
||||
VALUE enc;
|
||||
|
||||
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
||||
REQUIRE_OPEN_DB(ctx);
|
||||
|
||||
enc = rb_iv_get(self, "@encoding");
|
||||
|
||||
if(NIL_P(enc)) {
|
||||
sqlite3_exec(ctx->db, "PRAGMA encoding", enc_cb, (void *)self, NULL);
|
||||
}
|
||||
|
||||
return rb_iv_get(self, "@encoding");
|
||||
}
|
||||
#endif
|
||||
|
||||
void init_sqlite3_database()
|
||||
{
|
||||
ID id_utf16, id_results_as_hash, id_type_translation;
|
||||
#if 0
|
||||
VALUE mSqlite3 = rb_define_module("SQLite3");
|
||||
#endif
|
||||
cSqlite3Database = rb_define_class_under(mSqlite3, "Database", rb_cObject);
|
||||
|
||||
rb_define_alloc_func(cSqlite3Database, allocate);
|
||||
rb_define_method(cSqlite3Database, "initialize", initialize, -1);
|
||||
rb_define_method(cSqlite3Database, "close", sqlite3_rb_close, 0);
|
||||
rb_define_method(cSqlite3Database, "closed?", closed_p, 0);
|
||||
rb_define_method(cSqlite3Database, "total_changes", total_changes, 0);
|
||||
rb_define_method(cSqlite3Database, "trace", trace, -1);
|
||||
rb_define_method(cSqlite3Database, "last_insert_row_id", last_insert_row_id, 0);
|
||||
rb_define_method(cSqlite3Database, "define_function", define_function, 1);
|
||||
rb_define_method(cSqlite3Database, "define_aggregator", define_aggregator, 2);
|
||||
rb_define_method(cSqlite3Database, "interrupt", interrupt, 0);
|
||||
rb_define_method(cSqlite3Database, "errmsg", errmsg, 0);
|
||||
rb_define_method(cSqlite3Database, "errcode", errcode_, 0);
|
||||
rb_define_method(cSqlite3Database, "complete?", complete_p, 1);
|
||||
rb_define_method(cSqlite3Database, "changes", changes, 0);
|
||||
rb_define_method(cSqlite3Database, "authorizer=", set_authorizer, 1);
|
||||
rb_define_method(cSqlite3Database, "busy_handler", busy_handler, -1);
|
||||
rb_define_method(cSqlite3Database, "busy_timeout=", set_busy_timeout, 1);
|
||||
|
||||
#ifdef HAVE_SQLITE3_LOAD_EXTENSION
|
||||
rb_define_method(cSqlite3Database, "load_extension", load_extension, 1);
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SQLITE3_ENABLE_LOAD_EXTENSION
|
||||
rb_define_method(cSqlite3Database, "enable_load_extension", enable_load_extension, 1);
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_RUBY_ENCODING_H
|
||||
rb_define_method(cSqlite3Database, "encoding", db_encoding, 0);
|
||||
#endif
|
||||
|
||||
id_utf16 = rb_intern("utf16");
|
||||
sym_utf16 = ID2SYM(id_utf16);
|
||||
id_results_as_hash = rb_intern("results_as_hash");
|
||||
sym_results_as_hash = ID2SYM(id_results_as_hash);
|
||||
id_type_translation = rb_intern("type_translation");
|
||||
sym_type_translation = ID2SYM(id_type_translation);
|
||||
}
|
15
vendor/plugins/sqlite3-ruby/ext/sqlite3/database.h
vendored
Normal file
15
vendor/plugins/sqlite3-ruby/ext/sqlite3/database.h
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef SQLITE3_DATABASE_RUBY
|
||||
#define SQLITE3_DATABASE_RUBY
|
||||
|
||||
#include <sqlite3_ruby.h>
|
||||
|
||||
struct _sqlite3Ruby {
|
||||
sqlite3 *db;
|
||||
};
|
||||
|
||||
typedef struct _sqlite3Ruby sqlite3Ruby;
|
||||
typedef sqlite3Ruby * sqlite3RubyPtr;
|
||||
|
||||
void init_sqlite3_database();
|
||||
|
||||
#endif
|
94
vendor/plugins/sqlite3-ruby/ext/sqlite3/exception.c
vendored
Normal file
94
vendor/plugins/sqlite3-ruby/ext/sqlite3/exception.c
vendored
Normal file
|
@ -0,0 +1,94 @@
|
|||
#include <sqlite3_ruby.h>
|
||||
|
||||
void rb_sqlite3_raise(sqlite3 * db, int status)
|
||||
{
|
||||
VALUE klass = Qnil;
|
||||
|
||||
switch(status) {
|
||||
case SQLITE_OK:
|
||||
return;
|
||||
break;
|
||||
case SQLITE_ERROR:
|
||||
klass = rb_path2class("SQLite3::SQLException");
|
||||
break;
|
||||
case SQLITE_INTERNAL:
|
||||
klass = rb_path2class("SQLite3::InternalException");
|
||||
break;
|
||||
case SQLITE_PERM:
|
||||
klass = rb_path2class("SQLite3::PermissionException");
|
||||
break;
|
||||
case SQLITE_ABORT:
|
||||
klass = rb_path2class("SQLite3::AbortException");
|
||||
break;
|
||||
case SQLITE_BUSY:
|
||||
klass = rb_path2class("SQLite3::BusyException");
|
||||
break;
|
||||
case SQLITE_LOCKED:
|
||||
klass = rb_path2class("SQLite3::LockedException");
|
||||
break;
|
||||
case SQLITE_NOMEM:
|
||||
klass = rb_path2class("SQLite3::MemoryException");
|
||||
break;
|
||||
case SQLITE_READONLY:
|
||||
klass = rb_path2class("SQLite3::ReadOnlyException");
|
||||
break;
|
||||
case SQLITE_INTERRUPT:
|
||||
klass = rb_path2class("SQLite3::InterruptException");
|
||||
break;
|
||||
case SQLITE_IOERR:
|
||||
klass = rb_path2class("SQLite3::IOException");
|
||||
break;
|
||||
case SQLITE_CORRUPT:
|
||||
klass = rb_path2class("SQLite3::CorruptException");
|
||||
break;
|
||||
case SQLITE_NOTFOUND:
|
||||
klass = rb_path2class("SQLite3::NotFoundException");
|
||||
break;
|
||||
case SQLITE_FULL:
|
||||
klass = rb_path2class("SQLite3::FullException");
|
||||
break;
|
||||
case SQLITE_CANTOPEN:
|
||||
klass = rb_path2class("SQLite3::CantOpenException");
|
||||
break;
|
||||
case SQLITE_PROTOCOL:
|
||||
klass = rb_path2class("SQLite3::ProtocolException");
|
||||
break;
|
||||
case SQLITE_EMPTY:
|
||||
klass = rb_path2class("SQLite3::EmptyException");
|
||||
break;
|
||||
case SQLITE_SCHEMA:
|
||||
klass = rb_path2class("SQLite3::SchemaChangedException");
|
||||
break;
|
||||
case SQLITE_TOOBIG:
|
||||
klass = rb_path2class("SQLite3::TooBigException");
|
||||
break;
|
||||
case SQLITE_CONSTRAINT:
|
||||
klass = rb_path2class("SQLite3::ConstraintException");
|
||||
break;
|
||||
case SQLITE_MISMATCH:
|
||||
klass = rb_path2class("SQLite3::MismatchException");
|
||||
break;
|
||||
case SQLITE_MISUSE:
|
||||
klass = rb_path2class("SQLite3::MisuseException");
|
||||
break;
|
||||
case SQLITE_NOLFS:
|
||||
klass = rb_path2class("SQLite3::UnsupportedException");
|
||||
break;
|
||||
case SQLITE_AUTH:
|
||||
klass = rb_path2class("SQLite3::AuthorizationException");
|
||||
break;
|
||||
case SQLITE_FORMAT:
|
||||
klass = rb_path2class("SQLite3::FormatException");
|
||||
break;
|
||||
case SQLITE_RANGE:
|
||||
klass = rb_path2class("SQLite3::RangeException");
|
||||
break;
|
||||
case SQLITE_NOTADB:
|
||||
klass = rb_path2class("SQLite3::NotADatabaseException");
|
||||
break;
|
||||
default:
|
||||
klass = rb_eRuntimeError;
|
||||
}
|
||||
|
||||
rb_raise(klass, "%s", sqlite3_errmsg(db));
|
||||
}
|
8
vendor/plugins/sqlite3-ruby/ext/sqlite3/exception.h
vendored
Normal file
8
vendor/plugins/sqlite3-ruby/ext/sqlite3/exception.h
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef SQLITE3_EXCEPTION_RUBY
|
||||
#define SQLITE3_EXCEPTION_RUBY
|
||||
|
||||
#define CHECK(_db, _status) rb_sqlite3_raise(_db, _status);
|
||||
|
||||
void rb_sqlite3_raise(sqlite3 * db, int status);
|
||||
|
||||
#endif
|
39
vendor/plugins/sqlite3-ruby/ext/sqlite3/extconf.rb
vendored
Normal file
39
vendor/plugins/sqlite3-ruby/ext/sqlite3/extconf.rb
vendored
Normal file
|
@ -0,0 +1,39 @@
|
|||
ENV['RC_ARCHS'] = '' if RUBY_PLATFORM =~ /darwin/
|
||||
|
||||
require 'mkmf'
|
||||
|
||||
# :stopdoc:
|
||||
|
||||
RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC'] if ENV['CC']
|
||||
|
||||
sqlite = dir_config('sqlite3', ['/usr/local', '/opt/local', '/usr'])
|
||||
|
||||
if RUBY_PLATFORM =~ /mswin/
|
||||
$CFLAGS << ' -W3'
|
||||
else
|
||||
$CFLAGS << ' -O3 -Wall -Wcast-qual -Wwrite-strings -Wconversion' <<
|
||||
' -Wmissing-noreturn -Winline'
|
||||
end
|
||||
|
||||
def asplode missing
|
||||
if RUBY_PLATFORM =~ /mswin/
|
||||
abort "#{missing} is missing. Install SQLite3 from " +
|
||||
"http://www.sqlite.org/ first."
|
||||
else
|
||||
abort "#{missing} is missing. Try 'port install sqlite3 +universal' " +
|
||||
"or 'yum install sqlite3-devel'"
|
||||
end
|
||||
end
|
||||
|
||||
asplode('sqlite3.h') unless find_header 'sqlite3.h'
|
||||
asplode('sqlite3') unless find_library 'sqlite3', 'sqlite3_libversion_number'
|
||||
|
||||
# Functions defined in 1.9 but not 1.8
|
||||
have_func('rb_proc_arity')
|
||||
|
||||
# These functions may not be defined
|
||||
have_func('sqlite3_column_database_name')
|
||||
have_func('sqlite3_enable_load_extension')
|
||||
have_func('sqlite3_load_extension')
|
||||
|
||||
create_makefile('sqlite3/sqlite3_native')
|
33
vendor/plugins/sqlite3-ruby/ext/sqlite3/sqlite3.c
vendored
Normal file
33
vendor/plugins/sqlite3-ruby/ext/sqlite3/sqlite3.c
vendored
Normal file
|
@ -0,0 +1,33 @@
|
|||
#include <sqlite3_ruby.h>
|
||||
|
||||
VALUE mSqlite3;
|
||||
VALUE cSqlite3Blob;
|
||||
|
||||
static VALUE libversion(VALUE UNUSED(klass))
|
||||
{
|
||||
return INT2NUM(sqlite3_libversion_number());
|
||||
}
|
||||
|
||||
void Init_sqlite3_native()
|
||||
{
|
||||
/*
|
||||
* SQLite3 is a wrapper around the popular database
|
||||
* sqlite[http://sqlite.org].
|
||||
*
|
||||
* For an example of usage, see SQLite3::Database.
|
||||
*/
|
||||
mSqlite3 = rb_define_module("SQLite3");
|
||||
|
||||
/* A class for differentiating between strings and blobs, when binding them
|
||||
* into statements.
|
||||
*/
|
||||
cSqlite3Blob = rb_define_class_under(mSqlite3, "Blob", rb_cString);
|
||||
|
||||
/* Initialize the sqlite3 library */
|
||||
sqlite3_initialize();
|
||||
|
||||
init_sqlite3_database();
|
||||
init_sqlite3_statement();
|
||||
|
||||
rb_define_singleton_method(mSqlite3, "libversion", libversion, 0);
|
||||
}
|
43
vendor/plugins/sqlite3-ruby/ext/sqlite3/sqlite3_ruby.h
vendored
Normal file
43
vendor/plugins/sqlite3-ruby/ext/sqlite3/sqlite3_ruby.h
vendored
Normal file
|
@ -0,0 +1,43 @@
|
|||
#ifndef SQLITE3_RUBY
|
||||
#define SQLITE3_RUBY
|
||||
|
||||
#include <ruby.h>
|
||||
|
||||
#ifdef UNUSED
|
||||
#elif defined(__GNUC__)
|
||||
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
|
||||
#elif defined(__LCLINT__)
|
||||
# define UNUSED(x) /*@unused@*/ x
|
||||
#else
|
||||
# define UNUSED(x) x
|
||||
#endif
|
||||
|
||||
#ifndef RBIGNUM_LEN
|
||||
#define RBIGNUM_LEN(x) RBIGNUM(x)->len
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_RUBY_ENCODING_H
|
||||
#include <ruby/encoding.h>
|
||||
|
||||
#define UTF8_P(_obj) (rb_enc_get_index(_obj) == rb_utf8_encindex())
|
||||
#define UTF16_LE_P(_obj) (rb_enc_get_index(_obj) == rb_enc_find_index("UTF-16LE"))
|
||||
#define SQLITE3_UTF8_STR_NEW2(_obj) \
|
||||
(rb_enc_associate_index(rb_str_new2(_obj), rb_utf8_encindex()))
|
||||
|
||||
#else
|
||||
|
||||
#define SQLITE3_UTF8_STR_NEW2(_obj) (rb_str_new2(_obj))
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#include <sqlite3.h>
|
||||
|
||||
extern VALUE mSqlite3;
|
||||
extern VALUE cSqlite3Blob;
|
||||
|
||||
#include <database.h>
|
||||
#include <statement.h>
|
||||
#include <exception.h>
|
||||
|
||||
#endif
|
419
vendor/plugins/sqlite3-ruby/ext/sqlite3/statement.c
vendored
Normal file
419
vendor/plugins/sqlite3-ruby/ext/sqlite3/statement.c
vendored
Normal file
|
@ -0,0 +1,419 @@
|
|||
#include <sqlite3_ruby.h>
|
||||
|
||||
#define REQUIRE_OPEN_STMT(_ctxt) \
|
||||
if(!_ctxt->st) \
|
||||
rb_raise(rb_path2class("SQLite3::Exception"), "cannot use a closed statement");
|
||||
|
||||
VALUE cSqlite3Statement;
|
||||
|
||||
static void deallocate(void * ctx)
|
||||
{
|
||||
sqlite3StmtRubyPtr c = (sqlite3StmtRubyPtr)ctx;
|
||||
xfree(c);
|
||||
}
|
||||
|
||||
static VALUE allocate(VALUE klass)
|
||||
{
|
||||
sqlite3StmtRubyPtr ctx = xcalloc((size_t)1, sizeof(sqlite3StmtRuby));
|
||||
ctx->st = NULL;
|
||||
ctx->done_p = 0;
|
||||
|
||||
return Data_Wrap_Struct(klass, NULL, deallocate, ctx);
|
||||
}
|
||||
|
||||
/* call-seq: SQLite3::Statement.new(db, sql)
|
||||
*
|
||||
* Create a new statement attached to the given Database instance, and which
|
||||
* encapsulates the given SQL text. If the text contains more than one
|
||||
* statement (i.e., separated by semicolons), then the #remainder property
|
||||
* will be set to the trailing text.
|
||||
*/
|
||||
static VALUE initialize(VALUE self, VALUE db, VALUE sql)
|
||||
{
|
||||
sqlite3RubyPtr db_ctx;
|
||||
sqlite3StmtRubyPtr ctx;
|
||||
const char *tail = NULL;
|
||||
int status;
|
||||
|
||||
Data_Get_Struct(db, sqlite3Ruby, db_ctx);
|
||||
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
||||
|
||||
if(!db_ctx->db)
|
||||
rb_raise(rb_eArgError, "prepare called on a closed database");
|
||||
|
||||
#ifdef HAVE_RUBY_ENCODING_H
|
||||
if(!UTF8_P(sql)) {
|
||||
VALUE encoding = rb_funcall(db, rb_intern("encoding"), 0);
|
||||
rb_encoding * enc = NIL_P(encoding) ? rb_utf8_encoding() :
|
||||
rb_to_encoding(encoding);
|
||||
sql = rb_str_export_to_enc(sql, enc);
|
||||
}
|
||||
#endif
|
||||
|
||||
status = sqlite3_prepare_v2(
|
||||
db_ctx->db,
|
||||
(const char *)StringValuePtr(sql),
|
||||
(int)RSTRING_LEN(sql),
|
||||
&ctx->st,
|
||||
&tail
|
||||
);
|
||||
|
||||
CHECK(db_ctx->db, status);
|
||||
|
||||
rb_iv_set(self, "@connection", db);
|
||||
rb_iv_set(self, "@remainder", rb_str_new2(tail));
|
||||
rb_iv_set(self, "@columns", Qnil);
|
||||
rb_iv_set(self, "@types", Qnil);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/* call-seq: stmt.close
|
||||
*
|
||||
* Closes the statement by finalizing the underlying statement
|
||||
* handle. The statement must not be used after being closed.
|
||||
*/
|
||||
static VALUE sqlite3_rb_close(VALUE self)
|
||||
{
|
||||
sqlite3StmtRubyPtr ctx;
|
||||
sqlite3 * db;
|
||||
|
||||
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
||||
|
||||
REQUIRE_OPEN_STMT(ctx);
|
||||
|
||||
db = sqlite3_db_handle(ctx->st);
|
||||
CHECK(db, sqlite3_finalize(ctx->st));
|
||||
|
||||
ctx->st = NULL;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/* call-seq: stmt.closed?
|
||||
*
|
||||
* Returns true if the statement has been closed.
|
||||
*/
|
||||
static VALUE closed_p(VALUE self)
|
||||
{
|
||||
sqlite3StmtRubyPtr ctx;
|
||||
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
||||
|
||||
if(!ctx->st) return Qtrue;
|
||||
|
||||
return Qfalse;
|
||||
}
|
||||
|
||||
static VALUE step(VALUE self)
|
||||
{
|
||||
sqlite3StmtRubyPtr ctx;
|
||||
sqlite3_stmt *stmt;
|
||||
int value, length;
|
||||
VALUE list;
|
||||
#ifdef HAVE_RUBY_ENCODING_H
|
||||
rb_encoding * internal_encoding;
|
||||
int enc_index;
|
||||
#endif
|
||||
|
||||
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
||||
|
||||
REQUIRE_OPEN_STMT(ctx);
|
||||
|
||||
if(ctx->done_p) return Qnil;
|
||||
|
||||
#ifdef HAVE_RUBY_ENCODING_H
|
||||
{
|
||||
VALUE db = rb_iv_get(self, "@connection");
|
||||
VALUE encoding = rb_funcall(db, rb_intern("encoding"), 0);
|
||||
enc_index = NIL_P(encoding) ? rb_utf8_encindex() : rb_to_encoding_index(encoding);
|
||||
internal_encoding = rb_default_internal_encoding();
|
||||
}
|
||||
#endif
|
||||
|
||||
stmt = ctx->st;
|
||||
|
||||
value = sqlite3_step(stmt);
|
||||
length = sqlite3_column_count(stmt);
|
||||
list = rb_ary_new2((long)length);
|
||||
|
||||
switch(value) {
|
||||
case SQLITE_ROW:
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < length; i++) {
|
||||
switch(sqlite3_column_type(stmt, i)) {
|
||||
case SQLITE_INTEGER:
|
||||
rb_ary_push(list, LL2NUM(sqlite3_column_int64(stmt, i)));
|
||||
break;
|
||||
case SQLITE_FLOAT:
|
||||
rb_ary_push(list, rb_float_new(sqlite3_column_double(stmt, i)));
|
||||
break;
|
||||
case SQLITE_TEXT:
|
||||
{
|
||||
VALUE str = rb_tainted_str_new(
|
||||
(const char *)sqlite3_column_text(stmt, i),
|
||||
(long)sqlite3_column_bytes(stmt, i)
|
||||
);
|
||||
#ifdef HAVE_RUBY_ENCODING_H
|
||||
rb_enc_associate_index(str, enc_index);
|
||||
if(internal_encoding)
|
||||
str = rb_str_export_to_enc(str, internal_encoding);
|
||||
#endif
|
||||
rb_ary_push(list, str);
|
||||
}
|
||||
break;
|
||||
case SQLITE_BLOB:
|
||||
{
|
||||
VALUE str = rb_tainted_str_new(
|
||||
(const char *)sqlite3_column_blob(stmt, i),
|
||||
(long)sqlite3_column_bytes(stmt, i)
|
||||
);
|
||||
rb_ary_push(list, str);
|
||||
}
|
||||
break;
|
||||
case SQLITE_NULL:
|
||||
rb_ary_push(list, Qnil);
|
||||
break;
|
||||
default:
|
||||
rb_raise(rb_eRuntimeError, "bad type");
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SQLITE_DONE:
|
||||
ctx->done_p = 1;
|
||||
return Qnil;
|
||||
break;
|
||||
default:
|
||||
CHECK(sqlite3_db_handle(ctx->st), value);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/* call-seq: stmt.bind_param(key, value)
|
||||
*
|
||||
* Binds value to the named (or positional) placeholder. If +param+ is a
|
||||
* Fixnum, it is treated as an index for a positional placeholder.
|
||||
* Otherwise it is used as the name of the placeholder to bind to.
|
||||
*
|
||||
* See also #bind_params.
|
||||
*/
|
||||
static VALUE bind_param(VALUE self, VALUE key, VALUE value)
|
||||
{
|
||||
sqlite3StmtRubyPtr ctx;
|
||||
int status;
|
||||
int index;
|
||||
|
||||
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
||||
REQUIRE_OPEN_STMT(ctx);
|
||||
|
||||
switch(TYPE(key)) {
|
||||
case T_SYMBOL:
|
||||
key = rb_funcall(key, rb_intern("to_s"), 0);
|
||||
case T_STRING:
|
||||
if(RSTRING_PTR(key)[0] != ':') key = rb_str_plus(rb_str_new2(":"), key);
|
||||
index = sqlite3_bind_parameter_index(ctx->st, StringValuePtr(key));
|
||||
break;
|
||||
default:
|
||||
index = (int)NUM2INT(key);
|
||||
}
|
||||
|
||||
if(index == 0)
|
||||
rb_raise(rb_path2class("SQLite3::Exception"), "no such bind parameter");
|
||||
|
||||
switch(TYPE(value)) {
|
||||
case T_STRING:
|
||||
if(CLASS_OF(value) == cSqlite3Blob
|
||||
#ifdef HAVE_RUBY_ENCODING_H
|
||||
|| rb_enc_get_index(value) == rb_ascii8bit_encindex()
|
||||
#endif
|
||||
) {
|
||||
status = sqlite3_bind_blob(
|
||||
ctx->st,
|
||||
index,
|
||||
(const char *)StringValuePtr(value),
|
||||
(int)RSTRING_LEN(value),
|
||||
SQLITE_TRANSIENT
|
||||
);
|
||||
} else {
|
||||
#ifdef HAVE_RUBY_ENCODING_H
|
||||
if(!UTF8_P(value)) {
|
||||
VALUE db = rb_iv_get(self, "@connection");
|
||||
VALUE encoding = rb_funcall(db, rb_intern("encoding"), 0);
|
||||
rb_encoding * enc = rb_to_encoding(encoding);
|
||||
value = rb_str_export_to_enc(value, enc);
|
||||
}
|
||||
#endif
|
||||
|
||||
status = sqlite3_bind_text(
|
||||
ctx->st,
|
||||
index,
|
||||
(const char *)StringValuePtr(value),
|
||||
(int)RSTRING_LEN(value),
|
||||
SQLITE_TRANSIENT
|
||||
);
|
||||
}
|
||||
break;
|
||||
case T_BIGNUM:
|
||||
#if SIZEOF_LONG < 8
|
||||
if (RBIGNUM_LEN(value) * SIZEOF_BDIGITS <= 8) {
|
||||
status = sqlite3_bind_int64(ctx->st, index, (sqlite3_int64)NUM2LL(value));
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
case T_FLOAT:
|
||||
status = sqlite3_bind_double(ctx->st, index, NUM2DBL(value));
|
||||
break;
|
||||
case T_FIXNUM:
|
||||
status = sqlite3_bind_int64(ctx->st, index, (sqlite3_int64)FIX2LONG(value));
|
||||
break;
|
||||
case T_NIL:
|
||||
status = sqlite3_bind_null(ctx->st, index);
|
||||
break;
|
||||
default:
|
||||
rb_raise(rb_eRuntimeError, "can't prepare %s",
|
||||
rb_class2name(CLASS_OF(value)));
|
||||
break;
|
||||
}
|
||||
|
||||
CHECK(sqlite3_db_handle(ctx->st), status);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/* call-seq: stmt.reset!
|
||||
*
|
||||
* Resets the statement. This is typically done internally, though it might
|
||||
* occassionally be necessary to manually reset the statement.
|
||||
*/
|
||||
static VALUE reset_bang(VALUE self)
|
||||
{
|
||||
sqlite3StmtRubyPtr ctx;
|
||||
int status;
|
||||
|
||||
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
||||
REQUIRE_OPEN_STMT(ctx);
|
||||
|
||||
status = sqlite3_reset(ctx->st);
|
||||
CHECK(sqlite3_db_handle(ctx->st), status);
|
||||
|
||||
ctx->done_p = 0;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
/* call-seq: stmt.done?
|
||||
*
|
||||
* returns true if all rows have been returned.
|
||||
*/
|
||||
static VALUE done_p(VALUE self)
|
||||
{
|
||||
sqlite3StmtRubyPtr ctx;
|
||||
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
||||
|
||||
if(ctx->done_p) return Qtrue;
|
||||
return Qfalse;
|
||||
}
|
||||
|
||||
/* call-seq: stmt.column_count
|
||||
*
|
||||
* Returns the number of columns to be returned for this statement
|
||||
*/
|
||||
static VALUE column_count(VALUE self)
|
||||
{
|
||||
sqlite3StmtRubyPtr ctx;
|
||||
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
||||
REQUIRE_OPEN_STMT(ctx);
|
||||
|
||||
return INT2NUM((long)sqlite3_column_count(ctx->st));
|
||||
}
|
||||
|
||||
/* call-seq: stmt.column_name(index)
|
||||
*
|
||||
* Get the column name at +index+. 0 based.
|
||||
*/
|
||||
static VALUE column_name(VALUE self, VALUE index)
|
||||
{
|
||||
sqlite3StmtRubyPtr ctx;
|
||||
const char * name;
|
||||
|
||||
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
||||
REQUIRE_OPEN_STMT(ctx);
|
||||
|
||||
name = sqlite3_column_name(ctx->st, (int)NUM2INT(index));
|
||||
|
||||
if(name) return rb_str_new2(name);
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
/* call-seq: stmt.column_decltype(index)
|
||||
*
|
||||
* Get the column type at +index+. 0 based.
|
||||
*/
|
||||
static VALUE column_decltype(VALUE self, VALUE index)
|
||||
{
|
||||
sqlite3StmtRubyPtr ctx;
|
||||
const char * name;
|
||||
|
||||
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
||||
REQUIRE_OPEN_STMT(ctx);
|
||||
|
||||
name = sqlite3_column_decltype(ctx->st, (int)NUM2INT(index));
|
||||
|
||||
if(name) return rb_str_new2(name);
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
/* call-seq: stmt.bind_parameter_count
|
||||
*
|
||||
* Return the number of bind parameters
|
||||
*/
|
||||
static VALUE bind_parameter_count(VALUE self)
|
||||
{
|
||||
sqlite3StmtRubyPtr ctx;
|
||||
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
||||
REQUIRE_OPEN_STMT(ctx);
|
||||
|
||||
return INT2NUM((long)sqlite3_bind_parameter_count(ctx->st));
|
||||
}
|
||||
|
||||
#ifdef HAVE_SQLITE3_COLUMN_DATABASE_NAME
|
||||
|
||||
/* call-seq: stmt.database_name(column_index)
|
||||
*
|
||||
* Return the database name for the column at +column_index+
|
||||
*/
|
||||
static VALUE database_name(VALUE self, VALUE index)
|
||||
{
|
||||
sqlite3StmtRubyPtr ctx;
|
||||
Data_Get_Struct(self, sqlite3StmtRuby, ctx);
|
||||
REQUIRE_OPEN_STMT(ctx);
|
||||
|
||||
return SQLITE3_UTF8_STR_NEW2(
|
||||
sqlite3_column_database_name(ctx->st, NUM2INT(index)));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void init_sqlite3_statement()
|
||||
{
|
||||
cSqlite3Statement = rb_define_class_under(mSqlite3, "Statement", rb_cObject);
|
||||
|
||||
rb_define_alloc_func(cSqlite3Statement, allocate);
|
||||
rb_define_method(cSqlite3Statement, "initialize", initialize, 2);
|
||||
rb_define_method(cSqlite3Statement, "close", sqlite3_rb_close, 0);
|
||||
rb_define_method(cSqlite3Statement, "closed?", closed_p, 0);
|
||||
rb_define_method(cSqlite3Statement, "bind_param", bind_param, 2);
|
||||
rb_define_method(cSqlite3Statement, "reset!", reset_bang, 0);
|
||||
rb_define_method(cSqlite3Statement, "step", step, 0);
|
||||
rb_define_method(cSqlite3Statement, "done?", done_p, 0);
|
||||
rb_define_method(cSqlite3Statement, "column_count", column_count, 0);
|
||||
rb_define_method(cSqlite3Statement, "column_name", column_name, 1);
|
||||
rb_define_method(cSqlite3Statement, "column_decltype", column_decltype, 1);
|
||||
rb_define_method(cSqlite3Statement, "bind_parameter_count", bind_parameter_count, 0);
|
||||
|
||||
#ifdef HAVE_SQLITE3_COLUMN_DATABASE_NAME
|
||||
rb_define_method(cSqlite3Statement, "database_name", database_name, 1);
|
||||
#endif
|
||||
}
|
16
vendor/plugins/sqlite3-ruby/ext/sqlite3/statement.h
vendored
Normal file
16
vendor/plugins/sqlite3-ruby/ext/sqlite3/statement.h
vendored
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef SQLITE3_STATEMENT_RUBY
|
||||
#define SQLITE3_STATEMENT_RUBY
|
||||
|
||||
#include <sqlite3_ruby.h>
|
||||
|
||||
struct _sqlite3StmtRuby {
|
||||
sqlite3_stmt *st;
|
||||
int done_p;
|
||||
};
|
||||
|
||||
typedef struct _sqlite3StmtRuby sqlite3StmtRuby;
|
||||
typedef sqlite3StmtRuby * sqlite3StmtRubyPtr;
|
||||
|
||||
void init_sqlite3_statement();
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue