solr

package
v0.20.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package solr is a database/sql driver for Apache Solr, which runs SQL on a collection over HTTP, as Parallel SQL. It registers the one name "solr", and takes a DSN of the form solr://user:pass@host:8983/collection (D166).

db, err := sql.Open("solr", "solr://admin:pass@localhost:8983/books")

The driver reads only. The SQL of Solr refuses INSERT, UPDATE and DELETE, and the driver returns that refusal (D163). The server binds no argument, so the driver writes each one into the statement as a literal (D166). The answer names no type, so the driver reads the type of each column from metadata.COLUMNS and from the luke handler, once for each table and connection, and decodes each value by it (D166). Each request asks for the first tuple that names the columns, and the driver reads the tuples one at a time, to the tuple EOF. Solr has no transactions, and no way to stop a statement of /sql. docs/SOLR.md holds what the driver knows about the server.

Index

Constants

View Source
const ErrCut dbimp.Error = "the answer was cut short"

ErrCut is the error of an answer that ends before its tuple EOF. The server ends an answer so when the connection breaks, and after some rows it comes wrapped with dbimp.ErrIncomplete (D107 and D166).

View Source
const ModeFacet = "facet"

ModeFacet is the only value of the key mode. It is the aggregationMode that the server uses by default, and it gives every group of a GROUP BY.

View Source
const Name = "solr"

Name is the name that the driver registers with database/sql, and the scheme of its DSN.

Variables

This section is empty.

Functions

func WithOptions

func WithOptions(ctx context.Context, opts ...Option) context.Context

WithOptions returns a context that carries opts. Each statement started with the context applies them after the options of the DSN.

Types

type Config

type Config struct {
	// Host is the host of the server.
	Host string
	// Port is the port of the server, 8983 by default.
	Port int
	// TLS is true to speak HTTPS.
	TLS bool
	// User and Password are the credentials, which the driver sends with
	// basic authentication.
	User     string
	Password string
	// Collection is the collection whose /sql handler takes each statement.
	// FROM names the table, and any collection that the user can read serves
	// (D166). It is empty for a DSN with no path, and then each statement
	// needs WithDatabase.
	Collection string
	// Mode is the aggregationMode of each statement. It is ModeFacet by
	// default, and the only value that the driver takes (D166).
	Mode string
}

Config is the configuration of a connector, which the DSN holds. The caller owns it (D7).

func ParseDSN

func ParseDSN(dsn string) (*Config, error)

ParseDSN parses a DSN of the form solr://user:pass@host:port/collection?key=value (D27, D35 and D166). A path with more than one segment is refused, and so is every key that the driver does not know.

func (*Config) FormatDSN

func (cfg *Config) FormatDSN() string

FormatDSN returns the DSN of cfg. ParseDSN reads it back as cfg, for a Config that ParseDSN or NewConnector filled.

type Connector

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

Connector opens connections to one server. It owns its transport, and every connection shares it. A caller can build one from a Config and open it with sql.OpenDB.

func NewConnector

func NewConnector(cfg Config) *Connector

NewConnector returns a Connector for cfg. The connector keeps a copy of cfg, and fills each field that is zero with its default.

func (*Connector) Close

func (c *Connector) Close() error

Close closes the idle connections of the transport.

func (*Connector) Connect

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

Connect satisfies driver.Connector. A connection holds only the schemas that it read, so it sends no request.

func (*Connector) Driver

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

Driver satisfies driver.Connector.

type Driver

type Driver struct{}

Driver is the database/sql driver for Apache Solr.

func (Driver) Open

func (Driver) Open(string) (driver.Conn, error)

Open satisfies driver.Driver. database/sql opens a connection through OpenConnector, and Open returns an error, because opening a connection needs a context.

func (Driver) OpenConnector

func (Driver) OpenConnector(dsn string) (driver.Connector, error)

OpenConnector satisfies driver.DriverContext. It parses the DSN.

type Error

type Error struct {
	// HTTPStatus is the status code of the response. It is 200 for an
	// exception that came in the body.
	HTTPStatus int
	// Message is EXCEPTION, or the title of the page of HTML, or the status
	// text of the response when it has neither.
	Message string
	// contains filtered or unexported fields
}

Error is an error that Solr reported. A statement that fails answers HTTP 200 with the tuple EOF that holds EXCEPTION, before any row or after some (measured). A wrong password is HTTP 401, a request that the role does not allow is HTTP 403, and a statement that the SQL layer cannot plan can be HTTP 500, each with a page of HTML (measured).

func (*Error) Error

func (err *Error) Error() string

Error satisfies the error interface.

func (*Error) Is added in v0.18.0

func (err *Error) Is(target error) bool

Is reports whether err matches target. It matches dbimp.ErrAuthentication when the status is HTTP 401, which is a wrong password (recorded: "a wrong password"). Every other refusal does not match, such as HTTP 403 for a missing permission (D197).

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap returns the *dbimp.StatusError of the response, or nil for an exception of the body.

type Option

type Option = dbimp.Option[options]

Option sets an option of one statement (D109). An option comes from the DSN, then from the context through WithOptions, then from an argument of the statement, and a later one wins.

func WithDatabase

func WithDatabase(name string) Option

WithDatabase names the collection whose /sql handler takes the statement, in place of the collection of the DSN (D166). FROM still names the table.

func WithParameter

func WithParameter(name string, value any) Option

WithParameter sets a field of the form of POST /solr/<collection>/sql by its name, such as "numWorkers". A string is sent as it is, a []string as one field for each element, and any other value as its text. A field named here replaces one that the driver sets itself, such as "includeMetadata" or "aggregationMode". So WithParameter("aggregationMode", "map_reduce") turns on the mode that the DSN refuses, and WithParameter("includeMetadata", "false") makes the answer name no column before its rows.

func WithReadonly

func WithReadonly(bool) Option

WithReadonly asks that the statement write nothing. The SQL of Solr takes no write of any kind (D163), so every statement is read-only, and the option changes nothing.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout cannot give the server a time for the statement. The server has no setting that honors one: timeAllowed cut nothing (measured), so a statement with a timeout other than zero fails with dbimp.ErrNotSupported. The deadline of the context stops the request.

Jump to

Keyboard shortcuts

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