/** * rasqal_free_row: * @row: query result row * * Destructor - Free a query result row object. */ void rasqal_free_row(rasqal_row* row) { if(!row) return; if(--row->usage) return; if(row->values) { int i; for(i = 0; i < row->size; i++) { if(row->values[i]) rasqal_free_literal(row->values[i]); } RASQAL_FREE(array, row->values); } if(row->order_values) { int i; for(i = 0; i < row->order_size; i++) { if(row->order_values[i]) rasqal_free_literal(row->order_values[i]); } RASQAL_FREE(array, row->order_values); } RASQAL_FREE(rasqal_row, row); }
/* * rasqal_expression_evaluate_abs: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate SPARQL 1.1 RASQAL_EXPR_ABS (numeric) expression. * * Return value: A #rasqal_literal value or NULL on failure. */ rasqal_literal* rasqal_expression_evaluate_abs(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_literal* l1; rasqal_literal* result = NULL; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; if(!rasqal_literal_is_numeric(l1)) goto failed; result = rasqal_literal_abs(l1, error_p); rasqal_free_literal(l1); l1 = NULL; if(*error_p) goto failed; return result; failed: if(error_p) *error_p = 1; if(l1) rasqal_free_literal(l1); return NULL; }
/** * rasqal_engine_rowsort_calculate_order_values: * @query: query object * @row: row * * INTERNAL - Calculate the order condition values for a row * * Return value: non-0 on failure */ int rasqal_engine_rowsort_calculate_order_values(rasqal_query* query, rasqal_row* row) { int i; raptor_sequence *order_seq; if(!row->order_size) return 1; order_seq = rasqal_query_get_order_conditions_sequence(query); for(i = 0; i < row->order_size; i++) { rasqal_expression* e; rasqal_literal *l; e = (rasqal_expression*)raptor_sequence_get_at(order_seq, i); l = rasqal_expression_evaluate(query->world, &query->locator, e, query->compare_flags); if(row->order_values[i]) rasqal_free_literal(row->order_values[i]); if(l) { row->order_values[i] = rasqal_new_literal_from_literal(rasqal_literal_value(l)); rasqal_free_literal(l); } else row->order_values[i] = NULL; } return 0; }
/* * rasqal_expression_evaluate_lang: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_LANG (literal expr) expression. * * Return value: A #rasqal_literal value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_lang(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal* l1; int free_literal = 1; rasqal_variable* v = NULL; unsigned char* new_s; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; v = rasqal_literal_as_variable(l1); if(v) { rasqal_free_literal(l1); l1 = v->value; /* don't need v after this */ free_literal = 0; if(!l1) goto failed; } if(rasqal_literal_get_rdf_term_type(l1) != RASQAL_LITERAL_STRING) goto failed; if(l1->language) { size_t len = strlen(l1->language); new_s = (unsigned char*)RASQAL_MALLOC(cstring, len + 1); if(!new_s) goto failed; memcpy((char*)new_s, l1->language, len + 1); } else { new_s = (unsigned char*)RASQAL_MALLOC(cstring, 1); if(!new_s) goto failed; *new_s = '\0'; } if(free_literal) rasqal_free_literal(l1); /* after this new_s is owned by result */ return rasqal_new_string_literal(world, new_s, NULL, NULL, NULL); failed: if(error_p) *error_p = 1; if(free_literal) rasqal_free_literal(l1); return NULL; }
/* * rasqal_expression_evaluate_datatype: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_DATATYPE (string literal) expression. * * Return value: A #rasqal_literal URI value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_datatype(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal* l1; int free_literal = 1; rasqal_variable* v = NULL; raptor_uri* dt_uri = NULL; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; v = rasqal_literal_as_variable(l1); if(v) { rasqal_free_literal(l1); l1 = v->value; /* don't need v after this */ free_literal = 0; if(!l1) goto failed; } if(rasqal_literal_get_rdf_term_type(l1) != RASQAL_LITERAL_STRING) goto failed; if(l1->language) goto failed; /* The datatype of a plain literal is xsd:string */ dt_uri = l1->datatype; if(!dt_uri && l1->type == RASQAL_LITERAL_STRING) dt_uri = rasqal_xsd_datatype_type_to_uri(l1->world, RASQAL_LITERAL_XSD_STRING); if(!dt_uri) goto failed; dt_uri = raptor_uri_copy(dt_uri); if(free_literal) rasqal_free_literal(l1); /* after this dt_uri is owned by result */ return rasqal_new_uri_literal(world, dt_uri); failed: if(error_p) *error_p = 1; if(free_literal) rasqal_free_literal(l1); return NULL; }
/* * rasqal_expression_evaluate_in_set: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_IN and RASQAL_EXPR_NOT_IN (expr, * expr list) expression. * * Return value: A #rasqal_literal boolean value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_in_set(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; int size = raptor_sequence_size(e->args); int i; rasqal_literal* l1; int found = 0; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; for(i = 0; i < size; i++) { rasqal_expression* arg_e; rasqal_literal* arg_literal; arg_e = (rasqal_expression*)raptor_sequence_get_at(e->args, i); arg_literal = rasqal_expression_evaluate2(arg_e, eval_context, error_p); if(!arg_literal) goto failed; found = (rasqal_literal_equals_flags(l1, arg_literal, eval_context->flags, error_p) != 0); #if RASQAL_DEBUG > 1 if(*error_p) RASQAL_DEBUG1("rasqal_literal_equals_flags() returned: FAILURE\n"); else RASQAL_DEBUG2("rasqal_literal_equals_flags() returned: %d\n", found); #endif rasqal_free_literal(arg_literal); if(*error_p) goto failed; if(found) /* found - terminate search */ break; } rasqal_free_literal(l1); if(e->op == RASQAL_EXPR_NOT_IN) found = !found; return rasqal_new_boolean_literal(world, found); failed: if(error_p) *error_p = 1; if(l1) rasqal_free_literal(l1); return NULL; }
/* * rasqal_expression_evaluate_istype: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_ISBLANK, RASQAL_EXPR_ISURI, * RASQAL_EXPR_ISLITERAL and RASQAL_EXPR_ISNUMERIC (expr) * expressions. * * Return value: A #rasqal_literal boolean value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_istype(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal* l1; int free_literal = 1; rasqal_variable *v; int b; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; v = rasqal_literal_as_variable(l1); if(v) { rasqal_free_literal(l1); l1 = v->value; /* don't need v after this */ free_literal = 0; if(!l1) goto failed; } if(e->op == RASQAL_EXPR_ISBLANK) b = (l1->type == RASQAL_LITERAL_BLANK); else if(e->op == RASQAL_EXPR_ISLITERAL) b = (rasqal_literal_get_rdf_term_type(l1) == RASQAL_LITERAL_STRING); else if(e->op == RASQAL_EXPR_ISURI) b =(l1->type == RASQAL_LITERAL_URI); else b = (rasqal_literal_is_numeric(l1)); if(free_literal) rasqal_free_literal(l1); return rasqal_new_boolean_literal(world, b); failed: if(error_p) *error_p = 1; if(free_literal && l1) rasqal_free_literal(l1); return NULL; }
/* * rasqal_expression_evaluate_str: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_STR (literal expr) expression. * * Return value: A #rasqal_literal value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_str(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal* l1 = NULL; rasqal_literal* result = NULL; const unsigned char *s; size_t len; unsigned char *new_s; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; /* Note: flags removes RASQAL_COMPARE_XQUERY as this is the * explicit stringify operation and we want URIs as strings. */ s = rasqal_literal_as_counted_string(l1, &len, (eval_context->flags & ~RASQAL_COMPARE_XQUERY), error_p); if(!s || *error_p) goto failed; new_s = (unsigned char *)RASQAL_MALLOC(cstring, len + 1); if(!new_s) goto failed; memcpy((char*)new_s, (const char*)s, len + 1); /* after this new_s is owned by result */ result = rasqal_new_string_literal(world, new_s, NULL, NULL, NULL); if(l1) rasqal_free_literal(l1); return result; failed: if(error_p) *error_p = 1; if(l1) rasqal_free_literal(l1); return NULL; }
/* * rasqal_expression_evaluate_if: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_IF (condition, true expr, false expr) expressions. * * Return value: A #rasqal_literal value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_if(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_literal* l1; int b; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; /* IF condition */ b = rasqal_literal_as_boolean(l1, error_p); rasqal_free_literal(l1); if(*error_p) goto failed; /* condition is true: evaluate arg2 or false: evaluate arg3 */ return rasqal_expression_evaluate2(b ? e->arg2 : e->arg3, eval_context, error_p); failed: if(error_p) *error_p = 1; return NULL; }
/** * rasqal_free_triple: * @t: #rasqal_triple object. * * Destructor - destroy a #rasqal_triple object. **/ void rasqal_free_triple(rasqal_triple* t) { if(!t) return; if(t->subject) rasqal_free_literal(t->subject); if(t->predicate) rasqal_free_literal(t->predicate); if(t->object) rasqal_free_literal(t->object); if(t->origin) rasqal_free_literal(t->origin); RASQAL_FREE(rasqal_triple, t); }
/** * rasqal_free_variable: * @v: #rasqal_variable object * * Destructor - Destroy a Rasqal variable object. * **/ void rasqal_free_variable(rasqal_variable* v) { if(!v) return; #ifdef RASQAL_DEBUG_VARIABLE_USAGE v->usage--; RASQAL_DEBUG3("Variable %s usage decreased to %d\n", v->name, v->usage); if(v->usage) return; #else if(--v->usage) return; #endif if(v->name) RASQAL_FREE(char*, v->name); if(v->value) rasqal_free_literal(v->value); if(v->expression) rasqal_free_expression(v->expression); RASQAL_FREE(rasqal_variable, v); }
/* * rasqal_expression_evaluate_bnode_constructor: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_BNODE (string) expression. * * Return value: A #rasqal_literal blank node value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_bnode_constructor(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal *l1 = NULL; unsigned char *new_s = NULL; if(e->arg1) { const unsigned char *s; size_t len; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; s = rasqal_literal_as_counted_string(l1, &len, eval_context->flags, error_p); if(*error_p) goto failed; new_s = (unsigned char*)RASQAL_MALLOC(cstring, len + 1); if(!new_s) goto failed; memcpy((char*)new_s, s, len + 1); rasqal_free_literal(l1); } else { new_s = rasqal_world_generate_bnodeid(world, NULL); if(!new_s) goto failed; } /* after this new_s is owned by the result */ return rasqal_new_simple_literal(world, RASQAL_LITERAL_BLANK, new_s); failed: if(error_p) *error_p = 1; if(l1) rasqal_free_literal(l1); return NULL; }
static rasqal_literal* rasqal_builtin_agg_expression_execute_result(void* user_data) { rasqal_builtin_agg_expression_execute* b; b = (rasqal_builtin_agg_expression_execute*)user_data; if(b->expr->op == RASQAL_EXPR_COUNT) { rasqal_literal* result; result = rasqal_new_integer_literal(b->world, RASQAL_LITERAL_INTEGER, b->count); return result; } if(b->expr->op == RASQAL_EXPR_GROUP_CONCAT) { size_t len; unsigned char* str; rasqal_literal* result; len = raptor_stringbuffer_length(b->sb); str = (unsigned char*)RASQAL_MALLOC(cstring, len + 1); if(!str) return NULL; if(raptor_stringbuffer_copy_to_string(b->sb, str, len)) { RASQAL_FREE(cstring, str); return NULL; } result = rasqal_new_string_literal(b->world, str, NULL, NULL, NULL); return result; } if(b->expr->op == RASQAL_EXPR_AVG) { rasqal_literal* count_l; rasqal_literal* result; count_l = rasqal_new_integer_literal(b->world, RASQAL_LITERAL_INTEGER, b->count); result = rasqal_literal_divide(b->l, count_l, &b->error); rasqal_free_literal(count_l); if(b->error) { /* result will be NULL and error will be non-0 on division by 0 * in which case the result is literal(integer 0) */ result = rasqal_new_integer_literal(b->world, RASQAL_LITERAL_INTEGER, 0); } return result; } return rasqal_new_literal_from_literal(b->l); }
void rasqal_free_formula(rasqal_formula* formula) { if(!formula) return; if(formula->triples) raptor_free_sequence(formula->triples); if(formula->value) rasqal_free_literal(formula->value); RASQAL_FREE(rasqal_formula, formula); }
/** * rasqal_new_triple: * @subject: Triple subject. * @predicate: Triple predicate. * @object: Triple object. * * Constructor - create a new #rasqal_triple triple or triple pattern. * Takes ownership of the literals passed in. * * The triple origin can be set with rasqal_triple_set_origin(). * * Return value: a new #rasqal_triple or NULL on failure. **/ rasqal_triple* rasqal_new_triple(rasqal_literal* subject, rasqal_literal* predicate, rasqal_literal* object) { rasqal_triple* t; t = (rasqal_triple*)RASQAL_CALLOC(rasqal_triple, 1, sizeof(*t)); if(t) { t->subject = subject; t->predicate = predicate; t->object = object; } else { if(subject) rasqal_free_literal(subject); if(predicate) rasqal_free_literal(predicate); if(object) rasqal_free_literal(object); } return t; }
/* * rasqal_expression_evaluate_uri_constructor: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_URI and RASQAL_EXPR_IRI (string) * expressions. * * Return value: A #rasqal_literal URI value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_uri_constructor(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal* l1; raptor_uri* dt_uri; const unsigned char *s; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; s = rasqal_literal_as_string_flags(l1, eval_context->flags, error_p); if(*error_p) goto failed; dt_uri = raptor_new_uri_relative_to_base(world->raptor_world_ptr, eval_context->base_uri, s); rasqal_free_literal(l1); l1 = NULL; if(!dt_uri) goto failed; /* after this dt_uri is owned by the result literal */ return rasqal_new_uri_literal(world, dt_uri); failed: if(error_p) *error_p = 1; if(l1) rasqal_free_literal(l1); return NULL; }
/** * rasqal_row_set_values_from_variables_table: * @row: Result row * @vars_table: Variables table * * INTERNAL - Set the values of all variables in the row from the given variables table * */ void rasqal_row_set_values_from_variables_table(rasqal_row* row, rasqal_variables_table* vars_table) { int i; for(i = 0; i < row->size; i++) { rasqal_literal *l; l = rasqal_variables_table_get_value(vars_table, i); if(row->values[i]) rasqal_free_literal(row->values[i]); row->values[i] = rasqal_new_literal_from_literal(l); } }
/** * rasqal_free_variable: * @v: #rasqal_variable object * * Destructor - Destroy a Rasqal variable object. * **/ void rasqal_free_variable(rasqal_variable* v) { if(!v) return; if(v->name) RASQAL_FREE(cstring, (void*)v->name); if(v->value) rasqal_free_literal(v->value); if(v->expression) rasqal_free_expression(v->expression); RASQAL_FREE(rasqal_variable, v); }
/* * rasqal_expression_evaluate_sameterm: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_SAMETERM (expr1, expr2) expressions. * * Return value: A #rasqal_literal boolean value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_sameterm(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal* l1; rasqal_literal* l2; int b; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; l2 = rasqal_expression_evaluate2(e->arg2, eval_context, error_p); if(*error_p || !l2) goto failed; b = rasqal_literal_same_term(l1, l2); #if RASQAL_DEBUG > 1 RASQAL_DEBUG2("rasqal_literal_same_term() returned: %d\n", b); #endif rasqal_free_literal(l1); rasqal_free_literal(l2); return rasqal_new_boolean_literal(world, b); failed: if(error_p) *error_p = 1; if(l1) rasqal_free_literal(l1); return NULL; }
static void rasqal_builtin_agg_expression_execute_finish(void* user_data) { rasqal_builtin_agg_expression_execute* b; b = (rasqal_builtin_agg_expression_execute*)user_data; if(b->l) rasqal_free_literal(b->l); if(b->sb) raptor_free_stringbuffer(b->sb); RASQAL_FREE(rasqal_builtin_agg_expression_execute, b); }
/** * rasqal_row_set_value_at: * @row: query result row * @offset: offset into row (column number) * @value: literal value to set * * Set the value of a variable in a query result row * * Any existing row value is freed and the literal @value passed in * is copied. * * Return value: non-0 on failure */ int rasqal_row_set_value_at(rasqal_row* row, int offset, rasqal_literal* value) { if(!row || !value) return 1; if(offset < 0 || offset >= row->size) return 1; if(row->values[offset]) rasqal_free_literal(row->values[offset]); row->values[offset] = rasqal_new_literal_from_literal(value); return 0; }
/** * rasqal_variable_set_value: * @v: the #rasqal_variable object * @l: the #rasqal_literal value to set (or NULL) * * Set the value of a Rasqal variable. * * The variable value is an input parameter and is copied in, not shared. * If the variable value is NULL, any existing value is deleted. * **/ void rasqal_variable_set_value(rasqal_variable* v, rasqal_literal* l) { if(v->value) rasqal_free_literal(v->value); v->value = l; #ifdef RASQAL_DEBUG if(!v->name) RASQAL_FATAL1("variable has no name"); RASQAL_DEBUG2("setting variable %s to value ", v->name); rasqal_literal_print(v->value, stderr); fputc('\n', stderr); #endif }
/** * rasqal_row_to_nodes: * @row: Result row * * INTERNAL - Turn the given result row literals into RDF strings, URIs or blank literals. * * Return value: non-0 on failure */ int rasqal_row_to_nodes(rasqal_row* row) { int i; if(!row) return 1; for(i = 0; i < row->size; i++) { if(row->values[i]) { rasqal_literal* new_l; new_l = rasqal_literal_as_node(row->values[i]); if(!new_l) return -1; rasqal_free_literal(row->values[i]); row->values[i] = new_l; } } return 0; }
/* * rasqal_expression_evaluate_lang: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_LANG (literal expr) expression. * * Return value: A #rasqal_literal value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_lang(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal* l1; int free_literal = 1; rasqal_variable* v = NULL; unsigned char* new_s; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; v = rasqal_literal_as_variable(l1); if(v) { rasqal_free_literal(l1); l1 = v->value; /* don't need v after this */ free_literal = 0; if(!l1) goto failed; } if(rasqal_literal_get_rdf_term_type(l1) != RASQAL_LITERAL_STRING) goto failed; if(l1->language) { size_t len = strlen(l1->language); new_s = RASQAL_MALLOC(unsigned char*, len + 1); if(!new_s) goto failed; memcpy(RASQAL_GOOD_CAST(char*, new_s), l1->language, len + 1); } else {
static int rasqal_builtin_agg_expression_execute_reset(void* user_data) { rasqal_builtin_agg_expression_execute* b; b = (rasqal_builtin_agg_expression_execute*)user_data; b->count = 0; b->error = 0; if(b->l) { rasqal_free_literal(b->l); b->l = 0; } if(b->sb) { raptor_free_stringbuffer(b->sb); b->sb = raptor_new_stringbuffer(); if(!b) return 1; } return 0; }
void manifest_free_world(manifest_world* mw) { if(!mw) return; if(mw->rdfs_namespace_uri) raptor_free_uri(mw->rdfs_namespace_uri); if(mw->mf_namespace_uri) raptor_free_uri(mw->mf_namespace_uri); if(mw->t_namespace_uri) raptor_free_uri(mw->t_namespace_uri); if(mw->qt_namespace_uri) raptor_free_uri(mw->qt_namespace_uri); if(mw->dawgt_namespace_uri) raptor_free_uri(mw->dawgt_namespace_uri); if(mw->sd_namespace_uri) raptor_free_uri(mw->sd_namespace_uri); if(mw->mf_Manifest_uri) raptor_free_uri(mw->mf_Manifest_uri); if(mw->mf_entries_uri) raptor_free_uri(mw->mf_entries_uri); if(mw->mf_name_uri) raptor_free_uri(mw->mf_name_uri); if(mw->mf_action_uri) raptor_free_uri(mw->mf_action_uri); if(mw->mf_result_uri) raptor_free_uri(mw->mf_result_uri); if(mw->mf_resultCardinality_uri) raptor_free_uri(mw->mf_resultCardinality_uri); if(mw->rdf_type_uri) raptor_free_uri(mw->rdf_type_uri); if(mw->rdf_first_uri) raptor_free_uri(mw->rdf_first_uri); if(mw->rdf_rest_uri) raptor_free_uri(mw->rdf_rest_uri); if(mw->rdf_nil_uri) raptor_free_uri(mw->rdf_nil_uri); if(mw->rdfs_comment_uri) raptor_free_uri(mw->rdfs_comment_uri); if(mw->t_path_uri) raptor_free_uri(mw->t_path_uri); if(mw->qt_data_uri) raptor_free_uri(mw->qt_data_uri); if(mw->qt_graphData_uri) raptor_free_uri(mw->qt_graphData_uri); if(mw->qt_query_uri) raptor_free_uri(mw->qt_query_uri); if(mw->dawgt_approval_uri) raptor_free_uri(mw->dawgt_approval_uri); if(mw->sd_entailmentRegime_uri) raptor_free_uri(mw->sd_entailmentRegime_uri); if(mw->mf_Manifest_literal) rasqal_free_literal(mw->mf_Manifest_literal); if(mw->mf_entries_literal) rasqal_free_literal(mw->mf_entries_literal); if(mw->mf_name_literal) rasqal_free_literal(mw->mf_name_literal); if(mw->mf_action_literal) rasqal_free_literal(mw->mf_action_literal); if(mw->mf_result_literal) rasqal_free_literal(mw->mf_result_literal); if(mw->mf_resultCardinality_literal) rasqal_free_literal(mw->mf_resultCardinality_literal); if(mw->rdf_type_literal) rasqal_free_literal(mw->rdf_type_literal); if(mw->rdf_first_literal) rasqal_free_literal(mw->rdf_first_literal); if(mw->rdf_rest_literal) rasqal_free_literal(mw->rdf_rest_literal); if(mw->rdfs_comment_literal) rasqal_free_literal(mw->rdfs_comment_literal); if(mw->t_path_literal) rasqal_free_literal(mw->t_path_literal); if(mw->qt_data_literal) rasqal_free_literal(mw->qt_data_literal); if(mw->qt_graphData_literal) rasqal_free_literal(mw->qt_graphData_literal); if(mw->qt_query_literal) rasqal_free_literal(mw->qt_query_literal); if(mw->dawgt_approval_literal) rasqal_free_literal(mw->dawgt_approval_literal); if(mw->sd_entailmentRegime_literal) rasqal_free_literal(mw->sd_entailmentRegime_literal); free(mw); }
static rasqal_row* rasqal_aggregation_rowsource_read_row(rasqal_rowsource* rowsource, void *user_data) { rasqal_aggregation_rowsource_context* con; rasqal_row* row; int error = 0; con = (rasqal_aggregation_rowsource_context*)user_data; if(con->finished) return NULL; /* Iterate over input rows until last row seen or group done */ while(1) { error = 0; if(con->saved_row) row = con->saved_row; else row = rasqal_rowsource_read_row(con->rowsource); if(!row) { /* End of input - calculate last aggregation result */ con->finished = 1; break; } if(con->last_group_id != row->group_id) { int i; if(!con->saved_row && con->last_group_id >= 0) { /* Existing aggregation is done - return result */ /* save current row for next time this function is called */ con->saved_row = row; row = NULL; #ifdef RASQAL_DEBUG RASQAL_DEBUG2("Aggregation ending group %d", con->last_group_id); fputc('\n', DEBUG_FH); #endif /* Empty distinct maps */ for(i = 0; i < con->expr_count; i++) { rasqal_agg_expr_data* expr_data = &con->expr_data[i]; if(expr_data->map) { rasqal_free_map(expr_data->map); expr_data->map = NULL; } } break; } /* reference is now in 'row' variable */ con->saved_row = NULL; #ifdef RASQAL_DEBUG RASQAL_DEBUG2("Aggregation starting group %d", row->group_id); fputc('\n', DEBUG_FH); #endif /* next time this function is called we continue here */ for(i = 0; i < con->expr_count; i++) { rasqal_agg_expr_data* expr_data = &con->expr_data[i]; if(!expr_data->agg_user_data) { /* init once */ expr_data->agg_user_data = rasqal_builtin_agg_expression_execute_init(rowsource->world, expr_data->expr); if(!expr_data->agg_user_data) { error = 1; break; } } /* Init map for each group */ if(expr_data->expr->flags & RASQAL_EXPR_FLAG_DISTINCT) { expr_data->map = rasqal_new_literal_sequence_sort_map(1 /* is_distinct */, 0 /* compare_flags */); if(!expr_data->map) { error = 1; break; } } } if(error) break; con->last_group_id = row->group_id; } /* end if handling change of group ID */ /* Bind the values in the input row to the variables in the table */ rasqal_row_bind_variables(row, rowsource->query->vars_table); /* Evaluate the expressions giving a sequence of literals to * run the aggregation step over. */ if(1) { int i; if(!con->step_count) { /* copy first value row from input rowsource */ for(i = 0; i < con->input_values_count; i++) { rasqal_literal* value; value = rasqal_new_literal_from_literal(row->values[i]); raptor_sequence_set_at(con->input_values, i, value); } } con->step_count++; for(i = 0; i < con->expr_count; i++) { rasqal_agg_expr_data* expr_data = &con->expr_data[i]; raptor_sequence* seq; /* SPARQL Aggregation uses ListEvalE() to evaluate - ignoring * errors and filtering out expressions that fail */ seq = rasqal_expression_sequence_evaluate(rowsource->query, expr_data->exprs_seq, /* ignore_errors */ 1, &error); if(error) continue; if(expr_data->map) { if(rasqal_literal_sequence_sort_map_add_literal_sequence(expr_data->map, seq)) { /* duplicate found * * The above function just freed seq so no data is lost */ continue; } } #ifdef RASQAL_DEBUG RASQAL_DEBUG1("Aggregation step over literals: "); raptor_sequence_print(seq, DEBUG_FH); fputc('\n', DEBUG_FH); #endif error = rasqal_builtin_agg_expression_execute_step(expr_data->agg_user_data, seq); /* when DISTINCTing, seq remains owned by the map * otherwise seq is local and must be freed */ if(!expr_data->map) raptor_free_sequence(seq); if(error) break; } } rasqal_free_row(row); row = NULL; if(error) break; } /* end while reading rows */ if(error) { /* Discard row on error */ if(row) { rasqal_free_row(row); row = NULL; } } else if (con->last_group_id >= 0) { int offset = 0; int i; /* Generate result row and reset for next group */ row = rasqal_new_row(rowsource); /* Copy scalar results through */ for(i = 0; i < con->input_values_count; i++) { rasqal_literal* result; /* Reset: get and delete any stored input rowsource literal */ result = (rasqal_literal*)raptor_sequence_delete_at(con->input_values, i); rasqal_row_set_value_at(row, offset, result); rasqal_free_literal(result); offset++; } /* Set aggregate results */ for(i = 0; i < con->expr_count; i++) { rasqal_literal* result; rasqal_agg_expr_data* expr_data = &con->expr_data[i]; rasqal_variable* v; /* Calculate the result because the input ended or a new group started */ result = rasqal_builtin_agg_expression_execute_result(expr_data->agg_user_data); #ifdef RASQAL_DEBUG RASQAL_DEBUG1("Aggregation ending group with result: "); if(result) rasqal_literal_print(result, DEBUG_FH); else fputs("NULL", DEBUG_FH); fputc('\n', DEBUG_FH); #endif v = rasqal_rowsource_get_variable_by_offset(rowsource, offset); result = rasqal_new_literal_from_literal(result); /* it is OK to bind to NULL */ rasqal_variable_set_value(v, result); rasqal_row_set_value_at(row, offset, result); if(result) rasqal_free_literal(result); offset++; if(rasqal_builtin_agg_expression_execute_reset(expr_data->agg_user_data)) { rasqal_free_row(row); row = NULL; break; } } con->step_count = 0; if(row) row->offset = con->offset++; } return row; }
/** * rasqal_query_results_get_triple: * @query_results: #rasqal_query_results query_results * * Get the current triple in the result. * * The return value is a shared #raptor_statement. * * Return value: #raptor_statement or NULL if failed or results exhausted **/ raptor_statement* rasqal_query_results_get_triple(rasqal_query_results* query_results) { rasqal_query* query; int rc; rasqal_triple *t; rasqal_literal *s, *p, *o; raptor_statement *rs = NULL; unsigned char *nodeid; int skipped; RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(query_results, rasqal_query_results, NULL); if(query_results->failed || query_results->finished) return NULL; if(!rasqal_query_results_is_graph(query_results)) return NULL; query = query_results->query; if(!query) return NULL; if(query->verb == RASQAL_QUERY_VERB_DESCRIBE) return NULL; /* ensure we have a row to work on */ if(rasqal_query_results_ensure_have_row_internal(query_results)) return NULL; skipped = 0; while(1) { if(skipped) { rc = rasqal_query_results_next(query_results); if(rc) { rs = NULL; break; } query_results->current_triple_result = -1; } if(query_results->current_triple_result < 0) query_results->current_triple_result = 0; t = (rasqal_triple*)raptor_sequence_get_at(query->constructs, query_results->current_triple_result); rs = &query_results->result_triple; s = rasqal_literal_as_node(t->subject); if(!s) { rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_WARN, &query->locator, "Triple with unbound subject skipped"); skipped = 1; continue; } /* raptor v2 terms are copied, not shared */ if(rs->subject) { raptor_free_term(rs->subject); rs->subject = NULL; } switch(s->type) { case RASQAL_LITERAL_URI: rs->subject = raptor_new_term_from_uri(query_results->world->raptor_world_ptr, s->value.uri); break; case RASQAL_LITERAL_BLANK: nodeid = rasqal_prefix_id(query_results->result_count, (unsigned char*)s->string); rasqal_free_literal(s); if(!nodeid) { rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_FATAL, &query->locator, "Could not prefix subject blank identifier"); return NULL; } s = rasqal_new_simple_literal(query_results->world, RASQAL_LITERAL_BLANK, nodeid); if(!s) { rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_FATAL, &query->locator, "Could not create a new subject blank literal"); return NULL; } rs->subject = raptor_new_term_from_blank(query_results->world->raptor_world_ptr, nodeid); break; case RASQAL_LITERAL_QNAME: case RASQAL_LITERAL_PATTERN: case RASQAL_LITERAL_XSD_STRING: case RASQAL_LITERAL_BOOLEAN: case RASQAL_LITERAL_INTEGER: case RASQAL_LITERAL_DOUBLE: case RASQAL_LITERAL_FLOAT: case RASQAL_LITERAL_VARIABLE: case RASQAL_LITERAL_DECIMAL: case RASQAL_LITERAL_DATETIME: case RASQAL_LITERAL_UDT: case RASQAL_LITERAL_INTEGER_SUBTYPE: /* QNames should be gone by the time expression eval happens * Everything else is removed by rasqal_literal_as_node() above. */ case RASQAL_LITERAL_STRING: /* string [literal] subjects are not RDF */ case RASQAL_LITERAL_UNKNOWN: default: /* case RASQAL_LITERAL_STRING: */ rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_WARN, &query->locator, "Triple with non-URI/blank node subject skipped"); skipped = 1; break; } if(skipped) { if(s) rasqal_free_literal(s); continue; } p = rasqal_literal_as_node(t->predicate); if(!p) { rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_WARN, &query->locator, "Triple with unbound predicate skipped"); rasqal_free_literal(s); skipped = 1; continue; } switch(p->type) { case RASQAL_LITERAL_URI: /* raptor v2 terms are copied, not shared */ if(rs->predicate) { raptor_free_term(rs->predicate); rs->predicate = NULL; } rs->predicate = raptor_new_term_from_uri(query_results->world->raptor_world_ptr, p->value.uri); break; case RASQAL_LITERAL_QNAME: case RASQAL_LITERAL_PATTERN: case RASQAL_LITERAL_XSD_STRING: case RASQAL_LITERAL_BOOLEAN: case RASQAL_LITERAL_INTEGER: case RASQAL_LITERAL_DOUBLE: case RASQAL_LITERAL_FLOAT: case RASQAL_LITERAL_VARIABLE: case RASQAL_LITERAL_DECIMAL: case RASQAL_LITERAL_DATETIME: case RASQAL_LITERAL_UDT: case RASQAL_LITERAL_INTEGER_SUBTYPE: /* QNames should be gone by the time expression eval happens * Everything else is removed by rasqal_literal_as_node() above. */ case RASQAL_LITERAL_BLANK: case RASQAL_LITERAL_STRING: /* blank node or string [literal] predicates are not RDF */ case RASQAL_LITERAL_UNKNOWN: default: rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_WARN, &query->locator, "Triple with non-URI predicate skipped"); skipped = 1; break; } if(skipped) { rasqal_free_literal(s); if(p) rasqal_free_literal(p); continue; } o = rasqal_literal_as_node(t->object); if(!o) { rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_WARN, &query->locator, "Triple with unbound object skipped"); rasqal_free_literal(s); rasqal_free_literal(p); skipped = 1; continue; } /* raptor v2 terms are copied, not shared */ if(rs->object) { raptor_free_term(rs->object); rs->object = NULL; } switch(o->type) { case RASQAL_LITERAL_URI: rs->object = raptor_new_term_from_uri(query_results->world->raptor_world_ptr, o->value.uri); break; case RASQAL_LITERAL_BLANK: nodeid = rasqal_prefix_id(query_results->result_count, (unsigned char*)o->string); rasqal_free_literal(o); if(!nodeid) { rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_FATAL, &query->locator, "Could not prefix blank identifier"); rasqal_free_literal(s); rasqal_free_literal(p); return NULL; } o = rasqal_new_simple_literal(query_results->world, RASQAL_LITERAL_BLANK, nodeid); if(!o) { rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_FATAL, &query->locator, "Could not create a new subject blank literal"); rasqal_free_literal(s); rasqal_free_literal(p); return NULL; } rs->object = raptor_new_term_from_blank(query_results->world->raptor_world_ptr, nodeid); break; case RASQAL_LITERAL_STRING: rs->object = raptor_new_term_from_literal(query_results->world->raptor_world_ptr, o->string, o->datatype, (const unsigned char*)o->language); break; case RASQAL_LITERAL_QNAME: case RASQAL_LITERAL_PATTERN: case RASQAL_LITERAL_XSD_STRING: case RASQAL_LITERAL_BOOLEAN: case RASQAL_LITERAL_INTEGER: case RASQAL_LITERAL_DOUBLE: case RASQAL_LITERAL_FLOAT: case RASQAL_LITERAL_VARIABLE: case RASQAL_LITERAL_DECIMAL: case RASQAL_LITERAL_DATETIME: case RASQAL_LITERAL_UDT: case RASQAL_LITERAL_INTEGER_SUBTYPE: /* QNames should be gone by the time expression eval happens * Everything else is removed by rasqal_literal_as_node() above. */ case RASQAL_LITERAL_UNKNOWN: default: rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_WARN, &query->locator, "Triple with unknown object skipped"); skipped = 1; break; } if(skipped) { rasqal_free_literal(s); rasqal_free_literal(p); if(o) rasqal_free_literal(o); continue; } /* dispose previous triple if any */ if(query_results->triple) { rasqal_free_triple(query_results->triple); query_results->triple = NULL; } /* for saving s, p, o for later disposal */ query_results->triple = rasqal_new_triple(s, p, o); /* got triple, return it */ break; } return rs; }
static int rasqal_builtin_agg_expression_execute_step(void* user_data, raptor_sequence* literals) { rasqal_builtin_agg_expression_execute* b; rasqal_literal* l; int i; b = (rasqal_builtin_agg_expression_execute*)user_data; if(b->error) return 1; if(b->expr->op == RASQAL_EXPR_COUNT) { /* COUNT(*) : counts every row (does not care about literals) */ if(b->expr->arg1->op == RASQAL_EXPR_VARSTAR) b->count++; /* COUNT(expr list) : counts rows with non-empty sequence of literals */ else if(raptor_sequence_size(literals) > 0) b->count++; return 0; } /* Other aggregate functions count every row */ b->count++; for(i = 0; (l = (rasqal_literal*)raptor_sequence_get_at(literals, i)); i++) { rasqal_literal* result = NULL; if(b->expr->op == RASQAL_EXPR_SAMPLE) { /* Sample chooses the first literal it sees */ if(!b->l) b->l = rasqal_new_literal_from_literal(l); break; } if(b->expr->op == RASQAL_EXPR_GROUP_CONCAT) { const unsigned char* str; int error = 0; str = (const unsigned char*)rasqal_literal_as_string_flags(l, 0, &error); if(!error) { if(raptor_stringbuffer_length(b->sb)) raptor_stringbuffer_append_counted_string(b->sb, b->separator, 1, 1); raptor_stringbuffer_append_string(b->sb, str, 1); } continue; } if(!b->l) result = rasqal_new_literal_from_literal(l); else { if(b->expr->op == RASQAL_EXPR_SUM || b->expr->op == RASQAL_EXPR_AVG) { result = rasqal_literal_add(b->l, l, &b->error); } else if(b->expr->op == RASQAL_EXPR_MIN) { int cmp = rasqal_literal_compare(b->l, l, 0, &b->error); if(cmp <= 0) result = rasqal_new_literal_from_literal(b->l); else result = rasqal_new_literal_from_literal(l); } else if(b->expr->op == RASQAL_EXPR_MAX) { int cmp = rasqal_literal_compare(b->l, l, 0, &b->error); if(cmp >= 0) result = rasqal_new_literal_from_literal(b->l); else result = rasqal_new_literal_from_literal(l); } else { RASQAL_FATAL2("Builtin aggregation operation %d is not implemented", b->expr->op); } rasqal_free_literal(b->l); if(!result) b->error = 1; } b->l = result; #if RASQAL_DEBUG > 1 RASQAL_DEBUG3("Aggregation step result %s (error=%d)\n", (result ? (const char*)rasqal_literal_as_string(result) : "(NULL)"), b->error); #endif if(b->error) break; } return b->error; }
int main(int argc, char *argv[]) { #if 0 raptor_uri *xsd_uri; raptor_uri *dateTime_uri; rasqal_literal *l1, *l2; int fn_i; raptor_uri* fn_uri; const unsigned char *fn_name; rasqal_extension_fn fn; raptor_sequence *fn_args; char *error; rasqal_literal *fn_result; rasqal_world *world; world = rasqal_new_world(); if(!world || rasqal_world_open(world)) { fprintf(stderr, "%s: rasqal_world init failed\n", program); return(1); } xsd_uri = raptor_new_uri(raptor_xmlschema_datatypes_namespace_uri); dateTime_uri = raptor_new_uri_from_uri_local_name(xsd_uri, (const unsigned char*)"dateTime"); rasqal_init_datatypes(); fn_args = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_literal, (raptor_sequence_print_handler*)rasqal_literal_print); l1 = rasqal_new_string_literal((unsigned char*)strdup("2004-05-04"), NULL, raptor_uri_copy(dateTime_uri), NULL); raptor_sequence_push(fn_args, l1); l2 = rasqal_new_string_literal((unsigned char*)strdup("2003-01-02"), NULL, raptor_uri_copy(dateTime_uri), NULL); raptor_sequence_push(fn_args, l2); fn_i = 0; fn_name = rasqal_xsd_datatype_fns[fn_i].name; fn = rasqal_xsd_datatype_fns[fn_i].fn; fn_uri = rasqal_xsd_datatype_fns[fn_i].uri; error = NULL; fn_result = fn(fn_uri, fn_args, &error); raptor_free_sequence(fn_args); if(!fn_result) { if(error) fprintf(stderr, "function %s failed with error %s\n", fn_name, error); else fprintf(stderr, "function %s unknown error\n", fn_name); } else { fprintf(stderr, "function %s returned result: ", fn_name); rasqal_literal_print(fn_result, stderr); fputc('\n', stderr); } if(fn_result) rasqal_free_literal(fn_result); rasqal_finish_datatypes(); raptor_free_uri(xsd_uri); raptor_free_uri(dateTime_uri); rasqal_free_world(world); #endif return 0; }