MCPcopy Create free account
hub / github.com/ElementsProject/lightning / sqlite_setup

Function sqlite_setup

plugins/sql.c:248–305  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

246}
247
248static struct sqlite3 *sqlite_setup(struct plugin *plugin)
249{
250 int err;
251 struct sqlite3 *db;
252 char *errmsg;
253 struct sql *sql = sql_of(plugin);
254
255 if (sql->dbfilename) {
256 err = sqlite3_open_v2(sql->dbfilename, &db,
257 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
258 NULL);
259 } else {
260 err = sqlite3_open_v2("", &db,
261 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
262 | SQLITE_OPEN_MEMORY,
263 NULL);
264 }
265 if (err != SQLITE_OK)
266 plugin_err(plugin, "Could not create db: errcode %u", err);
267
268 sqlite3_extended_result_codes(db, 1);
269
270 /* From https://www.sqlite.org/c3ref/set_authorizer.html:
271 *
272 * Applications that need to process SQL from untrusted
273 * sources might also consider lowering resource limits using
274 * sqlite3_limit() and limiting database size using the
275 * max_page_count PRAGMA in addition to using an authorizer.
276 */
277 sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 1000000);
278 sqlite3_limit(db, SQLITE_LIMIT_SQL_LENGTH, 10000);
279 sqlite3_limit(db, SQLITE_LIMIT_COLUMN, 100);
280 sqlite3_limit(db, SQLITE_LIMIT_EXPR_DEPTH, 100);
281 sqlite3_limit(db, SQLITE_LIMIT_COMPOUND_SELECT, 10);
282 sqlite3_limit(db, SQLITE_LIMIT_VDBE_OP, 1000);
283 sqlite3_limit(db, SQLITE_LIMIT_ATTACHED, 1);
284 sqlite3_limit(db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 500);
285 sqlite3_limit(db, SQLITE_LIMIT_VARIABLE_NUMBER, 100);
286 sqlite3_limit(db, SQLITE_LIMIT_TRIGGER_DEPTH, 1);
287 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, 1);
288
289 err = sqlite3_exec(db, "PRAGMA foreign_keys = ON;", NULL, NULL, &errmsg);
290 if (err != SQLITE_OK)
291 plugin_err(plugin, "Could not set foreign_keys: %s", errmsg);
292
293 if (sql->dbfilename) {
294 err = sqlite3_exec(db,
295 "PRAGMA synchronous = OFF;"
296 "PRAGMA journal_mode = OFF;"
297 "PRAGMA temp_store = MEMORY;"
298 , NULL, NULL,
299 &errmsg);
300 if (err != SQLITE_OK)
301 plugin_err(plugin, "Could not disable sync: %s", errmsg);
302 }
303
304 return db;
305}

Callers 1

initFunction · 0.85

Calls 2

sql_ofFunction · 0.85
plugin_errFunction · 0.70

Tested by

no test coverage detected