From 7de0146dedd92648ba7df06f035275405b266813 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Fri, 29 Dec 2023 18:46:19 +0100 Subject: [PATCH] [ecmascript] localstorage-db.cpp -> localstorage-db.c This code is pure C. There are some memory leaks. They will be addressed later. --- src/ecmascript/Makefile | 6 +- src/ecmascript/localstorage-db.c | 172 ++++++++++++++++++++++++++ src/ecmascript/localstorage-db.cpp | 186 ----------------------------- src/ecmascript/meson.build | 6 +- 4 files changed, 178 insertions(+), 192 deletions(-) create mode 100644 src/ecmascript/localstorage-db.c delete mode 100644 src/ecmascript/localstorage-db.cpp diff --git a/src/ecmascript/Makefile b/src/ecmascript/Makefile index 10e4caac..4f1a7fcb 100644 --- a/src/ecmascript/Makefile +++ b/src/ecmascript/Makefile @@ -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 diff --git a/src/ecmascript/localstorage-db.c b/src/ecmascript/localstorage-db.c new file mode 100644 index 00000000..80a2cf29 --- /dev/null +++ b/src/ecmascript/localstorage-db.c @@ -0,0 +1,172 @@ +/* The localstorage database helper implementation. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include + +#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; +} diff --git a/src/ecmascript/localstorage-db.cpp b/src/ecmascript/localstorage-db.cpp deleted file mode 100644 index c6504984..00000000 --- a/src/ecmascript/localstorage-db.cpp +++ /dev/null @@ -1,186 +0,0 @@ -/* The localstorage database helper implementation. */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include -#include -#include -#include - -#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); - -} diff --git a/src/ecmascript/meson.build b/src/ecmascript/meson.build index 1837eb39..4ba4c3b4 100644 --- a/src/ecmascript/meson.build +++ b/src/ecmascript/meson.build @@ -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