DungeonCrawl
Loading...
Searching...
No Matches
database.c File Reference

Implents functionality for working with the database. More...

#include "database.h"
#include "../logging/logger.h"
#include <stdio.h>
#include <string.h>

Go to the source code of this file.

Functions

int db_open (db_connection_t *db_connection, const char *db_name)
 This function is for the opening of the database.
void db_close (db_connection_t *db_connection)
 This function is for the closing of the database.
int db_is_open (const db_connection_t *db_connection)
 This function is to check if the database is open.
int db_open_multiple_access (db_connection_t *db_connection, db_type_t type)
 This function is for the opening of the database with multiple access.

Detailed Description

Implents functionality for working with the database.

Definition in file database.c.

Function Documentation

◆ db_close()

void db_close ( db_connection_t * db_connection)

This function is for the closing of the database.

Parameters
db_connectionthe database conncetion

Definition at line 22 of file database.c.

22 {
23 sqlite3_close(db_connection->db);
24 db_connection->db = NULL;
25}

◆ db_is_open()

int db_is_open ( const db_connection_t * db_connection)

This function is to check if the database is open.

Parameters
db_connectionthe database connection
Returns
1 if open, otherwise 0

Definition at line 27 of file database.c.

27 {
28 if (db_connection->db == NULL) {
29 return 0;
30 }
31 return 1;
32}

◆ db_open()

int db_open ( db_connection_t * db_connection,
const char * db_name )

This function is for the opening of the database.

Parameters
db_connectionthe database connection
db_namethe path name of the database
Returns
0 for success

Definition at line 12 of file database.c.

12 {
13 int rc = sqlite3_open(db_name, &db_connection->db);
14 if (rc) {
15 log_msg(ERROR, "Database", "Can't open database: %s", sqlite3_errmsg(db_connection->db));
16 return DB_OPEN_STATUS_FAILURE;
17 }
18 return DB_OPEN_STATUS_SUCCESS;
19}
void log_msg(const log_level_t level, const char *module, const char *format,...)
Logs a formatted message with a specified log level and module.
Definition logger.c:246

◆ db_open_multiple_access()

int db_open_multiple_access ( db_connection_t * db_connection,
db_type_t type )

This function is for the opening of the database with multiple access.

Parameters
db_connectionthe database connection
typethe type of the database
Returns
0 for success

Definition at line 34 of file database.c.

34 {
35 const char* potential_paths[3];
36 switch (type) {
37 case DB_GAME:
38 potential_paths[0] = DB_BUILD_DIR_PATH(dungeoncrawl_game.db);
39 potential_paths[1] = DB_RESOURCE_PATH(game, dungeoncrawl_game.db);
40 potential_paths[2] = DB_RESOURCE_PATH_UP(game, dungeoncrawl_game.db);
41 break;
42 case DB_LOCAL:
43 potential_paths[0] = DB_BUILD_DIR_PATH(dungeoncrawl_local.db);
44 potential_paths[1] = DB_RESOURCE_PATH(local, dungeoncrawl_local.db);
45 potential_paths[2] = DB_RESOURCE_PATH_UP(local, dungeoncrawl_local.db);
46 break;
47 default:
48 log_msg(ERROR, "Database", "Invalid database type");
49 return DB_OPEN_STATUS_FAILURE;
50 }
51
52 for (int i = 0; i < sizeof(potential_paths) / sizeof(char*); i++) {
53 const int rc = sqlite3_open_v2(potential_paths[i], &db_connection->db, SQLITE_OPEN_READWRITE, NULL);
54 if (rc == SQLITE_OK) {
55 return DB_OPEN_STATUS_SUCCESS;
56 }
57 log_msg(WARNING, "Database", "Can't open database: %s", sqlite3_errmsg(db_connection->db));
58 }
59 return DB_OPEN_STATUS_FAILURE;
60}