Skip to content

Commit

Permalink
Merge pull request #455 from sparklemotion/remove-encoding
Browse files Browse the repository at this point in the history
Move the encoding method to Ruby
  • Loading branch information
tenderlove authored Jan 3, 2024
2 parents 792e290 + 627cc05 commit 345e5db
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 34 deletions.
34 changes: 0 additions & 34 deletions ext/sqlite3/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,38 +666,6 @@ static VALUE enable_load_extension(VALUE self, VALUE onoff)
}
#endif

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;

TypedData_Get_Struct(self, sqlite3Ruby, &database_type, 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");
}

/* call-seq: db.transaction_active?
*
* Returns +true+ if there is a transaction active, and +false+ otherwise.
Expand Down Expand Up @@ -868,8 +836,6 @@ void init_sqlite3_database(void)
rb_define_method(cSqlite3Database, "enable_load_extension", enable_load_extension, 1);
#endif

rb_define_method(cSqlite3Database, "encoding", db_encoding, 0);

rb_sqlite3_aggregator_init();
}

Expand Down
7 changes: 7 additions & 0 deletions lib/sqlite3/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ def initialize file, options = {}, zvfs = nil
end
end

# call-seq: db.encoding
#
# Fetch the encoding set on this database
def encoding
@encoding ||= Encoding.find(execute("PRAGMA encoding").first.first)
end

def type_translation= value # :nodoc:
warn(<<-eowarn) if $VERBOSE
#{caller[0]} is calling `SQLite3::Database#type_translation=` which is deprecated and will be removed in version 2.0.0.
Expand Down

0 comments on commit 345e5db

Please sign in to comment.