Cleanup database code

This commit is contained in:
topjohnwu
2024-12-29 03:52:21 -08:00
committed by John Wu
parent 10e47248de
commit 3ca6d06f69
7 changed files with 231 additions and 214 deletions

View File

@@ -126,20 +126,10 @@ using db_row = std::map<std::string_view, std::string_view>;
using db_row_cb = std::function<bool(db_row&)>;
struct owned_fd;
struct db_result {
db_result() = default;
db_result(const char *s) : err(s) {}
db_result(int code);
bool check_err();
operator bool() { return err.empty(); }
private:
std::string err;
};
int get_db_settings(db_settings &cfg, int key = -1);
int set_db_settings(int key, int value);
int get_db_strings(db_strings &str, int key = -1);
void rm_db_strings(int key);
void exec_sql(owned_fd client);
db_result db_exec(const char *sql);
db_result db_exec(const char *sql, const db_row_cb &fn);
bool db_exec(const char *sql);
bool db_exec(const char *sql, const db_row_cb &fn);

View File

@@ -0,0 +1,37 @@
#pragma once
#include <cxx.h>
#define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */
#define SQLITE_OK 0 /* Successful result */
#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
struct sqlite3;
struct sqlite3_stmt;
extern int (*sqlite3_open_v2)(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs);
extern int (*sqlite3_close)(sqlite3 *db);
extern int (*sqlite3_prepare_v2)(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **ppStmt, const char **pzTail);
extern int (*sqlite3_bind_parameter_count)(sqlite3_stmt*);
extern int (*sqlite3_bind_int)(sqlite3_stmt*, int, int);
extern int (*sqlite3_bind_text)(sqlite3_stmt*,int,const char*,int,void(*)(void*));
extern int (*sqlite3_column_count)(sqlite3_stmt *pStmt);
extern const char *(*sqlite3_column_name)(sqlite3_stmt*, int N);
extern const char *(*sqlite3_column_text)(sqlite3_stmt*, int iCol);
extern int (*sqlite3_step)(sqlite3_stmt*);
extern int (*sqlite3_finalize)(sqlite3_stmt *pStmt);
extern const char *(*sqlite3_errstr)(int);
using StringVec = rust::Vec<rust::String>;
using StringSlice = rust::Slice<rust::String>;
using StrSlice = rust::Slice<rust::Str>;
using sqlite_row_callback = void(*)(void*, StringSlice, StringSlice);
#define fn_run_ret(fn, ...) if (int rc = fn(__VA_ARGS__); rc != SQLITE_OK) return rc
bool load_sqlite();
int sql_exec(sqlite3 *db, rust::Str zSql, StrSlice args, sqlite_row_callback callback, void *v);