Example #1
0
rasqal_row_compatible*
rasqal_new_row_compatible(rasqal_variables_table* vt,
                          rasqal_rowsource *first_rowsource,
                          rasqal_rowsource *second_rowsource)
{
  rasqal_row_compatible* map = NULL;
  int count = rasqal_variables_table_get_total_variables_count(vt);
  int i;
  
  map = RASQAL_CALLOC(rasqal_row_compatible*, 1, sizeof(*map));
  if(!map)
    return NULL;
  map->variables_table = vt;
  map->first_rowsource = first_rowsource;
  map->second_rowsource = second_rowsource;
  map->variables_count = count;
  map->defined_in_map = RASQAL_CALLOC(int*, RASQAL_GOOD_CAST(size_t, 2 * count), sizeof(int));
  if(!map->defined_in_map) {
    RASQAL_FREE(rasqal_row_compatible, map);
    return NULL;
  }

  for(i = 0; i < count; i++) {
    rasqal_variable *v;
    int offset1;
    int offset2;

    v = rasqal_variables_table_get(vt, i);
    offset1 = rasqal_rowsource_get_variable_offset_by_name(first_rowsource,
                                                           v->name);
    offset2 = rasqal_rowsource_get_variable_offset_by_name(second_rowsource,
                                                           v->name);
    map->defined_in_map[i<<1] = offset1;
    map->defined_in_map[1 + (i<<1)] = offset2;
    if(offset1 >= 0 && offset2 >= 0)
      map->variables_in_both_rows_count++;
  }
  
  return map;
}
Example #2
0
/**
 * rasqal_rowsource_add_variable:
 * @rowsource: rasqal rowsource
 * @v: variable
 *
 * Add a variable to the rowsource if the variable is not already present
 *
 * Return value: variable offset or < 0 on failure
 **/
int
rasqal_rowsource_add_variable(rasqal_rowsource *rowsource, rasqal_variable* v)
{
    int offset;

    offset = rasqal_rowsource_get_variable_offset_by_name(rowsource, v->name);
    if(offset >= 0)
        return offset;

    v = rasqal_new_variable_from_variable(v);
    if(raptor_sequence_push(rowsource->variables_sequence, v))
        return -1;

    if(rowsource->size < 0)
        rowsource->size = 0;

    offset = rowsource->size;

    rowsource->size++;

    return offset;
}
Example #3
0
static void
rasqal_sparql_xml_sax2_start_element_handler(void *user_data,
                                             raptor_xml_element *xml_element)
{
  rasqal_rowsource_sparql_xml_context* con;
  int i;
  raptor_qname* name;
  rasqal_sparql_xml_read_state state=STATE_unknown;
  int attr_count;

  con=(rasqal_rowsource_sparql_xml_context*)user_data;

  name=raptor_xml_element_get_name(xml_element);

  for(i=STATE_first; i <= STATE_last; i++) {
    if(!strcmp((const char*)raptor_qname_get_local_name(name),
               sparql_xml_element_names[i])) {
      state=(rasqal_sparql_xml_read_state)i;
      con->state=state;
    }
  }

  if(state == STATE_unknown) {
    fprintf(stderr, "UNKNOWN element %s\n", raptor_qname_get_local_name(name));
    con->failed++;
  }

#ifdef TRACE_XML
  if(con->trace) {
    pad(stderr, con->depth);
    fprintf(stderr, "Element %s (%d)\n", raptor_qname_get_local_name(name),
            state);
  }
#endif
  
  attr_count=raptor_xml_element_get_attributes_count(xml_element);
  con->name=NULL;
  con->datatype=NULL;
  con->language=NULL;
  
  if(attr_count > 0) {
    raptor_qname** attrs=raptor_xml_element_get_attributes(xml_element);
    for(i=0; i < attr_count; i++) {
#ifdef TRACE_XML
      if(con->trace) {
        pad(stderr, con->depth+1);
        fprintf(stderr, "Attribute %s='%s'\n",
                raptor_qname_get_local_name(attrs[i]),
                raptor_qname_get_value(attrs[i]));
      }
#endif
      if(!strcmp((const char*)raptor_qname_get_local_name(attrs[i]),
                 "name"))
        con->name=(const char*)raptor_qname_get_counted_value(attrs[i],
                                                             &con->name_length);
      else if(!strcmp((const char*)raptor_qname_get_local_name(attrs[i]),
                      "datatype"))
        con->datatype=(const char*)raptor_qname_get_value(attrs[i]);
    }
  }
  if(raptor_xml_element_get_language(xml_element)) {
    con->language=(const char*)raptor_xml_element_get_language(xml_element);
#ifdef TRACE_XML
    if(con->trace) {
      pad(stderr, con->depth+1);
      fprintf(stderr, "xml:lang '%s'\n", con->language);
    }
#endif
  }

  switch(state) {
    case STATE_variable:
      if(con->name) {
        unsigned char* var_name;
        rasqal_variable *v;
        
        var_name = (unsigned char*)RASQAL_MALLOC(cstring, con->name_length+1);
        memcpy(var_name, con->name, con->name_length + 1);

        v = rasqal_variables_table_add(con->vars_table,
                                       RASQAL_VARIABLE_TYPE_NORMAL,
                                       var_name, NULL);
        if(v)
          rasqal_rowsource_add_variable(con->rowsource, v);
      }
      break;
      
    case STATE_result:
      if(1) {
        con->row = rasqal_new_row(con->rowsource);
        RASQAL_DEBUG2("Made new row %d\n", con->offset);
        con->offset++;
      }
      break;
      
    case STATE_binding:
      con->result_offset = rasqal_rowsource_get_variable_offset_by_name(con->rowsource, (const unsigned char*)con->name);
      break;
      
    case STATE_sparql:
    case STATE_head:
    case STATE_results:
    case STATE_literal:
    case STATE_bnode:
    case STATE_uri:
    case STATE_unknown:
    default:
      break;
  }
  
  con->depth++;
}