Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { HTTPClient http.Client Endpoint string // contains filtered or unexported fields }
Client queries to its SPARQL endpoint.
func (*Client) Query ¶
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 Literal ¶
Literal http://www.w3.org/TR/2004/REC-rdf-concepts-20040210/#dfn-literal
func (*Literal) UnmarshalXML ¶
UnmarshalXML unmarshals the literal element. `datatype` is decoded as URI. `lang` must have xml namespace.
type Option ¶
Option sets an option to the SPARQL client.
func WithHTTPClient ¶
HTTPClient replaces default HTTP client.
func WithPrefix ¶
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 ¶
Placeholders returns matching placeholder strings.
type PrefixedName ¶
type PrefixedName string
PrefixedName https://www.w3.org/TR/rdf-sparql-query/#rPrefixedName
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.
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) 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 ¶
func (*XMLResultParser) Parse(r io.ReadCloser) (QueryResult, error)