Subversion Repositories IRCServ

Rev

Blame | Last modification | View Log | Download | RSS feed

/* $Id: db.c 10 2024-08-26 13:37:23Z nishi $ */

#include "is_db.h"

#include "is_log.h"
#include "is_util.h"

#include <sqlite3.h>

#include <stdlib.h>

sqlite3* sql;

char* escape_sql(const char* input) {
        char* query = malloc(1);
        query[0] = 0;
        char cbuf[2];
        cbuf[1] = 0;
        int i;
        for(i = 0; input[i] != 0; i++) {
                if(input[i] == '\'') {
                        cbuf[0] = input[i];
                        char* tmp = query;
                        query = is_strcat(tmp, cbuf);
                        free(tmp);
                        cbuf[0] = input[i];
                        tmp = query;
                        query = is_strcat(tmp, cbuf);
                        free(tmp);
                } else {
                        cbuf[0] = input[i];
                        char* tmp = query;
                        query = is_strcat(tmp, cbuf);
                        free(tmp);
                }
        }
        return query;
}

int is_db_init(void) {
        int ret;
        ret = sqlite3_open(PREFIX "/lib/ircserv/db.sqlite3", &sql);
        if(ret != SQLITE_OK) {
                is_log("Failed to open the database");
                return 1;
        }
        return 0;
}

int is_run_sql(const char* txt) {
        char* err;
        int ret;
        ret = sqlite3_exec(sql, txt, NULL, NULL, &err);
        if(ret != SQLITE_OK) {
                char* str = is_strcat("SQL error: ", err);
                sqlite3_free(err);
                is_log(str);
                free(str);
        }
        return ret == SQLITE_OK ? 0 : 1;
}