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

Method xConnect

src/util/custom-table.cpp:105–155  ·  view source on GitHub ↗

This method uses the factory function to instantiate a new virtual table.

Source from the content-addressed store, hash-verified

103
104 // This method uses the factory function to instantiate a new virtual table.
105 static int xConnect(sqlite3* db_handle, void* _self, int argc, const char* const * argv, sqlite3_vtab** output, char** errOutput) {
106 CustomTable* self = static_cast<CustomTable*>(_self);
107 Napi::Env env = self->env;
108 Napi::HandleScope scope(env);
109
110 napi_value* args = ALLOC_ARRAY<napi_value>(argc);
111 for (int i = 0; i < argc; ++i) {
112 args[i] = StringFromUtf8(env, argv[i], -1);
113 }
114
115 // Run the factory function to receive a new virtual table definition.
116 Napi::Value returnValue = SafeCall(env, self->factory.Value(), env.Undefined(), argc, args);
117 delete[] args;
118
119 if (env.IsExceptionPending()) {
120 self->PropagateJSError();
121 return SQLITE_ERROR;
122 }
123
124 // Extract each part of the virtual table definition.
125 Napi::Array array = returnValue.As<Napi::Array>();
126 Napi::String sqlString = array.Get((uint32_t)0).As<Napi::String>();
127 Napi::Function generator = array.Get((uint32_t)1).As<Napi::Function>();
128 Napi::Array parameterNames = array.Get((uint32_t)2).As<Napi::Array>();
129 int safe_ints = array.Get((uint32_t)3).As<Napi::Number>().Int32Value();
130 bool direct_only = array.Get((uint32_t)4).As<Napi::Boolean>().Value();
131
132 std::string sql = sqlString.Utf8Value();
133 safe_ints = safe_ints < 2 ? safe_ints : static_cast<int>(self->db->GetState()->safe_ints);
134
135 // Copy the parameter names into a std::vector.
136 std::vector<std::string> parameter_names;
137 for (int i = 0, len = parameterNames.Length(); i < len; ++i) {
138 Napi::String parameterName = parameterNames.Get((uint32_t)i).As<Napi::String>();
139 parameter_names.emplace_back(parameterName.Utf8Value());
140 }
141
142 // Pass our SQL table definition to SQLite (this should never fail).
143 if (sqlite3_declare_vtab(db_handle, sql.c_str()) != SQLITE_OK) {
144 *errOutput = sqlite3_mprintf("failed to declare virtual table \"%s\"", argv[2]);
145 return SQLITE_ERROR;
146 }
147 if (direct_only && sqlite3_vtab_config(db_handle, SQLITE_VTAB_DIRECTONLY) != SQLITE_OK) {
148 *errOutput = sqlite3_mprintf("failed to configure virtual table \"%s\"", argv[2]);
149 return SQLITE_ERROR;
150 }
151
152 // Return the successfully created virtual table.
153 *output = (new VTab(self, generator, parameter_names, safe_ints))->Downcast();
154 return SQLITE_OK;
155 }
156
157 static int xDisconnect(sqlite3_vtab* vtab) {
158 delete VTab::Upcast(vtab);

Callers

nothing calls this directly

Calls 5

PropagateJSErrorMethod · 0.95
StringFromUtf8Function · 0.85
SafeCallFunction · 0.85
GetStateMethod · 0.80
DowncastMethod · 0.45

Tested by

no test coverage detected