Skip to content
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED True)

# Include directories
include_directories(/usr/include/)
include_directories(/usr/include/cjson/)
include_directories(/usr/include/mysql/server/)

# Add the plugin library
add_library(json2sql MODULE code/json-api.c)

target_link_libraries(json2sql cjson microhttpd mysqlservices)

# Set output directory for the plugin
set_target_properties(json2sql PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "/app/plugin" )
Expand Down
2 changes: 1 addition & 1 deletion Contributions.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
## Tech advisors
* Sergei Golubchik : cmake troubleshooting
* Markus Mäkelä : json + jwt tech advisor
* Vladislav Vaintroub : httpd tech adviso
* Vladislav Vaintroub : httpd tech advisor

### Thank you all !
10 changes: 7 additions & 3 deletions code/json-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
#include <stdio.h>

// MariaDB headers
#include <mysql.h>
#ifndef MYSQL_DYNAMIC_PLUGIN
#define MYSQL_DYNAMIC_PLUGIN
#endif

#include <mysql/plugin.h>

// micro httpd headers
Expand Down Expand Up @@ -74,13 +77,14 @@ static char* handle_get_request(const char *url) {
char colvalue[64];
char procname[64];
char query[512]="";
size_t query_len;

// initialize the JSON answer
cJSON *json = cJSON_CreateObject();

if (sscanf(url, "/v1/tables/%64[^/]/%64[^/]/%64[^/]/%64s", schema, table, colname, colvalue) == 4) {
// Here query database
snprintf(query, sizeof(query), "SELECT * FROM %s.%s WHERE %s = '%s'", schema, table, colname, colvalue);
query_len = snprintf(query, sizeof(query), "SELECT * FROM %s.%s WHERE %s = '%s'", schema, table, colname, colvalue);
cJSON_AddStringToObject(json, "source", "tables");
cJSON_AddStringToObject(json, "schema", schema);
cJSON_AddStringToObject(json, "table", table);
Expand Down Expand Up @@ -115,7 +119,7 @@ static char* handle_get_request(const char *url) {
return json_string; // Caller is responsible for freeing this memory
}

if (mysql_real_query(conn, STRING_WITH_LEN(query))) {
if (mysql_real_query(conn, query, query_len)) {
fprintf(stderr, "mysql_real_query failed\n");
mysql_close(conn);
cJSON_AddStringToObject(json, "status", "QUERY failed");
Expand Down
10 changes: 7 additions & 3 deletions docker/rockylinux/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ RUN yum clean all


# Add MariaDB 10.11 repository and install it + devel headers + housekeeping
RUN curl -sSL https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | bash -s -- --mariadb-server-version="mariadb-10.11" && \
RUN curl -sSL https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | bash -s -- --mariadb-server-version="mariadb-11.4" && \
yum install -y MariaDB-server MariaDB-client MariaDB-devel && yum clean all

# Set the working directory
Expand All @@ -22,11 +22,15 @@ WORKDIR /app
# Clone your GitHub repository
RUN git clone --depth 1 https://github.com/SylvainA77/json2sql-plugin.git .

# Alternately build from the top level directory of this repo
#COPY . .

# Build your project
RUN mkdir build && cd build && cmake .. && make
RUN mkdir build && cd build && cmake .. && cmake --build . --verbose

FROM mariadb:latest
FROM mariadb:11.4

RUN apt-get update && apt-get install -y libcjson1 libmicrohttpd12t64 && rm -rf /var/lib/apt/lists/*
COPY --from=build /app/plugin/libjson2sql.so /usr/lib/mysql/plugin
RUN printf "[mariadb]\nplugin-load-add=libjson2sql\n" > /etc/mysql/mariadb.conf.d/load_jsonplugin.cnf
# Expose the default json2sql port
Expand Down