示例#1
0
/*
 * rasqal_query_results_write_json1:
 * @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
 *
 * Write a JSON version of the query results format to an
 * iostream in a format - INTERNAL.
 * 
 * If the writing succeeds, the query results will be exhausted.
 * 
 * Return value: non-0 on failure
 **/
static int
rasqal_query_results_write_json1(rasqal_query_results_formatter* formatter,
                                 raptor_iostream *iostr,
                                 rasqal_query_results* results,
                                 raptor_uri *base_uri)
{
  rasqal_world* world = rasqal_query_results_get_world(results);
  rasqal_query* query = rasqal_query_results_get_query(results);
  int i;
  int row_comma;
  int column_comma = 0;
  rasqal_query_results_type type;

  type = rasqal_query_results_get_type(results);

  if(type != RASQAL_QUERY_RESULTS_BINDINGS &&
     type != RASQAL_QUERY_RESULTS_BOOLEAN) {
    rasqal_log_error_simple(query->world, RAPTOR_LOG_LEVEL_ERROR,
                            &query->locator,
                            "Cannot write JSON for %s query result format",
                            rasqal_query_results_type_label(type));
    return 1;
  }

  raptor_iostream_counted_string_write("{\n", 2, iostr);
  
  /* Header */
  raptor_iostream_counted_string_write("  \"head\": {\n", 12, iostr);
  
  if(rasqal_query_results_is_bindings(results)) {
    raptor_iostream_counted_string_write("    \"vars\": [ ", 14, iostr);
    for(i = 0; 1; i++) {
      const unsigned char *name;
      
      name = rasqal_query_results_get_binding_name(results, i);
      if(!name)
        break;
      
      /*     'x', */
      if(i > 0)
        raptor_iostream_counted_string_write(", ", 2, iostr);
      raptor_iostream_write_byte('\"', iostr);
      raptor_iostream_string_write(name, iostr);
      raptor_iostream_write_byte('\"', iostr);
    }
    raptor_iostream_counted_string_write(" ]\n", 3, iostr);
  }

  /* FIXME - could add link inside 'head': */
    
  /*   End Header */
  raptor_iostream_counted_string_write("  },\n", 5, iostr);


  /* Boolean Results */
  if(rasqal_query_results_is_boolean(results)) {
    raptor_iostream_counted_string_write("  ", 2, iostr);
    rasqal_iostream_write_json_boolean(iostr, "boolean", 
                                       rasqal_query_results_get_boolean(results));
    goto results3done;
  }

  /* Variable Binding Results */
  raptor_iostream_counted_string_write("  \"results\": {\n", 15, iostr);

  if(query) {
    raptor_iostream_counted_string_write("    ", 4, iostr);
    rasqal_iostream_write_json_boolean(iostr, "ordered", 
                                       (rasqal_query_get_order_condition(query, 0) != NULL));
    raptor_iostream_counted_string_write(",\n", 2, iostr);

    raptor_iostream_counted_string_write("    ", 4, iostr);
    rasqal_iostream_write_json_boolean(iostr, "distinct", 
                                       rasqal_query_get_distinct(query));
    raptor_iostream_counted_string_write(",\n", 2, iostr);
  }
  
  raptor_iostream_counted_string_write("    \"bindings\" : [\n", 19, iostr);

  row_comma = 0;
  while(!rasqal_query_results_finished(results)) {
    if(row_comma)
      raptor_iostream_counted_string_write(",\n", 2, iostr);

    /* Result row */
    raptor_iostream_counted_string_write("      {\n", 8, iostr);

    column_comma = 0;
    for(i = 0; i<rasqal_query_results_get_bindings_count(results); i++) {
      const unsigned char *name = rasqal_query_results_get_binding_name(results, i);
      rasqal_literal *l = rasqal_query_results_get_binding_value(results, i);

      if(column_comma)
        raptor_iostream_counted_string_write(",\n", 2, iostr);

      /*       <binding> */
      raptor_iostream_counted_string_write("        \"", 9, iostr);
      raptor_iostream_string_write(name, iostr);
      raptor_iostream_counted_string_write("\" : { ", 6, iostr);

      if(!l) {
        raptor_iostream_string_write("\"type\": \"unbound\", \"value\": null", iostr);
      } else {
        const unsigned char* str;
        size_t len;

        switch(l->type) {
          case RASQAL_LITERAL_URI:
            raptor_iostream_string_write("\"type\": \"uri\", \"value\": \"", iostr);
            str = RASQAL_GOOD_CAST(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("\"type\": \"bnode\", \"value\": \"", iostr);
            raptor_string_ntriples_write(l->string, l->string_len, '"', iostr);
            raptor_iostream_write_byte('"', iostr);
            break;

          case RASQAL_LITERAL_STRING:
            raptor_iostream_string_write("\"type\": \"literal\", \"value\": \"", iostr);
            raptor_string_ntriples_write(l->string, l->string_len, '"', iostr);
            raptor_iostream_write_byte('"', iostr);

            if(l->language) {
              raptor_iostream_string_write(",\n      \"xml:lang\" : \"", iostr);
              raptor_iostream_string_write(RASQAL_GOOD_CAST(const unsigned char*, l->language), iostr);
              raptor_iostream_write_byte('"', iostr);
            }

            if(l->datatype) {
              raptor_iostream_string_write(",\n      \"datatype\" : \"", 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(world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                                    "Cannot turn literal type %u into XML",
                                    l->type);
        }
      }

      /* End Binding */
      raptor_iostream_counted_string_write(" }", 2, iostr);
      column_comma = 1;
    }
示例#2
0
文件: roqet.c 项目: sengels/rasqal
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);
}