Tchatator413
TripEnArvor instant messaging protocol - JSON-based
Loading...
Searching...
No Matches
db.h
Go to the documentation of this file.
1
5
6#ifndef DB_H
7#define DB_H
8
9#include "errstatus.h"
10#include "tchatator413/cfg.h"
11#include "types.h"
12
14typedef void db_t;
15
18typedef struct {
21 serial_t id;
22 user_kind_t kind;
23 char const *email, *last_name, *first_name, *display_name;
24} user_t;
25
28typedef struct {
29 void *memory_owner_db;
30 msg_t *msgs;
31 size_t n_msgs;
33
44db_t *db_connect(cfg_t *cfg, int verbosity, char const *host, char const *port, char const *database, char const *username, char const *password);
45
48void db_destroy(db_t *db);
49
53void db_collect(void *memory_owner);
54
64
72serial_t db_get_user_id_by_email(db_t *db, cfg_t *cfg, char const *email);
73
81serial_t db_get_user_id_by_name(db_t *db, cfg_t *cfg, char const *name);
82
91
100errstatus_t db_get_msg(db_t *db, cfg_t *cfg, msg_t *msg, void **out_memory_owner_db);
101
110errstatus_t db_check_password(db_t *db, cfg_t *cfg, serial_t user_id, char const *password);
111
119int db_get_user_role(db_t *db, cfg_t *cfg, serial_t user_id);
120
128int db_count_msg(db_t *db, cfg_t *cfg, serial_t sender_id, serial_t recipient_id);
129
139serial_t db_send_msg(db_t *db, cfg_t *cfg, serial_t sender_id, serial_t recipient_id, char const *content);
140
149 int32_t limit,
150 int32_t offset,
151 serial_t recipient_id);
152
160
165typedef errstatus_t (*fn_transaction_t)(db_t *db, cfg_t *cfg, void *ctx);
166
174
175#endif // DB_H
Tchatator413 server configuration - Interface.
struct cfg cfg_t
An opaque handle to a configuration object.
Definition cfg.h:14
errstatus_t db_check_password(db_t *db, cfg_t *cfg, serial_t user_id, char const *password)
Check a password against the stored hash for an user.
errstatus_t db_verify_user_api_key(db_t *db, cfg_t *cfg, user_identity_t *out_user, api_key_t api_key)
Verify an API key.
void db_destroy(db_t *db)
Destroy a database connection.
errstatus_t db_transaction(db_t *db, cfg_t *cfg, fn_transaction_t body, void *ctx)
Perform a transaction.
errstatus_t db_rm_msg(db_t *db, cfg_t *cfg, serial_t msg_id)
Removes a message from the database.
serial_t db_send_msg(db_t *db, cfg_t *cfg, serial_t sender_id, serial_t recipient_id, char const *content)
Sends a message.
serial_t db_get_user_id_by_email(db_t *db, cfg_t *cfg, char const *email)
Get the ID of an user from their e-mail.
msg_list_t db_get_inbox(db_t *db, cfg_t *cfg, int32_t limit, int32_t offset, serial_t recipient_id)
Creates an array with the messages an user has recieved, sorted by sent/edited date,...
errstatus_t(* fn_transaction_t)(db_t *db, cfg_t *cfg, void *ctx)
A transaction body function.
Definition db.h:165
void db_collect(void *memory_owner)
Cleans up a memory owner.
db_t * db_connect(cfg_t *cfg, int verbosity, char const *host, char const *port, char const *database, char const *username, char const *password)
Initialize a database connection.
errstatus_t db_get_msg(db_t *db, cfg_t *cfg, msg_t *msg, void **out_memory_owner_db)
Retrieves a message from the database. If msg->id is undefined, the behavior is undefined.
serial_t db_get_user_id_by_name(db_t *db, cfg_t *cfg, char const *name)
Get the ID of an user from their name.
int db_get_user_role(db_t *db, cfg_t *cfg, serial_t user_id)
Get the role of an user.
errstatus_t db_get_user(db_t *db, cfg_t *cfg, user_t *user)
Fills a user record from its ID. If user->id is undefined, the behavior is undefined.
void db_t
An opaque handle to a database connection.
Definition db.h:14
int db_count_msg(db_t *db, cfg_t *cfg, serial_t sender_id, serial_t recipient_id)
Counts the amount of messages sent from sender to a recipient.
Error status enumeration - Interface.
errstatus_t
An error status.
Definition errstatus.h:12
Represents a list of messages.
Definition db.h:28
Represents a message in the chat application.
Definition types.h:61
Information about the identity of an user.
Definition types.h:47
Represents a user in the system.
Definition db.h:18
void * memory_owner_db
Handle to the database memory owner.
Definition db.h:20
Version 4 UUID.
Definition uuid.h:15
General types - Standalone header.
user_kind_t
The kind of an user.
Definition types.h:30
int32_t serial_t
A PosrgreSQL SERIAL primary key value starting at 1. (1..2^31-1)
Definition types.h:22