Socket
Socket
Sign inDemoInstall

github.com/cambridge-blockchain/sparql

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/cambridge-blockchain/sparql


Version published
Created
Source

sparql

Go package that contains functions and data structures for querying SPARQL endpoints and parsing the response into RDF terms, as well as other convenience functions for working with SPARQL queries.

Interacting with SPARQL endpoints

This snippet creates a RDF repository instance to interact with a SPARQL endpoint running on localhost, set up with HTTP digest authentication and a timeout of 1.5s:

repo, err := sparql.NewRepo("http://localhost:8890/sparql-auth",
  sparql.DigestAuth("dba", "dba"),
  sparql.Timeout(time.Millisecond*1500),
)
if err != nil {
	log.Fatal(err)
}

Issue a SPARQL query to the repository:

res, err := repo.Query("SELECT * WHERE { ?s ?p ?o } LIMIT 1")
if err != nil {
	log.Fatal(err)
}

See also the section below on using a query bank.

Working with SPARQL result sets

The results returned by Query is a struct corresponding to the application/sparql-results+json-data as returned by the SPARQL endpoint. To further work with the result set in rdf.Term format you can call either of these two methods on the results, res being the result returned by Query:

  • res.Results.Bindings() -> map[string][]rdf.Term

    Bindings returns a map of the bound variables in the SPARQL response, where each variable points to one or more RDF terms.

  • res.Results.Solutions() -> []map[string]rdf.Term

    Solutions returns a slice of the query solutions, each containing a map of all bindings to RDF terms.

Query bank

The package includes a query bank implementation. Write all your query templates in string or in a separate file if you like, and tag each query with a name. You can then easily prepare queries by using the Prepare method along with an anonymous struct with variables to interpolate into the query.

Example usage:

const queries = `
# Comments are ignored, except those tagging a query.

# tag: my-query
SELECT *
WHERE {
  ?s ?p ?o
} LIMIT {{.Limit}} OFFSET {{.Offset}}
`

f := bytes.NewBufferString(queries)
bank := LoadBank(f)

q, err := bank.Prepare("my-query", struct{ Limit, Offset int }{10, 100})
if err != nil {
	log.Fatal(err)
}


fmt.Println(q)
// Will print: "SELECT * WHERE { ?s ?p ?o } LIMIT 10 OFFSET 100"

FAQs

Package last updated on 06 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc