Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the encoding method to Ruby #455

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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