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

Function NODE_METHOD

src/objects/database.cpp:139–195  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

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