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

fix: enable and fix type-casting warnings #1689

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
"MACOSX_DEPLOYMENT_TARGET": "10.7",
},
"msvs_settings": {
"VCCLCompilerTool": { "ExceptionHandling": 1 },
"VCCLCompilerTool": {
"ExceptionHandling": 1,
"AdditionalOptions": [
"/w34244",
"/w34267"
]
},
},
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")"],
Expand Down
6 changes: 3 additions & 3 deletions src/database.cc
Original file line number Diff line number Diff line change
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 @@ -341,7 +341,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 @@ -550,7 +550,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
Original file line number Diff line number Diff line change
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 @@ -137,7 +137,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 @@ -222,7 +222,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 @@ -240,7 +240,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 @@ -296,20 +296,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 @@ -510,7 +512,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 @@ -610,14 +612,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 @@ -796,17 +798,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
Original file line number Diff line number Diff line change
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