/** * rasqal_query_results_rewind: * @query_results: #rasqal_query_results query_results * * Rewind stored query results to start * * This requires rasqal_query_set_store_results() to be called before * query execution. * * Return value: non-0 if rewinding is not available when results are not stored **/ int rasqal_query_results_rewind(rasqal_query_results* query_results) { int size; int limit = -1; int offset = -1; rasqal_query* query; RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(query_results, rasqal_query_results, 1); if(!query_results->results_sequence) return 1; size = raptor_sequence_size(query_results->results_sequence); /* This may be NULL for a static query result */ query = query_results->query; if(query) { /* If the query failed, it remains failed */ if(query->failed) return 1; limit = rasqal_query_get_limit(query); offset = rasqal_query_get_offset(query); } /* reset to first result */ query_results->finished = (size == 0); if(query && !limit) query_results->finished = 1; if(!query_results->finished) { /* Reset to first result, index-1 into sequence of results */ query_results->result_count = 0; /* skip past any OFFSET */ if(query && offset > 0) { query_results->result_count += offset; if(query_results->result_count >= size) query_results->finished = 1; } } if(query_results->finished) query_results->result_count = 0; else { if(query && query->constructs) rasqal_query_results_update_bindings(query_results); } return 0; }
/** * rasqal_query_check_limit_offset: * @query_results: query results object * @result_offset: offset to check * * INTERNAL - Check the query result count is in the limit and offset range if any. * * Return value: before range -1, in range 0, after range 1 */ int rasqal_query_check_limit_offset(rasqal_query* query, int result_offset) { int limit; int offset; if(!query) return 0; if(result_offset < 0) return -1; limit = rasqal_query_get_limit(query); /* Ensure ASK queries never do more than one result */ if(query->verb == RASQAL_QUERY_VERB_ASK) limit = 1; offset = rasqal_query_get_offset(query); if(offset > 0) { /* offset */ if(result_offset <= offset) return -1; if(limit >= 0) { /* offset and limit */ if(result_offset > (offset + limit)) { return 1; } } } else if(limit >= 0) { /* limit */ if(result_offset > limit) { return 1; } } return 0; }
/** * rasqal_query_results_get_row_by_offset: * @query_results: query result * @result_offset: index into result rows * * Get stored result row by an offset * * The result_offset index is 0-indexed into the subset of results * constrained by any query limit and offset. * * Return value: row or NULL if @result_offset is out of range */ rasqal_row* rasqal_query_results_get_row_by_offset(rasqal_query_results* query_results, int result_offset) { rasqal_query* query; int check; rasqal_row* row; int offset = 0; RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(query_results, rasqal_query_results, NULL); if(!query_results->results_sequence) return NULL; if(result_offset < 0) return NULL; query = query_results->query; if(query) offset = rasqal_query_get_offset(query); /* Adjust 0-indexed to query results 1-indexed + query result offset */ result_offset += 1 + offset; check = rasqal_query_check_limit_offset(query_results->query, result_offset); /* outside limit/offset range in some way */ if(check < 0 || check > 0) return NULL; row = (rasqal_row*)raptor_sequence_get_at(query_results->results_sequence, result_offset - 1); if(row) { row = rasqal_new_row_from_row(row); /* stored results may not be canonicalized yet - do it lazily */ rasqal_row_to_nodes(row); } return row; }
/** * rasqal_query_results_get_count: * @query_results: #rasqal_query_results query_results * * Get number of bindings so far. * * Return value: number of bindings found so far or < 0 on failure **/ int rasqal_query_results_get_count(rasqal_query_results* query_results) { rasqal_query* query; int offset; RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(query_results, rasqal_query_results, -1); if(query_results->failed) return -1; if(!rasqal_query_results_is_bindings(query_results)) return -1; query = query_results->query; offset = rasqal_query_get_offset(query); if(query && offset > 0) return query_results->result_count - offset; else return query_results->result_count; }
static void roqet_query_walk(rasqal_query *rq, FILE *fh, int indent) { rasqal_query_verb verb; int i; rasqal_graph_pattern* gp; raptor_sequence *seq; verb = rasqal_query_get_verb(rq); roqet_write_indent(fh, indent); fprintf(fh, "query verb: %s\n", rasqal_query_verb_as_string(verb)); i = rasqal_query_get_distinct(rq); if(i != 0) { roqet_write_indent(fh, indent); fprintf(fh, "query asks for distinct results\n"); } i = rasqal_query_get_limit(rq); if(i >= 0) { roqet_write_indent(fh, indent); fprintf(fh, "query asks for result limits %d\n", i); } i = rasqal_query_get_offset(rq); if(i >= 0) { roqet_write_indent(fh, indent); fprintf(fh, "query asks for result offset %d\n", i); } seq = rasqal_query_get_bound_variable_sequence(rq); if(seq && raptor_sequence_size(seq) > 0) { fprintf(fh, "query bound variables (%d): ", raptor_sequence_size(seq)); i = 0; while(1) { rasqal_variable* v = (rasqal_variable*)raptor_sequence_get_at(seq, i); if(!v) break; if(i > 0) fputs(", ", fh); roqet_query_write_variable(fh, v); i++; } fputc('\n', fh); } gp = rasqal_query_get_query_graph_pattern(rq); if(!gp) return; seq = rasqal_query_get_construct_triples_sequence(rq); if(seq && raptor_sequence_size(seq) > 0) { roqet_write_indent(fh, indent); fprintf(fh, "query construct triples (%d) {\n", raptor_sequence_size(seq)); i = 0; while(1) { rasqal_triple* t = rasqal_query_get_construct_triple(rq, i); if(!t) break; roqet_write_indent(fh, indent + 2); fprintf(fh, "triple #%d { ", i); rasqal_triple_print(t, fh); fputs(" }\n", fh); i++; } roqet_write_indent(fh, indent); fputs("}\n", fh); } /* look for binding rows */ seq = rasqal_query_get_bindings_variables_sequence(rq); if(seq) { roqet_write_indent(fh, indent); fprintf(fh, "bindings variables (%d): ", raptor_sequence_size(seq)); i = 0; while(1) { rasqal_variable* v = rasqal_query_get_bindings_variable(rq, i); if(!v) break; if(i > 0) fputs(", ", fh); roqet_query_write_variable(fh, v); i++; } fputc('\n', fh); seq = rasqal_query_get_bindings_rows_sequence(rq); fprintf(fh, "bindings rows (%d) {\n", raptor_sequence_size(seq)); i = 0; while(1) { rasqal_row* row; row = rasqal_query_get_bindings_row(rq, i); if(!row) break; roqet_write_indent(fh, indent + 2); fprintf(fh, "row #%d { ", i); rasqal_row_print(row, fh); fputs("}\n", fh); i++; } } fputs("query ", fh); roqet_graph_pattern_walk(gp, -1, fh, indent); }
static int librdf_query_rasqal_get_offset(librdf_query* query) { librdf_query_rasqal_context *context=(librdf_query_rasqal_context*)query->context; return rasqal_query_get_offset(context->rq); }
int rasqal_query_write_sparql_20060406(raptor_iostream *iostr, rasqal_query* query, raptor_uri *base_uri) { int i; sparql_writer_context wc; int limit, offset; rasqal_query_verb verb; wc.world = query->world; wc.base_uri = NULL; wc.type_uri = raptor_new_uri_for_rdf_concept(query->world->raptor_world_ptr, (const unsigned char*)"type"); wc.nstack = raptor_new_namespaces(query->world->raptor_world_ptr, 1); if(base_uri) { raptor_iostream_counted_string_write("BASE ", 5, iostr); rasqal_query_write_sparql_uri(&wc, iostr, base_uri); raptor_iostream_write_byte('\n', iostr); /* from now on all URIs are relative to this */ wc.base_uri = raptor_uri_copy(base_uri); } for(i = 0; 1 ; i++) { raptor_namespace *nspace; rasqal_prefix* p = rasqal_query_get_prefix(query, i); if(!p) break; raptor_iostream_counted_string_write("PREFIX ", 7, iostr); if(p->prefix) raptor_iostream_string_write(p->prefix, iostr); raptor_iostream_counted_string_write(": ", 2, iostr); rasqal_query_write_sparql_uri(&wc, iostr, p->uri); raptor_iostream_write_byte('\n', iostr); /* Use this constructor so we copy a URI directly */ nspace = raptor_new_namespace_from_uri(wc.nstack, p->prefix, p->uri, i); raptor_namespaces_start_namespace(wc.nstack, nspace); } if(query->explain) raptor_iostream_counted_string_write("EXPLAIN ", 8, iostr); verb = query->verb; /* These terms are deprecated */ if(query->verb == RASQAL_QUERY_VERB_INSERT || query->verb == RASQAL_QUERY_VERB_DELETE) { verb = RASQAL_QUERY_VERB_UPDATE; } /* Write SPARQL 1.1 (Draft) Update forms */ if(verb == RASQAL_QUERY_VERB_UPDATE) { rasqal_update_operation* update; /* Write SPARQL Update */ for(i = 0; (update = rasqal_query_get_update_operation(query, i)); i++) { int is_always_2_args = (update->type >= RASQAL_UPDATE_TYPE_ADD && update->type <= RASQAL_UPDATE_TYPE_COPY); if(update->type == RASQAL_UPDATE_TYPE_UPDATE) { /* update operations: * WITH ... INSERT { template } DELETE { template } WHERE { template } * INSERT/DELETE { template } WHERE { template } * INSERT/DELETE DATA { triples } */ if(update->graph_uri) { raptor_iostream_counted_string_write("WITH ", 5, iostr); rasqal_query_write_sparql_uri(&wc, iostr, update->graph_uri); raptor_iostream_write_byte('\n', iostr); } if(update->delete_templates) { raptor_iostream_counted_string_write("DELETE ", 7, iostr); if(update->flags & RASQAL_UPDATE_FLAGS_DATA) raptor_iostream_counted_string_write("DATA ", 5, iostr); rasqal_query_write_sparql_triple_data(&wc, iostr, update->delete_templates, 0); raptor_iostream_write_byte('\n', iostr); } if(update->insert_templates) { raptor_iostream_counted_string_write("INSERT ", 7, iostr); if(update->flags & RASQAL_UPDATE_FLAGS_DATA) raptor_iostream_counted_string_write("DATA ", 5, iostr); rasqal_query_write_sparql_triple_data(&wc, iostr, update->insert_templates, 0); raptor_iostream_write_byte('\n', iostr); } if(update->where) { raptor_iostream_counted_string_write("WHERE ", 6, iostr); rasqal_query_write_sparql_graph_pattern(&wc, iostr, update->where, -1, 0); raptor_iostream_write_byte('\n', iostr); } } else { /* admin operations: * CLEAR GRAPH graph-uri | DEFAULT | NAMED | ALL * CREATE (SILENT) GRAPH graph-uri | DEFAULT | NAMED | ALL * DROP (SILENT) GRAPH graph-uri * LOAD (SILENT) doc-uri / LOAD (SILENT) doc-uri INTO GRAPH graph-uri * ADD (SILENT) GraphOrDefault TO GraphOrDefault * MOVE (SILENT) GraphOrDefault TO GraphOrDefault * COPY (SILENT) GraphOrDefault TO GraphOrDefault */ raptor_iostream_string_write(rasqal_update_type_label(update->type), iostr); if(update->flags & RASQAL_UPDATE_FLAGS_SILENT) raptor_iostream_counted_string_write(" SILENT", 7, iostr); if(is_always_2_args) { /* ADD, MOVE, COPY are always 2-arg admin operations */ rasqal_query_write_graphref(&wc, iostr, update->graph_uri, RASQAL_UPDATE_GRAPH_ONE); raptor_iostream_counted_string_write(" TO", 3, iostr); rasqal_query_write_graphref(&wc, iostr, update->document_uri, RASQAL_UPDATE_GRAPH_ONE); } else if(update->type == RASQAL_UPDATE_TYPE_LOAD) { /* LOAD is 1 or 2 URIs and first one never has a GRAPH prefix */ raptor_iostream_write_byte(' ', iostr); rasqal_query_write_sparql_uri(&wc, iostr, update->document_uri); if(update->graph_uri) { raptor_iostream_counted_string_write(" INTO", 5, iostr); rasqal_query_write_graphref(&wc, iostr, update->graph_uri, RASQAL_UPDATE_GRAPH_ONE); } } else { /* everything else is defined by update->applies; only * CLEAR and DROP may apply to >1 graph */ rasqal_query_write_graphref(&wc, iostr, update->graph_uri, update->applies); } raptor_iostream_write_byte('\n', iostr); } } goto tidy; } if(verb != RASQAL_QUERY_VERB_CONSTRUCT) raptor_iostream_string_write(rasqal_query_verb_as_string(query->verb), iostr); if(query->distinct) { if(query->distinct == 1) raptor_iostream_counted_string_write(" DISTINCT", 9, iostr); else raptor_iostream_counted_string_write(" REDUCED", 8, iostr); } if(query->wildcard) raptor_iostream_counted_string_write(" *", 2, iostr); else if(verb == RASQAL_QUERY_VERB_DESCRIBE) { raptor_sequence *lit_seq = query->describes; int count = raptor_sequence_size(lit_seq); for(i = 0; i < count; i++) { rasqal_literal* l = (rasqal_literal*)raptor_sequence_get_at(lit_seq, i); raptor_iostream_write_byte(' ', iostr); rasqal_query_write_sparql_literal(&wc, iostr, l); } } else if(verb == RASQAL_QUERY_VERB_SELECT) { rasqal_query_write_sparql_select(&wc, iostr, query->selects); } raptor_iostream_write_byte('\n', iostr); if(query->data_graphs) { for(i = 0; 1; i++) { rasqal_data_graph* dg = rasqal_query_get_data_graph(query, i); if(!dg) break; if(dg->flags & RASQAL_DATA_GRAPH_NAMED) continue; rasqal_query_write_data_format_comment(&wc, iostr, dg); raptor_iostream_counted_string_write("FROM ", 5, iostr); rasqal_query_write_sparql_uri(&wc, iostr, dg->uri); raptor_iostream_counted_string_write("\n", 1, iostr); } for(i = 0; 1; i++) { rasqal_data_graph* dg = rasqal_query_get_data_graph(query, i); if(!dg) break; if(!(dg->flags & RASQAL_DATA_GRAPH_NAMED)) continue; rasqal_query_write_data_format_comment(&wc, iostr, dg); raptor_iostream_counted_string_write("FROM NAMED ", 11, iostr); rasqal_query_write_sparql_uri(&wc, iostr, dg->name_uri); raptor_iostream_write_byte('\n', iostr); } } if(query->constructs) { raptor_iostream_string_write("CONSTRUCT {\n", iostr); for(i = 0; 1; i++) { rasqal_triple* t = rasqal_query_get_construct_triple(query, i); if(!t) break; raptor_iostream_counted_string_write(" ", 2, iostr); rasqal_query_write_sparql_triple(&wc, iostr, t); raptor_iostream_write_byte('\n', iostr); } raptor_iostream_counted_string_write("}\n", 2, iostr); } if(query->query_graph_pattern) { raptor_iostream_counted_string_write("WHERE ", 6, iostr); rasqal_query_write_sparql_graph_pattern(&wc, iostr, query->query_graph_pattern, -1, 0); raptor_iostream_write_byte('\n', iostr); } if(rasqal_query_get_group_conditions_sequence(query)) { raptor_iostream_counted_string_write("GROUP BY ", 9, iostr); for(i = 0; 1; i++) { rasqal_expression* expr = rasqal_query_get_group_condition(query, i); if(!expr) break; if(i > 0) raptor_iostream_write_byte(' ', iostr); rasqal_query_write_sparql_expression(&wc, iostr, expr); } raptor_iostream_write_byte('\n', iostr); } if(rasqal_query_get_having_conditions_sequence(query)) { raptor_iostream_counted_string_write("HAVING ", 7, iostr); for(i = 0; 1; i++) { rasqal_expression* expr = rasqal_query_get_having_condition(query, i); if(!expr) break; if(i > 0) raptor_iostream_write_byte(' ', iostr); rasqal_query_write_sparql_expression(&wc, iostr, expr); } raptor_iostream_write_byte('\n', iostr); } if(rasqal_query_get_order_conditions_sequence(query)) { raptor_iostream_counted_string_write("ORDER BY ", 9, iostr); for(i = 0; 1; i++) { rasqal_expression* expr = rasqal_query_get_order_condition(query, i); if(!expr) break; if(i > 0) raptor_iostream_write_byte(' ', iostr); rasqal_query_write_sparql_expression(&wc, iostr, expr); } raptor_iostream_write_byte('\n', iostr); } limit = rasqal_query_get_limit(query); offset = rasqal_query_get_offset(query); if(limit >= 0 || offset >= 0) { if(limit >= 0) { raptor_iostream_counted_string_write("LIMIT ", 7, iostr); raptor_iostream_decimal_write(limit, iostr); } if(offset >= 0) { if(limit) raptor_iostream_write_byte(' ', iostr); raptor_iostream_counted_string_write("OFFSET ", 8, iostr); raptor_iostream_decimal_write(offset, iostr); } raptor_iostream_write_byte('\n', iostr); } if(query->bindings) rasqal_write_sparql_bindings(&wc, iostr, query->bindings); tidy: raptor_free_uri(wc.type_uri); if(wc.base_uri) raptor_free_uri(wc.base_uri); raptor_free_namespaces(wc.nstack); return 0; }