sparql

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2018 License: BSD-3-Clause Imports: 12 Imported by: 0

README

pipeline status coverage report

Go SQL driver for SPARQL

SUPER EXPERIMENTAL

A SPARQL-Driver for Go. Including database/sql implementation.

Usage

See examples.

FAQ

Q: Can I use ? for placeholders? A: No. Please use $1, $2, $3, ... as placeholders.

Q: Which version's golang is supported? A: go 1.11.x or later.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Binding

type Binding map[string]struct {
	Type     Type        `json:"type"`
	DataType IRI         `json:"datatype"`
	XMLLang  string      `json:"xml:lang"`
	Value    interface{} `json:"value"`
}

Binding is a part of a SPARQL query result json.

type Client

type Client struct {
	HTTPClient http.Client
	Logger     logger.Logger
	Endpoint   string
	// contains filtered or unexported fields
}

Client queries to its SPARQL endpoint.

func New

func New(endpoint string, opts ...Option) (*Client, error)

New returns `sparql.Client`.

func (*Client) Close

func (c *Client) Close() error

Close closes this client

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping sends a HTTP HEAD request to the endpoint.

func (*Client) Query

func (c *Client) Query(
	ctx context.Context,
	query string,
	params ...Param,
) (*QueryResult, error)

Query queries to the endpoint.

Example (Ask)
cli, err := New("http://ja.dbpedia.org/sparql",
	MaxIdleConns(100),
	IdleConnTimeout(90*time.Second),
	Timeout(30*time.Second),
	Prefix("dbpj", "http://ja.dbpedia.org/resource/"),
	Prefix("dbp-owl", "http://dbpedia.org/ontology/"),
)
if err != nil {
	panic(err)
}

ctx := context.Background()
if err2 := cli.Ping(ctx); err2 != nil {
	panic(err2)
}

// Ask
result, err := cli.Query(
	ctx,
	`ask { dbpj:有安杏果 dbp-owl:birthYear "1995"^^xsd:gYear . }`,
)
if err != nil {
	panic(err)
}
log.Printf("%+v\n", result)
Output:

Example (Iri_parameter)
cli, err := New("http://ja.dbpedia.org/sparql",
	MaxIdleConns(100),
	IdleConnTimeout(90*time.Second),
	Timeout(30*time.Second),
	Prefix("dbpj", "http://ja.dbpedia.org/resource/"),
	Prefix("dbp-owl", "http://dbpedia.org/ontology/"),
)
if err != nil {
	panic(err)
}

ctx := context.Background()
if err2 := cli.Ping(ctx); err2 != nil {
	panic(err2)
}

// IRI parameter
result, err := cli.Query(
	ctx,
	"select * where "+
		"{ $1 <http://ja.dbpedia.org/property/name> ?name . } LIMIT 10",
	Param{
		Ordinal: 1,
		Value:   IRI("http://ja.dbpedia.org/resource/ももいろクローバーZ"),
	},
)
if err != nil {
	panic(err)
}
log.Printf("%+v\n", result)
Output:

Example (Language_tag)
cli, err := New("http://ja.dbpedia.org/sparql",
	MaxIdleConns(100),
	IdleConnTimeout(90*time.Second),
	Timeout(30*time.Second),
	Prefix("dbpj", "http://ja.dbpedia.org/resource/"),
	Prefix("dbp-owl", "http://dbpedia.org/ontology/"),
)
if err != nil {
	panic(err)
}

ctx := context.Background()
if err2 := cli.Ping(ctx); err2 != nil {
	panic(err2)
}

// With language tags
result, err := cli.Query(
	ctx,
	`select * where { ?s <http://ja.dbpedia.org/property/name> $1 . } LIMIT 10`,
	Param{
		Ordinal: 1,
		Value: Literal{
			Value:       "ももいろクローバー",
			LanguageTag: "ja",
		},
	},
)
if err != nil {
	panic(err)
}
log.Printf("%+v\n", result)
Output:

Example (Parameter)
cli, err := New("http://ja.dbpedia.org/sparql",
	MaxIdleConns(100),
	IdleConnTimeout(90*time.Second),
	Timeout(30*time.Second),
	Prefix("dbpj", "http://ja.dbpedia.org/resource/"),
	Prefix("dbp-owl", "http://dbpedia.org/ontology/"),
)
if err != nil {
	panic(err)
}

ctx := context.Background()
if err2 := cli.Ping(ctx); err2 != nil {
	panic(err2)
}

// Parameter
result, err := cli.Query(
	ctx,
	`select * where { ?s <http://dbpedia.org/ontology/wikiPageID> $1 . } LIMIT 1`,
	Param{
		Ordinal: 1,
		Value:   1529557,
	},
)
if err != nil {
	panic(err)
}
log.Printf("%+v\n", result)
Output:

Example (Parameterized_subject_and_object)
cli, err := New("http://ja.dbpedia.org/sparql",
	MaxIdleConns(100),
	IdleConnTimeout(90*time.Second),
	Timeout(30*time.Second),
	Prefix("dbpj", "http://ja.dbpedia.org/resource/"),
	Prefix("dbp-owl", "http://dbpedia.org/ontology/"),
)
if err != nil {
	panic(err)
}

ctx := context.Background()
if err2 := cli.Ping(ctx); err2 != nil {
	panic(err2)
}

// Parameterized subject and object
result, err := cli.Query(ctx, `select * where { $1 $2 ?o . } LIMIT 1000`,
	Param{
		Ordinal: 1,
		Value:   PrefixedName("dbpj:有安杏果"),
	},
	Param{
		Ordinal: 2,
		Value:   PrefixedName("dbp-owl:birthYear"),
	},
)
if err != nil {
	panic(err)
}
log.Printf("%+v\n", result)
Output:

Example (Simple)
cli, err := New("http://ja.dbpedia.org/sparql",
	MaxIdleConns(100),
	IdleConnTimeout(90*time.Second),
	Timeout(30*time.Second),
	Prefix("dbpj", "http://ja.dbpedia.org/resource/"),
	Prefix("dbp-owl", "http://dbpedia.org/ontology/"),
)
if err != nil {
	panic(err)
}

ctx := context.Background()
if err2 := cli.Ping(ctx); err2 != nil {
	panic(err2)
}

// Simple use case
result, err := cli.Query(
	ctx,
	"select distinct * where "+
		"{ <http://ja.dbpedia.org/resource/東京都> ?p ?o . } LIMIT 10",
)
if err != nil {
	panic(err)
}
log.Printf("%+v\n", result)
Output:

Example (Typed_literal_prefixed_name)
cli, err := New("http://ja.dbpedia.org/sparql",
	MaxIdleConns(100),
	IdleConnTimeout(90*time.Second),
	Timeout(30*time.Second),
	Prefix("dbpj", "http://ja.dbpedia.org/resource/"),
	Prefix("dbp-owl", "http://dbpedia.org/ontology/"),
)
if err != nil {
	panic(err)
}

ctx := context.Background()
if err2 := cli.Ping(ctx); err2 != nil {
	panic(err2)
}

// Typed literal with PrefixedName
result, err := cli.Query(
	ctx,
	`select * where { ?s <http://dbpedia.org/ontology/birthYear> $1 . } LIMIT 1`,
	Param{
		Ordinal: 1,
		Value: Literal{
			Value:    "1995",
			DataType: PrefixedName("xsd:gYear"),
		},
	},
)
if err != nil {
	panic(err)
}
log.Printf("%+v\n", result)
Output:

Example (Typed_literal_with_iri)
cli, err := New("http://ja.dbpedia.org/sparql",
	MaxIdleConns(100),
	IdleConnTimeout(90*time.Second),
	Timeout(30*time.Second),
	Prefix("dbpj", "http://ja.dbpedia.org/resource/"),
	Prefix("dbp-owl", "http://dbpedia.org/ontology/"),
)
if err != nil {
	panic(err)
}

ctx := context.Background()
if err2 := cli.Ping(ctx); err2 != nil {
	panic(err2)
}

// Typed literal with IRI
result, err := cli.Query(
	ctx,
	"select * where "+
		"{ ?s <http://dbpedia.org/ontology/wikiPageLength> $1 . } LIMIT 1",
	Param{
		Ordinal: 1,
		Value: Literal{
			Value:    76516,
			DataType: IRI("http://www.w3.org/2001/XMLSchema#nonNegativeInteger"),
		},
	},
)
if err != nil {
	panic(err)
}
log.Printf("%+v\n", result)
Output:

type Head struct {
	Vars []string `json:"vars"`
}

Head is a part of a SPARQL query result json.

type IRI

type IRI string

IRI https://www.w3.org/TR/rdf-sparql-query/#rIRI_REF

func (IRI) Ref

func (i IRI) Ref() string

Ref returns IRI_REF.

type IRIRef

type IRIRef interface {
	Ref() string
}

IRIRef https://www.w3.org/TR/rdf-sparql-query/#rIRIref

type Literal

type Literal struct {
	Value       interface{}
	DataType    IRIRef
	LanguageTag string
}

Literal http://www.w3.org/TR/2004/REC-rdf-concepts-20040210/#dfn-literal

func (Literal) Serialize

func (l Literal) Serialize() string

Serialize returns the serialized literal string.

type Option

type Option func(*Client) error

Option sets an option to the SPARQL client.

func IdleConnTimeout

func IdleConnTimeout(timeout time.Duration) Option

IdleConnTimeout sets the maximum amount of time an idle (keep-alive) connection.

func MaxIdleConns

func MaxIdleConns(n int) Option

MaxIdleConns sets max idle connections.

func Prefix

func Prefix(prefix string, iri IRI) Option

Prefix sets a global PREFIX for all queries.

func Timeout

func Timeout(timeout time.Duration) Option

Timeout sets the connection timeout duration. Also KeepAlive timeout.

type Param

type Param struct {
	// Ordinal position of the parameter starting from one and is always set.
	Ordinal int
	// Value is the parameter value.
	Value interface{}
}

Param is a parameter to fill placeholders

func (Param) Serialize

func (p Param) Serialize() string

Serialize returns the serialized as query parameter. nolint: gocyclo

type PrefixedName

type PrefixedName string

PrefixedName https://www.w3.org/TR/rdf-sparql-query/#rPrefixedName

func (PrefixedName) Ref

func (p PrefixedName) Ref() string

Ref returns PrefixedName.

type QueryResult

type QueryResult struct {
	Head    Head    `json:"head"`
	Results Results `json:"results"`
	Boolean bool    `json:"boolean"`
}

QueryResult is a destination to decoding a SPARQL query result json.

type Results

type Results struct {
	Bindings []Binding `json:"bindings"`
}

Results is a part of a SPARQL query result json.

type Serializable

type Serializable interface {
	Serialize() string
}

Serializable serialize data to embed to queries.

type Type

type Type string

Type is the binding value type.

const (
	TypeIRI          Type = "uri"
	TypeLiteral      Type = "literal"
	TypeTypedLiteral Type = "typed-literal"
	TypeBlankNode    Type = "bnode"
)

Types https://www.w3.org/TR/rdf-sparql-json-res/#variable-binding-results

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL