sql

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

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 *sparql.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, err := sql.Open("sparql", "http://ja.dbpedia.org/sparql")
if err != nil {
	panic(err)
}

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 sparql.IRI
	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(
	nil,
	"https://api.hojin-info.go.jp/sparql",
	sparql.Timeout(5*time.Second),
	sparql.MaxIdleConns(0),
	sparql.IdleConnTimeout(0),
	sparql.Prefix("hj", "http://hojin-info.go.jp/ns/domain/biz/1#"),
	sparql.Prefix("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(
	driver driver.Driver,
	name string,
	opts ...sparql.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.

Jump to

Keyboard shortcuts

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