From: Sebastian Harl Date: Mon, 18 May 2015 19:18:40 +0000 (+0200) Subject: frontend/query: Split AST execution into a separate function. X-Git-Tag: sysdb-0.8.0~91 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=6c4a4e0023372ac71b99a089deb0331735977839;p=sysdb.git frontend/query: Split AST execution into a separate function. --- diff --git a/src/frontend/query.c b/src/frontend/query.c index 7fcdbe2..703853c 100644 --- a/src/frontend/query.c +++ b/src/frontend/query.c @@ -59,6 +59,43 @@ lookup_tojson(sdb_store_obj_t *obj, sdb_store_matcher_t *filter, return sdb_store_json_emit_full(f, obj, filter); } /* lookup_tojson */ +static int +query_exec(sdb_conn_t *conn, sdb_ast_node_t *ast) +{ + sdb_store_query_t *q; + sdb_strbuf_t *buf; + int status; + + q = sdb_store_query_prepare(ast); + if (! q) { + /* this shouldn't happen */ + sdb_strbuf_sprintf(conn->errbuf, "failed to compile AST"); + sdb_log(SDB_LOG_ERR, "frontend: failed to compile AST"); + return -1; + } + + buf = sdb_strbuf_create(1024); + if (! buf) { + sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); + sdb_object_deref(SDB_OBJ(q)); + return -1; + } + status = sdb_store_query_execute(q, buf, conn->errbuf); + if (status < 0) { + char query[conn->cmd_len + 1]; + strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len); + query[sizeof(query) - 1] = '\0'; + sdb_log(SDB_LOG_ERR, "frontend: failed to execute query '%s'", query); + } + else + sdb_connection_send(conn, status, + (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf)); + + sdb_strbuf_destroy(buf); + sdb_object_deref(SDB_OBJ(q)); + return status < 0 ? status : 0; +} /* query_exec */ + /* * public API */ @@ -68,9 +105,6 @@ sdb_fe_query(sdb_conn_t *conn) { sdb_llist_t *parsetree; sdb_ast_node_t *ast = NULL; - - sdb_store_query_t *q; - sdb_strbuf_t *buf = NULL; int status = 0; if ((! conn) || (conn->cmd != SDB_CONNECTION_QUERY)) @@ -110,43 +144,12 @@ sdb_fe_query(sdb_conn_t *conn) } } - q = sdb_store_query_prepare(ast); - if (! q) { - /* this shouldn't happen */ - sdb_strbuf_sprintf(conn->errbuf, "failed to compile AST"); - sdb_log(SDB_LOG_ERR, "frontend: failed to compile AST"); - status = -1; - } else { - buf = sdb_strbuf_create(1024); - if (! buf) { - sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); - sdb_object_deref(SDB_OBJ(q)); - return -1; - } - status = sdb_store_query_execute(q, buf, conn->errbuf); - if (status < 0) { - char query[conn->cmd_len + 1]; - strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len); - query[sizeof(query) - 1] = '\0'; - sdb_log(SDB_LOG_ERR, "frontend: failed to execute query '%s'", query); - } + if (ast) { + status = query_exec(conn, ast); + sdb_object_deref(SDB_OBJ(ast)); } - sdb_object_deref(SDB_OBJ(ast)); sdb_llist_destroy(parsetree); - - if (status < 0) { - sdb_object_deref(SDB_OBJ(q)); - sdb_strbuf_destroy(buf); - return status; - } - - assert(buf); - sdb_connection_send(conn, status, - (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf)); - - sdb_strbuf_destroy(buf); - sdb_object_deref(SDB_OBJ(q)); - return 0; + return status; } /* sdb_fe_query */ int