Changeset: fcc72f8cbb45 for MonetDB URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=fcc72f8cbb45 Modified Files: sql/backends/monet5/rest/rest_jsonstore.c sql/backends/monet5/rest/rest_jsonstore.h sql/backends/monet5/rest/rest_jsonstore_handle_get.c sql/backends/monet5/rest/rest_jsonstore_handle_get.h Branch: default Log Message:
implement design functions diffs (202 lines): diff --git a/sql/backends/monet5/rest/rest_jsonstore.c b/sql/backends/monet5/rest/rest_jsonstore.c --- a/sql/backends/monet5/rest/rest_jsonstore.c +++ b/sql/backends/monet5/rest/rest_jsonstore.c @@ -103,8 +103,17 @@ mserver_browser_get(const UriUriA uri) { } else { // The first path element is a table name // we cannot check this here, so we assume the table exists - mserver_rest_command = MONETDB_REST_DB_GETDOCID; - fprintf(stderr, "url: %s\n", uri.pathHead->text.first); + if (strncmp(uri.pathHead->next->text.first, API_SPECIAL_CHAR, 1) == 0) { + // This path element is on of the special cases + mserver_rest_command = MONETDB_REST_UNKWOWN_SPECIAL; + if (strncmp(uri.pathHead->next->text.first, MONETDB_REST_PATH_DESIGN, 7) == 0) { + mserver_rest_command = MONETDB_REST_GET_DESIGN; + fprintf(stderr, "special url: %s\n", uri.pathTail->text.first); + } + } else { + mserver_rest_command = MONETDB_REST_DB_GETDOCID; + fprintf(stderr, "url: %s\n", uri.pathHead->text.first); + } } } } @@ -125,7 +134,7 @@ mserver_browser_put(const UriUriA uri) { if (uri.absolutePath) { if (uri.pathHead != NULL) { if (uri.pathHead->next == NULL) { - if (strcmp(uri.pathHead->text.first, API_SPECIAL_CHAR) < 0) { + if (strncmp(uri.pathHead->text.first, API_SPECIAL_CHAR, 1) == 0) { // This path element is on of the special cases mserver_rest_command = MONETDB_REST_UNKWOWN_SPECIAL; } else { @@ -133,12 +142,24 @@ mserver_browser_put(const UriUriA uri) { fprintf(stderr, "url: %s\n", uri.pathHead->text.first); } } else { - if (strcmp(uri.pathHead->text.first, API_SPECIAL_CHAR) < 0) { + // We have multiple paths + if (strncmp(uri.pathHead->text.first, API_SPECIAL_CHAR, 1) == 0) { // This path element is on of the special cases mserver_rest_command = MONETDB_REST_UNKWOWN_SPECIAL; } else { - mserver_rest_command = MONETDB_REST_DB_UPDATE_DOC; + // The first path element is a table name + // we cannot check this here, so we assume the table exists fprintf(stderr, "url: %s\n", uri.pathHead->text.first); + if (strncmp(uri.pathHead->next->text.first, API_SPECIAL_CHAR, 1) == 0) { + // This path element is on of the special cases + mserver_rest_command = MONETDB_REST_UNKWOWN_SPECIAL; + if (strncmp(uri.pathHead->next->text.first, MONETDB_REST_PATH_DESIGN, 7) == 0) { + mserver_rest_command = MONETDB_REST_INSERT_DESIGN; + fprintf(stderr, "special url: %s\n", uri.pathTail->text.first); + } + } else { + mserver_rest_command = MONETDB_REST_DB_UPDATE_DOC; + } } } } else { @@ -248,6 +269,16 @@ char * get_docid(UriUriA uri) { return docid; } +static +char * get_designid(UriUriA uri) { + size_t len; + char * docid; + len = strlen(uri.pathHead->next->text.first); + docid = malloc(len + 1); + strncpy(docid, uri.pathHead->next->text.first, len); + return docid; +} + int handle_http_request (const char *url, const char *method, char **page, char * postdata) @@ -334,6 +365,16 @@ handle_http_request (const char *url, co case MONETDB_REST_NO_ATTACHMENT_PATH: RESTerror(page, mserver_rest_command); break; + case MONETDB_REST_INSERT_DESIGN: + dbname = get_dbname(uri); + docid = get_designid(uri); + RESTinsertDesign(page, dbname, docid, postdata); + break; + case MONETDB_REST_GET_DESIGN: + dbname = get_dbname(uri); + docid = get_designid(uri); + RESTgetDesign(page, dbname, docid); + break; default: /* error, unknown command */ ret = 1; diff --git a/sql/backends/monet5/rest/rest_jsonstore.h b/sql/backends/monet5/rest/rest_jsonstore.h --- a/sql/backends/monet5/rest/rest_jsonstore.h +++ b/sql/backends/monet5/rest/rest_jsonstore.h @@ -49,6 +49,8 @@ #define MONETDB_REST_DB_DELETE_DOC 13 #define MONETDB_REST_INSERT_ATTACHMENT 14 #define MONETDB_REST_NO_ATTACHMENT_PATH 15 +#define MONETDB_REST_INSERT_DESIGN 16 +#define MONETDB_REST_GET_DESIGN 17 #define MONETDB_REST_ATTACHMENT "attachment" #define MONETDB_REST_PATH_ALLDBS "_all_dbs" diff --git a/sql/backends/monet5/rest/rest_jsonstore_handle_get.c b/sql/backends/monet5/rest/rest_jsonstore_handle_get.c --- a/sql/backends/monet5/rest/rest_jsonstore_handle_get.c +++ b/sql/backends/monet5/rest/rest_jsonstore_handle_get.c @@ -114,15 +114,19 @@ str RESTcreateDB(char ** result, char * "CREATE TABLE json_%s ( " "_id uuid, _rev VARCHAR(34), " "js json); " + "CREATE TABLE jsondesign_%s ( " + "_id varchar(128), " + "_rev VARCHAR(34), " + "design json); " "CREATE TABLE jsonblob_%s ( " "_id uuid, " "mimetype varchar(128), " "filename varchar(128), " "value blob); "; - size_t len = 2 * strlen(dbname) + (8 * line) - (2 * place) + char0; + size_t len = 3 * strlen(dbname) + (12 * line) - (3 * place) + char0; querytext = malloc(len); - snprintf(querytext, len, query, dbname, dbname); + snprintf(querytext, len, query, dbname, dbname, dbname); msg = RESTsqlQuery(result, querytext); if (querytext != NULL) { @@ -369,3 +373,60 @@ str RESTdeleteAttach(char ** result, cha } return msg; } + +str RESTinsertDesign(char ** result, char * dbname, const char * doc_id, const char * doc) +{ + str msg = MAL_SUCCEED; + size_t len = strlen(dbname) + strlen(doc_id) + 2 * strlen(doc) + 85 + char0; + char * querytext = NULL; + + querytext = malloc(len); + snprintf(querytext, len, "INSERT INTO jsondesign_%s (_id, _rev, design ) VALUES ( '%s', concat('1-', md5('%s')), '%s');", dbname, doc_id, doc, doc); + + msg = RESTsqlQuery(result, querytext); + if (querytext != NULL) { + free(querytext); + } + if (strcmp(*result,"&2 1 -1\n") == 0) { + msg = RESTsqlQuery(result, result_ok); + } + return msg; +} + +str RESTgetDesign(char ** result, char * dbname, const char * doc_id) +{ + str msg = MAL_SUCCEED; + size_t len = strlen(dbname) + strlen(doc_id) + 108 + char0; + char * querytext = NULL; + char * begin_query; + char * end_query; + size_t query_len; + char * viewquery; + + querytext = malloc(len); + snprintf(querytext, len, "SELECT json_text(json_path(design, 'views.foo'), 'query') AS query FROM jsondesign_%s WHERE _id = '%s' LIMIT 1;", dbname, doc_id); + + msg = RESTsqlQuery(result, querytext); + /* + First implementation of running views stored in a json document + Missing error handling and sanity checks. They will be added + when the API and the corresponding implementation get are + defined. For now this is only a proof of concept that the idea + will work. Not production ready. + */ + if (msg == MAL_SUCCEED) { + begin_query = strstr((const char *)*result, "{ query , \""); + end_query = strstr((const char *)*result, " \" }"); + query_len = strlen(begin_query + 12) - strlen(end_query) + 1; + viewquery = malloc(query_len); + snprintf(viewquery, query_len, "%s", begin_query + 12); + msg = RESTsqlQuery(result, viewquery); + } + if (querytext != NULL) { + free(querytext); + } + if (viewquery != NULL) { + free(viewquery); + } + return msg; +} diff --git a/sql/backends/monet5/rest/rest_jsonstore_handle_get.h b/sql/backends/monet5/rest/rest_jsonstore_handle_get.h --- a/sql/backends/monet5/rest/rest_jsonstore_handle_get.h +++ b/sql/backends/monet5/rest/rest_jsonstore_handle_get.h @@ -47,5 +47,7 @@ rest_export str RESTerror(char **result, rest_export str RESTinsertAttach(char ** result, char * dbname, const char * attachment, const char * doc_id); rest_export str RESTgetAttach(char ** result, char * dbname, const char * doc_id); rest_export str RESTdeleteAttach(char ** result, char * dbname, const char * doc_id); +rest_export str RESTinsertDesign(char ** result, char * dbname, const char * doc_id, const char * doc); +rest_export str RESTgetDesign(char ** result, char * dbname, const char * doc_id); #endif _______________________________________________ checkin-list mailing list checkin-list@monetdb.org http://mail.monetdb.org/mailman/listinfo/checkin-list