Example #1
0
//NewSerializer construcs a new serializer based on a name defining the type, a mimeType and optional URI
func NewSerializer(world *World, name string, mimeType string, uri *Uri) (*Serializer, error) {

	serializer := Serializer{}

	var uriPtr *C.librdf_uri
	uriPtr = nil

	if uriPtr != nil {
		uriPtr = uri.librdf_uri
	}

	serializer.librdf_serializer = C.librdf_new_serializer(world.librdf_world, C.CString(name), C.CString(mimeType), uriPtr)

	runtime.SetFinalizer(&serializer, (*Serializer).Free)

	return &serializer, nil
}
Example #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
}