Skip to content

Commit

Permalink
fix: enable and fix type-casting warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rzhao271 committed Jun 12, 2023
1 parent dfbe3ba commit 181980b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
8 changes: 7 additions & 1 deletion binding.gyp
Expand Up @@ -14,7 +14,13 @@
"MACOSX_DEPLOYMENT_TARGET": "10.7",
},
"msvs_settings": {
"VCCLCompilerTool": { "ExceptionHandling": 1 },
"VCCLCompilerTool": {
"ExceptionHandling": 1,
"AdditionalOptions": [
"/we4244",
"/we4267"
]
},
},
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")"],
Expand Down
6 changes: 3 additions & 3 deletions src/database.cc
Expand Up @@ -14,7 +14,7 @@ Napi::FunctionReference Database::constructor;
Napi::Object Database::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
// declare napi_default_method here as it is only available in Node v14.12.0+
auto napi_default_method = static_cast<napi_property_attributes>(napi_writable | napi_configurable);
auto napi_default_method = static_cast<napi_property_attributes>(napi_writable | napi_configurable);

auto t = DefineClass(env, "Database", {
InstanceMethod("close", &Database::Close, napi_default_method),
Expand Down Expand Up @@ -352,7 +352,7 @@ Napi::Value Database::Configure(const Napi::CallbackInfo& info) {
REQUIRE_ARGUMENTS(2);

Napi::Function handle;
if (info[0].StrictEquals( Napi::String::New(env, "trace"))) {
if (info[0].StrictEquals( Napi::String::New(env, "trace"))) {
auto* baton = new Baton(db, handle);
db->Schedule(RegisterTraceCallback, baton);
}
Expand Down Expand Up @@ -561,7 +561,7 @@ void Database::UpdateCallback(Database *db, UpdateInfo* i) {
Napi::String::New(env, sqlite_authorizer_string(info->type)),
Napi::String::New(env, info->database.c_str()),
Napi::String::New(env, info->table.c_str()),
Napi::Number::New(env, info->rowid),
Napi::Number::New(env, static_cast<double>(info->rowid)),
};
EMIT_EVENT(db->Value(), 5, argv);
}
Expand Down
30 changes: 16 additions & 14 deletions src/statement.cc
Expand Up @@ -90,7 +90,7 @@ template <class T> void Statement::Error(T* baton) {
// { Database db, String sql, Array params, Function callback }
Statement::Statement(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Statement>(info) {
auto env = info.Env();
int length = info.Length();
auto length = info.Length();

if (length <= 0 || !Database::HasInstance(info[0])) {
Napi::TypeError::New(env, "Database object expected").ThrowAsJavaScriptException();
Expand Down Expand Up @@ -141,7 +141,7 @@ void Statement::Work_Prepare(napi_env e, void* data) {
stmt->status = sqlite3_prepare_v2(
baton->db->_handle,
baton->sql.c_str(),
baton->sql.size(),
static_cast<int>(baton->sql.size()),
&stmt->_handle,
NULL
);
Expand Down Expand Up @@ -226,7 +226,7 @@ template <class T> T* Statement::Bind(const Napi::CallbackInfo& info, int start,
auto env = info.Env();
Napi::HandleScope scope(env);

if (last < 0) last = info.Length();
if (last < 0) last = static_cast<int>(info.Length());
Napi::Function callback;
if (last > start && info[last - 1].IsFunction()) {
callback = info[last - 1].As<Napi::Function>();
Expand All @@ -244,7 +244,7 @@ template <class T> T* Statement::Bind(const Napi::CallbackInfo& info, int start,
baton->parameters.emplace_back(BindParameter((array).Get(i), i + 1));
}
}
else if (!info[start].IsObject() || OtherInstanceOf(info[start].As<Object>(), "RegExp")
else if (!info[start].IsObject() || OtherInstanceOf(info[start].As<Object>(), "RegExp")
|| OtherInstanceOf(info[start].As<Object>(), "Date") || info[start].IsBuffer()) {
// Parameters directly in array.
// Note: bind parameters start with 1.
Expand Down Expand Up @@ -300,20 +300,22 @@ bool Statement::Bind(const Parameters & parameters) {

switch (field->type) {
case SQLITE_INTEGER: {
status = sqlite3_bind_int(_handle, pos,
status = sqlite3_bind_int64(_handle, pos,
(static_cast<Values::Integer*>(field.get()))->value);
} break;
case SQLITE_FLOAT: {
status = sqlite3_bind_double(_handle, pos,
(static_cast<Values::Float*>(field.get()))->value);
} break;
case SQLITE_TEXT: {
status = sqlite3_bind_text(_handle, pos,
status = sqlite3_bind_text64(_handle, pos,
(static_cast<Values::Text*>(field.get()))->value.c_str(),
(static_cast<Values::Text*>(field.get()))->value.size(), SQLITE_TRANSIENT);
(static_cast<Values::Text*>(field.get()))->value.size(),
SQLITE_TRANSIENT,
SQLITE_UTF8);
} break;
case SQLITE_BLOB: {
status = sqlite3_bind_blob(_handle, pos,
status = sqlite3_bind_blob64(_handle, pos,
(static_cast<Values::Blob*>(field.get()))->value,
(static_cast<Values::Blob*>(field.get()))->length, SQLITE_TRANSIENT);
} break;
Expand Down Expand Up @@ -514,7 +516,7 @@ void Statement::Work_AfterRun(napi_env e, napi_status status, void* data) {
// Fire callbacks.
Napi::Function cb = baton->callback.Value();
if (IS_FUNCTION(cb)) {
(stmt->Value()).Set(Napi::String::New(env, "lastID"), Napi::Number::New(env, baton->inserted_id));
(stmt->Value()).Set(Napi::String::New(env, "lastID"), Napi::Number::New(env, static_cast<double>(baton->inserted_id)));
(stmt->Value()).Set( Napi::String::New(env, "changes"), Napi::Number::New(env, baton->changes));

Napi::Value argv[] = { env.Null() };
Expand Down Expand Up @@ -614,14 +616,14 @@ Napi::Value Statement::Each(const Napi::CallbackInfo& info) {
auto env = info.Env();
Statement* stmt = this;

int last = info.Length();
size_t last = info.Length();

Napi::Function completed;
if (last >= 2 && info[last - 1].IsFunction() && info[last - 2].IsFunction()) {
completed = info[--last].As<Napi::Function>();
}

auto baton = stmt->Bind<EachBaton>(info, 0, last);
auto baton = stmt->Bind<EachBaton>(info, 0, static_cast<int>(last));
if (baton == NULL) {
Napi::Error::New(env, "Data type is not supported").ThrowAsJavaScriptException();
return env.Null();
Expand Down Expand Up @@ -800,17 +802,17 @@ Napi::Value Statement::RowToJS(Napi::Env env, Row* row) {

switch (field->type) {
case SQLITE_INTEGER: {
value = Napi::Number::New(env, (static_cast<Values::Integer*>(field.get()))->value);
value = Napi::Number::New(env, static_cast<double>((static_cast<Values::Integer*>(field.get()))->value));
} break;
case SQLITE_FLOAT: {
value = Napi::Number::New(env, (static_cast<Values::Float*>(field.get()))->value);
} break;
case SQLITE_TEXT: {
value = Napi::String::New(env, (static_cast<Values::Text*>(field.get()))->value.c_str(),
value = Napi::String::New(env, (static_cast<Values::Text*>(field.get()))->value.c_str(),
(static_cast<Values::Text*>(field.get()))->value.size());
} break;
case SQLITE_BLOB: {
value = Napi::Buffer<char>::Copy(env, (static_cast<Values::Blob*>(field.get()))->value,
value = Napi::Buffer<char>::Copy(env, (static_cast<Values::Blob*>(field.get()))->value,
(static_cast<Values::Blob*>(field.get()))->length);
} break;
case SQLITE_NULL: {
Expand Down
6 changes: 3 additions & 3 deletions src/statement.h
Expand Up @@ -19,13 +19,13 @@ namespace node_sqlite3 {

namespace Values {
struct Field {
inline Field(unsigned short _index, unsigned short _type = SQLITE_NULL) :
inline Field(int _index, unsigned short _type = SQLITE_NULL) :
type(_type), index(_index) {}
inline Field(const char* _name, unsigned short _type = SQLITE_NULL) :
type(_type), index(0), name(_name) {}

unsigned short type;
unsigned short index;
int index;
std::string name;

virtual ~Field() = default;
Expand Down Expand Up @@ -62,7 +62,7 @@ namespace Values {
inline virtual ~Blob() override {
delete[] value;
}
int length;
size_t length;
char* value;
};

Expand Down

0 comments on commit 181980b

Please sign in to comment.