Skip to content

Commit 345e5db

Browse files
authored
Merge pull request #455 from sparklemotion/remove-encoding
Move the encoding method to Ruby
2 parents 792e290 + 627cc05 commit 345e5db

File tree

2 files changed

+7
-34
lines changed

2 files changed

+7
-34
lines changed

ext/sqlite3/database.c

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -666,38 +666,6 @@ static VALUE enable_load_extension(VALUE self, VALUE onoff)
666666
}
667667
#endif
668668

669-
static int enc_cb(void * _self, int UNUSED(columns), char **data, char **UNUSED(names))
670-
{
671-
VALUE self = (VALUE)_self;
672-
673-
int index = rb_enc_find_index(data[0]);
674-
rb_encoding * e = rb_enc_from_index(index);
675-
rb_iv_set(self, "@encoding", rb_enc_from_encoding(e));
676-
677-
return 0;
678-
}
679-
680-
/* call-seq: db.encoding
681-
*
682-
* Fetch the encoding set on this database
683-
*/
684-
static VALUE db_encoding(VALUE self)
685-
{
686-
sqlite3RubyPtr ctx;
687-
VALUE enc;
688-
689-
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
690-
REQUIRE_OPEN_DB(ctx);
691-
692-
enc = rb_iv_get(self, "@encoding");
693-
694-
if(NIL_P(enc)) {
695-
sqlite3_exec(ctx->db, "PRAGMA encoding", enc_cb, (void *)self, NULL);
696-
}
697-
698-
return rb_iv_get(self, "@encoding");
699-
}
700-
701669
/* call-seq: db.transaction_active?
702670
*
703671
* Returns +true+ if there is a transaction active, and +false+ otherwise.
@@ -868,8 +836,6 @@ void init_sqlite3_database(void)
868836
rb_define_method(cSqlite3Database, "enable_load_extension", enable_load_extension, 1);
869837
#endif
870838

871-
rb_define_method(cSqlite3Database, "encoding", db_encoding, 0);
872-
873839
rb_sqlite3_aggregator_init();
874840
}
875841

lib/sqlite3/database.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ def initialize file, options = {}, zvfs = nil
141141
end
142142
end
143143

144+
# call-seq: db.encoding
145+
#
146+
# Fetch the encoding set on this database
147+
def encoding
148+
@encoding ||= Encoding.find(execute("PRAGMA encoding").first.first)
149+
end
150+
144151
def type_translation= value # :nodoc:
145152
warn(<<-eowarn) if $VERBOSE
146153
#{caller[0]} is calling `SQLite3::Database#type_translation=` which is deprecated and will be removed in version 2.0.0.

0 commit comments

Comments
 (0)