MCPcopy Create free account
hub / github.com/WiseLibs/better-sqlite3 / NODE_METHOD

Function NODE_METHOD

src/objects/database.cpp:140–196  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

138}
139
140NODE_METHOD(Database::JS_new) {
141 assert(info.IsConstructCall());
142 REQUIRE_ARGUMENT_STRING(first, Napi::String filename);
143 REQUIRE_ARGUMENT_STRING(second, Napi::String filenameGiven);
144 REQUIRE_ARGUMENT_BOOLEAN(third, bool in_memory);
145 REQUIRE_ARGUMENT_BOOLEAN(fourth, bool readonly);
146 REQUIRE_ARGUMENT_BOOLEAN(fifth, bool must_exist);
147 REQUIRE_ARGUMENT_INT32(sixth, int timeout);
148 REQUIRE_ARGUMENT_ANY(seventh, Napi::Value logger);
149 REQUIRE_ARGUMENT_ANY(eighth, Napi::Value buffer);
150
151 UseAddon;
152 UseIsolate;
153 sqlite3* db_handle;
154 std::string utf8 = filename.Utf8Value();
155 int mask = readonly ? SQLITE_OPEN_READONLY
156 : must_exist ? SQLITE_OPEN_READWRITE
157 : (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
158
159 if (sqlite3_open_v2(utf8.c_str(), &db_handle, mask, NULL) != SQLITE_OK) {
160 ThrowSqliteError(env, addon, db_handle);
161 int status = sqlite3_close(db_handle);
162 assert(status == SQLITE_OK); ((void)status);
163 return env.Undefined();
164 }
165
166 assert(sqlite3_db_mutex(db_handle) == NULL);
167 sqlite3_extended_result_codes(db_handle, 1);
168 sqlite3_busy_timeout(db_handle, timeout);
169 sqlite3_limit(db_handle, SQLITE_LIMIT_LENGTH, MAX_BUFFER_SIZE < MAX_STRING_SIZE ? MAX_BUFFER_SIZE : MAX_STRING_SIZE);
170 sqlite3_limit(db_handle, SQLITE_LIMIT_SQL_LENGTH, MAX_STRING_SIZE);
171 int status = sqlite3_db_config(db_handle, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, NULL);
172 assert(status == SQLITE_OK); ((void)status);
173 status = sqlite3_db_config(db_handle, SQLITE_DBCONFIG_DEFENSIVE, 1, NULL);
174 assert(status == SQLITE_OK); ((void)status);
175
176 if (buffer.IsBuffer() && !Deserialize(env, buffer.As<Napi::Object>(), addon, db_handle, readonly)) {
177 int status = sqlite3_close(db_handle);
178 assert(status == SQLITE_OK); ((void)status);
179 return env.Undefined();
180 }
181
182 this->db_handle = db_handle;
183 open = true;
184 has_logger = logger.IsFunction();
185 if (has_logger) this->logger.Reset(logger, 1);
186 addon->dbs.insert(this);
187
188 Napi::Object _this = info.This().As<Napi::Object>();
189 SetFrozen(env, _this, addon->cs.memory, Napi::Boolean::New(env, in_memory));
190 SetFrozen(env, _this, addon->cs.readonly, Napi::Boolean::New(env, readonly));
191 SetFrozen(env, _this, addon->cs.name, filenameGiven);
192 SetInstanceGetter<Database, &Database::JS_open>(_this, "open", addon);
193 SetInstanceGetter<Database, &Database::JS_inTransaction>(_this, "inTransaction", addon);
194
195 return info.This();
196}
197

Callers

nothing calls this directly

Calls 7

SetFrozenFunction · 0.85
SafeConstructFunction · 0.85
IS_SKIPPEDFunction · 0.85
ThrowErrorFunction · 0.85
LogMethod · 0.80
ThrowDatabaseErrorMethod · 0.80
CloseHandlesMethod · 0.45

Tested by

no test coverage detected