sparql

package module
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: 5 Imported by: 0

README

GoDoc Build Status codecov Go Report Card

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 Conn

type Conn struct {
	Client *client.Client
}

Conn connects to a SPARQL source.

func (*Conn) Begin

func (*Conn) Begin() (driver.Tx, error)

Begin is not supported. SPARQL does not have transactions.

func (*Conn) Close

func (c *Conn) Close() error

Close closes this connection but nothing to do.

func (*Conn) Ping

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

Ping sends a HTTP HEAD request to the source.

func (*Conn) Prepare

func (c *Conn) Prepare(query string) (driver.Stmt, error)

Prepare returns a prepared statement.

func (*Conn) QueryContext

func (c *Conn) QueryContext(
	ctx context.Context,
	query string,
	args []driver.NamedValue,
) (driver.Rows, error)

QueryContext queries to a SPARQL source.

Example
db := sql.OpenDB(NewConnector("http://ja.dbpedia.org/sparql"))

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

rows, err := db.QueryContext(
	ctx,
	"select distinct * where "+
		"{ <http://ja.dbpedia.org/resource/東京都> ?p ?o .  } LIMIT 1",
)
if err != nil {
	panic(err)
}

for rows.Next() {
	var p, o client.URI
	if err := rows.Scan(&p, &o); err != nil {
		panic(err)
	}
	log.Printf("%T %v %T %v", p, p, o, o)
}

if err := rows.Close(); err != nil {
	panic(err)
}

var o int
if err := db.QueryRowContext(
	ctx,
	"select distinct * where "+
		"{ <http://ja.dbpedia.org/resource/東京都> "+
		"<http://dbpedia.org/ontology/wikiPageID> ?o .  } LIMIT 1",
).Scan(&o); err != nil {
	panic(err)
}
log.Printf("%T %v", o, o)

if err := db.Close(); err != nil {
	panic(err)
}
Output:

Example (Hojin_info)
db := sql.OpenDB(NewConnector(
	"https://api.hojin-info.go.jp/sparql",
	client.WithHTTPClient(&http.Client{
		Timeout: 5 * time.Second,
	}),
	client.WithPrefix("hj", "http://hojin-info.go.jp/ns/domain/biz/1#"),
	client.WithPrefix("ic", "http://imi.go.jp/ns/core/rdf#"),
))

ctx := context.Background()
if err := db.PingContext(ctx); err != nil {
	panic(err)
}

//noinspection SqlDialectInspection
rows, err := db.QueryContext(ctx, `
		SELECT ?v FROM <http://hojin-info.go.jp/graph/hojin>
		WHERE {
		?n ic:名称/ic:表記 ?v .
		FILTER regex(?v, "マネー")
		} LIMIT 100`)
if err != nil {
	panic(err)
}

for rows.Next() {
	var v string
	if err := rows.Scan(&v); err != nil {
		panic(err)
	}
	log.Println(v)
}

if err := rows.Close(); err != nil {
	panic(err)
}

if err := db.Close(); err != nil {
	panic(err)
}
Output:

type Connector

type Connector struct {
	Name string
	// contains filtered or unexported fields
}

Connector generates `driver.Conn` with a context.

func NewConnector

func NewConnector(
	name string,
	opts ...client.Option,
) *Connector

NewConnector returns `driver.Connector`.

func (*Connector) Connect

func (c *Connector) Connect(ctx context.Context) (driver.Conn, error)

Connect returns `driver.Conn` with a context.

func (*Connector) Driver

func (c *Connector) Driver() driver.Driver

Driver returns underlying `driver.Driver`.

type Driver

type Driver struct{}

Driver accesses SPARQL sources.

func (*Driver) Open

func (d *Driver) Open(name string) (driver.Conn, error)

Open returns `driver.Conn`.

func (*Driver) OpenConnector

func (d *Driver) OpenConnector(name string) (driver.Connector, error)

OpenConnector returns `driver.Connector`.

type Rows

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

Rows implements `driver.Rows` with `sparql.QueryResult`.

func (*Rows) Close

func (r *Rows) Close() error

Close closes the rows iterator.

func (*Rows) Columns

func (r *Rows) Columns() []string

Columns returns the names of the columns.

func (*Rows) Next

func (r *Rows) Next(dest []driver.Value) error

Next is called to populate the next row of data into the provided slice.

type Stmt

type Stmt struct {
	*client.Statement
}

Stmt implements `driver.Stmt` with `sparql.Statement`.

func (*Stmt) Close

func (s *Stmt) Close() error

Close closes the statement. Actually do nothing.

func (*Stmt) Exec

func (*Stmt) Exec(args []driver.Value) (driver.Result, error)

Exec is not supported. DO NOT USE.

func (*Stmt) NumInput

func (s *Stmt) NumInput() int

NumInput is not supported. Always return -1.

func (*Stmt) Query

func (s *Stmt) Query(args []driver.Value) (driver.Rows, error)

Query queries to the endpoint.

func (*Stmt) QueryContext

func (s *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error)

QueryContext queries to a SPARQL source.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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