sqlite works

This commit is contained in:
SimonFJ20 2025-03-05 11:52:02 +01:00
parent d537e0a709
commit 819cf738dc
3 changed files with 32 additions and 32 deletions

View File

@ -3,19 +3,19 @@ CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
email TEXT NOT NULL, email TEXT NOT NULL,
password_hash TEXT NOT NULL, password_hash TEXT NOT NULL,
balanceInDkkCent INTEGER NOT NULL balance_dkk_cent INTEGER NOT NULL
); );
CREATE TABLE IF NOT EXISTS products ( CREATE TABLE IF NOT EXISTS products (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name TEXT NOT NULL, name TEXT NOT NULL,
priceInDkkCent INTEGER NOT NULL price_dkk_cent INTEGER NOT NULL
); );
CREATE TABLE IF NOT EXISTS product_prices ( CREATE TABLE IF NOT EXISTS product_prices (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
product INTEGER NOT NULL, product INTEGER NOT NULL,
priceInDkkCent INTEGER NOT NULL, price_dkk_cent INTEGER NOT NULL,
FOREIGN KEY(product) REFERENCES products(id) FOREIGN KEY(product) REFERENCES products(id)
); );

View File

@ -64,39 +64,36 @@ l0_return:
json_value_free(body); json_value_free(body);
} }
static inline void insert_test_user(sqlite3* db)
{
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db,
"INSERT INTO users (email, password_hash, balance_dkk_cent) "
"VALUES (?, ?, ?)",
-1, &stmt, NULL);
char email[] = "testuser@email.com";
char password[] = "1234";
StrHash password_hash = str_hash(password);
char* password_hash_str = str_hash_to_string(password_hash);
sqlite3_bind_text(stmt, 1, email, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, password_hash_str, -1, SQLITE_STATIC);
sqlite3_bind_int(stmt, 3, 123);
int res = sqlite3_step(stmt);
if (res != SQLITE_DONE) {
fprintf(stderr, "error: could not insert test user: %s\n",
sqlite3_errmsg(db));
}
sqlite3_finalize(stmt);
}
HttpServer* server; HttpServer* server;
int main(void) int main(void)
{ {
char password[] = "Merc1234";
const char* other_password = "Merc1234";
{
StrHash password_hash = str_hash(password);
char* password_hash_str = str_hash_to_string(password_hash);
printf("'%s'\n", password_hash_str);
bool other_is_equal = str_hash_is_equal(password_hash, other_password);
printf("is_equal = %s\n", other_is_equal ? "true" : "false");
free(password_hash_str);
}
{
StrHash password_hash = str_hash(password);
char* password_hash_str = str_hash_to_string(password_hash);
printf("'%s'\n", password_hash_str);
bool other_is_equal = str_hash_is_equal(password_hash, other_password);
printf("is_equal = %s\n", other_is_equal ? "true" : "false");
free(password_hash_str);
}
return 0;
sqlite3* db; sqlite3* db;
int res = sqlite3_open("database.db", &db); int res = sqlite3_open("database.db", &db);
if (res != SQLITE_OK) { if (res != SQLITE_OK) {
@ -104,6 +101,8 @@ int main(void)
return -1; return -1;
} }
insert_test_user(db);
Cx cx = { .number = 1 }; Cx cx = { .number = 1 };
server = http_server_new((HttpServerOpts) { server = http_server_new((HttpServerOpts) {

View File

@ -48,6 +48,7 @@ char* string_copy(const String* string)
copy[string->size] = '\0'; copy[string->size] = '\0';
return copy; return copy;
} }
static inline StrHash str_hash_with_salt(const char* str, const uint8_t* salt) static inline StrHash str_hash_with_salt(const char* str, const uint8_t* salt)
{ {
if (strlen(str) >= MAX_HASH_INPUT_LEN - 1) { if (strlen(str) >= MAX_HASH_INPUT_LEN - 1) {