psql

package module
v0.0.0-...-bd41df2 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2021 License: MIT Imports: 19 Imported by: 0

README

go-psql

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NonceByteSize       = 12                           // standard nonce size for GCM cipher
	EncryptionKey       string                         // must be 128, 192 or 256 bit, blank disables encryption
	DecryptionErrorFunc func(interface{}, error) error // You can log or take other actions. The error returned will be returned from Scan function
)
View Source
var (
	ErrNil = errors.New("value is nil")
)
View Source
var ErrNilValue = errors.New("value is nil")
View Source
var (
	ErrNoRows = sql.ErrNoRows
)

Functions

func Delete

func Delete(ctx context.Context, c QueryClient, v Model) (int64, error)

func DeleteReturning

func DeleteReturning(ctx context.Context, c QueryClient, v Model) error

func Insert

func Insert(ctx context.Context, c QueryClient, v Model, cols ...string) error

returns id into the model

func InsertReturning

func InsertReturning(ctx context.Context, c QueryClient, v Model, cols ...string) error

func Quote

func Quote(str string) string

func RawSelect

func RawSelect(ctx context.Context, c QueryClient, outSlicePtr interface{}, q string, args ...interface{}) error

func Save

func Save(ctx context.Context, c QueryClient, v Model, cols ...string) error

func Update

func Update(ctx context.Context, c QueryClient, v Model, cols ...string) error

func UpdateReturning

func UpdateReturning(ctx context.Context, c QueryClient, v Model, cols ...string) error

Types

type Attrs

type Attrs = map[string]interface{}

type BulkInserter

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

func (BulkInserter) BulkInsert

func (bi BulkInserter) BulkInsert(p BulkProvider) error

Unlike the other Client funcs this executes immediately and does not return a Query that you Exec() on

func (BulkInserter) WithModelErrFunc

func (bi BulkInserter) WithModelErrFunc(errFunc ModelErrorFunc) BulkInserter

type BulkProvider

type BulkProvider interface {
	NextModel() Model
	Cap() int
}

Make sure all Models are the same type

type ChannelModelProvider

type ChannelModelProvider struct {
	Channel <-chan Model
	// contains filtered or unexported fields
}

Bulk provider for channels, see '*Client MonitorBulkInsertChannel()' for usage example

func NewChannelModelProvider

func NewChannelModelProvider(m Model, ch <-chan Model) *ChannelModelProvider

func (*ChannelModelProvider) Cap

func (p *ChannelModelProvider) Cap() int

func (*ChannelModelProvider) NextModel

func (p *ChannelModelProvider) NextModel() Model

type Client

type Client struct {
	*sql.DB
	// contains filtered or unexported fields
}

Client is a helper type to easily connect to PostgreSQL database instances.

It satisfies the `health.Metric` interface.

func NewClient

func NewClient(cfg *Config) *Client

func (*Client) BeginTx

func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

func (*Client) BulkInsert

func (c *Client) BulkInsert(p BulkProvider) error

func (*Client) BulkInserter

func (c *Client) BulkInserter() BulkInserter

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, v Model) (int64, error)

func (*Client) DeleteAll

func (c *Client) DeleteAll(table string) *Query

func (*Client) DeleteReturning

func (c *Client) DeleteReturning(ctx context.Context, v Model) error

func (*Client) Insert

func (c *Client) Insert(ctx context.Context, v Model, cols ...string) error

func (*Client) InsertReturning

func (c *Client) InsertReturning(ctx context.Context, v Model, cols ...string) error

func (*Client) MonitorBulkInsertChannel

func (c *Client) MonitorBulkInsertChannel(ch chan Model, errFunc ModelErrorFunc) error

func (*Client) Name

func (c *Client) Name() string

Name returns `"psql"`.

To better identify the `Client` may be wrapped in a prefixed metric:

health.PrefixedMetric{Prefix: "MyDatabaseName", BaseMetric: c}

func (*Client) RawQuery

func (c *Client) RawQuery(ctx context.Context, q string, args ...interface{}) (*QueryResult, error)

func (*Client) RawSelect

func (c *Client) RawSelect(ctx context.Context, outSlicePtr interface{}, q string, args ...interface{}) error

func (*Client) RunInTransaction

func (c *Client) RunInTransaction(ctx context.Context, f func(context.Context, *Tx) error, opts *sql.TxOptions) error

func (*Client) Save

func (c *Client) Save(ctx context.Context, v Model, cols ...string) error

func (*Client) Select

func (c *Client) Select(tableName string, cols ...string) *Query

func (*Client) Start

func (c *Client) Start(driverName string) error

func (*Client) Started

func (c *Client) Started() bool

func (*Client) Status

func (c *Client) Status(ctx context.Context) error

Status calls the client's `Ping` method to determine connectivity.

func (*Client) Stop

func (c *Client) Stop() error

func (*Client) Update

func (c *Client) Update(ctx context.Context, v Model, cols ...string) error

func (*Client) UpdateAll

func (c *Client) UpdateAll(table string, attrs Attrs) *Query

func (*Client) UpdateReturning

func (c *Client) UpdateReturning(ctx context.Context, v Model, cols ...string) error

type Config

type Config struct {
	DbName         string
	Schema         string
	User           string
	Password       string
	Host           string
	Port           string
	ConnectTimeout string
	SSLMode        string
}

type EncryptableString

type EncryptableString struct {
	NullString
}

EncryptableString is a struct containing a string that can be stored as an encrypted string in the database. It extends NullString so the value can be null. In memory the string will be in plain text. In the DB it is stored as a hex string where the first 8 bytes represent an integer which is the byte size of the nonce, The next nonce size bytes are the nonce and the rest of the bytes are the encrypted data

func NewEncryptableString

func NewEncryptableString(s string) EncryptableString

func (*EncryptableString) Scan

func (s *EncryptableString) Scan(value interface{}) error

Scan implements the Scanner interface. (converts from DB)

func (EncryptableString) Value

func (s EncryptableString) Value() (driver.Value, error)

Value implements the driver Valuer interface. (converts for DB)

type Inter

type Inter interface {
	Int() (int64, error)
}

Inter

type JSONObject

type JSONObject map[string]interface{}

func (JSONObject) Bool

func (o JSONObject) Bool(key string) (bool, error)

func (JSONObject) Float64

func (o JSONObject) Float64(key string) (float64, error)

func (JSONObject) Int

func (o JSONObject) Int(key string) (int, error)

func (JSONObject) Int64

func (o JSONObject) Int64(key string) (int64, error)

func (JSONObject) Object

func (o JSONObject) Object(key string) (JSONObject, error)

func (*JSONObject) Scan

func (o *JSONObject) Scan(src interface{}) error

func (JSONObject) Slice

func (o JSONObject) Slice(key string) ([]interface{}, error)

func (JSONObject) String

func (o JSONObject) String(key string) (string, error)

func (JSONObject) Value

func (o JSONObject) Value() (driver.Value, error)

type Model

type Model interface {
	TableName() string
}

type ModelErrorFunc

type ModelErrorFunc = func(Model, error)

type ModelHelper

type ModelHelper struct {
	Model Model
}

func (ModelHelper) Attributes

func (h ModelHelper) Attributes(only ...string) map[string]interface{}

Returns a map of the attributes of the struct excluding id

func (ModelHelper) ID

func (h ModelHelper) ID() (int64, error)

func (ModelHelper) SetID

func (h ModelHelper) SetID(id int64) error

type NullBool

type NullBool struct {
	sql.NullBool
}

NullBool is an alias for sql.NullBool data type

func InvalidNullBool

func InvalidNullBool() NullBool

NullBool convenience initializer for invalid (nil)

func NewNullBool

func NewNullBool(b bool) NullBool

NullBool convenience initializer

func (NullBool) IsNull

func (n NullBool) IsNull() bool

NullBool Nullable conformance

func (NullBool) MarshalJSON

func (n NullBool) MarshalJSON() ([]byte, error)

MarshalJSON for NullBool

func (*NullBool) UnmarshalJSON

func (n *NullBool) UnmarshalJSON(b []byte) error

UnmarshalJSON for NullBool

type NullFloat64

type NullFloat64 struct {
	sql.NullFloat64
}

NullFloat64 is an alias for sql.NullFloat64 data type

func InvalidNullFloat64

func InvalidNullFloat64() NullFloat64

NullFloat64 convenience initializer for invalid (nil)

func NewNullFloat64

func NewNullFloat64(f float64) NullFloat64

NullFloat64 convenience initializer

func (NullFloat64) IsNull

func (n NullFloat64) IsNull() bool

NullFloat64 Nullable conformance

func (NullFloat64) MarshalJSON

func (n NullFloat64) MarshalJSON() ([]byte, error)

MarshalJSON for NullFloat64

func (*NullFloat64) UnmarshalJSON

func (n *NullFloat64) UnmarshalJSON(b []byte) error

UnmarshalJSON for NullFloat64

type NullInt64

type NullInt64 struct {
	sql.NullInt64
}

NullInt64 is an alias for sql.NullInt64 data type

func InvalidNullInt64

func InvalidNullInt64() NullInt64

NullInt64 convenience initializer for invalid (nil)

func NewNullInt64

func NewNullInt64(i int64) NullInt64

NullInt64 convenience initializer

func (NullInt64) Int

func (n NullInt64) Int() (int64, error)

func (NullInt64) IsNull

func (n NullInt64) IsNull() bool

NullInt64 Nullable conformance

func (NullInt64) MarshalJSON

func (n NullInt64) MarshalJSON() ([]byte, error)

MarshalJSON for NullInt64

func (*NullInt64) UnmarshalJSON

func (n *NullInt64) UnmarshalJSON(b []byte) error

UnmarshalJSON for NullInt64

type NullString

type NullString struct {
	sql.NullString
}

NullString is an alias for sql.NullString data type

func InvalidNullString

func InvalidNullString() NullString

NullString convenience initializer for invalid (nil)

func NewNullString

func NewNullString(s string) NullString

NullString convenience initializer

func (NullString) IsNull

func (n NullString) IsNull() bool

NullString Nullable conformance

func (NullString) MarshalJSON

func (n NullString) MarshalJSON() ([]byte, error)

MarshalJSON for NullString

func (*NullString) UnmarshalJSON

func (n *NullString) UnmarshalJSON(b []byte) error

UnmarshalJSON for NullString

type NullTime

type NullTime struct {
	pq.NullTime
}

NullTime is an alias for mysql.NullTime data type

func InvalidNullTime

func InvalidNullTime() NullTime

NullTime convenience initializer for invalid (nil)

func NewNullTime

func NewNullTime(t time.Time) NullTime

NullTime convenience initializer

func (NullTime) IsNull

func (n NullTime) IsNull() bool

NullTime Nullable conformance

func (NullTime) MarshalJSON

func (n NullTime) MarshalJSON() ([]byte, error)

MarshalJSON for NullTime

func (*NullTime) UnmarshalJSON

func (n *NullTime) UnmarshalJSON(b []byte) error

UnmarshalJSON for NullTime

type Nullable

type Nullable interface {
	IsNull() bool
}

type Query

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

func DeleteAll

func DeleteAll(c QueryClient, table string) *Query

func DeleteQuery

func DeleteQuery(c QueryClient, tableName string) *Query

func InsertQuery

func InsertQuery(c QueryClient, tableName string, attrs map[string]interface{}) *Query

func Select

func Select(c QueryClient, tableName string, cols ...string) *Query

func SelectQuery

func SelectQuery(c QueryClient, tableName string, cols ...string) *Query

func SubQuery

func SubQuery() *Query

func UpdateAll

func UpdateAll(c QueryClient, table string, attrs Attrs) *Query

func UpdateQuery

func UpdateQuery(c QueryClient, tableName string, attrs map[string]interface{}) *Query

func (*Query) And

func (q *Query) And(and *Query) *Query

wraps on top of Ors

func (*Query) Exec

func (q *Query) Exec(ctx context.Context) (*QueryResult, error)

Exec() executes the query either with db.Exec() (RowsAffected) or db.Query() (Rows) depending on the query

Select returns Rows

Insert returns the Rows which represents the id of the inserted record by default

Delete and Update return RowsAffected unless the Returning() function is called on the query, then it returns Rows

func (*Query) Limit

func (q *Query) Limit(i int) *Query

limit for select query, must be > 0

func (*Query) Or

func (q *Query) Or(or *Query) *Query

func (*Query) OrderBy

func (q *Query) OrderBy(bys ...string) *Query

pass strings like "field_name ASC"

func (*Query) Returning

func (q *Query) Returning(cols ...string) *Query

default returns

func (*Query) Scan

func (q *Query) Scan(ctx context.Context, ptr interface{}) error

func (*Query) Slice

func (q *Query) Slice(ctx context.Context, outSlicePtr interface{}) error

func (*Query) Where

func (q *Query) Where(attrs map[string]interface{}) *Query

func (*Query) WhereNot

func (q *Query) WhereNot(attrs map[string]interface{}) *Query

func (*Query) WhereRaw

func (q *Query) WhereRaw(raw string, vals ...interface{}) *Query

Instead of the where clause using field = $1 use field = %v

type QueryClient

type QueryClient interface {
	Started() bool
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}

type QueryResult

type QueryResult struct {
	*sql.Rows
	RowsAffected int64
}

func RawQuery

func RawQuery(ctx context.Context, c QueryClient, q string, args ...interface{}) (*QueryResult, error)

func (*QueryResult) Scan

func (r *QueryResult) Scan(ctx context.Context, ptr interface{}) error

send in the pointer to scan a single value from a single row

func (*QueryResult) Slice

func (r *QueryResult) Slice(ctx context.Context, slicePtr interface{}) error

Pass in a pointer to a slice to convert the rows into

type Range

type Range struct {
	Start interface{}
	End   interface{}
}

type SliceModelProvider

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

Bulk provider for slices of Models

func NewSliceModelProvider

func NewSliceModelProvider(arr []Model) *SliceModelProvider

func (*SliceModelProvider) Cap

func (p *SliceModelProvider) Cap() int

func (*SliceModelProvider) NextModel

func (p *SliceModelProvider) NextModel() Model

type StringsBuilder

type StringsBuilder struct {
	strings.Builder
}

func (*StringsBuilder) WriteStrings

func (b *StringsBuilder) WriteStrings(strs ...string)

type Tx

type Tx struct {
	*sql.Tx
}

func (*Tx) Delete

func (tx *Tx) Delete(ctx context.Context, v Model) (int64, error)

func (*Tx) DeleteAll

func (tx *Tx) DeleteAll(table string) *Query

func (*Tx) DeleteReturning

func (tx *Tx) DeleteReturning(ctx context.Context, v Model) error

func (*Tx) Insert

func (tx *Tx) Insert(ctx context.Context, v Model, cols ...string) error

func (*Tx) InsertReturning

func (tx *Tx) InsertReturning(ctx context.Context, v Model, cols ...string) error

func (*Tx) RawQuery

func (tx *Tx) RawQuery(ctx context.Context, q string, args ...interface{}) (*QueryResult, error)

func (*Tx) RawSelect

func (tx *Tx) RawSelect(ctx context.Context, outSlicePtr interface{}, q string, args ...interface{}) error

func (*Tx) Save

func (tx *Tx) Save(ctx context.Context, v Model, cols ...string) error

func (*Tx) Select

func (tx *Tx) Select(tableName string, cols ...string) *Query

func (*Tx) Started

func (tx *Tx) Started() bool

func (*Tx) Update

func (tx *Tx) Update(ctx context.Context, v Model, cols ...string) error

func (*Tx) UpdateAll

func (tx *Tx) UpdateAll(table string, attrs Attrs) *Query

func (*Tx) UpdateReturning

func (tx *Tx) UpdateReturning(ctx context.Context, v Model, cols ...string) error

Jump to

Keyboard shortcuts

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