static rasqal_row* rasqal_new_row_common(rasqal_world* world, int size, int order_size) { rasqal_row* row; row = (rasqal_row*)RASQAL_CALLOC(rasqal_row, 1, sizeof(rasqal_row)); if(!row) return NULL; row->usage = 1; row->size = size; row->order_size = order_size; row->values = (rasqal_literal**)RASQAL_CALLOC(array, row->size, sizeof(rasqal_literal*)); if(!row->values) { rasqal_free_row(row); return NULL; } if(row->order_size > 0) { row->order_values = (rasqal_literal**)RASQAL_CALLOC(array, row->order_size, sizeof(rasqal_literal*)); if(!row->order_values) { rasqal_free_row(row); return NULL; } } row->group_id = -1; return row; }
static void* rasqal_builtin_agg_expression_execute_init(rasqal_world *world, rasqal_expression* expr) { rasqal_builtin_agg_expression_execute* b; b = (rasqal_builtin_agg_expression_execute*)RASQAL_CALLOC(rasqal_builtin_agg_expression_execute, sizeof(*b), 1); if(!b) return NULL; b->expr = expr; b->world = world; b->l = NULL; b->count = 0; b->error = 0; if(expr->op == RASQAL_EXPR_GROUP_CONCAT) { b->sb = raptor_new_stringbuffer(); if(!b) { rasqal_builtin_agg_expression_execute_finish(b); return NULL; } b->separator[0] = (unsigned char)' '; b->separator[1] = (unsigned char)'\0'; } return b; }
/** * rasqal_new_solution_modifier: * @query: rasqal query * @order_conditions: sequence of order condition expressions (or NULL) * @group_conditions: sequence of group by condition expressions (or NULL) * @having_conditions: sequence of (group by ...) having condition expressions (or NULL) * @limit: result limit LIMIT (>=0) or <0 if not given * @offset: result offset OFFSET (>=0) or <0 if not given * * INTERNAL - Create a new solution modifier object. * * Return value: a new #rasqal_solution_modifier object or NULL on failure **/ rasqal_solution_modifier* rasqal_new_solution_modifier(rasqal_query* query, raptor_sequence* order_conditions, raptor_sequence* group_conditions, raptor_sequence* having_conditions, int limit, int offset) { rasqal_solution_modifier* sm; RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(query, rasqal_query, NULL); sm = (rasqal_solution_modifier*)RASQAL_CALLOC(rasqal_solution_modifier, 1, sizeof(*sm)); if(!sm) return NULL; sm->query = query; sm->order_conditions = order_conditions; sm->group_conditions = group_conditions; sm->having_conditions = having_conditions; sm->limit = limit; sm->offset = offset; return sm; }
/** * rasqal_new_graph_rowsource: * @world: world object * @query: query object * @rowsource: input rowsource * @var: graph variable * * INTERNAL - create a new GRAPH rowsource that binds a variable * * The @rowsource becomes owned by the new rowsource * * Return value: new rowsource or NULL on failure */ rasqal_rowsource* rasqal_new_graph_rowsource(rasqal_world *world, rasqal_query *query, rasqal_rowsource* rowsource, rasqal_variable *var) { rasqal_graph_rowsource_context *con; int flags = 0; if(!world || !query || !rowsource || !var) return NULL; con = (rasqal_graph_rowsource_context*)RASQAL_CALLOC(rasqal_graph_rowsource_context, 1, sizeof(rasqal_graph_rowsource_context)); if(!con) return NULL; con->rowsource = rowsource; con->var = var; return rasqal_new_rowsource_from_handler(world, query, con, &rasqal_graph_rowsource_handler, query->vars_table, flags); }
int rasqal_query_results_format_register_factory(rasqal_world* world, const char *name, const char *label, const unsigned char* uri_string, rasqal_query_results_formatter_func writer, rasqal_query_results_formatter_func reader, rasqal_query_results_get_rowsource_func get_rowsource, const char *mime_type) { rasqal_query_results_format_factory* factory; factory = (rasqal_query_results_format_factory*)RASQAL_CALLOC(query_results_format_factory, 1, sizeof(*factory)); if(!factory) { rasqal_log_error_simple(world, RAPTOR_LOG_LEVEL_FATAL, NULL, "Out of memory in rasqal_query_results_format_register_factory()"); return 1; } factory->name = name; factory->label = label; factory->uri_string = uri_string; factory->writer = writer; factory->reader = reader; factory->get_rowsource = get_rowsource; factory->mime_type = mime_type; return raptor_sequence_push(world->query_results_formats, factory); }
/** * rasqal_new_variable_from_variable: * @v: #rasqal_variable to copy * * Copy Constructor - Create a new Rasqal variable from an existing one * * This does a deep copy of all variable fields * * Return value: a new #rasqal_variable or NULL on failure. **/ rasqal_variable* rasqal_new_variable_from_variable(rasqal_variable* v) { rasqal_variable* new_v; size_t name_len; unsigned char *new_name; new_v = (rasqal_variable*)RASQAL_CALLOC(rasqal_variable, 1, sizeof(rasqal_variable)); if(!new_v) return NULL; name_len = strlen((const char*)v->name); new_name = (unsigned char*)RASQAL_MALLOC(cstring, name_len+1); if(!new_name) { RASQAL_FREE(rasqal_variable, new_v); return NULL; } memcpy(new_name, v->name, name_len+1); new_v->vars_table = v->vars_table; new_v->name= new_name; new_v->value= rasqal_new_literal_from_literal(v->value); new_v->offset= v->offset; new_v->type= v->type; new_v->expression= rasqal_new_expression_from_expression(v->expression); return new_v; }
/** * rasqal_new_query_results_formatter2: * @world: rasqal_world object * @name: the query results format name (or NULL) * @mime_type: the query results format mime type (or NULL) * @format_uri: #raptor_uri query results format uri (or NULL) * * Constructor - create a new rasqal_query_results_formatter for an identified format. * * A query results format can be found by name, mime type or URI, all * of which are optional. If multiple fields are given, the first * match is given that matches the name, URI, mime_type in that * order. The default query results format will be used if all are * format identifying fields are NULL. * * rasqal_query_results_formats_enumerate() returns information on * the known query results names, labels, mime types and URIs. * * Return value: a new #rasqal_query_results_formatter object or NULL on failure */ rasqal_query_results_formatter* rasqal_new_query_results_formatter2(rasqal_world* world, const char *name, const char *mime_type, raptor_uri* format_uri) { rasqal_query_results_format_factory* factory; rasqal_query_results_formatter* formatter; int flags = 0; RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL); factory = rasqal_get_query_results_formatter_factory(world, name, format_uri, mime_type, flags); if(!factory) return NULL; formatter = (rasqal_query_results_formatter*)RASQAL_CALLOC(rasqal_query_results_formatter, 1, sizeof(*formatter)); if(!formatter) return NULL; formatter->factory = factory; formatter->mime_type = factory->mime_type; return formatter; }
rasqal_rowsource* rasqal_new_distinct_rowsource(rasqal_world *world, rasqal_query *query, rasqal_rowsource* rowsource) { rasqal_distinct_rowsource_context *con; int flags = 0; if(!world || !query || !rowsource) goto fail; con = (rasqal_distinct_rowsource_context*)RASQAL_CALLOC(rasqal_distinct_rowsource_context, 1, sizeof(rasqal_distinct_rowsource_context)); if(!con) goto fail; con->rowsource = rowsource; return rasqal_new_rowsource_from_handler(world, query, con, &rasqal_distinct_rowsource_handler, query->vars_table, flags); fail: if(rowsource) rasqal_free_rowsource(rowsource); return NULL; }
int rasqal_xsd_init(rasqal_world* world) { int i; world->xsd_namespace_uri = raptor_new_uri(world->raptor_world_ptr, raptor_xmlschema_datatypes_namespace_uri); if(!world->xsd_namespace_uri) return 1; world->xsd_datatype_uris = (raptor_uri**)RASQAL_CALLOC(ptrarray, SPARQL_XSD_NAMES_COUNT + 1, sizeof(raptor_uri*)); if(!world->xsd_datatype_uris) return 1; for(i = RASQAL_LITERAL_FIRST_XSD; i < SPARQL_XSD_NAMES_COUNT; i++) { const unsigned char* name = (const unsigned char*)sparql_xsd_names[i]; world->xsd_datatype_uris[i] = raptor_new_uri_from_uri_local_name(world->raptor_world_ptr, world->xsd_namespace_uri, name); if(!world->xsd_datatype_uris[i]) return 1; } return 0; }
static rasqal_data_graph* rasqal_new_data_graph_common(rasqal_world* world, raptor_uri* uri, raptor_iostream* iostr, raptor_uri* base_uri, raptor_uri* name_uri, int flags, const char* format_type, const char* format_name, raptor_uri* format_uri) { rasqal_data_graph* dg; dg = (rasqal_data_graph*)RASQAL_CALLOC(rasqal_data_graph, 1, sizeof(*dg)); if(dg) { dg->world = world; dg->usage = 1; if(iostr) dg->iostr = iostr; else if(uri) dg->uri = raptor_uri_copy(uri); if(name_uri) dg->name_uri = raptor_uri_copy(name_uri); dg->flags = flags; if(format_type) { size_t len = strlen(format_type); dg->format_type = RASQAL_MALLOC(string, len + 1); if(!dg->format_type) goto error; memcpy(dg->format_type, format_type, len + 1); } if(format_name) { size_t len = strlen(format_name); dg->format_name = RASQAL_MALLOC(string, len + 1); if(!dg->format_name) goto error; memcpy(dg->format_name, format_name, len + 1); } if(format_uri) dg->format_uri = raptor_uri_copy(format_uri); if(base_uri) dg->base_uri = raptor_uri_copy(base_uri); } return dg; error: rasqal_free_data_graph(dg); return NULL; }
/** * rasqal_new_service: * @world: rasqal_world object * @service_uri: sparql protocol service URI * @query_string: query string (or NULL) * @data_graphs: sequence of #rasqal_data_graph graphs for service * * Constructor - create a new rasqal protocol service object. * * Create a structure to execute a sparql protocol service at * @service_uri running the query @query_string and returning * a sparql result set. * * All arguments are copied by the service object * * Return value: a new #rasqal_query object or NULL on failure */ rasqal_service* rasqal_new_service(rasqal_world* world, raptor_uri* service_uri, const char* query_string, raptor_sequence* data_graphs) { rasqal_service* svc; size_t len = 0; RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL); RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(service_uri, raptor_uri, NULL); svc = (rasqal_service*)RASQAL_CALLOC(rasqal_service, 1, sizeof(*svc)); if(!svc) return NULL; svc->world = world; svc->service_uri = raptor_uri_copy(service_uri); if(query_string) { len = strlen(query_string); svc->query_string = RASQAL_MALLOC(cstring, len + 1); if(!svc->query_string) { rasqal_free_service(svc); return NULL; } memcpy(svc->query_string, query_string, len + 1); } svc->query_string_len = len; if(data_graphs) { int i; rasqal_data_graph* dg; #ifdef HAVE_RAPTOR2_API svc->data_graphs = raptor_new_sequence((raptor_data_free_handler)rasqal_free_data_graph, NULL); #else svc->data_graphs = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_data_graph, NULL); #endif if(!svc->data_graphs) { rasqal_free_service(svc); return NULL; } for(i = 0; (dg = (rasqal_data_graph*)raptor_sequence_get_at(data_graphs, i)); i++) { raptor_sequence_push(svc->data_graphs, rasqal_new_data_graph_from_data_graph(dg)); } } return svc; }
/** * rasqal_new_rowsource_from_handler: * @query: query object * @user_data: pointer to context information to pass in to calls * @handler: pointer to handler methods * @vars_table: variables table to use for rows * @flags: 0 (none defined so far) * * Create a new rowsource over a user-defined handler. * * Return value: new #rasqal_rowsource object or NULL on failure **/ rasqal_rowsource* rasqal_new_rowsource_from_handler(rasqal_world* world, rasqal_query* query, void *user_data, const rasqal_rowsource_handler *handler, rasqal_variables_table* vars_table, int flags) { rasqal_rowsource* rowsource; if(!handler) return NULL; if(handler->version < 1 || handler->version > 1) return NULL; rowsource = (rasqal_rowsource*)RASQAL_CALLOC(rasqal_rowsource, 1, sizeof(rasqal_rowsource)); if(!rowsource) { if(handler->finish) handler->finish(NULL, user_data); return NULL; } rowsource->world = world; rowsource->query = query; rowsource->user_data = (void*)user_data; rowsource->handler = handler; rowsource->flags = flags; rowsource->size = -1; rowsource->generate_group = 0; if(vars_table) rowsource->vars_table = rasqal_new_variables_table_from_variables_table(vars_table); else rowsource->vars_table = NULL; rowsource->variables_sequence = raptor_new_sequence((raptor_data_free_handler)rasqal_free_variable, (raptor_data_print_handler)rasqal_variable_print); if(!rowsource->variables_sequence) { rasqal_free_rowsource(rowsource); return NULL; } if(rowsource->handler->init && rowsource->handler->init(rowsource, rowsource->user_data)) { RASQAL_DEBUG2("rowsource %s init failed\n", rowsource->handler->name); rasqal_free_rowsource(rowsource); return NULL; } return rowsource; }
/** * rasqal_new_triple_from_triple: * @t: Triple to copy. * * Copy constructor - create a new #rasqal_triple from an existing one. * * Return value: a new #rasqal_triple or NULL on failure. **/ rasqal_triple* rasqal_new_triple_from_triple(rasqal_triple* t) { rasqal_triple* newt; newt = (rasqal_triple*)RASQAL_CALLOC(rasqal_triple, 1, sizeof(*newt)); if(newt) { newt->subject = rasqal_new_literal_from_literal(t->subject); newt->predicate = rasqal_new_literal_from_literal(t->predicate); newt->object = rasqal_new_literal_from_literal(t->object); } return newt; }
static raptor_avltree_iterator* raptor_new_avltree_iterator(raptor_avltree* tree, void* range, raptor_data_free_handler range_free_handler, int direction) { raptor_avltree_iterator* iterator; iterator = (raptor_avltree_iterator*)RASQAL_CALLOC(avltree_iterator, sizeof(*iterator), 1); iterator->tree = tree; iterator->index = 0; return iterator; }
rasqal_formula* rasqal_new_formula(rasqal_world* world) { rasqal_formula* f; RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL); f = (rasqal_formula*)RASQAL_CALLOC(rasqal_formula, 1, sizeof(*f)); if(!f) return NULL; f->world = world; return f; }
/** * rasqal_row_set_order_size: * @row: Result row * @order_size: number of order conditions * * INTERNAL - Initialise the row with space to handle @order_size order conditions being evaluated * * Return value: non-0 on failure */ int rasqal_row_set_order_size(rasqal_row *row, int order_size) { row->order_size = order_size; if(row->order_size > 0) { row->order_values = (rasqal_literal**)RASQAL_CALLOC(array, row->order_size, sizeof(rasqal_literal*)); if(!row->order_values) { row->order_size = -1; return 1; } } return 0; }
static raptor_avltree* raptor_new_avltree(raptor_data_compare_handler compare_handler, raptor_data_free_handler free_handler, unsigned int flags) { raptor_avltree* tree; tree = (raptor_avltree*)RASQAL_CALLOC(raptor_avltree, sizeof(*tree), 1); if(!tree) return NULL; tree->compare_handler = compare_handler; tree->free_handler = free_handler; tree->seq = raptor_new_sequence(free_handler, NULL); return tree; }
/** * rasqal_new_variables_table: * @world: rasqal world * * Constructor - create a new variables table * * Return value: new variables table or NULL On failure */ rasqal_variables_table* rasqal_new_variables_table(rasqal_world* world) { rasqal_variables_table* vt; vt = (rasqal_variables_table*)RASQAL_CALLOC(rasqal_variables_table, 1, sizeof(rasqal_variables_table)); if(!vt) return NULL; vt->usage = 1; vt->world = world; #ifdef HAVE_RAPTOR2_API vt->variables_sequence = raptor_new_sequence((raptor_data_free_handler)rasqal_free_variable, (raptor_data_print_handler)rasqal_variable_print); #else vt->variables_sequence = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_variable, (raptor_sequence_print_handler*)rasqal_variable_print); #endif if(!vt->variables_sequence) goto tidy; #ifdef HAVE_RAPTOR2_API vt->anon_variables_sequence = raptor_new_sequence((raptor_data_free_handler)rasqal_free_variable, (raptor_data_print_handler)rasqal_variable_print); #else vt->anon_variables_sequence = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_variable, (raptor_sequence_print_handler*)rasqal_variable_print); #endif if(!vt->anon_variables_sequence) goto tidy; vt->variable_names = NULL; return vt; tidy: rasqal_free_variables_table(vt); vt = NULL; return vt; }
rasqal_rowsource* rasqal_new_groupby_rowsource(rasqal_world *world, rasqal_query* query, rasqal_rowsource* rowsource, raptor_sequence* expr_seq) { rasqal_groupby_rowsource_context* con; int flags = 0; if(!world || !query) return NULL; con = (rasqal_groupby_rowsource_context*)RASQAL_CALLOC(rasqal_groupby_rowsource_context, 1, sizeof(*con)); if(!con) goto fail; con->rowsource = rowsource; con->expr_seq_size = 0; if(expr_seq) { con->expr_seq = rasqal_expression_copy_expression_sequence(expr_seq); if(!con->expr_seq) goto fail; con->expr_seq_size = raptor_sequence_size(expr_seq); } return rasqal_new_rowsource_from_handler(world, query, con, &rasqal_groupby_rowsource_handler, query->vars_table, flags); fail: if(rowsource) rasqal_free_rowsource(rowsource); if(expr_seq) raptor_free_sequence(expr_seq); return NULL; }
/** * rasqal_row_expand_size: * @row: Result row * @size: number of variables * * INTERNAL - Expand the row to be able to handle @size variables * * Return value: non-0 on failure */ int rasqal_row_expand_size(rasqal_row *row, int size) { rasqal_literal** nvalues; /* do not allow row size to contract & lose data */ if(row->size > size) return 1; nvalues = (rasqal_literal**)RASQAL_CALLOC(array, size, sizeof(rasqal_literal*)); if(!nvalues) return 1; memcpy(nvalues, row->values, sizeof(rasqal_literal*) * row->size); RASQAL_FREE(array, row->values); row->values = nvalues; row->size = size; return 0; }
/* * rasqal_query_language_register_factory: * @factory: pointer to function to call to register the factory * * INTERNAL - Register a query language syntax handled by a query factory * * Return value: new factory or NULL on failure **/ RASQAL_EXTERN_C rasqal_query_language_factory* rasqal_query_language_register_factory(rasqal_world *world, int (*factory) (rasqal_query_language_factory*)) { rasqal_query_language_factory *query = NULL; query = (rasqal_query_language_factory*)RASQAL_CALLOC(rasqal_query_language_factory, 1, sizeof(*query)); if(!query) goto tidy; query->world = world; if(raptor_sequence_push(world->query_languages, query)) return NULL; /* on error, query is already freed by the sequence */ /* Call the query registration function on the new object */ if(factory(query)) return NULL; /* query is owned and freed by the query_languages sequence */ if(raptor_syntax_description_validate(&query->desc)) { rasqal_log_error_simple(world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Query language format description failed to validate\n"); goto tidy; } #if defined(RASQAL_DEBUG) && RASQAL_DEBUG > 1 RASQAL_DEBUG3("Registered query language %s with context size %d\n", query->names[0], query->context_length); #endif return query; /* Clean up on failure */ tidy: if(query) rasqal_free_query_language_factory(query); return NULL; }
/* * rasqal_query_results_getrowsource_sparql_xml: * @world: rasqal world object * @iostr: #raptor_iostream to read the query results from * @base_uri: #raptor_uri base URI of the input format * * Read the fourth version of the SPARQL XML query results format from an * iostream in a format returning a rwosurce - INTERNAL. * * Return value: a new rasqal_rowsource or NULL on failure **/ static rasqal_rowsource* rasqal_query_results_get_rowsource_sparql_xml(rasqal_query_results_formatter* formatter, rasqal_world *world, rasqal_variables_table* vars_table, raptor_iostream *iostr, raptor_uri *base_uri) { rasqal_rowsource_sparql_xml_context* con; con=(rasqal_rowsource_sparql_xml_context*)RASQAL_CALLOC(rasqal_rowsource_sparql_xml_context, 1, sizeof(rasqal_rowsource_sparql_xml_context)); if(!con) return NULL; con->world=world; con->base_uri = base_uri ? raptor_uri_copy(base_uri) : NULL; con->iostr=iostr; con->locator.uri=base_uri; con->sax2 = raptor_new_sax2(world->raptor_world_ptr, &con->locator, con); if(!con->sax2) return NULL; raptor_sax2_set_start_element_handler(con->sax2, rasqal_sparql_xml_sax2_start_element_handler); raptor_sax2_set_characters_handler(con->sax2, rasqal_sparql_xml_sax2_characters_handler); raptor_sax2_set_end_element_handler(con->sax2, rasqal_sparql_xml_sax2_end_element_handler); con->results_sequence = raptor_new_sequence((raptor_data_free_handler)rasqal_free_row, (raptor_data_print_handler)rasqal_row_print); con->vars_table = rasqal_new_variables_table_from_variables_table(vars_table); return rasqal_new_rowsource_from_handler(world, NULL, con, &rasqal_rowsource_sparql_xml_handler, con->vars_table, 0); }
rasqal_triples_match* rasqal_new_triples_match(rasqal_query* query, rasqal_triples_source* triples_source, rasqal_triple_meta *m, rasqal_triple *t) { rasqal_triples_match* rtm; if(!triples_source) return NULL; rtm = (rasqal_triples_match *)RASQAL_CALLOC(rasqal_triples_match, 1, sizeof(rasqal_triples_match)); if(rtm) { rtm->world = query->world; /* exact if there are no variables in the triple parts */ rtm->is_exact = 1; if(rasqal_literal_as_variable(t->predicate) || rasqal_literal_as_variable(t->subject) || rasqal_literal_as_variable(t->object)) rtm->is_exact = 0; if(rtm->is_exact) { if(!triples_source->triple_present(triples_source, triples_source->user_data, t)) { rasqal_free_triples_match(rtm); rtm = NULL; } } else { if(triples_source->init_triples_match(rtm, triples_source, triples_source->user_data, m, t)) { rasqal_free_triples_match(rtm); rtm = NULL; } } } return rtm; }
/** * rasqal_new_query_results: * @world: rasqal world object * @query: query object (or NULL) * @type: query results (expected) type * @vars_table: variables table * * Create a new query results set * * The @query may be NULL for result set objects that are standalone * and not attached to any particular query * * Return value: a new query result object or NULL on failure **/ rasqal_query_results* rasqal_new_query_results(rasqal_world* world, rasqal_query* query, rasqal_query_results_type type, rasqal_variables_table* vars_table) { rasqal_query_results* query_results; RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL); RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(vars_table, rasqal_variables_table, NULL); query_results = (rasqal_query_results*)RASQAL_CALLOC(rasqal_query_results, 1, sizeof(*query_results)); if(!query_results) return NULL; query_results->world = world; query_results->type = type; query_results->finished = 0; query_results->executed = 0; query_results->failed = 0; query_results->query = query; query_results->result_count = 0; query_results->execution_data = NULL; query_results->row = NULL; query_results->ask_result = -1; query_results->store_results = 0; query_results->current_triple_result = -1; /* initialize static query_results->result_triple */ raptor_statement_init(&query_results->result_triple, world->raptor_world_ptr); query_results->triple = NULL; query_results->results_sequence = NULL; query_results->size = 0; query_results->vars_table = rasqal_new_variables_table_from_variables_table(vars_table); return query_results; }
/** * 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_query_results_execute_with_engine: * @query_results: the #rasqal_query_results object * @engine: execution factory * @store_results: non-0 to store query results * * INTERNAL - Create a new query results set executing a prepared query with the given execution engine * * return value: non-0 on failure **/ int rasqal_query_results_execute_with_engine(rasqal_query_results* query_results, const rasqal_query_execution_factory* engine, int store_results) { int rc = 0; size_t ex_data_size; rasqal_query* query; RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(query_results, rasqal_query_results, 1); query = query_results->query; if(query->failed) return 1; query_results->execution_factory = engine; /* set executed flag early to enable cleanup on error */ query_results->executed = 1; /* ensure stored results are present if ordering or distincting are being done */ query_results->store_results = (store_results || rasqal_query_get_order_conditions_sequence(query) || query->distinct); ex_data_size = query_results->execution_factory->execution_data_size; if(ex_data_size > 0) { query_results->execution_data = RASQAL_CALLOC(data, 1, ex_data_size); if(!query_results->execution_data) return 1; } else query_results->execution_data = NULL; /* Update the current datetime once per query execution */ rasqal_world_reset_now(query->world); if(query_results->execution_factory->execute_init) { rasqal_engine_error execution_error = RASQAL_ENGINE_OK; int execution_flags = 0; if(query_results->store_results) execution_flags |= 1; rc = query_results->execution_factory->execute_init(query_results->execution_data, query, query_results, execution_flags, &execution_error); if(rc || execution_error != RASQAL_ENGINE_OK) { query_results->failed = 1; return 1; } } #ifdef RASQAL_DEBUG RASQAL_DEBUG1("After execute_init, query is now:\n"); rasqal_query_print(query, stderr); #endif /* Choose either to execute all now and store OR do it on demand (lazy) */ if(query_results->store_results) rc = rasqal_query_results_execute_and_store_results(query_results); return rc; }
/** * rasqal_variables_table_add: * @vt: #rasqal_variables_table to associate the variable with * @type: variable type defined by enumeration rasqal_variable_type * @name: variable name * @value: variable #rasqal_literal value (or NULL) * * Constructor - Create a new variable and add it to the variables table * * The @name and @value become owned by the rasqal_variable structure * * Return value: a new #rasqal_variable or NULL on failure. **/ rasqal_variable* rasqal_variables_table_add(rasqal_variables_table* vt, rasqal_variable_type type, const unsigned char *name, rasqal_literal *value) { int i; rasqal_variable* v; raptor_sequence* seq = NULL; int* count_p = NULL; if(!vt) return NULL; switch(type) { case RASQAL_VARIABLE_TYPE_ANONYMOUS: seq = vt->anon_variables_sequence; count_p = &vt->anon_variables_count; break; case RASQAL_VARIABLE_TYPE_NORMAL: seq = vt->variables_sequence; count_p = &vt->variables_count; break; case RASQAL_VARIABLE_TYPE_UNKNOWN: default: RASQAL_DEBUG2("Unknown variable type %d", type); return NULL; } for(i = 0; i < raptor_sequence_size(seq); i++) { v = (rasqal_variable*)raptor_sequence_get_at(seq, i); if(!strcmp((const char*)v->name, (const char*)name)) { /* name already present, do not need a copy */ RASQAL_FREE(cstring, name); return v; } } v = (rasqal_variable*)RASQAL_CALLOC(rasqal_variable, 1, sizeof(rasqal_variable)); if(v) { v->vars_table = vt; v->type= type; v->name= name; v->value= value; if(count_p) v->offset= (*count_p); if(seq && raptor_sequence_push(seq, v)) return NULL; if(type == RASQAL_VARIABLE_TYPE_ANONYMOUS) { /* new anon variable: add base offset */ v->offset += vt->variables_count; } else { /* new normal variable: move all anon variable offsets up 1 */ for(i = 0; i < vt->anon_variables_count; i++) { rasqal_variable* anon_v; anon_v = (rasqal_variable*)raptor_sequence_get_at(vt->anon_variables_sequence, i); anon_v->offset++; } } /* Increment count and free var names only after sequence push succeeded */ if(count_p) (*count_p)++; if(vt->variable_names) { RASQAL_FREE(cstrings, vt->variable_names); vt->variable_names = NULL; } } else { RASQAL_FREE(cstring, name); if(value) rasqal_free_literal(value); } return v; }
/** * rasqal_new_triples_source: * @query: query * * INTERNAL - Create a new triples source * * Return value: a new triples source or NULL on failure */ rasqal_triples_source* rasqal_new_triples_source(rasqal_query* query) { rasqal_triples_source_factory* rtsf = &query->world->triples_source_factory; rasqal_triples_source* rts; int rc = 0; rts = (rasqal_triples_source*)RASQAL_CALLOC(rasqal_triples_source, 1, sizeof(rasqal_triples_source)); if(!rts) return NULL; rts->user_data = RASQAL_CALLOC(user_data, 1, rtsf->user_data_size); if(!rts->user_data) { RASQAL_FREE(rasqal_triples_source, rts); return NULL; } rts->query = query; if(rtsf->version >= 2 && rtsf->init_triples_source) { /* rasqal_triples_source_factory API V2 */ rc = rtsf->init_triples_source(query, rtsf->user_data, rts->user_data, rts, rasqal_triples_source_error_handler); /* if there is an error, it will have been already reported more * specifically via the error handler so no need to do it again * below in a generic form. */ goto error_tidy; } else /* rasqal_triples_source_factory API V1 */ rc = rtsf->new_triples_source(query, rtsf->user_data, rts->user_data, rts); /* Failure if the returned triples source API version is not in the * supported range */ if(!(rts->version >= RASQAL_TRIPLES_SOURCE_MIN_VERSION && rts->version <= RASQAL_TRIPLES_SOURCE_MAX_VERSION) ) { rasqal_log_error_simple(query->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create triples source - API %d not in range %d to %d", rts->version, RASQAL_TRIPLES_SOURCE_MIN_VERSION, RASQAL_TRIPLES_SOURCE_MAX_VERSION); rc = 1; } if(rc) { if(rc > 0) { rasqal_log_error_simple(query->world, RAPTOR_LOG_LEVEL_ERROR, &query->locator, "Failed to make triples source."); } else { rasqal_log_error_simple(query->world, RAPTOR_LOG_LEVEL_ERROR, &query->locator, "No data to query."); } } error_tidy: if(rc) { RASQAL_FREE(user_data, rts->user_data); RASQAL_FREE(rasqal_triples_source, rts); return NULL; } return rts; }
rasqal_rowsource* rasqal_new_aggregation_rowsource(rasqal_world *world, rasqal_query* query, rasqal_rowsource* rowsource, raptor_sequence* exprs_seq, raptor_sequence* vars_seq) { rasqal_aggregation_rowsource_context* con; int flags = 0; int size; int i; if(!world || !query || !rowsource || !exprs_seq || !vars_seq) goto fail; exprs_seq = rasqal_expression_copy_expression_sequence(exprs_seq); vars_seq = rasqal_variable_copy_variable_sequence(vars_seq); size = raptor_sequence_size(exprs_seq); if(size != raptor_sequence_size(vars_seq)) { RASQAL_DEBUG3("expressions sequence size %d does not match vars sequence size %d\n", size, raptor_sequence_size(vars_seq)); goto fail; } con = (rasqal_aggregation_rowsource_context*)RASQAL_CALLOC(rasqal_aggregation_rowsource_context, 1, sizeof(*con)); if(!con) goto fail; con->rowsource = rowsource; con->exprs_seq = exprs_seq; con->vars_seq = vars_seq; /* allocate per-expr data */ con->expr_count = size; con->expr_data = (rasqal_agg_expr_data*)RASQAL_CALLOC(rasqal_agg_expr_data, sizeof(rasqal_agg_expr_data), size); if(!con->expr_data) goto fail; /* Initialise per-expr data */ for(i = 0; i < size; i++) { rasqal_expression* expr = (rasqal_expression *)raptor_sequence_get_at(exprs_seq, i); rasqal_variable* variable = (rasqal_variable*)raptor_sequence_get_at(vars_seq, i); rasqal_agg_expr_data* expr_data = &con->expr_data[i]; expr_data->expr = rasqal_new_expression_from_expression(expr); expr_data->variable = variable; /* Prepare expression arguments sequence in per-expr data */ if(expr->args) { /* list of #rasqal_expression arguments already in expr * #RASQAL_EXPR_FUNCTION and #RASQAL_EXPR_GROUP_CONCAT */ expr_data->exprs_seq = rasqal_expression_copy_expression_sequence(expr->args); } else { /* single argument */ expr_data->exprs_seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_expression, (raptor_data_print_handler)rasqal_expression_print); raptor_sequence_push(expr_data->exprs_seq, rasqal_new_expression_from_expression(expr->arg1)); } } return rasqal_new_rowsource_from_handler(world, query, con, &rasqal_aggregation_rowsource_handler, query->vars_table, flags); fail: if(rowsource) rasqal_free_rowsource(rowsource); if(exprs_seq) raptor_free_sequence(exprs_seq); if(vars_seq) raptor_free_sequence(vars_seq); return NULL; }
static int rasqal_groupby_rowsource_process(rasqal_rowsource* rowsource, rasqal_groupby_rowsource_context* con) { /* already processed */ if(con->processed) return 0; con->processed = 1; /* Empty expression list - no need to read rows */ if(!con->expr_seq || !con->expr_seq_size) { con->group_id++; return 0; } con->tree = raptor_new_avltree(rasqal_rowsource_groupby_literal_sequence_compare, (raptor_data_free_handler)rasqal_free_groupby_tree_node, /* flags */ 0); if(!con->tree) return 1; raptor_avltree_set_print_handler(con->tree, rasqal_rowsource_groupby_tree_print_node); while(1) { rasqal_row* row; row = rasqal_rowsource_read_row(con->rowsource); if(!row) break; rasqal_row_bind_variables(row, rowsource->query->vars_table); if(con->expr_seq) { raptor_sequence* literal_seq; rasqal_groupby_tree_node key; rasqal_groupby_tree_node* node; literal_seq = rasqal_expression_sequence_evaluate(rowsource->query, con->expr_seq, /* ignore_errors */ 0, /* literal_seq */ NULL, /* error_p */ NULL); if(!literal_seq) { /* FIXME - what to do on errors? */ continue; } memset(&key, '\0', sizeof(key)); key.con = con; key.literals = literal_seq; node = (rasqal_groupby_tree_node*)raptor_avltree_search(con->tree, &key); if(!node) { /* New Group */ node = (rasqal_groupby_tree_node*)RASQAL_CALLOC(rasqal_groupby_tree_node, sizeof(*node), 1); if(!node) { raptor_free_sequence(literal_seq); return 1; } node->con = con; node->group_id = ++con->group_id; /* node now owns literal_seq */ node->literals = literal_seq; #ifdef HAVE_RAPTOR2_API node->rows = raptor_new_sequence((raptor_data_free_handler)rasqal_free_row, (raptor_data_print_handler)rasqal_row_print); #else node->rows = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_row, (raptor_sequence_print_handler*)rasqal_row_print); #endif if(!node->rows) { rasqal_free_groupby_tree_node(node); return 1; } /* after this, node is owned by con->tree */ raptor_avltree_add(con->tree, node); } else raptor_free_sequence(literal_seq); row->group_id = node->group_id; /* after this, node owns the row */ raptor_sequence_push(node->rows, row); } } #ifdef RASQAL_DEBUG if(con->tree) { fputs("Grouping ", DEBUG_FH); raptor_avltree_print(con->tree, DEBUG_FH); fputs("\n", DEBUG_FH); } #endif con->group_iterator = raptor_new_avltree_iterator(con->tree, NULL, NULL, 1); con->group_row_index = 0; con->offset = 0; return 0; }