Skip to content

Add functions to push clause to query #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/src/ast_call_subquery.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ void cypher_ast_call_subquery_replace_query
node->query = query;
}

void cypher_ast_call_subquery_push_clause(
cypher_astnode_t *astnode,
cypher_astnode_t *clause,
unsigned int index
) {
REQUIRE_TYPE(astnode, CYPHER_AST_CALL_SUBQUERY, NULL);
struct call_subquery *subquery_node =
container_of(astnode, struct call_subquery, _astnode);

cypher_astnode_t *new_query =
cypher_ast_query_push_clause(subquery_node->query, clause, index);

cypher_astnode_free(subquery_node->query);
astnode->children[0] = new_query;
subquery_node->query = new_query;
}

cypher_astnode_t *clone(const cypher_astnode_t *self,
cypher_astnode_t **children)
{
Expand Down
42 changes: 42 additions & 0 deletions lib/src/ast_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,48 @@ void cypher_ast_query_replace_clauses(
astnode->nchildren -= end_index - start_index;
}

cypher_astnode_t *cypher_ast_query_push_clause(
cypher_astnode_t *astnode,
cypher_astnode_t *clause,
unsigned int index
)
{
REQUIRE_TYPE(astnode, CYPHER_AST_QUERY, NULL);
REQUIRE_TYPE(clause, CYPHER_AST_QUERY_CLAUSE, NULL);
struct query *query = container_of(astnode, struct query, _astnode);

unsigned int nchildren = astnode->nchildren + 1;
unsigned int nclauses = query->nclauses + 1;

cypher_astnode_t **clauses = calloc(nclauses, sizeof(cypher_astnode_t *));
if (clauses == NULL) {
return NULL;
}

cypher_astnode_t **children = calloc(nchildren, sizeof(cypher_astnode_t *));
if (children == NULL) {
return NULL;
}

//insert new clause
for(uint i = 0; i < index; i++) {
clauses[i] = query->clauses[i];
children[i] = query->clauses[i];
}
clauses[index] = clause;
children[index] = clause;
for(uint i = index + 1; i < nclauses; i++) {
clauses[i] = query->clauses[i - 1];
children[i] = query->clauses[i - 1];
}

cypher_astnode_t *new_query = cypher_ast_query(NULL, 0, clauses, nclauses,
children, nchildren, astnode->range);
free(clauses);
free(children);

return new_query;
}

ssize_t detailstr(const cypher_astnode_t *self, char *str, size_t size)
{
Expand Down
20 changes: 20 additions & 0 deletions lib/src/ast_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ void cypher_ast_statement_replace_body
}


void cypher_ast_statement_push_clause
(
cypher_astnode_t *astnode,
cypher_astnode_t *clause,
unsigned int index
)
{
REQUIRE_TYPE(astnode, CYPHER_AST_STATEMENT, NULL);
REQUIRE_TYPE(clause, CYPHER_AST_QUERY_CLAUSE, NULL);
struct statement *node = container_of(astnode, struct statement, _astnode);

cypher_astnode_t *body =
cypher_ast_query_push_clause(node->body, clause, index);

astnode->children[child_index(astnode, node->body)] = body;
cypher_astnode_free(node->body);
node->body = body;
}


cypher_astnode_t *clone(const cypher_astnode_t *self,
cypher_astnode_t **children)
{
Expand Down
56 changes: 56 additions & 0 deletions lib/src/cypher-parser.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,26 @@ cypher_astnode_t *cypher_ast_statement(cypher_astnode_t * const *options,
void cypher_ast_statement_replace_body(cypher_astnode_t *astnode,
const cypher_astnode_t *body);

/**
* Insert clause at position `index` of a `CYPHER_AST_STATEMENT` node.
*
* If the node is not an instance of `CYPHER_AST_STATEMENT` then the result will
* be undefined.
*
* If the clause is not an instance of `CYPHER_AST_QUERY_CLAUSE` then the result
* will be undefined.
*
* @param [node] The AST node.
* @param [clause] The AST clause node.
* @param [index] The index of the new clause.
*/
void cypher_ast_statement_push_clause
(
cypher_astnode_t *astnode,
cypher_astnode_t *clause,
unsigned int index
);

/**
* Get the number of options in a `CYPHER_AST_STATEMENT` node.
*
Expand Down Expand Up @@ -1328,6 +1348,26 @@ void cypher_ast_query_set_clause(
void cypher_ast_query_replace_clauses(
cypher_astnode_t *astnode, cypher_astnode_t *clause, unsigned int start_index, unsigned int end_index);

/**
* Insert clause at position `index` of a `CYPHER_AST_QUERY` node.
*
* If the node is not an instance of `CYPHER_AST_QUERY` then the result will
* be undefined.
*
* If the clause is not an instance of `CYPHER_AST_QUERY_CLAUSE` then the result
* will be undefined.
*
* @param [node] The AST node.
* @param [clause] The AST clause node.
* @param [index] The index of the new clause.
* @return The query, or NULL if an error occurs.
*/
cypher_astnode_t *cypher_ast_query_push_clause(
cypher_astnode_t *astnode,
cypher_astnode_t *clause,
unsigned int index
);

/**
* Replace the clauses of a `CYPHER_AST_FOREACH` node.
*
Expand Down Expand Up @@ -3130,6 +3170,22 @@ void cypher_ast_call_subquery_replace_query
cypher_astnode_t *query
);

/**
* Insert a new clause at position `index` of a `CYPHER_AST_CALL_SUBQUERY` node.
*
* If the node is not an instance of `CYPHER_AST_CALL_SUBQUERY` then the
* result will be undefined.
*
* @param [astnode] The AST node.
* @param [query] The query to replace the existing query with.
* @param [index] Position of the new clause
*/
void cypher_ast_call_subquery_push_clause(
cypher_astnode_t *astnode,
cypher_astnode_t *clause,
unsigned int index
);

/**
* Get the proc name of a `CYPHER_AST_CALL` node.
*
Expand Down