1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00
elinks/src/ecmascript/localstorage-db.c

185 lines
3.9 KiB
C
Raw Normal View History

2021-10-25 11:43:07 -04:00
/* The localstorage database helper implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "elinks.h"
#include "src/ecmascript/ecmascript.h"
2021-02-24 03:56:06 -05:00
int
db_prepare_structure(char *db_name)
{
sqlite3_stmt *stmt;
sqlite3 *db;
int rc;
rc = sqlite3_open(db_name, &db);
if (rc)
{
//DBG("Error opening localStorage database.");
rc=sqlite3_close(db);
return(-1);
}
sqlite3_busy_timeout(db, 2000);
rc=sqlite3_prepare_v2(db, "CREATE TABLE storage (key TEXT, value TEXT);", -1, &stmt, NULL);
rc=sqlite3_step(stmt);
rc=sqlite3_finalize(stmt);
rc=sqlite3_close(db);
return(0);
}
2021-02-24 03:56:06 -05:00
int
2022-01-30 06:48:21 -05:00
db_delete_from(char *db_name, const char *key)
{
sqlite3_stmt *stmt;
sqlite3 *db;
int rc;
int affected_rows = 0;
rc = sqlite3_open(db_name, &db);
if (rc)
{
//DBG("Error opening localStorage database.");
rc=sqlite3_close(db);
return(-1);
}
sqlite3_busy_timeout(db, 2000);
rc=sqlite3_prepare_v2(db, "DELETE FROM storage WHERE key = ?;", -1, &stmt, NULL);
rc=sqlite3_bind_text(stmt, 1, key, strlen(key), SQLITE_STATIC);
rc=sqlite3_step(stmt);
rc=sqlite3_finalize(stmt);
affected_rows=sqlite3_changes(db);
rc=sqlite3_close(db);
return(affected_rows);
}
2021-02-24 03:56:06 -05:00
int
2022-01-30 06:48:21 -05:00
db_insert_into(char *db_name, const char *key, const char *value)
{
sqlite3_stmt *stmt;
sqlite3 *db;
int rc;
int affected_rows = 0;
rc = sqlite3_open(db_name, &db);
if (rc) {
//DBG("Error opening localStorage database.");
rc=sqlite3_close(db);
return(-1);
}
sqlite3_busy_timeout(db, 2000);
rc=sqlite3_prepare_v2(db, "INSERT INTO storage (value,key) VALUES (?,?);", -1, &stmt, NULL);
rc=sqlite3_bind_text(stmt, 1, value, strlen(value), SQLITE_STATIC);
rc=sqlite3_bind_text(stmt, 2, key, strlen(key), SQLITE_STATIC);
rc=sqlite3_step(stmt);
rc=sqlite3_finalize(stmt);
affected_rows=sqlite3_changes(db);
rc=sqlite3_close(db);
return(affected_rows);
}
2021-02-24 03:56:06 -05:00
int
2022-01-30 06:48:21 -05:00
db_update_set(char *db_name, const char *key, const char *value)
{
sqlite3_stmt *stmt;
sqlite3 *db;
int rc;
int affected_rows = 0;
rc = sqlite3_open(db_name, &db);
if (rc) {
//DBG("Error opening localStorage database.");
rc=sqlite3_close(db);
return(-1);
}
sqlite3_busy_timeout(db, 2000);
rc=sqlite3_prepare_v2(db, "UPDATE storage SET value = ? where key = ?;", -1, &stmt, NULL);
rc=sqlite3_bind_text(stmt, 1, value, strlen(value), SQLITE_STATIC);
rc=sqlite3_bind_text(stmt, 2, key, strlen(key), SQLITE_STATIC);
rc=sqlite3_step(stmt);
rc=sqlite3_finalize(stmt);
affected_rows=sqlite3_changes(db);
rc=sqlite3_close(db);
return(affected_rows);
}
2021-02-24 03:56:06 -05:00
char *
2022-01-30 06:48:21 -05:00
db_query_by_value(char *db_name, const char *value)
{
sqlite3_stmt *stmt;
sqlite3 *db;
int rc;
char *result;
rc = sqlite3_open(db_name, &db);
if (rc)
{
//DBG("Error opening localStorage database.");
rc=sqlite3_close(db);
2021-02-24 03:56:06 -05:00
return stracpy("");
}
sqlite3_busy_timeout(db, 2000);
rc=sqlite3_prepare_v2(db, "SELECT key FROM storage WHERE value = ? LIMIT 1;", -1, &stmt, NULL);
rc=sqlite3_bind_text(stmt, 1, value, strlen(value), SQLITE_STATIC);
2021-02-24 03:56:06 -05:00
2022-01-30 06:48:21 -05:00
if (sqlite3_column_text(stmt,1)!= NULL) {
result=stracpy((const char *)sqlite3_column_text(stmt, 1));
2021-02-24 03:56:06 -05:00
} else {
result=stracpy("");
}
rc=sqlite3_finalize(stmt);
rc=sqlite3_close(db);
return(result);
}
2021-02-24 03:56:06 -05:00
char *
2022-01-30 06:48:21 -05:00
db_query_by_key(char *db_name, const char *key)
{
sqlite3_stmt *stmt;
sqlite3 *db;
int rc;
char *result;
rc = sqlite3_open(db_name, &db);
if (rc)
{
//DBG("Error opening localStorage database.");
rc=sqlite3_close(db);
return nullptr;
}
sqlite3_busy_timeout(db, 2000);
rc=sqlite3_prepare_v2(db, "SELECT * FROM storage WHERE key = ? LIMIT 1;", -1, &stmt, NULL);
rc=sqlite3_bind_text(stmt, 1, key, strlen(key), SQLITE_STATIC);
rc=sqlite3_step(stmt);
2022-01-30 06:48:21 -05:00
if (sqlite3_column_text(stmt,1)!= NULL) {
result=stracpy((const char *)sqlite3_column_text(stmt, 1));
2021-02-24 03:56:06 -05:00
} else {
result = nullptr;
}
rc=sqlite3_finalize(stmt);
rc=sqlite3_close(db);
return(result);
}