This commit is contained in:
vms 2019-08-20 15:36:51 +02:00
parent 1b7ec24a2f
commit 39ebf92cc0
4 changed files with 27 additions and 17 deletions

View File

@ -1,7 +1,8 @@
#ifndef C_TEMPLATE_SIDE_MODULE_API_H
#define C_TEMPLATE_SIDE_MODULE_API_H
//#include <string.h>
#include <string.h>
#include <stdlib.h>
/*
* Concatenate preprocessor tokens A and B without expanding macro definitions
@ -43,7 +44,7 @@ char * module_name ## _call(const char *ptr, int length) { \
\
unsigned int result_size = 0; \
for (int i = 0; i < 4; ++i) { \
result_size = result_size | ((unsigned int)result[i] << 8*i); \
result_size = result_size | (module_name ## _load(result + i) << 8*i); \
} \
\
char *result_out = malloc(result_size + 1); \

View File

@ -35,8 +35,16 @@ char *prepare_response(const char *response, int response_length) {
const char *add_post_request(const json_t *json);
const char *fetch_posts_request(const json_t *json);
bool isInited = 0;
const char *invoke(char *str, int length) {
if(0 == isInited) {
create_scheme();
isInited = 1;
}
wasm_log(str, length);
wasm_log("\n", 1);
json_t pool[10];
const unsigned int pool_size = sizeof pool / sizeof *pool;
@ -75,8 +83,8 @@ const char *invoke(char *str, int length) {
}
const char *add_post_request(const json_t *json) {
const json_t *message_json = json_getProperty(json, "message");
const json_t *username_json = json_getProperty(json, "username");
const json_t *message_json = json_getProperty(json, "message");
if(0 == message_json || 0 == username_json) {
const char error[] = "Given json doesn't contain message or username field";
return prepare_response(error, sizeof error);
@ -87,11 +95,11 @@ const char *add_post_request(const json_t *json) {
return prepare_response(error, sizeof error);
}
const char *message = json_getValue(message_json);
const char *username = json_getValue(username_json);
const char *message = json_getValue(message_json);
const char *add_post_result = add_post(message, strlen(message),
username, strlen(username));
const char *add_post_result = add_post(username, strlen(username),
message, strlen(message));
if(0 == add_post_result) {
const char error[] = "add_post failed";
return prepare_response(error, sizeof error);

View File

@ -1,4 +1,5 @@
#include "../sdk/side_module_api.h"
#include "../sdk/logger.h"
#include <stdlib.h>
#include <stdio.h>
@ -12,9 +13,9 @@ void create_scheme() {
sqlite_call(create_sql, sizeof(create_sql));
}
char *add_post(const char *message, int message_length, const char *username, int username_length) {
char *add_post(const char *username, int username_length, const char *message, int message_length) {
// at now wasm-ld has 1024 bytes for stack permission by default - that why dynamic allocation here
const int request_size = message_length + username_length + 50;
const int request_size = username_length + message_length + 50;
char *add_sql = (char *)malloc(request_size);
const int add_sql_length = snprintf(add_sql, request_size, "INSERT INTO messages VALUES(%s, %s)", message, username);
@ -30,10 +31,10 @@ char *get_all_posts(int offset, int count) {
char *get_sql = (char *)malloc(256);
const int get_sql_length = snprintf(get_sql, 256,
"SELECT json_group_array(\n"
" json_object('message', message, 'username', username)\n"
" ) AS json_result FROM (\n"
" SELECT * FROM messages LIMIT %d OFFSET %d\n"
"SELECT json_group_array("
" json_object('message', message, 'username', username)"
" ) AS json_result FROM ("
" SELECT * FROM messages LIMIT %d OFFSET %d"
" )", count, offset);
if(get_sql_length < 0) {
return 0;
@ -48,10 +49,10 @@ char *get_posts_by_username(const char *username, int username_length, int offse
char *get_sql = (char *)malloc(request_size);
const int add_sql_length = snprintf(get_sql, request_size,
"\"SELECT json_group_array(\n"
" json_object('message', message, 'username', username)\n"
" ) AS json_result FROM (\n"
" SELECT * FROM messages where username = '%s' LIMIT %d OFFSET %d\n"
"SELECT json_group_array("
" json_object('message', message, 'username', username)"
" ) AS json_result FROM ("
" SELECT * FROM messages where username = '%s' LIMIT %d OFFSET %d"
" )",
username, count, offset);
if(add_sql_length < 0) {

View File

@ -3,7 +3,7 @@
void create_scheme();
char *add_post(const char *message, int message_length, const char *username, int username_length);
char *add_post(const char *username, int username_length, const char *message, int message_length);
char *get_all_posts(int offset, int count);