done
This commit is contained in:
73
node_modules/better-sqlite3/src/util/bind-map.cpp
generated
vendored
Normal file
73
node_modules/better-sqlite3/src/util/bind-map.cpp
generated
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
class BindMap {
|
||||
public:
|
||||
|
||||
// This nested class represents a single mapping between a parameter name
|
||||
// and its associated parameter index in a prepared statement.
|
||||
class Pair { friend class BindMap;
|
||||
public:
|
||||
|
||||
inline int GetIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
inline v8::Local<v8::String> GetName(v8::Isolate* isolate) {
|
||||
return name.Get(isolate);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
explicit Pair(v8::Isolate* isolate, const char* name, int index)
|
||||
: name(isolate, InternalizedFromUtf8(isolate, name, -1)), index(index) {}
|
||||
|
||||
explicit Pair(v8::Isolate* isolate, Pair* pair)
|
||||
: name(isolate, pair->name), index(pair->index) {}
|
||||
|
||||
const v8::Global<v8::String> name;
|
||||
const int index;
|
||||
};
|
||||
|
||||
explicit BindMap(char _) {
|
||||
assert(_ == 0);
|
||||
pairs = NULL;
|
||||
capacity = 0;
|
||||
length = 0;
|
||||
}
|
||||
|
||||
~BindMap() {
|
||||
while (length) pairs[--length].~Pair();
|
||||
FREE_ARRAY<Pair>(pairs);
|
||||
}
|
||||
|
||||
inline Pair* GetPairs() {
|
||||
return pairs;
|
||||
}
|
||||
|
||||
inline int GetSize() {
|
||||
return length;
|
||||
}
|
||||
|
||||
// Adds a pair to the bind map, expanding the capacity if necessary.
|
||||
void Add(v8::Isolate* isolate, const char* name, int index) {
|
||||
assert(name != NULL);
|
||||
if (capacity == length) Grow(isolate);
|
||||
new (pairs + length++) Pair(isolate, name, index);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void Grow(v8::Isolate* isolate) {
|
||||
assert(capacity == length);
|
||||
capacity = (capacity << 1) | 2;
|
||||
Pair* new_pairs = ALLOC_ARRAY<Pair>(capacity);
|
||||
for (int i = 0; i < length; ++i) {
|
||||
new (new_pairs + i) Pair(isolate, pairs + i);
|
||||
pairs[i].~Pair();
|
||||
}
|
||||
FREE_ARRAY<Pair>(pairs);
|
||||
pairs = new_pairs;
|
||||
}
|
||||
|
||||
Pair* pairs;
|
||||
int capacity;
|
||||
int length;
|
||||
};
|
||||
193
node_modules/better-sqlite3/src/util/binder.cpp
generated
vendored
Normal file
193
node_modules/better-sqlite3/src/util/binder.cpp
generated
vendored
Normal file
@@ -0,0 +1,193 @@
|
||||
class Binder {
|
||||
public:
|
||||
|
||||
explicit Binder(sqlite3_stmt* _handle) {
|
||||
handle = _handle;
|
||||
param_count = sqlite3_bind_parameter_count(_handle);
|
||||
anon_index = 0;
|
||||
success = true;
|
||||
}
|
||||
|
||||
bool Bind(NODE_ARGUMENTS info, int argc, Statement* stmt) {
|
||||
assert(anon_index == 0);
|
||||
Result result = BindArgs(info, argc, stmt);
|
||||
if (success && result.count != param_count) {
|
||||
if (result.count < param_count) {
|
||||
if (!result.bound_object && stmt->GetBindMap(OnlyIsolate)->GetSize()) {
|
||||
Fail(ThrowTypeError, "Missing named parameters");
|
||||
} else {
|
||||
Fail(ThrowRangeError, "Too few parameter values were provided");
|
||||
}
|
||||
} else {
|
||||
Fail(ThrowRangeError, "Too many parameter values were provided");
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
struct Result {
|
||||
int count;
|
||||
bool bound_object;
|
||||
};
|
||||
|
||||
static bool IsPlainObject(v8::Isolate* isolate, v8::Local<v8::Object> obj) {
|
||||
v8::Local<v8::Value> proto = GET_PROTOTYPE(obj);
|
||||
v8::Local<v8::Context> ctx = obj->GetCreationContext().ToLocalChecked();
|
||||
ctx->Enter();
|
||||
v8::Local<v8::Value> baseProto = GET_PROTOTYPE(v8::Object::New(isolate));
|
||||
ctx->Exit();
|
||||
return proto->StrictEquals(baseProto) || proto->StrictEquals(v8::Null(isolate));
|
||||
}
|
||||
|
||||
void Fail(void (*Throw)(const char* _), const char* message) {
|
||||
assert(success == true);
|
||||
assert((Throw == NULL) == (message == NULL));
|
||||
assert(Throw == ThrowError || Throw == ThrowTypeError || Throw == ThrowRangeError || Throw == NULL);
|
||||
if (Throw) Throw(message);
|
||||
success = false;
|
||||
}
|
||||
|
||||
int NextAnonIndex() {
|
||||
while (sqlite3_bind_parameter_name(handle, ++anon_index) != NULL) {}
|
||||
return anon_index;
|
||||
}
|
||||
|
||||
// Binds the value at the given index or throws an appropriate error.
|
||||
void BindValue(v8::Isolate* isolate, v8::Local<v8::Value> value, int index) {
|
||||
int status = Data::BindValueFromJS(isolate, handle, index, value);
|
||||
if (status != SQLITE_OK) {
|
||||
switch (status) {
|
||||
case -1:
|
||||
return Fail(ThrowTypeError, "SQLite3 can only bind numbers, strings, bigints, buffers, and null");
|
||||
case SQLITE_TOOBIG:
|
||||
return Fail(ThrowRangeError, "The bound string, buffer, or bigint is too big");
|
||||
case SQLITE_RANGE:
|
||||
return Fail(ThrowRangeError, "Too many parameter values were provided");
|
||||
case SQLITE_NOMEM:
|
||||
return Fail(ThrowError, "Out of memory");
|
||||
default:
|
||||
return Fail(ThrowError, "An unexpected error occured while trying to bind parameters");
|
||||
}
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
// Binds each value in the array or throws an appropriate error.
|
||||
// The number of successfully bound parameters is returned.
|
||||
int BindArray(v8::Isolate* isolate, v8::Local<v8::Array> arr) {
|
||||
UseContext;
|
||||
uint32_t length = arr->Length();
|
||||
if (length > INT_MAX) {
|
||||
Fail(ThrowRangeError, "Too many parameter values were provided");
|
||||
return 0;
|
||||
}
|
||||
int len = static_cast<int>(length);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
v8::MaybeLocal<v8::Value> maybeValue = arr->Get(ctx, i);
|
||||
if (maybeValue.IsEmpty()) {
|
||||
Fail(NULL, NULL);
|
||||
return i;
|
||||
}
|
||||
BindValue(isolate, maybeValue.ToLocalChecked(), NextAnonIndex());
|
||||
if (!success) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
// Binds all named parameters using the values found in the given object.
|
||||
// The number of successfully bound parameters is returned.
|
||||
// If a named parameter is missing from the object, an error is thrown.
|
||||
// This should only be invoked once per instance.
|
||||
int BindObject(v8::Isolate* isolate, v8::Local<v8::Object> obj, Statement* stmt) {
|
||||
UseContext;
|
||||
BindMap* bind_map = stmt->GetBindMap(isolate);
|
||||
BindMap::Pair* pairs = bind_map->GetPairs();
|
||||
int len = bind_map->GetSize();
|
||||
|
||||
for (int i = 0; i < len; ++i) {
|
||||
v8::Local<v8::String> key = pairs[i].GetName(isolate);
|
||||
|
||||
// Check if the named parameter was provided.
|
||||
v8::Maybe<bool> has_property = obj->HasOwnProperty(ctx, key);
|
||||
if (has_property.IsNothing()) {
|
||||
Fail(NULL, NULL);
|
||||
return i;
|
||||
}
|
||||
if (!has_property.FromJust()) {
|
||||
v8::String::Utf8Value param_name(isolate, key);
|
||||
Fail(ThrowRangeError, (std::string("Missing named parameter \"") + *param_name + "\"").c_str());
|
||||
return i;
|
||||
}
|
||||
|
||||
// Get the current property value.
|
||||
v8::MaybeLocal<v8::Value> maybeValue = obj->Get(ctx, key);
|
||||
if (maybeValue.IsEmpty()) {
|
||||
Fail(NULL, NULL);
|
||||
return i;
|
||||
}
|
||||
|
||||
BindValue(isolate, maybeValue.ToLocalChecked(), pairs[i].GetIndex());
|
||||
if (!success) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
// Binds all parameters using the values found in the arguments object.
|
||||
// Anonymous parameter values can be directly in the arguments object or in an Array.
|
||||
// Named parameter values can be provided in a plain Object argument.
|
||||
// Only one plain Object argument may be provided.
|
||||
// If an error occurs, an appropriate error is thrown.
|
||||
// The return value is a struct indicating how many parameters were successfully bound
|
||||
// and whether or not it tried to bind an object.
|
||||
Result BindArgs(NODE_ARGUMENTS info, int argc, Statement* stmt) {
|
||||
UseIsolate;
|
||||
int count = 0;
|
||||
bool bound_object = false;
|
||||
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
v8::Local<v8::Value> arg = info[i];
|
||||
|
||||
if (arg->IsArray()) {
|
||||
count += BindArray(isolate, arg.As<v8::Array>());
|
||||
if (!success) break;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg->IsObject() && !node::Buffer::HasInstance(arg)) {
|
||||
v8::Local<v8::Object> obj = arg.As<v8::Object>();
|
||||
if (IsPlainObject(isolate, obj)) {
|
||||
if (bound_object) {
|
||||
Fail(ThrowTypeError, "You cannot specify named parameters in two different objects");
|
||||
break;
|
||||
}
|
||||
bound_object = true;
|
||||
|
||||
count += BindObject(isolate, obj, stmt);
|
||||
if (!success) break;
|
||||
continue;
|
||||
} else if (stmt->GetBindMap(isolate)->GetSize()) {
|
||||
Fail(ThrowTypeError, "Named parameters can only be passed within plain objects");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
BindValue(isolate, arg, NextAnonIndex());
|
||||
if (!success) break;
|
||||
count += 1;
|
||||
}
|
||||
|
||||
return { count, bound_object };
|
||||
}
|
||||
|
||||
sqlite3_stmt* handle;
|
||||
int param_count;
|
||||
int anon_index; // This value should only be used by NextAnonIndex()
|
||||
bool success; // This value should only be set by Fail()
|
||||
};
|
||||
172
node_modules/better-sqlite3/src/util/constants.cpp
generated
vendored
Normal file
172
node_modules/better-sqlite3/src/util/constants.cpp
generated
vendored
Normal file
@@ -0,0 +1,172 @@
|
||||
class CS {
|
||||
public:
|
||||
|
||||
v8::Local<v8::String> Code(v8::Isolate* isolate, int code) {
|
||||
auto element = codes.find(code);
|
||||
if (element != codes.end()) return element->second.Get(isolate);
|
||||
return StringFromUtf8(isolate, (std::string("UNKNOWN_SQLITE_ERROR_") + std::to_string(code)).c_str(), -1);
|
||||
}
|
||||
|
||||
explicit CS(v8::Isolate* isolate) {
|
||||
SetString(isolate, database, "database");
|
||||
SetString(isolate, reader, "reader");
|
||||
SetString(isolate, source, "source");
|
||||
SetString(isolate, memory, "memory");
|
||||
SetString(isolate, readonly, "readonly");
|
||||
SetString(isolate, name, "name");
|
||||
SetString(isolate, next, "next");
|
||||
SetString(isolate, length, "length");
|
||||
SetString(isolate, done, "done");
|
||||
SetString(isolate, value, "value");
|
||||
SetString(isolate, changes, "changes");
|
||||
SetString(isolate, lastInsertRowid, "lastInsertRowid");
|
||||
SetString(isolate, statement, "statement");
|
||||
SetString(isolate, column, "column");
|
||||
SetString(isolate, table, "table");
|
||||
SetString(isolate, type, "type");
|
||||
SetString(isolate, totalPages, "totalPages");
|
||||
SetString(isolate, remainingPages, "remainingPages");
|
||||
|
||||
SetCode(isolate, SQLITE_OK, "SQLITE_OK");
|
||||
SetCode(isolate, SQLITE_ERROR, "SQLITE_ERROR");
|
||||
SetCode(isolate, SQLITE_INTERNAL, "SQLITE_INTERNAL");
|
||||
SetCode(isolate, SQLITE_PERM, "SQLITE_PERM");
|
||||
SetCode(isolate, SQLITE_ABORT, "SQLITE_ABORT");
|
||||
SetCode(isolate, SQLITE_BUSY, "SQLITE_BUSY");
|
||||
SetCode(isolate, SQLITE_LOCKED, "SQLITE_LOCKED");
|
||||
SetCode(isolate, SQLITE_NOMEM, "SQLITE_NOMEM");
|
||||
SetCode(isolate, SQLITE_READONLY, "SQLITE_READONLY");
|
||||
SetCode(isolate, SQLITE_INTERRUPT, "SQLITE_INTERRUPT");
|
||||
SetCode(isolate, SQLITE_IOERR, "SQLITE_IOERR");
|
||||
SetCode(isolate, SQLITE_CORRUPT, "SQLITE_CORRUPT");
|
||||
SetCode(isolate, SQLITE_NOTFOUND, "SQLITE_NOTFOUND");
|
||||
SetCode(isolate, SQLITE_FULL, "SQLITE_FULL");
|
||||
SetCode(isolate, SQLITE_CANTOPEN, "SQLITE_CANTOPEN");
|
||||
SetCode(isolate, SQLITE_PROTOCOL, "SQLITE_PROTOCOL");
|
||||
SetCode(isolate, SQLITE_EMPTY, "SQLITE_EMPTY");
|
||||
SetCode(isolate, SQLITE_SCHEMA, "SQLITE_SCHEMA");
|
||||
SetCode(isolate, SQLITE_TOOBIG, "SQLITE_TOOBIG");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT, "SQLITE_CONSTRAINT");
|
||||
SetCode(isolate, SQLITE_MISMATCH, "SQLITE_MISMATCH");
|
||||
SetCode(isolate, SQLITE_MISUSE, "SQLITE_MISUSE");
|
||||
SetCode(isolate, SQLITE_NOLFS, "SQLITE_NOLFS");
|
||||
SetCode(isolate, SQLITE_AUTH, "SQLITE_AUTH");
|
||||
SetCode(isolate, SQLITE_FORMAT, "SQLITE_FORMAT");
|
||||
SetCode(isolate, SQLITE_RANGE, "SQLITE_RANGE");
|
||||
SetCode(isolate, SQLITE_NOTADB, "SQLITE_NOTADB");
|
||||
SetCode(isolate, SQLITE_NOTICE, "SQLITE_NOTICE");
|
||||
SetCode(isolate, SQLITE_WARNING, "SQLITE_WARNING");
|
||||
SetCode(isolate, SQLITE_ROW, "SQLITE_ROW");
|
||||
SetCode(isolate, SQLITE_DONE, "SQLITE_DONE");
|
||||
|
||||
SetCode(isolate, SQLITE_ERROR_MISSING_COLLSEQ, "SQLITE_ERROR_MISSING_COLLSEQ");
|
||||
SetCode(isolate, SQLITE_ERROR_RETRY, "SQLITE_ERROR_RETRY");
|
||||
SetCode(isolate, SQLITE_ERROR_SNAPSHOT, "SQLITE_ERROR_SNAPSHOT");
|
||||
SetCode(isolate, SQLITE_IOERR_READ, "SQLITE_IOERR_READ");
|
||||
SetCode(isolate, SQLITE_IOERR_SHORT_READ, "SQLITE_IOERR_SHORT_READ");
|
||||
SetCode(isolate, SQLITE_IOERR_WRITE, "SQLITE_IOERR_WRITE");
|
||||
SetCode(isolate, SQLITE_IOERR_FSYNC, "SQLITE_IOERR_FSYNC");
|
||||
SetCode(isolate, SQLITE_IOERR_DIR_FSYNC, "SQLITE_IOERR_DIR_FSYNC");
|
||||
SetCode(isolate, SQLITE_IOERR_TRUNCATE, "SQLITE_IOERR_TRUNCATE");
|
||||
SetCode(isolate, SQLITE_IOERR_FSTAT, "SQLITE_IOERR_FSTAT");
|
||||
SetCode(isolate, SQLITE_IOERR_UNLOCK, "SQLITE_IOERR_UNLOCK");
|
||||
SetCode(isolate, SQLITE_IOERR_RDLOCK, "SQLITE_IOERR_RDLOCK");
|
||||
SetCode(isolate, SQLITE_IOERR_DELETE, "SQLITE_IOERR_DELETE");
|
||||
SetCode(isolate, SQLITE_IOERR_BLOCKED, "SQLITE_IOERR_BLOCKED");
|
||||
SetCode(isolate, SQLITE_IOERR_NOMEM, "SQLITE_IOERR_NOMEM");
|
||||
SetCode(isolate, SQLITE_IOERR_ACCESS, "SQLITE_IOERR_ACCESS");
|
||||
SetCode(isolate, SQLITE_IOERR_CHECKRESERVEDLOCK, "SQLITE_IOERR_CHECKRESERVEDLOCK");
|
||||
SetCode(isolate, SQLITE_IOERR_LOCK, "SQLITE_IOERR_LOCK");
|
||||
SetCode(isolate, SQLITE_IOERR_CLOSE, "SQLITE_IOERR_CLOSE");
|
||||
SetCode(isolate, SQLITE_IOERR_DIR_CLOSE, "SQLITE_IOERR_DIR_CLOSE");
|
||||
SetCode(isolate, SQLITE_IOERR_SHMOPEN, "SQLITE_IOERR_SHMOPEN");
|
||||
SetCode(isolate, SQLITE_IOERR_SHMSIZE, "SQLITE_IOERR_SHMSIZE");
|
||||
SetCode(isolate, SQLITE_IOERR_SHMLOCK, "SQLITE_IOERR_SHMLOCK");
|
||||
SetCode(isolate, SQLITE_IOERR_SHMMAP, "SQLITE_IOERR_SHMMAP");
|
||||
SetCode(isolate, SQLITE_IOERR_SEEK, "SQLITE_IOERR_SEEK");
|
||||
SetCode(isolate, SQLITE_IOERR_DELETE_NOENT, "SQLITE_IOERR_DELETE_NOENT");
|
||||
SetCode(isolate, SQLITE_IOERR_MMAP, "SQLITE_IOERR_MMAP");
|
||||
SetCode(isolate, SQLITE_IOERR_GETTEMPPATH, "SQLITE_IOERR_GETTEMPPATH");
|
||||
SetCode(isolate, SQLITE_IOERR_CONVPATH, "SQLITE_IOERR_CONVPATH");
|
||||
SetCode(isolate, SQLITE_IOERR_VNODE, "SQLITE_IOERR_VNODE");
|
||||
SetCode(isolate, SQLITE_IOERR_AUTH, "SQLITE_IOERR_AUTH");
|
||||
SetCode(isolate, SQLITE_IOERR_BEGIN_ATOMIC, "SQLITE_IOERR_BEGIN_ATOMIC");
|
||||
SetCode(isolate, SQLITE_IOERR_COMMIT_ATOMIC, "SQLITE_IOERR_COMMIT_ATOMIC");
|
||||
SetCode(isolate, SQLITE_IOERR_ROLLBACK_ATOMIC, "SQLITE_IOERR_ROLLBACK_ATOMIC");
|
||||
SetCode(isolate, SQLITE_IOERR_DATA, "SQLITE_IOERR_DATA");
|
||||
SetCode(isolate, SQLITE_IOERR_CORRUPTFS, "SQLITE_IOERR_CORRUPTFS");
|
||||
SetCode(isolate, SQLITE_IOERR_IN_PAGE, "SQLITE_IOERR_IN_PAGE");
|
||||
SetCode(isolate, SQLITE_LOCKED_SHAREDCACHE, "SQLITE_LOCKED_SHAREDCACHE");
|
||||
SetCode(isolate, SQLITE_LOCKED_VTAB, "SQLITE_LOCKED_VTAB");
|
||||
SetCode(isolate, SQLITE_BUSY_RECOVERY, "SQLITE_BUSY_RECOVERY");
|
||||
SetCode(isolate, SQLITE_BUSY_SNAPSHOT, "SQLITE_BUSY_SNAPSHOT");
|
||||
SetCode(isolate, SQLITE_CANTOPEN_NOTEMPDIR, "SQLITE_CANTOPEN_NOTEMPDIR");
|
||||
SetCode(isolate, SQLITE_CANTOPEN_ISDIR, "SQLITE_CANTOPEN_ISDIR");
|
||||
SetCode(isolate, SQLITE_CANTOPEN_FULLPATH, "SQLITE_CANTOPEN_FULLPATH");
|
||||
SetCode(isolate, SQLITE_CANTOPEN_CONVPATH, "SQLITE_CANTOPEN_CONVPATH");
|
||||
SetCode(isolate, SQLITE_CANTOPEN_DIRTYWAL, "SQLITE_CANTOPEN_DIRTYWAL");
|
||||
SetCode(isolate, SQLITE_CANTOPEN_SYMLINK, "SQLITE_CANTOPEN_SYMLINK");
|
||||
SetCode(isolate, SQLITE_CORRUPT_VTAB, "SQLITE_CORRUPT_VTAB");
|
||||
SetCode(isolate, SQLITE_CORRUPT_SEQUENCE, "SQLITE_CORRUPT_SEQUENCE");
|
||||
SetCode(isolate, SQLITE_CORRUPT_INDEX, "SQLITE_CORRUPT_INDEX");
|
||||
SetCode(isolate, SQLITE_READONLY_RECOVERY, "SQLITE_READONLY_RECOVERY");
|
||||
SetCode(isolate, SQLITE_READONLY_CANTLOCK, "SQLITE_READONLY_CANTLOCK");
|
||||
SetCode(isolate, SQLITE_READONLY_ROLLBACK, "SQLITE_READONLY_ROLLBACK");
|
||||
SetCode(isolate, SQLITE_READONLY_DBMOVED, "SQLITE_READONLY_DBMOVED");
|
||||
SetCode(isolate, SQLITE_READONLY_CANTINIT, "SQLITE_READONLY_CANTINIT");
|
||||
SetCode(isolate, SQLITE_READONLY_DIRECTORY, "SQLITE_READONLY_DIRECTORY");
|
||||
SetCode(isolate, SQLITE_ABORT_ROLLBACK, "SQLITE_ABORT_ROLLBACK");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_CHECK, "SQLITE_CONSTRAINT_CHECK");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_COMMITHOOK, "SQLITE_CONSTRAINT_COMMITHOOK");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_FOREIGNKEY, "SQLITE_CONSTRAINT_FOREIGNKEY");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_FUNCTION, "SQLITE_CONSTRAINT_FUNCTION");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_NOTNULL, "SQLITE_CONSTRAINT_NOTNULL");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_PRIMARYKEY, "SQLITE_CONSTRAINT_PRIMARYKEY");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_TRIGGER, "SQLITE_CONSTRAINT_TRIGGER");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_UNIQUE, "SQLITE_CONSTRAINT_UNIQUE");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_VTAB, "SQLITE_CONSTRAINT_VTAB");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_ROWID, "SQLITE_CONSTRAINT_ROWID");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_PINNED, "SQLITE_CONSTRAINT_PINNED");
|
||||
SetCode(isolate, SQLITE_CONSTRAINT_DATATYPE, "SQLITE_CONSTRAINT_DATATYPE");
|
||||
SetCode(isolate, SQLITE_NOTICE_RECOVER_WAL, "SQLITE_NOTICE_RECOVER_WAL");
|
||||
SetCode(isolate, SQLITE_NOTICE_RECOVER_ROLLBACK, "SQLITE_NOTICE_RECOVER_ROLLBACK");
|
||||
SetCode(isolate, SQLITE_NOTICE_RBU, "SQLITE_NOTICE_RBU");
|
||||
SetCode(isolate, SQLITE_WARNING_AUTOINDEX, "SQLITE_WARNING_AUTOINDEX");
|
||||
SetCode(isolate, SQLITE_AUTH_USER, "SQLITE_AUTH_USER");
|
||||
SetCode(isolate, SQLITE_OK_LOAD_PERMANENTLY, "SQLITE_OK_LOAD_PERMANENTLY");
|
||||
SetCode(isolate, SQLITE_OK_SYMLINK, "SQLITE_OK_SYMLINK");
|
||||
}
|
||||
|
||||
v8::Global<v8::String> database;
|
||||
v8::Global<v8::String> reader;
|
||||
v8::Global<v8::String> source;
|
||||
v8::Global<v8::String> memory;
|
||||
v8::Global<v8::String> readonly;
|
||||
v8::Global<v8::String> name;
|
||||
v8::Global<v8::String> next;
|
||||
v8::Global<v8::String> length;
|
||||
v8::Global<v8::String> done;
|
||||
v8::Global<v8::String> value;
|
||||
v8::Global<v8::String> changes;
|
||||
v8::Global<v8::String> lastInsertRowid;
|
||||
v8::Global<v8::String> statement;
|
||||
v8::Global<v8::String> column;
|
||||
v8::Global<v8::String> table;
|
||||
v8::Global<v8::String> type;
|
||||
v8::Global<v8::String> totalPages;
|
||||
v8::Global<v8::String> remainingPages;
|
||||
|
||||
private:
|
||||
|
||||
static void SetString(v8::Isolate* isolate, v8::Global<v8::String>& constant, const char* str) {
|
||||
constant.Reset(isolate, InternalizedFromLatin1(isolate, str));
|
||||
}
|
||||
|
||||
void SetCode(v8::Isolate* isolate, int code, const char* str) {
|
||||
codes.emplace(std::piecewise_construct,
|
||||
std::forward_as_tuple(code),
|
||||
std::forward_as_tuple(isolate, InternalizedFromLatin1(isolate, str)));
|
||||
}
|
||||
|
||||
std::unordered_map<int, v8::Global<v8::String> > codes;
|
||||
};
|
||||
121
node_modules/better-sqlite3/src/util/custom-aggregate.cpp
generated
vendored
Normal file
121
node_modules/better-sqlite3/src/util/custom-aggregate.cpp
generated
vendored
Normal file
@@ -0,0 +1,121 @@
|
||||
class CustomAggregate : public CustomFunction {
|
||||
public:
|
||||
|
||||
explicit CustomAggregate(
|
||||
v8::Isolate* isolate,
|
||||
Database* db,
|
||||
const char* name,
|
||||
v8::Local<v8::Value> start,
|
||||
v8::Local<v8::Function> step,
|
||||
v8::Local<v8::Value> inverse,
|
||||
v8::Local<v8::Value> result,
|
||||
bool safe_ints
|
||||
) :
|
||||
CustomFunction(isolate, db, name, step, safe_ints),
|
||||
invoke_result(result->IsFunction()),
|
||||
invoke_start(start->IsFunction()),
|
||||
inverse(isolate, inverse->IsFunction() ? inverse.As<v8::Function>() : v8::Local<v8::Function>()),
|
||||
result(isolate, result->IsFunction() ? result.As<v8::Function>() : v8::Local<v8::Function>()),
|
||||
start(isolate, start) {}
|
||||
|
||||
static void xStep(sqlite3_context* invocation, int argc, sqlite3_value** argv) {
|
||||
xStepBase(invocation, argc, argv, &CustomAggregate::fn);
|
||||
}
|
||||
|
||||
static void xInverse(sqlite3_context* invocation, int argc, sqlite3_value** argv) {
|
||||
xStepBase(invocation, argc, argv, &CustomAggregate::inverse);
|
||||
}
|
||||
|
||||
static void xValue(sqlite3_context* invocation) {
|
||||
xValueBase(invocation, false);
|
||||
}
|
||||
|
||||
static void xFinal(sqlite3_context* invocation) {
|
||||
xValueBase(invocation, true);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
static inline void xStepBase(sqlite3_context* invocation, int argc, sqlite3_value** argv, const v8::Global<v8::Function> CustomAggregate::*ptrtm) {
|
||||
AGGREGATE_START();
|
||||
|
||||
v8::Local<v8::Value> args_fast[5];
|
||||
v8::Local<v8::Value>* args = argc <= 4 ? args_fast : ALLOC_ARRAY<v8::Local<v8::Value>>(argc + 1);
|
||||
args[0] = acc->value.Get(isolate);
|
||||
if (argc != 0) Data::GetArgumentsJS(isolate, args + 1, argv, argc, self->safe_ints);
|
||||
|
||||
v8::MaybeLocal<v8::Value> maybeReturnValue = (self->*ptrtm).Get(isolate)->Call(OnlyContext, v8::Undefined(isolate), argc + 1, args);
|
||||
if (args != args_fast) delete[] args;
|
||||
|
||||
if (maybeReturnValue.IsEmpty()) {
|
||||
self->PropagateJSError(invocation);
|
||||
} else {
|
||||
v8::Local<v8::Value> returnValue = maybeReturnValue.ToLocalChecked();
|
||||
if (!returnValue->IsUndefined()) acc->value.Reset(isolate, returnValue);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void xValueBase(sqlite3_context* invocation, bool is_final) {
|
||||
AGGREGATE_START();
|
||||
|
||||
if (!is_final) {
|
||||
acc->is_window = true;
|
||||
} else if (acc->is_window) {
|
||||
DestroyAccumulator(invocation);
|
||||
return;
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> result = acc->value.Get(isolate);
|
||||
if (self->invoke_result) {
|
||||
v8::MaybeLocal<v8::Value> maybeResult = self->result.Get(isolate)->Call(OnlyContext, v8::Undefined(isolate), 1, &result);
|
||||
if (maybeResult.IsEmpty()) {
|
||||
self->PropagateJSError(invocation);
|
||||
return;
|
||||
}
|
||||
result = maybeResult.ToLocalChecked();
|
||||
}
|
||||
|
||||
Data::ResultValueFromJS(isolate, invocation, result, self);
|
||||
if (is_final) DestroyAccumulator(invocation);
|
||||
}
|
||||
|
||||
struct Accumulator { public:
|
||||
v8::Global<v8::Value> value;
|
||||
bool initialized;
|
||||
bool is_window;
|
||||
};
|
||||
|
||||
Accumulator* GetAccumulator(sqlite3_context* invocation) {
|
||||
Accumulator* acc = static_cast<Accumulator*>(sqlite3_aggregate_context(invocation, sizeof(Accumulator)));
|
||||
if (!acc->initialized) {
|
||||
assert(acc->value.IsEmpty());
|
||||
acc->initialized = true;
|
||||
if (invoke_start) {
|
||||
v8::MaybeLocal<v8::Value> maybeSeed = start.Get(isolate).As<v8::Function>()->Call(OnlyContext, v8::Undefined(isolate), 0, NULL);
|
||||
if (maybeSeed.IsEmpty()) PropagateJSError(invocation);
|
||||
else acc->value.Reset(isolate, maybeSeed.ToLocalChecked());
|
||||
} else {
|
||||
assert(!start.IsEmpty());
|
||||
acc->value.Reset(isolate, start);
|
||||
}
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
|
||||
static void DestroyAccumulator(sqlite3_context* invocation) {
|
||||
Accumulator* acc = static_cast<Accumulator*>(sqlite3_aggregate_context(invocation, sizeof(Accumulator)));
|
||||
assert(acc->initialized);
|
||||
acc->value.Reset();
|
||||
}
|
||||
|
||||
void PropagateJSError(sqlite3_context* invocation) {
|
||||
DestroyAccumulator(invocation);
|
||||
CustomFunction::PropagateJSError(invocation);
|
||||
}
|
||||
|
||||
const bool invoke_result;
|
||||
const bool invoke_start;
|
||||
const v8::Global<v8::Function> inverse;
|
||||
const v8::Global<v8::Function> result;
|
||||
const v8::Global<v8::Value> start;
|
||||
};
|
||||
59
node_modules/better-sqlite3/src/util/custom-function.cpp
generated
vendored
Normal file
59
node_modules/better-sqlite3/src/util/custom-function.cpp
generated
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
class CustomFunction : protected DataConverter {
|
||||
public:
|
||||
|
||||
explicit CustomFunction(
|
||||
v8::Isolate* isolate,
|
||||
Database* db,
|
||||
const char* name,
|
||||
v8::Local<v8::Function> fn,
|
||||
bool safe_ints
|
||||
) :
|
||||
name(name),
|
||||
db(db),
|
||||
isolate(isolate),
|
||||
fn(isolate, fn),
|
||||
safe_ints(safe_ints) {}
|
||||
|
||||
virtual ~CustomFunction() {}
|
||||
|
||||
static void xDestroy(void* self) {
|
||||
delete static_cast<CustomFunction*>(self);
|
||||
}
|
||||
|
||||
static void xFunc(sqlite3_context* invocation, int argc, sqlite3_value** argv) {
|
||||
FUNCTION_START();
|
||||
|
||||
v8::Local<v8::Value> args_fast[4];
|
||||
v8::Local<v8::Value>* args = NULL;
|
||||
if (argc != 0) {
|
||||
args = argc <= 4 ? args_fast : ALLOC_ARRAY<v8::Local<v8::Value>>(argc);
|
||||
Data::GetArgumentsJS(isolate, args, argv, argc, self->safe_ints);
|
||||
}
|
||||
|
||||
v8::MaybeLocal<v8::Value> maybeReturnValue = self->fn.Get(isolate)->Call(OnlyContext, v8::Undefined(isolate), argc, args);
|
||||
if (args != args_fast) delete[] args;
|
||||
|
||||
if (maybeReturnValue.IsEmpty()) self->PropagateJSError(invocation);
|
||||
else Data::ResultValueFromJS(isolate, invocation, maybeReturnValue.ToLocalChecked(), self);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
void PropagateJSError(sqlite3_context* invocation) {
|
||||
assert(db->GetState()->was_js_error == false);
|
||||
db->GetState()->was_js_error = true;
|
||||
sqlite3_result_error(invocation, "", 0);
|
||||
}
|
||||
|
||||
std::string GetDataErrorPrefix() {
|
||||
return std::string("User-defined function ") + name + "() returned";
|
||||
}
|
||||
|
||||
private:
|
||||
const std::string name;
|
||||
Database* const db;
|
||||
protected:
|
||||
v8::Isolate* const isolate;
|
||||
const v8::Global<v8::Function> fn;
|
||||
const bool safe_ints;
|
||||
};
|
||||
409
node_modules/better-sqlite3/src/util/custom-table.cpp
generated
vendored
Normal file
409
node_modules/better-sqlite3/src/util/custom-table.cpp
generated
vendored
Normal file
@@ -0,0 +1,409 @@
|
||||
class CustomTable {
|
||||
public:
|
||||
|
||||
explicit CustomTable(
|
||||
v8::Isolate* isolate,
|
||||
Database* db,
|
||||
const char* name,
|
||||
v8::Local<v8::Function> factory
|
||||
) :
|
||||
addon(db->GetAddon()),
|
||||
isolate(isolate),
|
||||
db(db),
|
||||
name(name),
|
||||
factory(isolate, factory) {}
|
||||
|
||||
static void Destructor(void* self) {
|
||||
delete static_cast<CustomTable*>(self);
|
||||
}
|
||||
|
||||
static sqlite3_module MODULE;
|
||||
static sqlite3_module EPONYMOUS_MODULE;
|
||||
|
||||
private:
|
||||
|
||||
// This nested class is instantiated on each CREATE VIRTUAL TABLE statement.
|
||||
class VTab { friend class CustomTable;
|
||||
explicit VTab(
|
||||
CustomTable* parent,
|
||||
v8::Local<v8::Function> generator,
|
||||
std::vector<std::string> parameter_names,
|
||||
bool safe_ints
|
||||
) :
|
||||
parent(parent),
|
||||
parameter_count(parameter_names.size()),
|
||||
safe_ints(safe_ints),
|
||||
generator(parent->isolate, generator),
|
||||
parameter_names(parameter_names) {
|
||||
((void)base);
|
||||
}
|
||||
|
||||
static inline CustomTable::VTab* Upcast(sqlite3_vtab* vtab) {
|
||||
return reinterpret_cast<VTab*>(vtab);
|
||||
}
|
||||
|
||||
inline sqlite3_vtab* Downcast() {
|
||||
return reinterpret_cast<sqlite3_vtab*>(this);
|
||||
}
|
||||
|
||||
sqlite3_vtab base;
|
||||
CustomTable * const parent;
|
||||
const int parameter_count;
|
||||
const bool safe_ints;
|
||||
const v8::Global<v8::Function> generator;
|
||||
const std::vector<std::string> parameter_names;
|
||||
};
|
||||
|
||||
// This nested class is instantiated each time a virtual table is scanned.
|
||||
class Cursor { friend class CustomTable;
|
||||
static inline CustomTable::Cursor* Upcast(sqlite3_vtab_cursor* cursor) {
|
||||
return reinterpret_cast<Cursor*>(cursor);
|
||||
}
|
||||
|
||||
inline sqlite3_vtab_cursor* Downcast() {
|
||||
return reinterpret_cast<sqlite3_vtab_cursor*>(this);
|
||||
}
|
||||
|
||||
inline CustomTable::VTab* GetVTab() {
|
||||
return VTab::Upcast(base.pVtab);
|
||||
}
|
||||
|
||||
sqlite3_vtab_cursor base;
|
||||
v8::Global<v8::Object> iterator;
|
||||
v8::Global<v8::Function> next;
|
||||
v8::Global<v8::Array> row;
|
||||
bool done;
|
||||
sqlite_int64 rowid;
|
||||
};
|
||||
|
||||
// This nested class is used by Data::ResultValueFromJS to report errors.
|
||||
class TempDataConverter : DataConverter { friend class CustomTable;
|
||||
explicit TempDataConverter(CustomTable* parent) :
|
||||
parent(parent),
|
||||
status(SQLITE_OK) {}
|
||||
|
||||
void PropagateJSError(sqlite3_context* invocation) {
|
||||
status = SQLITE_ERROR;
|
||||
parent->PropagateJSError();
|
||||
}
|
||||
|
||||
std::string GetDataErrorPrefix() {
|
||||
return std::string("Virtual table module \"") + parent->name + "\" yielded";
|
||||
}
|
||||
|
||||
CustomTable * const parent;
|
||||
int status;
|
||||
};
|
||||
|
||||
// Although this function does nothing, we cannot use xConnect directly,
|
||||
// because that would cause SQLite to register an eponymous virtual table.
|
||||
static int xCreate(sqlite3* db_handle, void* _self, int argc, const char* const * argv, sqlite3_vtab** output, char** errOutput) {
|
||||
return xConnect(db_handle, _self, argc, argv, output, errOutput);
|
||||
}
|
||||
|
||||
// This method uses the factory function to instantiate a new virtual table.
|
||||
static int xConnect(sqlite3* db_handle, void* _self, int argc, const char* const * argv, sqlite3_vtab** output, char** errOutput) {
|
||||
CustomTable* self = static_cast<CustomTable*>(_self);
|
||||
v8::Isolate* isolate = self->isolate;
|
||||
v8::HandleScope scope(isolate);
|
||||
UseContext;
|
||||
|
||||
v8::Local<v8::Value>* args = ALLOC_ARRAY<v8::Local<v8::Value>>(argc);
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
args[i] = StringFromUtf8(isolate, argv[i], -1);
|
||||
}
|
||||
|
||||
// Run the factory function to receive a new virtual table definition.
|
||||
v8::MaybeLocal<v8::Value> maybeReturnValue = self->factory.Get(isolate)->Call(ctx, v8::Undefined(isolate), argc, args);
|
||||
delete[] args;
|
||||
|
||||
if (maybeReturnValue.IsEmpty()) {
|
||||
self->PropagateJSError();
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
|
||||
// Extract each part of the virtual table definition.
|
||||
v8::Local<v8::Array> returnValue = maybeReturnValue.ToLocalChecked().As<v8::Array>();
|
||||
v8::Local<v8::String> sqlString = returnValue->Get(ctx, 0).ToLocalChecked().As<v8::String>();
|
||||
v8::Local<v8::Function> generator = returnValue->Get(ctx, 1).ToLocalChecked().As<v8::Function>();
|
||||
v8::Local<v8::Array> parameterNames = returnValue->Get(ctx, 2).ToLocalChecked().As<v8::Array>();
|
||||
int safe_ints = returnValue->Get(ctx, 3).ToLocalChecked().As<v8::Int32>()->Value();
|
||||
bool direct_only = returnValue->Get(ctx, 4).ToLocalChecked().As<v8::Boolean>()->Value();
|
||||
|
||||
v8::String::Utf8Value sql(isolate, sqlString);
|
||||
safe_ints = safe_ints < 2 ? safe_ints : static_cast<int>(self->db->GetState()->safe_ints);
|
||||
|
||||
// Copy the parameter names into a std::vector.
|
||||
std::vector<std::string> parameter_names;
|
||||
for (int i = 0, len = parameterNames->Length(); i < len; ++i) {
|
||||
v8::Local<v8::String> parameterName = parameterNames->Get(ctx, i).ToLocalChecked().As<v8::String>();
|
||||
v8::String::Utf8Value parameter_name(isolate, parameterName);
|
||||
parameter_names.emplace_back(*parameter_name);
|
||||
}
|
||||
|
||||
// Pass our SQL table definition to SQLite (this should never fail).
|
||||
if (sqlite3_declare_vtab(db_handle, *sql) != SQLITE_OK) {
|
||||
*errOutput = sqlite3_mprintf("failed to declare virtual table \"%s\"", argv[2]);
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
if (direct_only && sqlite3_vtab_config(db_handle, SQLITE_VTAB_DIRECTONLY) != SQLITE_OK) {
|
||||
*errOutput = sqlite3_mprintf("failed to configure virtual table \"%s\"", argv[2]);
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
|
||||
// Return the successfully created virtual table.
|
||||
*output = (new VTab(self, generator, parameter_names, safe_ints))->Downcast();
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static int xDisconnect(sqlite3_vtab* vtab) {
|
||||
delete VTab::Upcast(vtab);
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static int xOpen(sqlite3_vtab* vtab, sqlite3_vtab_cursor** output) {
|
||||
*output = (new Cursor())->Downcast();
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
static int xClose(sqlite3_vtab_cursor* cursor) {
|
||||
delete Cursor::Upcast(cursor);
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
// This method uses a fresh cursor to start a new scan of a virtual table.
|
||||
// The args and idxNum are provided by xBestIndex (idxStr is unused).
|
||||
// idxNum is a bitmap that provides the proper indices of the received args.
|
||||
static int xFilter(sqlite3_vtab_cursor* _cursor, int idxNum, const char* idxStr, int argc, sqlite3_value** argv) {
|
||||
Cursor* cursor = Cursor::Upcast(_cursor);
|
||||
VTab* vtab = cursor->GetVTab();
|
||||
CustomTable* self = vtab->parent;
|
||||
Addon* addon = self->addon;
|
||||
v8::Isolate* isolate = self->isolate;
|
||||
v8::HandleScope scope(isolate);
|
||||
UseContext;
|
||||
|
||||
// Convert the SQLite arguments into JavaScript arguments. Note that
|
||||
// the values in argv may be in the wrong order, so we fix that here.
|
||||
v8::Local<v8::Value> args_fast[4];
|
||||
v8::Local<v8::Value>* args = NULL;
|
||||
int parameter_count = vtab->parameter_count;
|
||||
if (parameter_count != 0) {
|
||||
args = parameter_count <= 4 ? args_fast : ALLOC_ARRAY<v8::Local<v8::Value>>(parameter_count);
|
||||
int argn = 0;
|
||||
bool safe_ints = vtab->safe_ints;
|
||||
for (int i = 0; i < parameter_count; ++i) {
|
||||
if (idxNum & 1 << i) {
|
||||
args[i] = Data::GetValueJS(isolate, argv[argn++], safe_ints);
|
||||
// If any arguments are NULL, the result set is necessarily
|
||||
// empty, so don't bother to run the generator function.
|
||||
if (args[i]->IsNull()) {
|
||||
if (args != args_fast) delete[] args;
|
||||
cursor->done = true;
|
||||
return SQLITE_OK;
|
||||
}
|
||||
} else {
|
||||
args[i] = v8::Undefined(isolate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Invoke the generator function to create a new iterator.
|
||||
v8::MaybeLocal<v8::Value> maybeIterator = vtab->generator.Get(isolate)->Call(ctx, v8::Undefined(isolate), parameter_count, args);
|
||||
if (args != args_fast) delete[] args;
|
||||
|
||||
if (maybeIterator.IsEmpty()) {
|
||||
self->PropagateJSError();
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
|
||||
// Store the iterator and its next() method; we'll be using it a lot.
|
||||
v8::Local<v8::Object> iterator = maybeIterator.ToLocalChecked().As<v8::Object>();
|
||||
v8::Local<v8::Function> next = iterator->Get(ctx, addon->cs.next.Get(isolate)).ToLocalChecked().As<v8::Function>();
|
||||
cursor->iterator.Reset(isolate, iterator);
|
||||
cursor->next.Reset(isolate, next);
|
||||
cursor->rowid = 0;
|
||||
|
||||
// Advance the iterator/cursor to the first row.
|
||||
return xNext(cursor->Downcast());
|
||||
}
|
||||
|
||||
// This method advances a virtual table's cursor to the next row.
|
||||
// SQLite will call this method repeatedly, driving the generator function.
|
||||
static int xNext(sqlite3_vtab_cursor* _cursor) {
|
||||
Cursor* cursor = Cursor::Upcast(_cursor);
|
||||
CustomTable* self = cursor->GetVTab()->parent;
|
||||
Addon* addon = self->addon;
|
||||
v8::Isolate* isolate = self->isolate;
|
||||
v8::HandleScope scope(isolate);
|
||||
UseContext;
|
||||
|
||||
v8::Local<v8::Object> iterator = cursor->iterator.Get(isolate);
|
||||
v8::Local<v8::Function> next = cursor->next.Get(isolate);
|
||||
|
||||
v8::MaybeLocal<v8::Value> maybeRecord = next->Call(ctx, iterator, 0, NULL);
|
||||
if (maybeRecord.IsEmpty()) {
|
||||
self->PropagateJSError();
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
|
||||
v8::Local<v8::Object> record = maybeRecord.ToLocalChecked().As<v8::Object>();
|
||||
bool done = record->Get(ctx, addon->cs.done.Get(isolate)).ToLocalChecked().As<v8::Boolean>()->Value();
|
||||
if (!done) {
|
||||
cursor->row.Reset(isolate, record->Get(ctx, addon->cs.value.Get(isolate)).ToLocalChecked().As<v8::Array>());
|
||||
}
|
||||
cursor->done = done;
|
||||
cursor->rowid += 1;
|
||||
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
// If this method returns 1, SQLite will stop scanning the virtual table.
|
||||
static int xEof(sqlite3_vtab_cursor* cursor) {
|
||||
return Cursor::Upcast(cursor)->done;
|
||||
}
|
||||
|
||||
// This method extracts some column from the cursor's current row.
|
||||
static int xColumn(sqlite3_vtab_cursor* _cursor, sqlite3_context* invocation, int column) {
|
||||
Cursor* cursor = Cursor::Upcast(_cursor);
|
||||
CustomTable* self = cursor->GetVTab()->parent;
|
||||
TempDataConverter temp_data_converter(self);
|
||||
v8::Isolate* isolate = self->isolate;
|
||||
v8::HandleScope scope(isolate);
|
||||
|
||||
v8::Local<v8::Array> row = cursor->row.Get(isolate);
|
||||
v8::MaybeLocal<v8::Value> maybeColumnValue = row->Get(OnlyContext, column);
|
||||
if (maybeColumnValue.IsEmpty()) {
|
||||
temp_data_converter.PropagateJSError(NULL);
|
||||
} else {
|
||||
Data::ResultValueFromJS(isolate, invocation, maybeColumnValue.ToLocalChecked(), &temp_data_converter);
|
||||
}
|
||||
return temp_data_converter.status;
|
||||
}
|
||||
|
||||
// This method outputs the rowid of the cursor's current row.
|
||||
static int xRowid(sqlite3_vtab_cursor* cursor, sqlite_int64* output) {
|
||||
*output = Cursor::Upcast(cursor)->rowid;
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
// This method tells SQLite how to *plan* queries on our virtual table.
|
||||
// It gets invoked (typically multiple times) during db.prepare().
|
||||
static int xBestIndex(sqlite3_vtab* vtab, sqlite3_index_info* output) {
|
||||
int parameter_count = VTab::Upcast(vtab)->parameter_count;
|
||||
int argument_count = 0;
|
||||
std::vector<std::pair<int, int>> forwarded;
|
||||
|
||||
for (int i = 0, len = output->nConstraint; i < len; ++i) {
|
||||
auto item = output->aConstraint[i];
|
||||
|
||||
// The SQLITE_INDEX_CONSTRAINT_LIMIT and SQLITE_INDEX_CONSTRAINT_OFFSET
|
||||
// operators have no left-hand operand, and so for those operators the
|
||||
// corresponding item.iColumn is meaningless.
|
||||
// We don't care those constraints.
|
||||
if (item.op == SQLITE_INDEX_CONSTRAINT_LIMIT || item.op == SQLITE_INDEX_CONSTRAINT_OFFSET) {
|
||||
continue;
|
||||
}
|
||||
// We only care about constraints on parameters, not regular columns.
|
||||
if (item.iColumn >= 0 && item.iColumn < parameter_count) {
|
||||
if (item.op != SQLITE_INDEX_CONSTRAINT_EQ) {
|
||||
sqlite3_free(vtab->zErrMsg);
|
||||
vtab->zErrMsg = sqlite3_mprintf(
|
||||
"virtual table parameter \"%s\" can only be constrained by the '=' operator",
|
||||
VTab::Upcast(vtab)->parameter_names.at(item.iColumn).c_str());
|
||||
return SQLITE_ERROR;
|
||||
}
|
||||
if (!item.usable) {
|
||||
// Don't allow SQLite to make plans that ignore arguments.
|
||||
// Otherwise, a user could pass arguments, but then they
|
||||
// could appear undefined in the generator function.
|
||||
return SQLITE_CONSTRAINT;
|
||||
}
|
||||
forwarded.emplace_back(item.iColumn, i);
|
||||
}
|
||||
}
|
||||
|
||||
// Tell SQLite to forward arguments to xFilter.
|
||||
std::sort(forwarded.begin(), forwarded.end());
|
||||
for (std::pair<int, int> pair : forwarded) {
|
||||
int bit = 1 << pair.first;
|
||||
if (!(output->idxNum & bit)) {
|
||||
output->idxNum |= bit;
|
||||
output->aConstraintUsage[pair.second].argvIndex = ++argument_count;
|
||||
output->aConstraintUsage[pair.second].omit = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Use a very high estimated cost so SQLite is not tempted to invoke the
|
||||
// generator function within a loop, if it can be avoided.
|
||||
output->estimatedCost = output->estimatedRows = 1000000000 / (argument_count + 1);
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
void PropagateJSError() {
|
||||
assert(db->GetState()->was_js_error == false);
|
||||
db->GetState()->was_js_error = true;
|
||||
}
|
||||
|
||||
Addon* const addon;
|
||||
v8::Isolate* const isolate;
|
||||
Database* const db;
|
||||
const std::string name;
|
||||
const v8::Global<v8::Function> factory;
|
||||
};
|
||||
|
||||
sqlite3_module CustomTable::MODULE = {
|
||||
0, /* iVersion */
|
||||
xCreate, /* xCreate */
|
||||
xConnect, /* xConnect */
|
||||
xBestIndex, /* xBestIndex */
|
||||
xDisconnect, /* xDisconnect */
|
||||
xDisconnect, /* xDestroy */
|
||||
xOpen, /* xOpen */
|
||||
xClose, /* xClose */
|
||||
xFilter, /* xFilter */
|
||||
xNext, /* xNext */
|
||||
xEof, /* xEof */
|
||||
xColumn, /* xColumn */
|
||||
xRowid, /* xRowid */
|
||||
NULL, /* xUpdate */
|
||||
NULL, /* xBegin */
|
||||
NULL, /* xSync */
|
||||
NULL, /* xCommit */
|
||||
NULL, /* xRollback */
|
||||
NULL, /* xFindMethod */
|
||||
NULL, /* xRename */
|
||||
NULL, /* xSavepoint */
|
||||
NULL, /* xRelease */
|
||||
NULL, /* xRollbackTo */
|
||||
NULL, /* xShadowName */
|
||||
NULL /* xIntegrity */
|
||||
};
|
||||
|
||||
sqlite3_module CustomTable::EPONYMOUS_MODULE = {
|
||||
0, /* iVersion */
|
||||
NULL, /* xCreate */
|
||||
xConnect, /* xConnect */
|
||||
xBestIndex, /* xBestIndex */
|
||||
xDisconnect, /* xDisconnect */
|
||||
xDisconnect, /* xDestroy */
|
||||
xOpen, /* xOpen */
|
||||
xClose, /* xClose */
|
||||
xFilter, /* xFilter */
|
||||
xNext, /* xNext */
|
||||
xEof, /* xEof */
|
||||
xColumn, /* xColumn */
|
||||
xRowid, /* xRowid */
|
||||
NULL, /* xUpdate */
|
||||
NULL, /* xBegin */
|
||||
NULL, /* xSync */
|
||||
NULL, /* xCommit */
|
||||
NULL, /* xRollback */
|
||||
NULL, /* xFindMethod */
|
||||
NULL, /* xRename */
|
||||
NULL, /* xSavepoint */
|
||||
NULL, /* xRelease */
|
||||
NULL, /* xRollbackTo */
|
||||
NULL, /* xShadowName */
|
||||
NULL /* xIntegrity */
|
||||
};
|
||||
17
node_modules/better-sqlite3/src/util/data-converter.cpp
generated
vendored
Normal file
17
node_modules/better-sqlite3/src/util/data-converter.cpp
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
class DataConverter {
|
||||
public:
|
||||
|
||||
void ThrowDataConversionError(sqlite3_context* invocation, bool isBigInt) {
|
||||
if (isBigInt) {
|
||||
ThrowRangeError((GetDataErrorPrefix() + " a bigint that was too big").c_str());
|
||||
} else {
|
||||
ThrowTypeError((GetDataErrorPrefix() + " an invalid value").c_str());
|
||||
}
|
||||
PropagateJSError(invocation);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
virtual void PropagateJSError(sqlite3_context* invocation) = 0;
|
||||
virtual std::string GetDataErrorPrefix() = 0;
|
||||
};
|
||||
194
node_modules/better-sqlite3/src/util/data.cpp
generated
vendored
Normal file
194
node_modules/better-sqlite3/src/util/data.cpp
generated
vendored
Normal file
@@ -0,0 +1,194 @@
|
||||
#define JS_VALUE_TO_SQLITE(to, value, isolate, ...) \
|
||||
if (value->IsNumber()) { \
|
||||
return sqlite3_##to##_double( \
|
||||
__VA_ARGS__, \
|
||||
value.As<v8::Number>()->Value() \
|
||||
); \
|
||||
} else if (value->IsBigInt()) { \
|
||||
bool lossless; \
|
||||
int64_t v = value.As<v8::BigInt>()->Int64Value(&lossless); \
|
||||
if (lossless) { \
|
||||
return sqlite3_##to##_int64(__VA_ARGS__, v); \
|
||||
} \
|
||||
} else if (value->IsString()) { \
|
||||
v8::String::Utf8Value utf8(isolate, value.As<v8::String>()); \
|
||||
return sqlite3_##to##_text( \
|
||||
__VA_ARGS__, \
|
||||
*utf8, \
|
||||
utf8.length(), \
|
||||
SQLITE_TRANSIENT \
|
||||
); \
|
||||
} else if (node::Buffer::HasInstance(value)) { \
|
||||
const char* data = node::Buffer::Data(value); \
|
||||
return sqlite3_##to##_blob( \
|
||||
__VA_ARGS__, \
|
||||
data ? data : "", \
|
||||
node::Buffer::Length(value), \
|
||||
SQLITE_TRANSIENT \
|
||||
); \
|
||||
} else if (value->IsNull() || value->IsUndefined()) { \
|
||||
return sqlite3_##to##_null(__VA_ARGS__); \
|
||||
}
|
||||
|
||||
#define SQLITE_VALUE_TO_JS(from, isolate, safe_ints, ...) \
|
||||
switch (sqlite3_##from##_type(__VA_ARGS__)) { \
|
||||
case SQLITE_INTEGER: \
|
||||
if (safe_ints) { \
|
||||
return v8::BigInt::New( \
|
||||
isolate, \
|
||||
sqlite3_##from##_int64(__VA_ARGS__) \
|
||||
); \
|
||||
} \
|
||||
case SQLITE_FLOAT: \
|
||||
return v8::Number::New( \
|
||||
isolate, \
|
||||
sqlite3_##from##_double(__VA_ARGS__) \
|
||||
); \
|
||||
case SQLITE_TEXT: \
|
||||
return StringFromUtf8( \
|
||||
isolate, \
|
||||
reinterpret_cast<const char*>(sqlite3_##from##_text(__VA_ARGS__)), \
|
||||
sqlite3_##from##_bytes(__VA_ARGS__) \
|
||||
); \
|
||||
case SQLITE_BLOB: \
|
||||
return node::Buffer::Copy( \
|
||||
isolate, \
|
||||
static_cast<const char*>(sqlite3_##from##_blob(__VA_ARGS__)), \
|
||||
sqlite3_##from##_bytes(__VA_ARGS__) \
|
||||
).ToLocalChecked(); \
|
||||
default: \
|
||||
assert(sqlite3_##from##_type(__VA_ARGS__) == SQLITE_NULL); \
|
||||
return v8::Null(isolate); \
|
||||
} \
|
||||
assert(false);
|
||||
|
||||
namespace Data {
|
||||
|
||||
static const char FLAT = 0;
|
||||
static const char PLUCK = 1;
|
||||
static const char EXPAND = 2;
|
||||
static const char RAW = 3;
|
||||
|
||||
v8::Local<v8::Value> GetValueJS(v8::Isolate* isolate, sqlite3_stmt* handle, int column, bool safe_ints) {
|
||||
SQLITE_VALUE_TO_JS(column, isolate, safe_ints, handle, column);
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> GetValueJS(v8::Isolate* isolate, sqlite3_value* value, bool safe_ints) {
|
||||
SQLITE_VALUE_TO_JS(value, isolate, safe_ints, value);
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> GetExpandedRowJS(v8::Isolate* isolate, v8::Local<v8::Context> ctx, sqlite3_stmt* handle, bool safe_ints) {
|
||||
v8::Local<v8::Object> row = v8::Object::New(isolate);
|
||||
int column_count = sqlite3_column_count(handle);
|
||||
for (int i = 0; i < column_count; ++i) {
|
||||
const char* table_raw = sqlite3_column_table_name(handle, i);
|
||||
v8::Local<v8::String> table = InternalizedFromUtf8(isolate, table_raw == NULL ? "$" : table_raw, -1);
|
||||
v8::Local<v8::String> column = InternalizedFromUtf8(isolate, sqlite3_column_name(handle, i), -1);
|
||||
v8::Local<v8::Value> value = Data::GetValueJS(isolate, handle, i, safe_ints);
|
||||
if (row->HasOwnProperty(ctx, table).FromJust()) {
|
||||
row->Get(ctx, table).ToLocalChecked().As<v8::Object>()->Set(ctx, column, value).FromJust();
|
||||
} else {
|
||||
v8::Local<v8::Object> nested = v8::Object::New(isolate);
|
||||
row->Set(ctx, table, nested).FromJust();
|
||||
nested->Set(ctx, column, value).FromJust();
|
||||
}
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
#if !defined(NODE_MODULE_VERSION) || NODE_MODULE_VERSION < 127
|
||||
|
||||
v8::Local<v8::Value> GetFlatRowJS(v8::Isolate* isolate, v8::Local<v8::Context> ctx, sqlite3_stmt* handle, bool safe_ints) {
|
||||
v8::Local<v8::Object> row = v8::Object::New(isolate);
|
||||
int column_count = sqlite3_column_count(handle);
|
||||
for (int i = 0; i < column_count; ++i) {
|
||||
row->Set(ctx,
|
||||
InternalizedFromUtf8(isolate, sqlite3_column_name(handle, i), -1),
|
||||
Data::GetValueJS(isolate, handle, i, safe_ints)
|
||||
).FromJust();
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> GetRawRowJS(v8::Isolate* isolate, v8::Local<v8::Context> ctx, sqlite3_stmt* handle, bool safe_ints) {
|
||||
v8::Local<v8::Array> row = v8::Array::New(isolate);
|
||||
int column_count = sqlite3_column_count(handle);
|
||||
for (int i = 0; i < column_count; ++i) {
|
||||
row->Set(ctx, i, Data::GetValueJS(isolate, handle, i, safe_ints)).FromJust();
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> GetRowJS(v8::Isolate* isolate, v8::Local<v8::Context> ctx, sqlite3_stmt* handle, bool safe_ints, char mode) {
|
||||
if (mode == FLAT) return GetFlatRowJS(isolate, ctx, handle, safe_ints);
|
||||
if (mode == PLUCK) return GetValueJS(isolate, handle, 0, safe_ints);
|
||||
if (mode == EXPAND) return GetExpandedRowJS(isolate, ctx, handle, safe_ints);
|
||||
if (mode == RAW) return GetRawRowJS(isolate, ctx, handle, safe_ints);
|
||||
assert(false);
|
||||
return v8::Local<v8::Value>();
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
v8::Local<v8::Value> GetFlatRowJS(v8::Isolate* isolate, sqlite3_stmt* handle, bool safe_ints) {
|
||||
int column_count = sqlite3_column_count(handle);
|
||||
v8::LocalVector<v8::Name> keys(isolate);
|
||||
v8::LocalVector<v8::Value> values(isolate);
|
||||
keys.reserve(column_count);
|
||||
values.reserve(column_count);
|
||||
for (int i = 0; i < column_count; ++i) {
|
||||
keys.emplace_back(
|
||||
InternalizedFromUtf8(isolate, sqlite3_column_name(handle, i), -1).As<v8::Name>()
|
||||
);
|
||||
values.emplace_back(
|
||||
Data::GetValueJS(isolate, handle, i, safe_ints)
|
||||
);
|
||||
}
|
||||
return v8::Object::New(
|
||||
isolate,
|
||||
GET_PROTOTYPE(v8::Object::New(isolate)),
|
||||
keys.data(),
|
||||
values.data(),
|
||||
column_count
|
||||
);
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> GetRawRowJS(v8::Isolate* isolate, sqlite3_stmt* handle, bool safe_ints) {
|
||||
int column_count = sqlite3_column_count(handle);
|
||||
v8::LocalVector<v8::Value> row(isolate);
|
||||
row.reserve(column_count);
|
||||
for (int i = 0; i < column_count; ++i) {
|
||||
row.emplace_back(Data::GetValueJS(isolate, handle, i, safe_ints));
|
||||
}
|
||||
return v8::Array::New(isolate, row.data(), row.size());
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> GetRowJS(v8::Isolate* isolate, v8::Local<v8::Context> ctx, sqlite3_stmt* handle, bool safe_ints, char mode) {
|
||||
if (mode == FLAT) return GetFlatRowJS(isolate, handle, safe_ints);
|
||||
if (mode == PLUCK) return GetValueJS(isolate, handle, 0, safe_ints);
|
||||
if (mode == EXPAND) return GetExpandedRowJS(isolate, ctx, handle, safe_ints);
|
||||
if (mode == RAW) return GetRawRowJS(isolate, handle, safe_ints);
|
||||
assert(false);
|
||||
return v8::Local<v8::Value>();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void GetArgumentsJS(v8::Isolate* isolate, v8::Local<v8::Value>* out, sqlite3_value** values, int argument_count, bool safe_ints) {
|
||||
assert(argument_count > 0);
|
||||
for (int i = 0; i < argument_count; ++i) {
|
||||
out[i] = Data::GetValueJS(isolate, values[i], safe_ints);
|
||||
}
|
||||
}
|
||||
|
||||
int BindValueFromJS(v8::Isolate* isolate, sqlite3_stmt* handle, int index, v8::Local<v8::Value> value) {
|
||||
JS_VALUE_TO_SQLITE(bind, value, isolate, handle, index);
|
||||
return value->IsBigInt() ? SQLITE_TOOBIG : -1;
|
||||
}
|
||||
|
||||
void ResultValueFromJS(v8::Isolate* isolate, sqlite3_context* invocation, v8::Local<v8::Value> value, DataConverter* converter) {
|
||||
JS_VALUE_TO_SQLITE(result, value, isolate, invocation);
|
||||
converter->ThrowDataConversionError(invocation, value->IsBigInt());
|
||||
}
|
||||
|
||||
}
|
||||
109
node_modules/better-sqlite3/src/util/helpers.cpp
generated
vendored
Normal file
109
node_modules/better-sqlite3/src/util/helpers.cpp
generated
vendored
Normal file
@@ -0,0 +1,109 @@
|
||||
inline v8::Local<v8::String> StringFromUtf8(v8::Isolate* isolate, const char* data, int length) {
|
||||
return v8::String::NewFromUtf8(isolate, data, v8::NewStringType::kNormal, length).ToLocalChecked();
|
||||
}
|
||||
|
||||
inline v8::Local<v8::String> InternalizedFromUtf8(v8::Isolate* isolate, const char* data, int length) {
|
||||
return v8::String::NewFromUtf8(isolate, data, v8::NewStringType::kInternalized, length).ToLocalChecked();
|
||||
}
|
||||
|
||||
inline v8::Local<v8::Value> InternalizedFromUtf8OrNull(v8::Isolate* isolate, const char* data, int length) {
|
||||
if (data == NULL) return v8::Null(isolate);
|
||||
return InternalizedFromUtf8(isolate, data, length);
|
||||
}
|
||||
|
||||
inline v8::Local<v8::String> InternalizedFromLatin1(v8::Isolate* isolate, const char* str) {
|
||||
return v8::String::NewFromOneByte(isolate, reinterpret_cast<const uint8_t*>(str), v8::NewStringType::kInternalized).ToLocalChecked();
|
||||
}
|
||||
|
||||
inline void SetFrozen(v8::Isolate* isolate, v8::Local<v8::Context> ctx, v8::Local<v8::Object> obj, v8::Global<v8::String>& key, v8::Local<v8::Value> value) {
|
||||
obj->DefineOwnProperty(ctx, key.Get(isolate), value, static_cast<v8::PropertyAttribute>(v8::DontDelete | v8::ReadOnly)).FromJust();
|
||||
}
|
||||
|
||||
void ThrowError(const char* message) { EasyIsolate; isolate->ThrowException(v8::Exception::Error(StringFromUtf8(isolate, message, -1))); }
|
||||
void ThrowTypeError(const char* message) { EasyIsolate; isolate->ThrowException(v8::Exception::TypeError(StringFromUtf8(isolate, message, -1))); }
|
||||
void ThrowRangeError(const char* message) { EasyIsolate; isolate->ThrowException(v8::Exception::RangeError(StringFromUtf8(isolate, message, -1))); }
|
||||
|
||||
// Determines whether to skip the given character at the start of an SQL string.
|
||||
inline bool IS_SKIPPED(char c) {
|
||||
return c == ' ' || c == ';' || (c >= '\t' && c <= '\r');
|
||||
}
|
||||
|
||||
// Allocates an empty array, without calling constructors/initializers.
|
||||
template<class T> inline T* ALLOC_ARRAY(size_t count) {
|
||||
return static_cast<T*>(::operator new[](count * sizeof(T)));
|
||||
}
|
||||
|
||||
// Deallocates an array, without calling destructors.
|
||||
template<class T> inline void FREE_ARRAY(T* array_pointer) {
|
||||
::operator delete[](array_pointer);
|
||||
}
|
||||
|
||||
v8::Local<v8::FunctionTemplate> NewConstructorTemplate(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::External> data,
|
||||
v8::FunctionCallback func,
|
||||
const char* name
|
||||
) {
|
||||
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate, func, data);
|
||||
t->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
t->SetClassName(InternalizedFromLatin1(isolate, name));
|
||||
return t;
|
||||
}
|
||||
|
||||
void SetPrototypeMethod(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::External> data,
|
||||
v8::Local<v8::FunctionTemplate> recv,
|
||||
const char* name,
|
||||
v8::FunctionCallback func
|
||||
) {
|
||||
v8::HandleScope scope(isolate);
|
||||
recv->PrototypeTemplate()->Set(
|
||||
InternalizedFromLatin1(isolate, name),
|
||||
v8::FunctionTemplate::New(isolate, func, data, v8::Signature::New(isolate, recv))
|
||||
);
|
||||
}
|
||||
|
||||
void SetPrototypeSymbolMethod(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::External> data,
|
||||
v8::Local<v8::FunctionTemplate> recv,
|
||||
v8::Local<v8::Symbol> symbol,
|
||||
v8::FunctionCallback func
|
||||
) {
|
||||
v8::HandleScope scope(isolate);
|
||||
recv->PrototypeTemplate()->Set(
|
||||
symbol,
|
||||
v8::FunctionTemplate::New(isolate, func, data, v8::Signature::New(isolate, recv))
|
||||
);
|
||||
}
|
||||
|
||||
void SetPrototypeGetter(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::External> data,
|
||||
v8::Local<v8::FunctionTemplate> recv,
|
||||
const char* name,
|
||||
v8::AccessorNameGetterCallback func
|
||||
) {
|
||||
v8::HandleScope scope(isolate);
|
||||
recv->InstanceTemplate()->SetNativeDataProperty(
|
||||
InternalizedFromLatin1(isolate, name),
|
||||
func,
|
||||
0,
|
||||
data
|
||||
);
|
||||
}
|
||||
|
||||
#if defined(V8_ENABLE_SANDBOX)
|
||||
// When V8 Sandbox is enabled (in newer Electron versions), we need to use Buffer::Copy
|
||||
// instead of Buffer::New to ensure the ArrayBuffer backing store is allocated inside the sandbox
|
||||
static inline v8::MaybeLocal<v8::Object> BufferSandboxNew(v8::Isolate* isolate, char* data, size_t length, void (*finalizeCallback)(char*, void*), void* finalizeHint) {
|
||||
v8::MaybeLocal<v8::Object> buffer = node::Buffer::Copy(isolate, data, length);
|
||||
finalizeCallback(data, finalizeHint);
|
||||
return buffer;
|
||||
}
|
||||
#define SAFE_NEW_BUFFER(env, data, length, finalizeCallback, finalizeHint) BufferSandboxNew(env, data, length, finalizeCallback, finalizeHint)
|
||||
#else
|
||||
// When V8 Sandbox is not enabled, we can use the more efficient Buffer::New
|
||||
#define SAFE_NEW_BUFFER(env, data, length, finalizeCallback, finalizeHint) node::Buffer::New(env, data, length, finalizeCallback, finalizeHint)
|
||||
#endif
|
||||
70
node_modules/better-sqlite3/src/util/macros.cpp
generated
vendored
Normal file
70
node_modules/better-sqlite3/src/util/macros.cpp
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
#define NODE_ARGUMENTS const v8::FunctionCallbackInfo<v8::Value>&
|
||||
#define NODE_ARGUMENTS_POINTER const v8::FunctionCallbackInfo<v8::Value>*
|
||||
#define NODE_METHOD(name) void name(NODE_ARGUMENTS info)
|
||||
#define NODE_GETTER(name) void name(v8::Local<v8::Name> _, const v8::PropertyCallbackInfo<v8::Value>& info)
|
||||
#define INIT(name) v8::Local<v8::Function> name(v8::Isolate* isolate, v8::Local<v8::External> data)
|
||||
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >= 13
|
||||
// v8::Object::GetPrototype has been deprecated. See http://crbug.com/333672197
|
||||
#define GET_PROTOTYPE(obj) ((obj)->GetPrototypeV2())
|
||||
#else
|
||||
#define GET_PROTOTYPE(obj) ((obj)->GetPrototype())
|
||||
#endif
|
||||
|
||||
#define EasyIsolate v8::Isolate* isolate = v8::Isolate::GetCurrent()
|
||||
#define OnlyIsolate info.GetIsolate()
|
||||
#define OnlyContext isolate->GetCurrentContext()
|
||||
#define OnlyAddon static_cast<Addon*>(info.Data().As<v8::External>()->Value())
|
||||
#define UseIsolate v8::Isolate* isolate = OnlyIsolate
|
||||
#define UseContext v8::Local<v8::Context> ctx = OnlyContext
|
||||
#define UseAddon Addon* addon = OnlyAddon
|
||||
#define Unwrap node::ObjectWrap::Unwrap
|
||||
|
||||
#define REQUIRE_ARGUMENT_ANY(at, var) \
|
||||
if (info.Length() <= (at())) \
|
||||
return ThrowTypeError("Expected a "#at" argument"); \
|
||||
var = info[at()]
|
||||
|
||||
#define _REQUIRE_ARGUMENT(at, var, Type, message, ...) \
|
||||
if (info.Length() <= (at()) || !info[at()]->Is##Type()) \
|
||||
return ThrowTypeError("Expected "#at" argument to be "#message); \
|
||||
var = (info[at()].As<v8::Type>())__VA_ARGS__
|
||||
|
||||
#define REQUIRE_ARGUMENT_INT32(at, var) \
|
||||
_REQUIRE_ARGUMENT(at, var, Int32, a 32-bit signed integer, ->Value())
|
||||
#define REQUIRE_ARGUMENT_BOOLEAN(at, var) \
|
||||
_REQUIRE_ARGUMENT(at, var, Boolean, a boolean, ->Value())
|
||||
#define REQUIRE_ARGUMENT_STRING(at, var) \
|
||||
_REQUIRE_ARGUMENT(at, var, String, a string)
|
||||
#define REQUIRE_ARGUMENT_OBJECT(at, var) \
|
||||
_REQUIRE_ARGUMENT(at, var, Object, an object)
|
||||
#define REQUIRE_ARGUMENT_FUNCTION(at, var) \
|
||||
_REQUIRE_ARGUMENT(at, var, Function, a function)
|
||||
|
||||
#define REQUIRE_DATABASE_OPEN(db) \
|
||||
if (!db->open) \
|
||||
return ThrowTypeError("The database connection is not open")
|
||||
#define REQUIRE_DATABASE_NOT_BUSY(db) \
|
||||
if (db->busy) \
|
||||
return ThrowTypeError("This database connection is busy executing a query")
|
||||
#define REQUIRE_DATABASE_NO_ITERATORS(db) \
|
||||
if (db->iterators) \
|
||||
return ThrowTypeError("This database connection is busy executing a query")
|
||||
#define REQUIRE_DATABASE_NO_ITERATORS_UNLESS_UNSAFE(db) \
|
||||
if (!db->unsafe_mode) { \
|
||||
REQUIRE_DATABASE_NO_ITERATORS(db); \
|
||||
} ((void)0)
|
||||
#define REQUIRE_STATEMENT_NOT_LOCKED(stmt) \
|
||||
if (stmt->locked) \
|
||||
return ThrowTypeError("This statement is busy executing a query")
|
||||
|
||||
#define first() 0
|
||||
#define second() 1
|
||||
#define third() 2
|
||||
#define fourth() 3
|
||||
#define fifth() 4
|
||||
#define sixth() 5
|
||||
#define seventh() 6
|
||||
#define eighth() 7
|
||||
#define ninth() 8
|
||||
#define tenth() 9
|
||||
71
node_modules/better-sqlite3/src/util/query-macros.cpp
generated
vendored
Normal file
71
node_modules/better-sqlite3/src/util/query-macros.cpp
generated
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
#define STATEMENT_BIND(handle) \
|
||||
Binder binder(handle); \
|
||||
if (!binder.Bind(info, info.Length(), stmt)) { \
|
||||
sqlite3_clear_bindings(handle); \
|
||||
return; \
|
||||
} ((void)0)
|
||||
|
||||
#define STATEMENT_THROW_LOGIC() \
|
||||
db->ThrowDatabaseError(); \
|
||||
if (!bound) { sqlite3_clear_bindings(handle); } \
|
||||
return
|
||||
|
||||
#define STATEMENT_RETURN_LOGIC(return_value) \
|
||||
info.GetReturnValue().Set(return_value); \
|
||||
if (!bound) { sqlite3_clear_bindings(handle); } \
|
||||
return
|
||||
|
||||
#define STATEMENT_START_LOGIC(RETURNS_DATA_CHECK, MUTATE_CHECK) \
|
||||
Statement* stmt = Unwrap<Statement>(info.This()); \
|
||||
RETURNS_DATA_CHECK(); \
|
||||
sqlite3_stmt* handle = stmt->handle; \
|
||||
Database* db = stmt->db; \
|
||||
REQUIRE_DATABASE_OPEN(db->GetState()); \
|
||||
REQUIRE_DATABASE_NOT_BUSY(db->GetState()); \
|
||||
MUTATE_CHECK(); \
|
||||
const bool bound = stmt->bound; \
|
||||
if (!bound) { \
|
||||
STATEMENT_BIND(handle); \
|
||||
} else if (info.Length() > 0) { \
|
||||
return ThrowTypeError("This statement already has bound parameters"); \
|
||||
} ((void)0)
|
||||
|
||||
|
||||
#define STATEMENT_THROW() db->GetState()->busy = false; STATEMENT_THROW_LOGIC()
|
||||
#define STATEMENT_RETURN(x) db->GetState()->busy = false; STATEMENT_RETURN_LOGIC(x)
|
||||
#define STATEMENT_START(x, y) \
|
||||
STATEMENT_START_LOGIC(x, y); \
|
||||
db->GetState()->busy = true; \
|
||||
UseIsolate; \
|
||||
if (db->Log(isolate, handle)) { \
|
||||
STATEMENT_THROW(); \
|
||||
} ((void)0)
|
||||
|
||||
|
||||
#define DOES_NOT_MUTATE() REQUIRE_STATEMENT_NOT_LOCKED(stmt)
|
||||
#define DOES_MUTATE() \
|
||||
REQUIRE_STATEMENT_NOT_LOCKED(stmt); \
|
||||
REQUIRE_DATABASE_NO_ITERATORS_UNLESS_UNSAFE(db->GetState())
|
||||
#define DOES_ADD_ITERATOR() \
|
||||
DOES_NOT_MUTATE(); \
|
||||
if (db->GetState()->iterators == USHRT_MAX) \
|
||||
return ThrowRangeError("Too many active database iterators")
|
||||
#define REQUIRE_STATEMENT_RETURNS_DATA() \
|
||||
if (!stmt->returns_data) \
|
||||
return ThrowTypeError("This statement does not return data. Use run() instead")
|
||||
#define ALLOW_ANY_STATEMENT() \
|
||||
((void)0)
|
||||
|
||||
|
||||
#define _FUNCTION_START(type) \
|
||||
type* self = static_cast<type*>(sqlite3_user_data(invocation)); \
|
||||
v8::Isolate* isolate = self->isolate; \
|
||||
v8::HandleScope scope(isolate)
|
||||
|
||||
#define FUNCTION_START() \
|
||||
_FUNCTION_START(CustomFunction)
|
||||
|
||||
#define AGGREGATE_START() \
|
||||
_FUNCTION_START(CustomAggregate); \
|
||||
Accumulator* acc = self->GetAccumulator(invocation); \
|
||||
if (acc->value.IsEmpty()) return
|
||||
49
node_modules/better-sqlite3/src/util/row-builder.cpp
generated
vendored
Normal file
49
node_modules/better-sqlite3/src/util/row-builder.cpp
generated
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
class RowBuilder {
|
||||
public:
|
||||
|
||||
explicit RowBuilder(
|
||||
v8::Isolate* isolate,
|
||||
sqlite3_stmt* handle,
|
||||
bool safe_ints
|
||||
) :
|
||||
isolate(isolate),
|
||||
handle(handle),
|
||||
column_count(-1),
|
||||
safe_ints(safe_ints),
|
||||
keys(isolate) {}
|
||||
|
||||
v8::Local<v8::Value> GetRowJS() {
|
||||
if (column_count < 0) {
|
||||
column_count = sqlite3_column_count(handle);
|
||||
keys.reserve(column_count);
|
||||
for (int i = 0; i < column_count; ++i) {
|
||||
keys.emplace_back(
|
||||
InternalizedFromUtf8(isolate, sqlite3_column_name(handle, i), -1)
|
||||
.As<v8::Name>()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
v8::LocalVector<v8::Value> values(isolate);
|
||||
values.reserve(column_count);
|
||||
for (int i = 0; i < column_count; ++i) {
|
||||
values.emplace_back(
|
||||
Data::GetValueJS(isolate, handle, i, safe_ints)
|
||||
);
|
||||
}
|
||||
|
||||
return v8::Object::New(isolate,
|
||||
GET_PROTOTYPE(v8::Object::New(isolate)),
|
||||
keys.data(),
|
||||
values.data(),
|
||||
column_count
|
||||
);
|
||||
}
|
||||
|
||||
private:
|
||||
v8::Isolate* isolate;
|
||||
sqlite3_stmt* handle;
|
||||
int column_count;
|
||||
const bool safe_ints;
|
||||
v8::LocalVector<v8::Name> keys;
|
||||
};
|
||||
Reference in New Issue
Block a user