Commit 2faff908 authored by Matthieu Dorier's avatar Matthieu Dorier
Browse files

enabled convering UnQLiteValue to and from Json::Value

parent dc4ec31a
# list of source files
set(sonata-client-src Client.cpp Database.cpp Collection.cpp)
set(sonata-server-src Provider.cpp Backend.cpp UnQLiteBackend.cpp)
set(sonata-server-src Provider.cpp Backend.cpp UnQLiteBackend.cpp UnQLiteVM.cpp UnQLiteValue.cpp)
set(sonata-admin-src Admin.cpp)
# load package helper for generating cmake CONFIG packages
......
#include "UnQLiteVM.hpp"
namespace sonata {
int UnQLiteVM::snta_db_create(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::snta_db_attach(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::snta_db_detach(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::snta_db_destroy(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::sntd_coll_create(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::sntd_coll_exists(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::sntd_coll_open(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::sntd_coll_drop(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::sntd_execute(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::sntc_store(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::sntc_fetch(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::sntc_filter(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::sntc_update(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::sntc_all(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::sntc_last_record_id(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::sntc_size(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::sntc_erase(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::sntr_wait(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
int UnQLiteVM::sntr_test(unqlite_context *pCtx, int argc, unqlite_value **argv) {
// TODO
return UNQLITE_OK;
}
}
......@@ -19,6 +19,7 @@ class UnQLiteVM {
: m_code(code)
, m_db(database) {
compile();
registerSonataFunctions();
}
UnQLiteVM(UnQLiteVM&& other) = delete;
......@@ -74,6 +75,33 @@ class UnQLiteVM {
if(ret != UNQLITE_OK) parse_and_throw_error();
}
void registerSonataFunctions() {
int ret;
// Admin functions
ret = unqlite_create_function(m_vm, "snta_db_create", snta_db_create, nullptr);
ret = unqlite_create_function(m_vm, "snta_db_attach", snta_db_attach, nullptr);
ret = unqlite_create_function(m_vm, "snta_db_detach", snta_db_detach, nullptr);
ret = unqlite_create_function(m_vm, "snta_db_destroy", snta_db_destroy, nullptr);
// Database functions
ret = unqlite_create_function(m_vm, "sntd_coll_create", sntd_coll_create, nullptr);
ret = unqlite_create_function(m_vm, "sntd_coll_exists", sntd_coll_exists, nullptr);
ret = unqlite_create_function(m_vm, "sntd_coll_open", sntd_coll_open, nullptr);
ret = unqlite_create_function(m_vm, "sntd_coll_drop", sntd_coll_drop, nullptr);
ret = unqlite_create_function(m_vm, "sntd_execute", sntd_execute, nullptr);
// Collection functions
ret = unqlite_create_function(m_vm, "sntc_store", sntc_store, nullptr);
ret = unqlite_create_function(m_vm, "sntc_fetch", sntc_fetch, nullptr);
ret = unqlite_create_function(m_vm, "sntc_filter", sntc_filter, nullptr);
ret = unqlite_create_function(m_vm, "sntc_update", sntc_update, nullptr);
ret = unqlite_create_function(m_vm, "sntc_all", sntc_all, nullptr);
ret = unqlite_create_function(m_vm, "sntc_last_record_id", sntc_last_record_id, nullptr);
ret = unqlite_create_function(m_vm, "sntc_size", sntc_size, nullptr);
ret = unqlite_create_function(m_vm, "sntc_erase", sntc_erase, nullptr);
// Wait functions
ret = unqlite_create_function(m_vm, "sntr_wait", sntr_wait, nullptr);
ret = unqlite_create_function(m_vm, "sntr_test", sntr_test, nullptr);
}
void parse_and_throw_error() {
const char *errorBuffer = nullptr;
int len = 0;
......@@ -100,6 +128,30 @@ class UnQLiteVM {
return UNQLITE_OK;
}
}
// Functions to expose into the VM
static int snta_db_create(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int snta_db_attach(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int snta_db_detach(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int snta_db_destroy(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int sntd_coll_create(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int sntd_coll_exists(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int sntd_coll_open(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int sntd_coll_drop(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int sntd_execute(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int sntc_store(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int sntc_fetch(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int sntc_filter(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int sntc_update(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int sntc_all(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int sntc_last_record_id(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int sntc_size(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int sntc_erase(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int sntr_wait(unqlite_context *pCtx, int argc, unqlite_value **argv);
static int sntr_test(unqlite_context *pCtx, int argc, unqlite_value **argv);
};
}
......
#include "UnQLiteValue.hpp"
namespace sonata {
int UnQLiteValue::fillJsonValueMapCallback(unqlite_value *pKey, unqlite_value *pValue, void *pUserData) {
Json::Value* result = reinterpret_cast<Json::Value*>(pUserData);
Json::Value item = UnQLiteValue::convertType(pValue, UnQLiteValue::type<Json::Value>());
const char* key = unqlite_value_to_string(pKey, nullptr);
(*result)[key] = std::move(item);
return UNQLITE_OK;
}
int UnQLiteValue::fillJsonValueArrayCallback(unqlite_value *pKey, unqlite_value *pValue, void *pUserData) {
Json::Value* result = reinterpret_cast<Json::Value*>(pUserData);
Json::Value item = UnQLiteValue::convertType(pValue, UnQLiteValue::type<Json::Value>());
result->append(std::move(item));
return UNQLITE_OK;
}
}
......@@ -6,8 +6,10 @@
#include <string>
#include <vector>
#include <map>
#include <cstring>
#include <unordered_map>
#include <iostream>
#include <json/json.h>
#include "sonata/Exception.hpp"
#include "invoke/invoke.hpp"
......@@ -65,6 +67,53 @@ class UnQLiteValue {
unqlite_value_null(m_value);
}
UnQLiteValue(const Json::Value& val, unqlite_vm* vm)
: m_vm(vm) {
switch(val.type()) {
case Json::nullValue:
m_value = unqlite_vm_new_scalar(vm);
unqlite_value_null(m_value);
break;
case Json::intValue:
m_value = unqlite_vm_new_scalar(vm);
unqlite_value_int64(m_value, val.asInt64());
break;
case Json::uintValue:
m_value = unqlite_vm_new_scalar(vm);
unqlite_value_int64(m_value, val.asInt64());
break;
case Json::realValue:
m_value = unqlite_vm_new_scalar(vm);
unqlite_value_double(m_value, val.asDouble());
break;
case Json::stringValue:
m_value = unqlite_vm_new_scalar(vm);
unqlite_value_string(m_value, val.asString().c_str(), -1);
break;
case Json::booleanValue:
m_value = unqlite_vm_new_scalar(vm);
unqlite_value_bool(m_value, val.asBool());
break;
case Json::arrayValue:
m_value = unqlite_vm_new_array(vm);
for(unsigned i=0; i < val.size(); i++) {
unqlite_value* index = unqlite_vm_new_scalar(vm);
unqlite_value_int64(index, i);
UnQLiteValue element(val[i], vm);
unqlite_array_add_elem(m_value, index, element.m_value);
unqlite_vm_release_value(vm, index);
}
break;
case Json::objectValue:
m_value = unqlite_vm_new_array(vm);
for(auto it = val.begin(); it != val.end(); it++) {
UnQLiteValue element(*it, vm);
unqlite_array_add_strkey_elem(m_value, it.name().c_str(), element.m_value);
}
break;
}
}
UnQLiteValue(const Null&, unqlite_vm* vm)
: m_vm(vm)
, m_value(unqlite_vm_new_scalar(vm)) {
......@@ -421,6 +470,30 @@ class UnQLiteValue {
return result;
}
static int fillJsonValueMapCallback(unqlite_value *pKey, unqlite_value *pValue, void *pUserData);
static int fillJsonValueArrayCallback(unqlite_value *pKey, unqlite_value *pValue, void *pUserData);
static Json::Value convertType(unqlite_value* value, const type<Json::Value>&) {
Json::Value result;
if(unqlite_value_is_null(value)) {
} else if(unqlite_value_is_int(value)) {
result = (int64_t)unqlite_value_to_int64(value);
} else if(unqlite_value_is_float(value)) {
result = unqlite_value_to_double(value);
} else if(unqlite_value_is_bool(value)) {
result = (bool)unqlite_value_to_bool(value);
} else if(unqlite_value_is_string(value)) {
result = unqlite_value_to_string(value, nullptr);
} else if(unqlite_value_is_json_object(value)) {
unqlite_array_walk(value, fillJsonValueMapCallback, &result);
} else if(unqlite_value_is_json_array(value)) {
unqlite_array_walk(value, fillJsonValueArrayCallback, &result);
} else {
// TODO error
}
return result;
}
static bool checkType(unqlite_value* value, const type<Null>&) {
return unqlite_value_is_null(value);
}
......@@ -494,7 +567,7 @@ int UnQLiteValue::fillMapCallback(unqlite_value *pKey, unqlite_value *pValue, vo
(*m)[key] = UnQLiteValue::convertType(pValue, UnQLiteValue::type<typename MapType::mapped_type>());
return UNQLITE_OK;
}
template<typename Stream>
struct UnQLitePrinterArgs {
Stream* stream;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment