Skip to content

Commit

Permalink
fix: fix type-casting warnings (#32)
Browse files Browse the repository at this point in the history
Co-authored-by: Robo <hop2deep@gmail.com>
  • Loading branch information
rzhao271 and deepak1556 committed Jun 14, 2023
1 parent 29af3e9 commit 97429a0
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 27 deletions.
2 changes: 2 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"ExceptionHandling": 1,
"AdditionalOptions": [
"/guard:cf",
"/w34244",
"/we4267",
"/ZH:SHA_256"
]
},
Expand Down
2 changes: 1 addition & 1 deletion src/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ template <class Item, class Parent> class Async {
NODE_SQLITE3_MUTEX_LOCK(&async->mutex)
rows.swap(async->data);
NODE_SQLITE3_MUTEX_UNLOCK(&async->mutex)
for (unsigned int i = 0, size = rows.size(); i < size; i++) {
for (size_t i = 0, size = rows.size(); i < size; i++) {
async->callback(async->parent, rows[i]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/backup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Backup::Backup(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Backup>(info)
return;
}

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

if (length <= 0 || !Database::HasInstance(info[0])) {
Napi::TypeError::New(env, "Database object expected").ThrowAsJavaScriptException();
Expand Down
4 changes: 2 additions & 2 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+
napi_property_attributes napi_default_method = static_cast<napi_property_attributes>(napi_writable | napi_configurable);
napi_property_attributes napi_default_method = static_cast<napi_property_attributes>(napi_writable | napi_configurable);

Napi::Function t = DefineClass(env, "Database", {
InstanceMethod("close", &Database::Close, napi_default_method),
Expand Down Expand Up @@ -573,7 +573,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
41 changes: 22 additions & 19 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) {
Napi::Env env = info.Env();
int length = info.Length();
size_t 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 @@ -222,11 +222,10 @@ template <class T> Values::Field*
}
}

template <class T> T* Statement::Bind(const Napi::CallbackInfo& info, int start, int last) {
template <class T> T* Statement::Bind(const Napi::CallbackInfo& info, size_t start, size_t last) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

if (last < 0) last = info.Length();
Napi::Function callback;
if (last > start && info[last - 1].IsFunction()) {
callback = info[last - 1].As<Napi::Function>();
Expand All @@ -238,16 +237,18 @@ template <class T> T* Statement::Bind(const Napi::CallbackInfo& info, int start,
if (start < last) {
if (info[start].IsArray()) {
Napi::Array array = info[start].As<Napi::Array>();
int length = array.Length();
uint32_t length = array.Length();
// Note: bind parameters start with 1.
for (int i = 0, pos = 1; i < length; i++, pos++) {
int pos = 1;
for (uint32_t i = 0; i < length; i++, pos++) {
baton->parameters.push_back(BindParameter((array).Get(i), pos));
}
}
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.
for (int i = start, pos = 1; i < last; i++, pos++) {
int pos = 1;
for (size_t i = start; i < last; i++, pos++) {
baton->parameters.push_back(BindParameter(info[i], pos));
}
}
Expand Down Expand Up @@ -292,7 +293,7 @@ bool Statement::Bind(const Parameters & parameters) {
Values::Field* field = *it;

if (field != NULL) {
unsigned int pos;
int pos;
if (field->index > 0) {
pos = field->index;
}
Expand All @@ -302,20 +303,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,
((Values::Integer*)field)->value);
} break;
case SQLITE_FLOAT: {
status = sqlite3_bind_double(_handle, pos,
((Values::Float*)field)->value);
} break;
case SQLITE_TEXT: {
status = sqlite3_bind_text(_handle, pos,
status = sqlite3_bind_text64(_handle, pos,
((Values::Text*)field)->value.c_str(),
((Values::Text*)field)->value.size(), SQLITE_TRANSIENT);
((Values::Text*)field)->value.size(),
SQLITE_TRANSIENT,
SQLITE_UTF8);
} break;
case SQLITE_BLOB: {
status = sqlite3_bind_blob(_handle, pos,
status = sqlite3_bind_blob64(_handle, pos,
((Values::Blob*)field)->value,
((Values::Blob*)field)->length, SQLITE_TRANSIENT);
} break;
Expand All @@ -338,7 +341,7 @@ Napi::Value Statement::Bind(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Statement* stmt = this;

Baton* baton = stmt->Bind<Baton>(info);
Baton* baton = stmt->Bind<Baton>(info, 0, info.Length());
if (baton == NULL) {
Napi::TypeError::New(env, "Data type is not supported").ThrowAsJavaScriptException();
return env.Null();
Expand Down Expand Up @@ -390,7 +393,7 @@ Napi::Value Statement::Get(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Statement* stmt = this;

Baton* baton = stmt->Bind<RowBaton>(info);
Baton* baton = stmt->Bind<RowBaton>(info, 0, info.Length());
if (baton == NULL) {
Napi::Error::New(env, "Data type is not supported").ThrowAsJavaScriptException();
return env.Null();
Expand Down Expand Up @@ -462,7 +465,7 @@ Napi::Value Statement::Run(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Statement* stmt = this;

Baton* baton = stmt->Bind<RunBaton>(info);
Baton* baton = stmt->Bind<RunBaton>(info, 0, info.Length());
if (baton == NULL) {
Napi::Error::New(env, "Data type is not supported").ThrowAsJavaScriptException();
return env.Null();
Expand Down Expand Up @@ -517,7 +520,7 @@ void Statement::Work_AfterRun(napi_env e, napi_status status, void* data) {
// Fire callbacks.
Napi::Function cb = baton->callback.Value();
if (!cb.IsUndefined() && cb.IsFunction()) {
(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 All @@ -532,7 +535,7 @@ Napi::Value Statement::All(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Statement* stmt = this;

Baton* baton = stmt->Bind<RowsBaton>(info);
Baton* baton = stmt->Bind<RowsBaton>(info, 0, info.Length());
if (baton == NULL) {
Napi::Error::New(env, "Data type is not supported").ThrowAsJavaScriptException();
return env.Null();
Expand Down Expand Up @@ -618,7 +621,7 @@ Napi::Value Statement::Each(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Statement* stmt = this;

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

Napi::Function completed;
if (last >= 2 && info[last - 1].IsFunction() && info[last - 2].IsFunction()) {
Expand Down Expand Up @@ -813,7 +816,7 @@ Napi::Value Statement::RowToJS(Napi::Env env, Row* row) {

switch (field->type) {
case SQLITE_INTEGER: {
value = Napi::Number::New(env, ((Values::Integer*)field)->value);
value = Napi::Number::New(env, static_cast<double>(((Values::Integer*)field)->value));
} break;
case SQLITE_FLOAT: {
value = Napi::Number::New(env, ((Values::Float*)field)->value);
Expand Down
8 changes: 4 additions & 4 deletions src/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,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;
};

Expand Down Expand Up @@ -57,7 +57,7 @@ namespace Values {
inline ~Blob() {
free(value);
}
int length;
size_t length;
char* value;
};

Expand Down Expand Up @@ -221,7 +221,7 @@ class Statement : public Napi::ObjectWrap<Statement> {
void Finalize_();

template <class T> inline Values::Field* BindParameter(const Napi::Value source, T pos);
template <class T> T* Bind(const Napi::CallbackInfo& info, int start = 0, int end = -1);
template <class T> T* Bind(const Napi::CallbackInfo& info, size_t start, size_t end);
bool Bind(const Parameters &parameters);

static void GetRow(Row* row, sqlite3_stmt* stmt);
Expand Down

0 comments on commit 97429a0

Please sign in to comment.