1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-11-04 08:17:17 -05:00

[ecmascript] localstorage-db.cpp -> localstorage-db.c

This code is pure C. There are some memory leaks.
They will be addressed later.
This commit is contained in:
Witold Filipczyk 2023-12-29 18:46:19 +01:00
parent 78483386c2
commit 7de0146ded
4 changed files with 178 additions and 192 deletions

View File

@ -10,11 +10,11 @@ SUBDIRS-$(CONFIG_QUICKJS) += quickjs
SUBDIRS-$(CONFIG_ECMASCRIPT_SMJS) += spidermonkey
OBJS-$(CONFIG_ECMASCRIPT_SMJS) += ecmascript.obj ecmascript-c.obj localstorage-db.obj spidermonkey.obj timer.o
OBJS-$(CONFIG_ECMASCRIPT_SMJS) += ecmascript.obj ecmascript-c.obj localstorage-db.o spidermonkey.obj timer.o
OBJS-$(CONFIG_MUJS) += ecmascript.obj ecmascript-c.obj localstorage-db.obj mujs.obj timer.o
OBJS-$(CONFIG_MUJS) += ecmascript.obj ecmascript-c.obj localstorage-db.o mujs.obj timer.o
OBJS-$(CONFIG_QUICKJS) += ecmascript.obj ecmascript-c.obj localstorage-db.obj quickjs.obj timer.o
OBJS-$(CONFIG_QUICKJS) += ecmascript.obj ecmascript-c.obj localstorage-db.o quickjs.obj timer.o
ifeq ($(CONFIG_ECMASCRIPT_SMJS), yes)
CONFIG_ANY_SPIDERMONKEY = yes

View File

@ -0,0 +1,172 @@
/* 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 "ecmascript/localstorage-db.h"
#include "util/string.h"
int
db_prepare_structure(char *db_name)
{
sqlite3_stmt *stmt;
sqlite3 *db;
int 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;
}
int
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;
}
int
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;
}
int
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;
}
char *
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);
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);
if (sqlite3_column_text(stmt, 1) != NULL) {
result = stracpy((const char *)sqlite3_column_text(stmt, 1));
} else {
result = stracpy("");
}
rc = sqlite3_finalize(stmt);
rc = sqlite3_close(db);
return result;
}
char *
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 NULL;
}
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);
if (sqlite3_column_text(stmt, 1) != NULL) {
result = stracpy((const char *)sqlite3_column_text(stmt, 1));
} else {
result = NULL;
}
rc = sqlite3_finalize(stmt);
rc = sqlite3_close(db);
return result;
}

View File

@ -1,186 +0,0 @@
/* 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 "ecmascript/ecmascript.h"
#include "ecmascript/localstorage-db.h"
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);
}
int
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);
}
int
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);
}
int
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);
}
char *
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);
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);
if (sqlite3_column_text(stmt,1)!= NULL) {
result=stracpy((const char *)sqlite3_column_text(stmt, 1));
} else {
result=stracpy("");
}
rc=sqlite3_finalize(stmt);
rc=sqlite3_close(db);
return(result);
}
char *
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);
if (sqlite3_column_text(stmt,1)!= NULL) {
result=stracpy((const char *)sqlite3_column_text(stmt, 1));
} else {
result = nullptr;
}
rc=sqlite3_finalize(stmt);
rc=sqlite3_close(db);
return(result);
}

View File

@ -1,6 +1,6 @@
#INCLUDES += $(SPIDERMONKEY_CFLAGS)
if conf_data.get('CONFIG_ECMASCRIPT_SMJS')
srcs += files('ecmascript.cpp', 'ecmascript-c.cpp', 'localstorage-db.cpp', 'spidermonkey.cpp', 'timer.c')
srcs += files('ecmascript.cpp', 'ecmascript-c.cpp', 'localstorage-db.c', 'spidermonkey.cpp', 'timer.c')
subdir('spidermonkey')
endif
@ -17,12 +17,12 @@ if CONFIG_ANY_SPIDERMONKEY
endif
if conf_data.get('CONFIG_MUJS')
srcs += files('ecmascript.cpp', 'ecmascript-c.cpp', 'localstorage-db.cpp', 'mujs.cpp', 'timer.c')
srcs += files('ecmascript.cpp', 'ecmascript-c.cpp', 'localstorage-db.c', 'mujs.cpp', 'timer.c')
subdir('mujs')
endif
if conf_data.get('CONFIG_QUICKJS')
srcs += files('ecmascript.cpp', 'ecmascript-c.cpp', 'localstorage-db.cpp', 'quickjs.cpp', 'timer.c')
srcs += files('ecmascript.cpp', 'ecmascript-c.cpp', 'localstorage-db.c', 'quickjs.cpp', 'timer.c')
subdir('quickjs')
endif