Skip to content

Commit

Permalink
db.d: close database on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiascode committed Jan 3, 2025
1 parent 262e79d commit 4e8af0d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/db.d
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import soulfind.defines : blue, default_max_users, default_port, norm;
import std.conv : to;
import std.exception : ifThrown;
import std.file : exists, isFile;
import std.stdio : writefln;
import std.stdio : writefln, writeln;
import std.string : format, replace, toStringz;

struct SdbUserStats
Expand All @@ -39,6 +39,7 @@ class Sdb

this(string filename)
{
debug(db) writefln!("DB: Using database: %s")(filename);
open_db(filename);

if (!exists(filename) || !isFile(filename))
Expand Down Expand Up @@ -72,12 +73,24 @@ class Sdb
init_config();
}

~this()
{
debug(db) writeln("DB: Shutting down...");
close_db();
}

@trusted
private void open_db(string filename)
{
sqlite3_open(filename.toStringz(), &db);
}

@trusted
private void close_db()
{
sqlite3_close(db);
}

private void init_config()
{
const sql = format!(
Expand Down
1 change: 1 addition & 0 deletions src/setup/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ int run(string[] args)

sdb = new Sdb(db_filename);
try { main_menu(); } catch (StdioException) {}
sdb = null;
return 0;
}

Expand Down

0 comments on commit 4e8af0d

Please sign in to comment.