/* * 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_xsd_is_datatype_uri(rasqal_world* world, raptor_uri* uri) { return (rasqal_xsd_datatype_uri_to_type(world, uri) != RASQAL_LITERAL_UNKNOWN); }
/* * 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 * @csv_escape: non-0 if values are written escaped with CSV rules, else turtle * @variable_prefix: char to print before a variable name or NUL * @eol_str: end of line string * @eol_str_len: length of @eol_str * * 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, const char sep, int csv_escape, const char variable_prefix, const char* eol_str, size_t eol_str_len) { rasqal_query* query = rasqal_query_results_get_query(results); int i; 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 */ for(i = 0; 1; i++) { const unsigned char *name; name = rasqal_query_results_get_binding_name(results, i); if(!name) break; if(i > 0) raptor_iostream_write_byte(sep, iostr); if(variable_prefix) raptor_iostream_write_byte(variable_prefix, iostr); raptor_iostream_string_write(name, iostr); } raptor_iostream_counted_string_write(eol_str, eol_str_len, iostr); /* Variable Binding Results */ vars_count = rasqal_query_results_get_bindings_count(results); while(!rasqal_query_results_finished(results)) { /* Result row */ for(i = 0; i < vars_count; i++) { rasqal_literal *l = rasqal_query_results_get_binding_value(results, i); if(i > 0) raptor_iostream_write_byte(sep, iostr); if(l) { const unsigned char* str; size_t len; switch(l->type) { case RASQAL_LITERAL_URI: str = RASQAL_GOOD_CAST(const unsigned char*, raptor_uri_as_counted_string(l->value.uri, &len)); if(csv_escape) rasqal_iostream_write_csv_string(str, len, iostr); else { raptor_iostream_write_byte('<', iostr); if(str && len > 0) raptor_string_ntriples_write(str, len, '"', iostr); raptor_iostream_write_byte('>', iostr); } break; case RASQAL_LITERAL_BLANK: raptor_bnodeid_ntriples_write(l->string, l->string_len, iostr); break; case RASQAL_LITERAL_STRING: if(csv_escape) { rasqal_iostream_write_csv_string(l->string, l->string_len, iostr); } else { 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(RASQAL_GOOD_CAST(const unsigned char*, l->language), iostr); } if(l->datatype) { raptor_iostream_string_write("^^<", iostr); str = RASQAL_GOOD_CAST(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_DATE: 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 */ }