/** * rasqal_variable_write: * @v: the #rasqal_variable object * @iostr: the #raptor_iostream handle to write to * * Write a Rasqal variable to an iostream in a debug format. * * The write debug format may change in any release. * **/ void rasqal_variable_write(rasqal_variable* v, raptor_iostream* iostr) { if(!v || !iostr) return; if(v->type == RASQAL_VARIABLE_TYPE_ANONYMOUS) raptor_iostream_counted_string_write("anon-variable(", 14, iostr); else raptor_iostream_counted_string_write("variable(", 9, iostr); raptor_iostream_string_write(v->name, iostr); if(v->expression) { raptor_iostream_write_byte('=', iostr); rasqal_expression_write(v->expression, iostr); } if(v->value) { raptor_iostream_write_byte('=', iostr); rasqal_literal_write(v->value, iostr); } #ifdef RASQAL_DEBUG_VARIABLE_USAGE raptor_iostream_write_byte('[', iostr); raptor_iostream_decimal_write(v->usage, iostr); raptor_iostream_write_byte(']', iostr); #endif raptor_iostream_write_byte(')', iostr); }
static void emit_mime_type_q(unsigned char q, raptor_iostream* iostr) { if(q < 10) { raptor_iostream_string_write("q 0.", iostr); raptor_iostream_decimal_write((int)q, iostr); } else raptor_iostream_string_write("q 1.0", iostr); }
/* * Return value: non-0 if equal */ static int compare_query_results_compare(compare_query_results* cqr) { int differences = 0; int i; int rowi; int size1; int size2; int row_differences_count = 0; size1 = rasqal_query_results_get_bindings_count(cqr->qr1); size2 = rasqal_query_results_get_bindings_count(cqr->qr2); if(size1 != size2) { cqr->message.level = RAPTOR_LOG_LEVEL_ERROR; cqr->message.text = "Results have different numbers of bindings"; if(cqr->log_handler) cqr->log_handler(cqr->log_user_data, &cqr->message); differences++; goto done; } /* check variables in each results project the same variables */ for(i = 0; 1; i++) { const unsigned char* v1; const unsigned char* v2; v1 = rasqal_query_results_get_binding_name(cqr->qr1, i); v2 = rasqal_query_results_get_binding_name(cqr->qr2, i); if(!v1 && !v2) break; if(v1 && v2) { if(strcmp((const char*)v1, (const char*)v2)) { /* different names */ differences++; } } else /* one is NULL, the other is a name */ differences++; } if(differences) { cqr->message.level = RAPTOR_LOG_LEVEL_ERROR; cqr->message.text = "Results have different binding names"; if(cqr->log_handler) cqr->log_handler(cqr->log_user_data, &cqr->message); goto done; } /* set results to be stored? */ /* sort rows by something ? As long as the sort is the same it * probably does not matter what the method is. */ /* what to do about blank nodes? */ /* for each row */ for(rowi = 0; 1; rowi++) { int bindingi; rasqal_row* row1 = rasqal_query_results_get_row_by_offset(cqr->qr1, rowi); rasqal_row* row2 = rasqal_query_results_get_row_by_offset(cqr->qr2, rowi); int this_row_different = 0; if(!row1 && !row2) break; /* for each variable in row1 (== same variables in row2) */ for(bindingi = 0; bindingi < size1; bindingi++) { /* we know the binding names are the same */ const unsigned char* name; rasqal_literal *value1; rasqal_literal *value2; int error = 0; name = rasqal_query_results_get_binding_name(cqr->qr1, bindingi); value1 = rasqal_query_results_get_binding_value(cqr->qr1, bindingi); value2 = rasqal_query_results_get_binding_value(cqr->qr2, bindingi); /* should have compare as native flag? * RASQAL_COMPARE_XQUERY doesn't compare all values */ if(!rasqal_literal_equals_flags(value1, value2, RASQAL_COMPARE_XQUERY, &error)) { /* if different report it */ raptor_world* raptor_world_ptr; void *string; size_t length; raptor_iostream* string_iostr; raptor_world_ptr = rasqal_world_get_raptor(cqr->world); string_iostr = raptor_new_iostream_to_string(raptor_world_ptr, &string, &length, (raptor_data_malloc_handler)malloc); raptor_iostream_counted_string_write("Difference in row ", 18, string_iostr); raptor_iostream_decimal_write(rowi + 1, string_iostr); raptor_iostream_counted_string_write(" binding '", 10, string_iostr); raptor_iostream_string_write(name, string_iostr); raptor_iostream_counted_string_write("' ", 2, string_iostr); raptor_iostream_string_write(cqr->qr1_label, string_iostr); raptor_iostream_counted_string_write(" value ", 7, string_iostr); rasqal_literal_write(value1, string_iostr); raptor_iostream_write_byte(' ', string_iostr); raptor_iostream_string_write(cqr->qr2_label, string_iostr); raptor_iostream_counted_string_write(" value ", 7, string_iostr); rasqal_literal_write(value2, string_iostr); raptor_iostream_write_byte(' ', string_iostr); /* this allocates and copies result into 'string' */ raptor_free_iostream(string_iostr); cqr->message.level = RAPTOR_LOG_LEVEL_ERROR; cqr->message.text = (const char*)string; if(cqr->log_handler) cqr->log_handler(cqr->log_user_data, &cqr->message); free(string); differences++; this_row_different = 1; } } /* end for each var */ if(this_row_different) row_differences_count++; rasqal_query_results_next(cqr->qr1); rasqal_query_results_next(cqr->qr2); } /* end for each row */ if(row_differences_count) { cqr->message.level = RAPTOR_LOG_LEVEL_ERROR; cqr->message.text = "Results have different values"; if(cqr->log_handler) cqr->log_handler(cqr->log_user_data, &cqr->message); } done: return (differences == 0); }
/* * rasqal_query_results_write_sv: * @iostr: #raptor_iostream to write the query to * @results: #rasqal_query_results query results format * @base_uri: #raptor_uri base URI of the output format * @label: name of this format for errors * @sep: column sep character * * INTERNAL - Write a @sep-separated values version of the query results format to an iostream. * * If the writing succeeds, the query results will be exhausted. * * Return value: non-0 on failure **/ static int rasqal_query_results_write_sv(raptor_iostream *iostr, rasqal_query_results* results, raptor_uri *base_uri, const char* label, char sep) { rasqal_query* query = rasqal_query_results_get_query(results); int i; int count = 1; #define empty_value_str_len 0 static const char empty_value_str[empty_value_str_len+1] = ""; #define nl_str_len 1 static const char nl_str[nl_str_len+1] = "\n"; int vars_count; if(!rasqal_query_results_is_bindings(results)) { rasqal_log_error_simple(query->world, RAPTOR_LOG_LEVEL_ERROR, &query->locator, "Can only write %s format for variable binding results", label); return 1; } /* Header */ raptor_iostream_counted_string_write("Result", 6, iostr); for(i = 0; 1; i++) { const unsigned char *name; name = rasqal_query_results_get_binding_name(results, i); if(!name) break; raptor_iostream_write_byte(sep, iostr); raptor_iostream_string_write(name, iostr); } raptor_iostream_counted_string_write(nl_str, nl_str_len, iostr); /* Variable Binding Results */ vars_count = rasqal_query_results_get_bindings_count(results); while(!rasqal_query_results_finished(results)) { /* Result row */ raptor_iostream_decimal_write(count++, iostr); for(i = 0; i < vars_count; i++) { rasqal_literal *l = rasqal_query_results_get_binding_value(results, i); raptor_iostream_write_byte(sep, iostr); if(!l) { if(empty_value_str_len) raptor_iostream_counted_string_write(empty_value_str, empty_value_str_len, iostr); } else switch(l->type) { const unsigned char* str; size_t len; case RASQAL_LITERAL_URI: raptor_iostream_string_write("uri(", iostr); str = (const unsigned char*)raptor_uri_as_counted_string(l->value.uri, &len); raptor_string_ntriples_write(str, len, '"', iostr); raptor_iostream_write_byte(')', iostr); break; case RASQAL_LITERAL_BLANK: raptor_iostream_string_write("blank(", iostr); raptor_string_ntriples_write(l->string, l->string_len, '"', iostr); raptor_iostream_write_byte(')', iostr); break; case RASQAL_LITERAL_STRING: if(l->datatype && l->valid) { rasqal_literal_type ltype; ltype = rasqal_xsd_datatype_uri_to_type(l->world, l->datatype); if(ltype >= RASQAL_LITERAL_INTEGER && ltype <= RASQAL_LITERAL_DECIMAL) { /* write integer, float, double and decimal XSD typed * data without quotes, datatype or language */ raptor_string_ntriples_write(l->string, l->string_len, '\0', iostr); break; } } raptor_iostream_write_byte('"', iostr); raptor_string_ntriples_write(l->string, l->string_len, '"', iostr); raptor_iostream_write_byte('"', iostr); if(l->language) { raptor_iostream_write_byte('@', iostr); raptor_iostream_string_write((const unsigned char*)l->language, iostr); } if(l->datatype) { raptor_iostream_string_write("^^uri(", iostr); str = (const unsigned char*)raptor_uri_as_counted_string(l->datatype, &len); raptor_string_ntriples_write(str, len, '"', iostr); raptor_iostream_write_byte(')', iostr); } break; case RASQAL_LITERAL_PATTERN: case RASQAL_LITERAL_QNAME: case RASQAL_LITERAL_INTEGER: case RASQAL_LITERAL_XSD_STRING: case RASQAL_LITERAL_BOOLEAN: 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: case RASQAL_LITERAL_UNKNOWN: default: rasqal_log_error_simple(query->world, RAPTOR_LOG_LEVEL_ERROR, &query->locator, "Cannot turn literal type %d into %s", l->type, label); } /* End Binding */ } /* End Result Row */ raptor_iostream_counted_string_write(nl_str, nl_str_len, iostr); rasqal_query_results_next(results); } /* end sparql */ return 0; }
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; }
static void rasqal_query_write_sparql_literal(sparql_writer_context *wc, raptor_iostream* iostr, rasqal_literal* l) { if(!l) { raptor_iostream_counted_string_write("null", 4, iostr); return; } switch(l->type) { case RASQAL_LITERAL_URI: rasqal_query_write_sparql_uri(wc, iostr, l->value.uri); break; case RASQAL_LITERAL_BLANK: raptor_iostream_counted_string_write("_:", 2, iostr); raptor_iostream_string_write(l->string, iostr); break; case RASQAL_LITERAL_STRING: raptor_iostream_write_byte('"', iostr); raptor_string_ntriples_write(l->string, l->string_len, '"', iostr); raptor_iostream_write_byte('"', iostr); if(l->language) { raptor_iostream_write_byte('@', iostr); raptor_iostream_string_write(l->language, iostr); } if(l->datatype) { raptor_iostream_counted_string_write("^^", 2, iostr); rasqal_query_write_sparql_uri(wc, iostr, l->datatype); } break; case RASQAL_LITERAL_QNAME: raptor_iostream_counted_string_write("QNAME(", 6, iostr); raptor_iostream_counted_string_write(l->string, l->string_len, iostr); raptor_iostream_write_byte(')', iostr); break; case RASQAL_LITERAL_INTEGER: raptor_iostream_decimal_write(l->value.integer, iostr); break; case RASQAL_LITERAL_BOOLEAN: case RASQAL_LITERAL_DOUBLE: case RASQAL_LITERAL_FLOAT: case RASQAL_LITERAL_DECIMAL: raptor_iostream_counted_string_write(l->string, l->string_len, iostr); break; case RASQAL_LITERAL_VARIABLE: rasqal_query_write_sparql_variable(wc, iostr, l->value.variable); break; case RASQAL_LITERAL_DATETIME: case RASQAL_LITERAL_XSD_STRING: case RASQAL_LITERAL_UDT: case RASQAL_LITERAL_INTEGER_SUBTYPE: if(1) { raptor_uri* dt_uri; raptor_iostream_write_byte('"', iostr); raptor_string_ntriples_write(l->string, l->string_len, '"', iostr); raptor_iostream_counted_string_write("\"^^", 3, iostr); if(l->type == RASQAL_LITERAL_UDT) dt_uri = l->datatype; else dt_uri = rasqal_xsd_datatype_type_to_uri(l->world, l->type); rasqal_query_write_sparql_uri(wc, iostr, dt_uri); } break; case RASQAL_LITERAL_UNKNOWN: case RASQAL_LITERAL_PATTERN: default: RASQAL_FATAL2("Literal type %d cannot be written as a SPARQL literal", l->type); } }