grpcsql

package module
v0.0.0-...-181d263 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2018 License: Apache-2.0 Imports: 14 Imported by: 0

README

go-grpc-sql Build Status Coverage Status Go Report Card GoDoc

This repository provides the grpcsql package, which can be used to expose a Go SQL driver.Driver instance over gRPC.

Documentation

The documentation for this package can be found on Godoc.

Documentation

Overview

Package grpcsql exposes a SQL database over a gRPC endpoint.

Example
listener, err := net.Listen("tcp", ":0")
if err != nil {
	log.Fatalf("failed to create listener: %v", err)
}
server := grpcsql.NewServer(&sqlite3.SQLiteDriver{})
go server.Serve(listener)
defer server.Stop()

dialer := func() (*grpc.ClientConn, error) {
	return grpc.Dial(listener.Addr().String(), grpc.WithInsecure())
}
driver := grpcsql.NewDriver(dialer)
sql.Register("grpc", driver)

db, err := sql.Open("grpc", ":memory:")
if err != nil {
	log.Fatalf("failed to create grpc database: %s", err)
}
defer db.Close()

tx, err := db.Begin()
if err != nil {
	log.Fatalf("failed to create grpc transaction: %s", err)
}
defer tx.Rollback()

result, err := tx.Exec("CREATE TABLE test (n INTEGER); INSERT INTO test(n) VALUES (1)")
if err != nil {
	log.Fatalf("failed to execute create table statement over grpc: %s", err)
}

result, err = tx.Exec("INSERT INTO test(n) VALUES (2)")
if err != nil {
	log.Fatalf("failed to execute insert statement over grpc: %s", err)
}

rows, err := tx.Query("SELECT n FROM test ORDER BY n")
if err != nil {
	log.Fatalf("failed to select rows over grpc: %s", err)
}
types, err := rows.ColumnTypes()
if len(types) != 1 {
	log.Fatalf("wrong count of column types: %d", len(types))
}
name := types[0].DatabaseTypeName()

if err != nil {
	log.Fatalf("failed to fetch column types over grpc: %s", err)
}

numbers := []int{}
for rows.Next() {
	var n int
	if err := rows.Scan(&n); err != nil {
		log.Fatalf("failed to scan row over grpc: %s", err)
	}
	numbers = append(numbers, n)
}
if err := rows.Err(); err != nil {
	log.Fatalf("rows error over grpc: %s", err)
}
defer rows.Close()
Output:

2 <nil>
1 <nil>
INTEGER
[1 2]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewServer

func NewServer(driver driver.Driver, opt ...grpc.ServerOption) *grpc.Server

NewServer is a convenience for creating a gRPC server with a registered SQL gateway backed by the given driver.

Types

type Conn

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

Conn wraps a connection to a gRPC SQL gateway.

func (*Conn) Begin deprecated

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

Begin starts and returns a new transaction.

Deprecated: Drivers should implement ConnBeginTx instead (or additionally).

func (*Conn) Close

func (c *Conn) Close() error

Close invalidates and potentially stops any current prepared statements and transactions, marking this connection as no longer in use.

func (*Conn) Exec deprecated

func (c *Conn) Exec(query string, args []driver.Value) (driver.Result, error)

Exec may return ErrSkip.

Deprecated: Drivers should implement ExecerContext instead (or additionally).

func (*Conn) Prepare

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

Prepare returns a prepared statement, bound to this connection.

type Dialer

type Dialer func() (conn *grpc.ClientConn, err error)

Dialer is a function that can create a gRPC connection.

type Driver

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

Driver implements the database/sql/driver interface and executes the relevant statements over gRPC.

func NewDriver

func NewDriver(dialer Dialer) *Driver

NewDriver creates a new gRPC SQL driver for creating connections to backend gateways.

func (*Driver) Open

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

Open a new connection against a gRPC SQL server.

To establish the gRPC connection, the dialer passed to NewDriver() will used.

The given data source name must be one that the driver attached to the remote Gateway can understand.

type Gateway

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

Gateway mapping gRPC requests to SQL queries.

func NewGateway

func NewGateway(drv driver.Driver) *Gateway

NewGateway creates a new gRPC gateway executing requests against the given SQL driver.

func (*Gateway) Conn

func (s *Gateway) Conn(stream protocol.SQL_ConnServer) error

Conn creates a new database connection using the underlying driver, and start accepting requests for it.

type Result

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

Result is the result of a query execution.

func (*Result) LastInsertId

func (s *Result) LastInsertId() (int64, error)

LastInsertId returns the database's auto-generated ID.

func (*Result) RowsAffected

func (s *Result) RowsAffected() (int64, error)

RowsAffected returns the number of rows affected by the query.

type Rows

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

Rows is an iterator over an executed query's results.

func (*Rows) Close

func (r *Rows) Close() error

Close closes the rows iterator.

func (*Rows) ColumnTypeDatabaseTypeName

func (r *Rows) ColumnTypeDatabaseTypeName(i int) string

ColumnTypeDatabaseTypeName implements RowsColumnTypeDatabaseTypeName.

func (*Rows) ColumnTypeScanType

func (r *Rows) ColumnTypeScanType(i int) reflect.Type

ColumnTypeScanType implements RowsColumnTypeScanType.

func (*Rows) Columns

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

Columns returns the names of the columns. The number of columns of the result is inferred from the length of the slice. If a particular column name isn't known, an empty string should be returned for that entry.

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. The provided slice will be the same size as the Columns() are wide.

Next should return io.EOF when there are no more rows.

type Stmt

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

Stmt is a prepared statement. It is bound to a Conn and not used by multiple goroutines concurrently.

func (*Stmt) Close

func (s *Stmt) Close() error

Close closes the statement.

func (*Stmt) Exec

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

Exec executes a query that doesn't return rows, such

func (*Stmt) NumInput

func (s *Stmt) NumInput() int

NumInput returns the number of placeholder parameters.

func (*Stmt) Query

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

Query executes a query that may return rows, such as a

type Tx

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

Tx is a transaction.

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit the transaction.

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback the transaction.

Directories

Path Synopsis
internal
protocol
Package protocol defines the wire format for SQL statements and results.
Package protocol defines the wire format for SQL statements and results.

Jump to

Keyboard shortcuts

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