client

package
v1.0.0-alpha Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2019 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BNode

type BNode string

BNode represents blank node.

type Client

type Client struct {
	HTTPClient http.Client
	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. Actually nothing to do to close the HTTP client.

func (*Client) Ping

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

Ping sends a HTTP HEAD request to the endpoint.

func (*Client) Prepare

func (c *Client) Prepare(query string) *Statement

Prepare returns `*sparql.Statement`.

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",
	WithHTTPClient(&http.Client{
		Transport: &http.Transport{
			MaxIdleConns:        100,
			MaxIdleConnsPerHost: 100,
			IdleConnTimeout:     90 * time.Second,
		},
		Timeout: 30 * time.Second,
	}),
	WithPrefix("dbpj", "http://ja.dbpedia.org/resource/"),
	WithPrefix("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.Println(result.Boolean())
if err := result.Close(); err != nil {
	panic(err)
}
Output:

Example (Language_tag)
cli, err := New("http://ja.dbpedia.org/sparql",
	WithHTTPClient(&http.Client{
		Transport: &http.Transport{
			MaxIdleConns:        100,
			MaxIdleConnsPerHost: 100,
			IdleConnTimeout:     90 * time.Second,
		},
		Timeout: 30 * time.Second,
	}),
	WithPrefix("dbpj", "http://ja.dbpedia.org/resource/"),
	WithPrefix("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.Println(result.Next())
if err := result.Close(); err != nil {
	panic(err)
}
Output:

Example (Parameter)
cli, err := New("http://ja.dbpedia.org/sparql",
	WithHTTPClient(&http.Client{
		Transport: &http.Transport{
			MaxIdleConns:        100,
			MaxIdleConnsPerHost: 100,
			IdleConnTimeout:     90 * time.Second,
		},
		Timeout: 30 * time.Second,
	}),
	WithPrefix("dbpj", "http://ja.dbpedia.org/resource/"),
	WithPrefix("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.Println(result.Next())
if err := result.Close(); err != nil {
	panic(err)
}
Output:

Example (Parameterized_subject_and_object)
cli, err := New("http://ja.dbpedia.org/sparql",
	WithHTTPClient(&http.Client{
		Transport: &http.Transport{
			MaxIdleConns:        100,
			MaxIdleConnsPerHost: 100,
			IdleConnTimeout:     90 * time.Second,
		},
		Timeout: 30 * time.Second,
	}),
	WithPrefix("dbpj", "http://ja.dbpedia.org/resource/"),
	WithPrefix("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.Println(result.Next())
if err := result.Close(); err != nil {
	panic(err)
}
Output:

Example (Simple)
cli, err := New("http://ja.dbpedia.org/sparql",
	WithHTTPClient(&http.Client{
		Transport: &http.Transport{
			MaxIdleConns:        100,
			MaxIdleConnsPerHost: 100,
			IdleConnTimeout:     90 * time.Second,
		},
		Timeout: 30 * time.Second,
	}),
	WithPrefix("dbpj", "http://ja.dbpedia.org/resource/"),
	WithPrefix("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.Println(result.Next())
if err := result.Close(); err != nil {
	panic(err)
}
Output:

Example (Typed_literal_prefixed_name)
cli, err := New("http://ja.dbpedia.org/sparql",
	WithHTTPClient(&http.Client{
		Transport: &http.Transport{
			MaxIdleConns:        100,
			MaxIdleConnsPerHost: 100,
			IdleConnTimeout:     90 * time.Second,
		},
		Timeout: 30 * time.Second,
	}),
	WithPrefix("dbpj", "http://ja.dbpedia.org/resource/"),
	WithPrefix("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.Println(result.Next())
if err := result.Close(); err != nil {
	panic(err)
}
Output:

Example (Typed_literal_with_uri)
cli, err := New("http://ja.dbpedia.org/sparql",
	WithHTTPClient(&http.Client{
		Timeout: 90 * time.Second,
	}),
	WithPrefix("dbpj", "http://ja.dbpedia.org/resource/"),
	WithPrefix("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 URI
result, err := cli.Query(
	ctx,
	"select * where "+
		"{ ?s <http://dbpedia.org/ontology/wikiPageLength> $1 . } LIMIT 1",
	Param{
		Ordinal: 1,
		Value: Literal{
			Value:    "76516",
			DataType: URI("http://www.w3.org/2001/XMLSchema#nonNegativeInteger"),
		},
	},
)
if err != nil {
	panic(err)
}
log.Println(result.Next())
if err := result.Close(); err != nil {
	panic(err)
}
Output:

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

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

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

type IRIRef

type IRIRef interface {
	Ref() string
}

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

type Literal

type Literal struct {
	Value       string
	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.

func (*Literal) UnmarshalXML

func (l *Literal) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals the literal element. `datatype` is decoded as URI. `lang` must have xml namespace.

type Option

type Option func(*Client) error

Option sets an option to the SPARQL client.

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) Option

HTTPClient replaces default HTTP client.

func WithPrefix

func WithPrefix(prefix string, uri URI) Option

WithPrefix sets a global PREFIX for all queries.

func WithResultParser

func WithResultParser(resultParser ResultParser) Option

type Param

type Param struct {
	// If the Name is not empty it should be used for the parameter identifier and
	// not the ordinal position.
	//
	// Name will not have a symbol prefix.
	Name string
	// 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) Placeholders

func (p Param) Placeholders() []string

Placeholders returns matching placeholder strings.

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 interface {
	Variables() []string
	Next() (map[string]Value, error)
	Boolean() (bool, error)

	io.Closer
}

QueryResult is a SPARQL query result.

func DecodeXMLQueryResult

func DecodeXMLQueryResult(r io.ReadCloser) (QueryResult, error)

DecodeXMLQueryResult decodes responded XML Query Result.

type ResultParser

type ResultParser interface {
	// Format returns a format name string. It's used as a `format` request header value.
	Format() string
	// Parse parses query result stream.
	Parse(reader io.ReadCloser) (QueryResult, error)
}

ResultParser is the parser for specific format query results.

func NewXMLResultParser

func NewXMLResultParser() ResultParser

NewXMLResultParser returns `XMLResultParser` as the implementation of `ResultParser`.

type Serializable

type Serializable interface {
	Serialize() string
}

Serializable serialize data to embed to queries.

type Statement

type Statement struct {
	// contains filtered or unexported fields
}

Statement is prepared statement.

func (*Statement) Query

func (s *Statement) Query(
	ctx context.Context,
	params ...Param,
) (QueryResult, error)

Query queries to the endpoint.

type URI

type URI string

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

func (URI) Ref

func (i URI) Ref() string

Ref returns IRI_REF.

type Value

type Value interface{}

Value is an interface holding one of the binding (or boolean) types: URI, Literal, BNode or bool.

type XMLQueryResult

type XMLQueryResult struct {
	// contains filtered or unexported fields
}

XMLQueryResult is the implementation to decode SPARQL Query Results XML Format. https://www.w3.org/TR/rdf-sparql-XMLres/

func (*XMLQueryResult) Boolean

func (x *XMLQueryResult) Boolean() (bool, error)

func (*XMLQueryResult) Close

func (x *XMLQueryResult) Close() error

func (*XMLQueryResult) Next

func (x *XMLQueryResult) Next() (map[string]Value, error)

func (*XMLQueryResult) Variables

func (x *XMLQueryResult) Variables() []string

Variables returns query variables.

type XMLResultParser

type XMLResultParser struct {
}

XMLResultParser is the parser for XML-formatted query results.

func (*XMLResultParser) Format

func (*XMLResultParser) Format() string

Format returns a format name string. It's used as a `format` request header value.

func (*XMLResultParser) Parse

Jump to

Keyboard shortcuts

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