Esempio n. 1
0
//FindStatements creates a channel used to iterate the set of statements in the model that matched the given partial statement
//	bufferSize indicates how many statements can be on the channel at one time
func (model *Model) FindStatements(partialStatement *Statement, bufferSize int) chan *Statement {
	chanStatement := make(chan *Statement, bufferSize)

	go func() {
		stream := C.librdf_model_find_statements(model.librdf_model, partialStatement.librdf_statement)
		var librdfStatement *C.librdf_statement
		var statement *Statement
		var err error
		var endOfStream C.int

		if stream == nil {
			panic(errors.New("librdf returned null stream"))
		} else {
			endOfStream = C.librdf_stream_end(stream)
			for endOfStream == 0 {

				if librdfStatement = C.librdf_stream_get_object(stream); librdfStatement == nil {
					panic(errors.New("librdf returned null statement"))
				}

				if statement, err = NewStatement(model.world); err != nil {
					panic(errors.New("Unable to create statement"))
				}
				statement.librdf_statement = librdfStatement
				chanStatement <- statement

				C.librdf_stream_next(stream)
				endOfStream = C.librdf_stream_end(stream)
			}
			C.librdf_free_stream(stream)
		}

		close(chanStatement)
	}()

	return chanStatement
}
Esempio n. 2
0
//ExecuteQueryToFormattedString executes a query and serializes the results to a string in the format provided
func (model *Model) ExecuteQueryToFormattedString(query *Query, format string) (string, error) {
	var librdf_query *C.librdf_query

	cQueryString := C.CString(query.queryString)
	if cQueryString != nil {
		defer C.free(unsafe.Pointer(cQueryString))
	}

	cName := C.CString(query.name)
	if cName != nil {
		defer C.free(unsafe.Pointer(cName))
	}

	cFormat := C.CString(format)
	if cFormat != nil {
		defer C.free(unsafe.Pointer(cFormat))
	}

	librdf_query = C.librdf_new_query(query.world.librdf_world, (*C.char)(unsafe.Pointer(cName)), nil, (*C.uchar)(unsafe.Pointer(cQueryString)), nil)

	if librdf_query == nil {
		return "", errors.New("Unable to create query for execution")
	}

	results := C.librdf_model_query_execute(model.librdf_model, librdf_query)

	if results == nil {
		return "", errors.New("Failed to execute query")
	}

	var cFormattedString *C.uchar
	isBindings := C.librdf_query_results_is_bindings(results)
	isBoolean := C.librdf_query_results_is_boolean(results)

	if isBindings == 0 || isBoolean == 0 {
		cFormattedString = C.librdf_query_results_to_string2(results, (*C.char)(unsafe.Pointer(cFormat)), nil, nil, nil)
	} else {
		var serializer *C.librdf_serializer
		if serializer = C.librdf_new_serializer(query.world.librdf_world, (*C.char)(unsafe.Pointer(cFormat)), nil, nil); serializer == nil {
			return "", errors.New("Failed to build serializer")
		}
		defer C.librdf_free_serializer(serializer)

		var stream *C.librdf_stream
		if stream = C.librdf_query_results_as_stream(results); stream == nil {
			return "", errors.New("Failed to build stream from results")
		}
		defer C.librdf_free_stream(stream)

		cFormattedString = C.librdf_serializer_serialize_stream_to_string(serializer, nil, stream)
	}

	if cFormattedString == nil {
		return "", errors.New("Failed to build formatted string from results")
	}
	defer C.free(unsafe.Pointer(cFormattedString))

	formattedString := C.GoString((*C.char)(unsafe.Pointer(cFormattedString)))

	return formattedString, nil
}