Update vendored Sqlite3-ruby to 1.3.0

Also, some tweaks to Maruku.
This commit is contained in:
Jacques Distler 2010-06-10 22:42:33 -05:00
parent 9a80cacc34
commit 4f8759cdf3
55 changed files with 3071 additions and 8028 deletions

View 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);
}

View 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

View 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));
}

View 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

View 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')

View 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);
}

View 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

View 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
}

View 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

View file

@ -1,4 +0,0 @@
extconf.rb
post-clean.rb
post-distclean.rb
sqlite3_api.i

View file

@ -1,146 +0,0 @@
SHELL = /bin/sh
#### Start of system configuration section. ####
srcdir = .
topdir = /opt/local/lib/ruby/1.8/i686-darwin9.2.2
hdrdir = $(topdir)
VPATH = $(srcdir):$(topdir):$(hdrdir)
prefix = $(DESTDIR)/opt/local
exec_prefix = $(prefix)
sitedir = $(prefix)/lib/ruby/site_ruby
rubylibdir = $(libdir)/ruby/$(ruby_version)
archdir = $(rubylibdir)/$(arch)
sbindir = $(exec_prefix)/sbin
vendordir = $(prefix)/lib/ruby/vendor_ruby
datadir = $(prefix)/share
includedir = $(prefix)/include
infodir = $(prefix)/info
sysconfdir = $(prefix)/etc
mandir = $(DESTDIR)/opt/local/share/man
libdir = $(exec_prefix)/lib
sharedstatedir = $(prefix)/com
oldincludedir = $(DESTDIR)/usr/include
sitearchdir = $(sitelibdir)/$(sitearch)
vendorarchdir = $(vendorlibdir)/$(vendorarch)
bindir = $(exec_prefix)/bin
localstatedir = $(prefix)/var
vendorlibdir = $(vendordir)/$(ruby_version)
sitelibdir = $(sitedir)/$(ruby_version)
libexecdir = $(exec_prefix)/libexec
CC = gcc
LIBRUBY = $(LIBRUBY_SO)
LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
LIBRUBYARG_SHARED = -l$(RUBY_SO_NAME)
LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static
RUBY_EXTCONF_H =
CFLAGS = -fno-common -O2 -fno-common -pipe -fno-common
INCFLAGS = -I. -I. -I/opt/local/lib/ruby/1.8/i686-darwin9.2.2 -I.
CPPFLAGS = -DHAVE_SQLITE3_H -I/usr/local/include -I/opt/local/include
CXXFLAGS = $(CFLAGS)
DLDFLAGS = -L. -L/opt/local/lib
LDSHARED = cc -dynamic -bundle -undefined suppress -flat_namespace
AR = ar
EXEEXT =
RUBY_INSTALL_NAME = ruby
RUBY_SO_NAME = ruby
arch = i686-darwin9.2.2
sitearch = i686-darwin9.2.2
vendorarch = i686-darwin9.2.2
ruby_version = 1.8
ruby = /opt/local/bin/ruby
RUBY = $(ruby)
RM = rm -f
MAKEDIRS = mkdir -p
INSTALL = /usr/bin/install -c
INSTALL_PROG = $(INSTALL) -m 0755
INSTALL_DATA = $(INSTALL) -m 644
COPY = cp
#### End of system configuration section. ####
preload =
libpath = . $(libdir) /usr/local/lib
LIBPATH = -L"." -L"$(libdir)" -L"/usr/local/lib"
DEFFILE =
CLEANFILES = mkmf.log
DISTCLEANFILES =
extout =
extout_prefix =
target_prefix =
LOCAL_LIBS =
LIBS = $(LIBRUBYARG_SHARED) -lsqlite3 -lpthread -ldl -lobjc
SRCS = sqlite3_api_wrap.c
OBJS = sqlite3_api_wrap.o
TARGET = sqlite3_api
DLLIB = $(TARGET).bundle
EXTSTATIC =
STATIC_LIB =
RUBYCOMMONDIR = $(sitedir)$(target_prefix)
RUBYLIBDIR = $(sitelibdir)$(target_prefix)
RUBYARCHDIR = $(sitearchdir)$(target_prefix)
TARGET_SO = $(DLLIB)
CLEANLIBS = $(TARGET).bundle $(TARGET).il? $(TARGET).tds $(TARGET).map
CLEANOBJS = *.o *.a *.s[ol] *.pdb *.exp *.bak
all: $(DLLIB)
static: $(STATIC_LIB)
clean:
@-$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES)
distclean: clean
@-$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
@-$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
realclean: distclean
install: install-so install-rb
install-so: $(RUBYARCHDIR)
install-so: $(RUBYARCHDIR)/$(DLLIB)
$(RUBYARCHDIR)/$(DLLIB): $(DLLIB)
$(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)
install-rb: pre-install-rb install-rb-default
install-rb-default: pre-install-rb-default
pre-install-rb: Makefile
pre-install-rb-default: Makefile
$(RUBYARCHDIR):
$(MAKEDIRS) $@
site-install: site-install-so site-install-rb
site-install-so: install-so
site-install-rb: install-rb
.SUFFIXES: .c .m .cc .cxx .cpp .C .o
.cc.o:
$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $<
.cxx.o:
$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $<
.cpp.o:
$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $<
.C.o:
$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $<
.c.o:
$(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) -c $<
$(DLLIB): $(OBJS)
@-$(RM) $@
$(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
$(OBJS): ruby.h defines.h

View file

@ -1,10 +0,0 @@
require 'mkmf'
dir_config( "sqlite3" )
have_library( "rt", "fdatasync" )
if have_header( "sqlite3.h" ) && have_library( "sqlite3", "sqlite3_open" ) then
$CFLAGS << " -fno-strict-aliasing" unless RUBY_PLATFORM =~ /mswin/
create_makefile( "sqlite3_api" )
end

View file

@ -1,71 +0,0 @@
have_library: checking for fdatasync() in -lrt... -------------------- no
"gcc -o conftest -I. -I/opt/local/lib/ruby/1.8/i686-darwin9.2.2 -I. -I/usr/local/include -I/opt/local/include -O2 -fno-common -pipe -fno-common conftest.c -L"." -L"/opt/local/lib" -L"/usr/local/lib" -L. -L/opt/local/lib -lruby-static -lrt -lpthread -ldl -lobjc "
conftest.c: In function t:
conftest.c:3: error: fdatasync undeclared (first use in this function)
conftest.c:3: error: (Each undeclared identifier is reported only once
conftest.c:3: error: for each function it appears in.)
checked program was:
/* begin */
1: /*top*/
2: int main() { return 0; }
3: int t() { void ((*volatile p)()); p = (void ((*)()))fdatasync; return 0; }
/* end */
"gcc -o conftest -I. -I/opt/local/lib/ruby/1.8/i686-darwin9.2.2 -I. -I/usr/local/include -I/opt/local/include -O2 -fno-common -pipe -fno-common conftest.c -L"." -L"/opt/local/lib" -L"/usr/local/lib" -L. -L/opt/local/lib -lruby-static -lrt -lpthread -ldl -lobjc "
ld: library not found for -lrt
collect2: ld returned 1 exit status
checked program was:
/* begin */
1: /*top*/
2: int main() { return 0; }
3: int t() { fdatasync(); return 0; }
/* end */
"gcc -o conftest -I. -I/opt/local/lib/ruby/1.8/i686-darwin9.2.2 -I. -I/usr/local/include -I/opt/local/include -O2 -fno-common -pipe -fno-common conftest.c -L"." -L"/opt/local/lib" -L"/usr/local/lib" -L. -L/opt/local/lib -lruby-static -lrt -lpthread -ldl -lobjc "
ld: library not found for -lrt
collect2: ld returned 1 exit status
checked program was:
/* begin */
1: int fdatasync();
2: /*top*/
3: int main() { return 0; }
4: int t() { fdatasync(); return 0; }
/* end */
--------------------
have_header: checking for sqlite3.h... -------------------- yes
"gcc -E -I. -I/opt/local/lib/ruby/1.8/i686-darwin9.2.2 -I. -I/usr/local/include -I/opt/local/include -O2 -fno-common -pipe -fno-common conftest.c -o conftest.i"
checked program was:
/* begin */
1: #include <sqlite3.h>
/* end */
--------------------
have_library: checking for sqlite3_open() in -lsqlite3... -------------------- yes
"gcc -o conftest -I. -I/opt/local/lib/ruby/1.8/i686-darwin9.2.2 -I. -I/usr/local/include -I/opt/local/include -O2 -fno-common -pipe -fno-common conftest.c -L"." -L"/opt/local/lib" -L"/usr/local/lib" -L. -L/opt/local/lib -lruby-static -lsqlite3 -lpthread -ldl -lobjc "
conftest.c: In function t:
conftest.c:3: error: sqlite3_open undeclared (first use in this function)
conftest.c:3: error: (Each undeclared identifier is reported only once
conftest.c:3: error: for each function it appears in.)
checked program was:
/* begin */
1: /*top*/
2: int main() { return 0; }
3: int t() { void ((*volatile p)()); p = (void ((*)()))sqlite3_open; return 0; }
/* end */
"gcc -o conftest -I. -I/opt/local/lib/ruby/1.8/i686-darwin9.2.2 -I. -I/usr/local/include -I/opt/local/include -O2 -fno-common -pipe -fno-common conftest.c -L"." -L"/opt/local/lib" -L"/usr/local/lib" -L. -L/opt/local/lib -lruby-static -lsqlite3 -lpthread -ldl -lobjc "
checked program was:
/* begin */
1: /*top*/
2: int main() { return 0; }
3: int t() { sqlite3_open(); return 0; }
/* end */
--------------------

View file

@ -1,362 +0,0 @@
%module "SQLite3::driver::native::API"
%include "typemaps.i"
%{
#include <sqlite3.h>
#include "ruby.h"
#ifndef RSTRING_PTR
#define RSTRING_PTR(s) (RSTRING(s)->ptr)
#endif
#ifndef RSTRING_LEN
#define RSTRING_LEN(s) (RSTRING(s)->len)
#endif
#ifndef STR2CSTR
#define STR2CSTR StringValueCStr
#endif
#define Init_API Init_sqlite3_api
struct CallbackData {
VALUE proc;
VALUE proc2;
VALUE data;
};
typedef struct CallbackData CallbackData;
typedef void RUBY_BLOB;
typedef void RUBY_VALBLOB;
int Sqlite3_ruby_busy_handler(void* data,int value) {
VALUE result;
CallbackData *cb = (CallbackData*)data;
result = rb_funcall(
cb->proc, rb_intern("call"), 2, cb->data, INT2FIX(value) );
return FIX2INT(result);
}
static void mark_CallbackData(void* ptr) {
CallbackData* cb = (CallbackData*)ptr;
if (cb->proc != Qnil)
rb_gc_mark(cb->proc);
if (cb->proc2 != Qnil)
rb_gc_mark(cb->proc2);
if (cb->data != Qnil)
rb_gc_mark(cb->data);
}
int Sqlite3_ruby_authorizer(void* data,int type,
const char* a,const char* b,const char* c,const char* d)
{
VALUE result;
CallbackData *cb = (CallbackData*)data;
result = rb_funcall(
cb->proc, rb_intern("call"), 6, cb->data, INT2FIX(type),
( a ? rb_str_new2(a) : Qnil ), ( b ? rb_str_new2(b) : Qnil ),
( c ? rb_str_new2(c) : Qnil ), ( d ? rb_str_new2(d) : Qnil ) );
return FIX2INT(result);
}
void Sqlite3_ruby_trace(void* data, const char *sql) {
CallbackData *cb = (CallbackData*)data;
rb_funcall( cb->proc, rb_intern("call"), 2, cb->data,
sql ? rb_str_new2(sql) : Qnil );
}
void Sqlite3_ruby_function_step(sqlite3_context* ctx,int n,
sqlite3_value** args)
{
CallbackData *data;
VALUE rb_args;
VALUE *rb_context;
int idx;
data = (CallbackData*)sqlite3_user_data(ctx);
if( data->proc2 != Qnil ) {
rb_context = (VALUE*)sqlite3_aggregate_context(ctx,sizeof(VALUE));
if( *rb_context == 0 ) {
*rb_context = rb_hash_new();
rb_gc_register_address( rb_context );
}
}
rb_args = rb_ary_new2(n+1);
rb_ary_push( rb_args, SWIG_NewPointerObj(ctx,SWIGTYPE_p_sqlite3_context,0) );
for( idx = 0; idx < n; idx++ ) {
rb_ary_push( rb_args, SWIG_NewPointerObj(args[idx],
SWIGTYPE_p_sqlite3_value,0) );
}
rb_apply( data->proc, rb_intern("call"), rb_args );
}
void Sqlite3_ruby_function_final(sqlite3_context *ctx) {
VALUE *rb_context;
CallbackData *data;
rb_context = (VALUE*)sqlite3_aggregate_context(ctx,sizeof(VALUE));
if( *rb_context == 0 ) {
*rb_context = rb_hash_new();
rb_gc_register_address( rb_context );
}
data = (CallbackData*)sqlite3_user_data(ctx);
rb_funcall( data->proc2, rb_intern("call"), 1,
SWIG_NewPointerObj(ctx,SWIGTYPE_p_sqlite3_context,0) );
rb_gc_unregister_address( rb_context );
}
%}
%markfunc CallbackData "mark_CallbackData";
struct CallbackData {
VALUE proc;
VALUE proc2;
VALUE data;
};
%typemap(in) const void *str {
$1 = (void*)RSTRING_PTR($input);
}
%typemap(in) (const char *filename, sqlite3**) {
$1 = STR2CSTR($input);
$2 = (sqlite3**)malloc( sizeof( sqlite3* ) );
}
%typemap(argout) (const char *filename, sqlite3**) {
VALUE ary;
ary = rb_ary_new2(2);
rb_ary_push( ary, $result );
rb_ary_push( ary, SWIG_NewPointerObj( *$2, SWIGTYPE_p_sqlite3, 0 ) );
free( $2 );
$result = ary;
}
%typemap(in) (const void *filename, sqlite3**) {
$1 = (void*)RSTRING_PTR($input);
$2 = (sqlite3**)malloc( sizeof( sqlite3* ) );
}
%typemap(argout) (const void *filename, sqlite3**) {
VALUE ary;
ary = rb_ary_new2(2);
rb_ary_push( ary, $result );
rb_ary_push( ary, SWIG_NewPointerObj( *$2, SWIGTYPE_p_sqlite3, 0 ) );
free( $2 );
$result = ary;
}
typedef void RUBY_BLOB;
%typemap(out) const RUBY_BLOB * {
$result = $1 ?
rb_str_new( (char*)$1, sqlite3_column_bytes( arg1, arg2 ) ) : Qnil;
}
typedef void RUBY_VALBLOB;
%typemap(out) const RUBY_VALBLOB * {
$result = $1 ? rb_str_new( (char*)$1, sqlite3_value_bytes( arg1 ) ) : Qnil;
}
%typemap(out) const void * {
int i;
if( $1 ) {
for( i = 0; ((char*)$1)[i]; i += 2 );
$result = rb_str_new( (char*)$1, i );
} else $result = Qnil;
}
%typemap(in) (const char * sql,int,sqlite3_stmt**,const char**) (sqlite3_stmt *stmt, char *errmsg) {
$1 = RSTRING_PTR($input);
$2 = RSTRING_LEN($input);
$3 = &stmt2;
$4 = &errmsg2;
}
%typemap(argout) (const char* sql,int,sqlite3_stmt**,const char**) {
VALUE ary;
ary = rb_ary_new2(3);
rb_ary_push( ary, $result );
rb_ary_push( ary, SWIG_NewPointerObj( stmt2, SWIGTYPE_p_sqlite3_stmt, 0 ) );
rb_ary_push( ary, errmsg2 ? rb_str_new2( errmsg2 ) : Qnil );
$result = ary;
}
%typemap(in) (const void* sql,int,sqlite3_stmt**,const void**) (sqlite3_stmt *stmt, void *errmsg) {
$1 = RSTRING_PTR($input);
$2 = RSTRING_LEN($input);
$3 = &stmt2;
$4 = &errmsg2;
}
%typemap(argout) (const void* sql,int,sqlite3_stmt**,const void**) {
VALUE ary;
int i;
for( i = 0; ((char*)errmsg2)[i]; i += 2 );
ary = rb_ary_new2(3);
rb_ary_push( ary, $result );
rb_ary_push( ary, SWIG_NewPointerObj( stmt2, SWIGTYPE_p_sqlite3_stmt, 0 ) );
rb_ary_push( ary, errmsg2 ? rb_str_new( (char*)errmsg2, i ) : Qnil );
$result = ary;
}
%typemap(in) (const void *blob,int) {
$1 = (void*)RSTRING_PTR($input);
$2 = RSTRING_LEN($input);
}
%typemap(in) (const void *blob,int,void(*free)(void*)) {
$1 = (void*)RSTRING_PTR($input);
$2 = RSTRING_LEN($input);
$3 = SQLITE_TRANSIENT;
}
%typemap(in) (const char *text,int) {
$1 = RSTRING_PTR($input);
$2 = RSTRING_LEN($input);
}
%typemap(in) (const char *text,int,void(*free)(void*)) {
$1 = RSTRING_PTR($input);
$2 = RSTRING_LEN($input);
$3 = SQLITE_TRANSIENT;
}
%typemap(in) (const void *utf16,int) {
$1 = (void*)RSTRING_PTR($input);
$2 = RSTRING_LEN($input);
}
%typemap(in) (const void *utf16,int,void(*free)(void*)) {
$1 = (void*)RSTRING_PTR($input);
$2 = RSTRING_LEN($input);
$3 = SQLITE_TRANSIENT;
}
%typemap(out) sqlite_int64 {
$result = rb_ll2inum( $1 );
}
%typemap(out) const char * {
$result = $1 ? rb_str_new2($1) : Qnil;
}
%typemap(in) sqlite_int64 {
$1 = rb_num2ll( $input );
}
%typemap(in) (sqlite3_context*,int data_size) {
SWIG_ConvertPtr($input,(void**)&$1, SWIGTYPE_p_sqlite3_context, 1);
$2 = 4;
}
%typemap(out) VALUE* {
$result = *(VALUE*)$1;
}
%constant int Sqlite3_ruby_busy_handler(void*,int);
%constant int Sqlite3_ruby_authorizer(void*,int,const char*,const char*,const char*,const char*);
%constant void Sqlite3_ruby_trace(void*,const char*);
%constant void Sqlite3_ruby_function_step(sqlite3_context* ctx,int n,
sqlite3_value** args);
%constant void Sqlite3_ruby_function_final(sqlite3_context* ctx);
const char *sqlite3_libversion(void);
int sqlite3_close(sqlite3*);
sqlite_int64 sqlite3_last_insert_rowid(sqlite3*);
int sqlite3_changes(sqlite3*);
int sqlite3_total_changes(sqlite3*);
void sqlite3_interrupt(sqlite3*);
int sqlite3_complete(const char*);
int sqlite3_complete16(const void *str);
int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
int sqlite3_busy_timeout(sqlite3*,int);
int sqlite3_set_authorizer(sqlite3*, int(*)(void*,int,const char*,const char*,const char*,const char*), void*);
int sqlite3_trace(sqlite3*, void(*)(void*,const char*), void*);
int sqlite3_open(const char *filename, sqlite3 **);
int sqlite3_open16(const void *filename, sqlite3 **);
int sqlite3_errcode(sqlite3*);
const char *sqlite3_errmsg(sqlite3*);
const void *sqlite3_errmsg16(sqlite3*);
int sqlite3_prepare(sqlite3*,const char* sql,int,sqlite3_stmt**,const char**);
int sqlite3_prepare16(sqlite3*,const void* sql,int,sqlite3_stmt**,const void**);
int sqlite3_bind_blob(sqlite3_stmt*,int,const void *blob,int,void(*free)(void*));
int sqlite3_bind_double(sqlite3_stmt*,int,double);
int sqlite3_bind_int(sqlite3_stmt*,int,int);
int sqlite3_bind_int64(sqlite3_stmt*,int,sqlite_int64);
int sqlite3_bind_null(sqlite3_stmt*,int);
int sqlite3_bind_text(sqlite3_stmt*,int,const char*text,int,void(*free)(void*));
int sqlite3_bind_text16(sqlite3_stmt*,int,const void*utf16,int,void(*free)(void*));
int sqlite3_bind_parameter_count(sqlite3_stmt*);
const char *sqlite3_bind_parameter_name(sqlite3_stmt*,int);
int sqlite3_bind_parameter_index(sqlite3_stmt*,const char*);
int sqlite3_column_count(sqlite3_stmt*);
const char *sqlite3_column_name(sqlite3_stmt*,int);
const void *sqlite3_column_name16(sqlite3_stmt*,int);
const char *sqlite3_column_decltype(sqlite3_stmt*,int);
const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
int sqlite3_step(sqlite3_stmt*);
int sqlite3_data_count(sqlite3_stmt*);
const RUBY_BLOB *sqlite3_column_blob(sqlite3_stmt*,int);
int sqlite3_column_bytes(sqlite3_stmt*,int);
int sqlite3_column_bytes16(sqlite3_stmt*,int);
double sqlite3_column_double(sqlite3_stmt*,int);
double sqlite3_column_int(sqlite3_stmt*,int);
sqlite_int64 sqlite3_column_int64(sqlite3_stmt*,int);
const char *sqlite3_column_text(sqlite3_stmt*,int);
const void *sqlite3_column_text16(sqlite3_stmt*,int);
int sqlite3_column_type(sqlite3_stmt*,int);
int sqlite3_finalize(sqlite3_stmt*);
int sqlite3_reset(sqlite3_stmt*);
int sqlite3_create_function(sqlite3*,const char*str,int,int,void*,void(*func)(sqlite3_context*,int,sqlite3_value**),void(*step)(sqlite3_context*,int,sqlite3_value**),void(*final)(sqlite3_context*));
int sqlite3_create_function16(sqlite3*,const void*str,int,int,void*,void(*func)(sqlite3_context*,int,sqlite3_value**),void(*step)(sqlite3_context*,int,sqlite3_value**),void(*final)(sqlite3_context*));
int sqlite3_aggregate_count(sqlite3_context*);
const RUBY_VALBLOB *sqlite3_value_blob(sqlite3_value*);
int sqlite3_value_bytes(sqlite3_value*);
int sqlite3_value_bytes16(sqlite3_value*);
double sqlite3_value_double(sqlite3_value*);
int sqlite3_value_int(sqlite3_value*);
sqlite_int64 sqlite3_value_int64(sqlite3_value*);
const char *sqlite3_value_text(sqlite3_value*);
const void *sqlite3_value_text16(sqlite3_value*);
const void *sqlite3_value_text16le(sqlite3_value*);
const void *sqlite3_value_text16be(sqlite3_value*);
int sqlite3_value_type(sqlite3_value*);
void sqlite3_result_blob(sqlite3_context*,const void *blob,int,void(*free)(void*));
void sqlite3_result_double(sqlite3_context*,double);
void sqlite3_result_error(sqlite3_context*,const char *text,int);
void sqlite3_result_error16(sqlite3_context*,const void *blob,int);
void sqlite3_result_int(sqlite3_context*,int);
void sqlite3_result_int64(sqlite3_context*,sqlite_int64);
void sqlite3_result_text(sqlite3_context*,const char* text,int,void(*free)(void*));
void sqlite3_result_text16(sqlite3_context*,const void* utf16,int,void(*free)(void*));
void sqlite3_result_text16le(sqlite3_context*,const void* utf16,int,void(*free)(void*));
void sqlite3_result_text16be(sqlite3_context*,const void* utf16,int,void(*free)(void*));
void sqlite3_result_value(sqlite3_context*,sqlite3_value*);
VALUE *sqlite3_aggregate_context(sqlite3_context*,int data_size);

File diff suppressed because it is too large Load diff

View file

@ -1,7 +0,0 @@
REM This is not guaranteed to work, ever. It's just a little helper
REM script that I threw together to help me build the win32 version of
REM the library. If someone with more win32-fu than I wants to make
REM something more robust, please feel free! I'd love to include it.
REM -- Jamis Buck
cl /LD /Ie:\WinSDK\Include /Ic:\ruby\lib\ruby\1.8\i386-mswin32 /Ic:\ruby\sqlite3 /Ic:\ruby\src\ruby-1.8.4_2006-04-14 sqlite3_api_wrap.c /link /LIBPATH:c:\ruby\sqlite3 /LIBPATH:e:\WinSDK\Lib /LIBPATH:c:\ruby\lib sqlite3.lib msvcrt-ruby18.lib