From 627cc056ffa692220f7e87b3f8770f9458ac745d Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 3 Jan 2024 13:43:29 -0800 Subject: [PATCH] Move the encoding method to Ruby We don't need to define this in C --- ext/sqlite3/database.c | 34 ---------------------------------- lib/sqlite3/database.rb | 7 +++++++ 2 files changed, 7 insertions(+), 34 deletions(-) diff --git a/ext/sqlite3/database.c b/ext/sqlite3/database.c index bd7b2ea6..8bbe9f6b 100644 --- a/ext/sqlite3/database.c +++ b/ext/sqlite3/database.c @@ -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. @@ -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(); } diff --git a/lib/sqlite3/database.rb b/lib/sqlite3/database.rb index 5d004a23..206918f9 100644 --- a/lib/sqlite3/database.rb +++ b/lib/sqlite3/database.rb @@ -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.