gorqlite: within.website/gorqlite Index | Files

package gorqlite

import "within.website/gorqlite"

gorqlite A golang database/sql driver for rqlite, the distributed consistent sqlite.

Copyright (c)2016 andrew fabbro (andrew@fabbro.org)

See LICENSE.md for license. tl;dr: MIT. Conveniently, the same licese as rqlite.

Project home page: https://github.com/raindo308/gorqlite

Learn more about rqlite at: https://github.com/rqlite/rqlite

Index

Package Files

api.go cluster.go conn.go gorqlite.go prepared_statement.go query.go write.go

func EscapeString Uses

func EscapeString(value string) string

EscapeString sql-escapes a string.

func TraceOff Uses

func TraceOff()

TraceOff()

Turns off tracing output. Once you call TraceOff(), no further info is sent to the io.Writer, unless it is TraceOn'd again.

func TraceOn Uses

func TraceOn(w io.Writer)

TraceOn()

Turns on tracing output to the io.Writer of your choice.

Trace output is very detailed and verbose, as you might expect.

Normally, you should run with tracing off, as it makes absolutely no concession to performance and is intended for debugging/dev use.

type Connection Uses

type Connection struct {
    ID string //   generated in init()
    // contains filtered or unexported fields
}

func Open Uses

func Open(connURL string) (Connection, error)
*****************************************************************

Open() creates and returns a "connection" to rqlite.

Since rqlite is stateless, there is no actual connection. Open() creates and initializes a gorqlite Connection type, which represents various config information.

The URL should be in a form like this:

	http://localhost:4001

	http://     default, no auth, localhost:4001
	https://    default, no auth, localhost:4001, using https

	http://localhost:1234
	http://mary:secret2@localhost:1234

  https://mary:secret2@somewhere.example.com:1234
  https://mary:secret2@somewhere.example.com // will use 4001
 * ****************************************************************

func (*Connection) Close Uses

func (conn *Connection) Close()

func (*Connection) ConsistencyLevel Uses

func (conn *Connection) ConsistencyLevel() (string, error)

func (*Connection) Leader Uses

func (conn *Connection) Leader() (string, error)

func (*Connection) Peers Uses

func (conn *Connection) Peers() ([]string, error)

func (*Connection) Query Uses

func (conn *Connection) Query(sqlStatements []string) (results []QueryResult, err error)

Query() is used to perform SELECT operations in the database.

It takes an array of SQL statements and executes them in a single transaction, returning an array of QueryResult vars.

func (*Connection) QueryOne Uses

func (conn *Connection) QueryOne(sqlStatement string) (qr QueryResult, err error)

QueryOne() is a convenience method that wraps Query() into a single-statement method.

func (*Connection) SetConsistencyLevel Uses

func (conn *Connection) SetConsistencyLevel(levelDesired string) error

func (*Connection) Write Uses

func (conn *Connection) Write(sqlStatements []string) (results []WriteResult, err error)

Write() is used to perform DDL/DML in the database. ALTER, CREATE, DELETE, DROP, INSERT, UPDATE, etc. all go through Write().

Write() takes an array of SQL statements, and returns an equal-sized array of WriteResults, each corresponding to the SQL statement that produced it.

All statements are executed as a single transaction.

Write() returns an error if one is encountered during its operation. If it's something like a call to the rqlite API, then it'll return that error. If one statement out of several has an error, it will return a generic "there were %d statement errors" and you'll have to look at the individual statement's Err for more info.

func (*Connection) WriteOne Uses

func (conn *Connection) WriteOne(sqlStatement string) (wr WriteResult, err error)

type PreparedStatement Uses

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

PreparedStatement is a simple wrapper around fmt.Sprintf for prepared SQL statements.

func NewPreparedStatement Uses

func NewPreparedStatement(body string) PreparedStatement

NewPreparedStatement takes a sprintf syntax SQL query for later binding of parameters.

func (PreparedStatement) Bind Uses

func (p PreparedStatement) Bind(args ...interface{}) string

Bind takes arguments and SQL-escapes them, then calling fmt.Sprintf.

type QueryResult Uses

type QueryResult struct {
    Err error

    Timing float64
    // contains filtered or unexported fields
}

A QueryResult type holds the results of a call to Query(). You could think of it as a rowset.

So if you were to query:

SELECT id, name FROM some_table;

then a QueryResult would hold any errors from that query, a list of columns and types, and the actual row values.

Query() returns an array of QueryResult vars, while QueryOne() returns a single variable.

func (*QueryResult) Columns Uses

func (qr *QueryResult) Columns() []string

Columns returns a list of the column names for this QueryResult.

func (*QueryResult) Map Uses

func (qr *QueryResult) Map() (map[string]interface{}, error)

Map() returns the current row (as advanced by Next()) as a map[string]interface{}

The key is a string corresponding to a column name. The value is the corresponding column.

Note that only json values are supported, so you will need to type the interface{} accordingly.

func (*QueryResult) Next Uses

func (qr *QueryResult) Next() bool

Next() positions the QueryResult result pointer so that Scan() or Map() is ready.

You should call Next() first, but gorqlite will fix it if you call Map() or Scan() before the initial Next().

A common idiom:

rows := conn.Write(something)
for rows.Next() {
	// your Scan/Map and processing here.
}

func (*QueryResult) NumRows Uses

func (qr *QueryResult) NumRows() int64

NumRows() returns the number of rows returned by the query.

func (*QueryResult) RowNumber Uses

func (qr *QueryResult) RowNumber() int64

RowNumber() returns the current row number as Next() iterates through the result's rows.

func (*QueryResult) Scan Uses

func (qr *QueryResult) Scan(dest ...interface{}) error

Scan() takes a list of pointers and then updates them to reflect he current row's data.

Note that only the following data types are used, and they are a subset of the types JSON uses:

string, for JSON strings
float64, for JSON numbers
int64, as a convenient extension
nil for JSON null

booleans, JSON arrays, and JSON objects are not supported, since sqlite does not support them.

func (*QueryResult) Types Uses

func (qr *QueryResult) Types() []string

Types() returns an array of the column's types.

Note that sqlite will repeat the type you tell it, but in many cases, it's ignored. So you can initialize a column as CHAR(3) but it's really TEXT. See https://www.sqlite.org/datatype3.html

This info may additionally conflict with the reality that your data is being JSON encoded/decoded.

type WriteResult Uses

type WriteResult struct {
    Err          error // don't trust the rest if this isn't nil
    Timing       float64
    RowsAffected int64 // affected by the change
    LastInsertID int64 // if relevant, otherwise zero value
    // contains filtered or unexported fields
}

A WriteResult holds the result of a single statement sent to Write().

Write() returns an array of WriteResult vars, while WriteOne() returns a single WriteResult.

Package gorqlite imports 12 packages (graph). Updated 2019-06-09. Refresh now. Tools for package owners.