xdb

package module
v0.13.158 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2024 License: MIT Imports: 22 Imported by: 5

README

xdb

Extensions around standard sql package

Usage

go get github.com/effective-security/xdb

Schema generator

Usage: xdbcli <command>

SQL schema tool

Flags:
  -h, --help                 Show context-sensitive help.
  -D, --debug                Enable debug mode
      --o="table"            Print output format: json|yaml|table
      --sql-source=STRING    SQL sources, if not provided, will be used from XDB_DATASOURCE env var

Commands:
  schema generate        generate Go model for database schema
  schema columns         prints database schema
  schema tables          prints database tables and dependencies
  schema views           prints database views and dependencies
  schema foreign-keys    prints Foreign Keys

Run "xdbcli <command> --help" for more information on a command.

Examples:

export XDB_DATASOURCE="postgres://${XDB_PG_USER}:${XDB_PG_PASSWORD}@${XDB_PG_HOST}:${XDB_PG_PORT}?sslmode=disable" 

Print tables:

bin/xdbcli schema tables --db testdb
public.org
public.orgmember
public.schema_migrations
public.user

Print columns

bin/xdbcli schema columns --db testdb --table orgmember --dependencies
Schema: public
Table: org

       NAME      |           TYPE           | NULL | MAX | REF  
-----------------+--------------------------+------+-----+------
  id             | bigint                   | NO   |     |      
  name           | character varying        | NO   | 64  |      
  email          | character varying        | NO   | 160 |      
  billing_email  | character varying        | NO   | 160 |      
  company        | character varying        | NO   | 64  |      
  street_address | character varying        | NO   | 256 |      
  city           | character varying        | NO   | 32  |      
  postal_code    | character varying        | NO   | 16  |      
  region         | character varying        | NO   | 16  |      
  country        | character varying        | NO   | 16  |      
  phone          | character varying        | NO   | 32  |      
  created_at     | timestamp with time zone | YES  |     |      
  updated_at     | timestamp with time zone | YES  |     |      
  quota          | jsonb                    | YES  |     |      
  settings       | jsonb                    | YES  |     | 

Print FK

bin/xdbcli schema foreign-keys --db testdb                      
           NAME          | SCHEMA |   TABLE   | COLUMN  | FK SCHEMA | FK TABLE | FK COLUMN  
-------------------------+--------+-----------+---------+-----------+----------+------------
  orgmember_org_id_fkey  | public | orgmember | org_id  | public    | org      | id         
  orgmember_user_id_fkey | public | orgmember | user_id | public    | user     | id

Generate model

xdbcli --sql-source=$(DATASOURCE) \
  schema generate \
  --dependencies \
  --db=testdb \
  --view=vwMembership \
  --out-model=./testdata/e2e/postgres/model \
  --out-schema=./testdata/e2e/postgres/schema

Documentation

Index

Constants

View Source
const (
	MaxLenForName     = 64
	MaxLenForEmail    = 160
	MaxLenForShortURL = 256
)

Max values, common for strings

View Source
const DefaultPageSize = 500

DefaultPageSize is the default page size

Variables

View Source
var DefaultTimeFormat = "2006-01-02T15:04:05.999Z07:00"

DefaultTimeFormat is the default format for Time.String()

View Source
var DefaultTrucate = time.Millisecond

DefaultTrucate is the default time to truncate as Postgres time precision is default to 6 However, JavaScript and AWS accept time milliseconds only, 3 digits, so we truncate to 3

View Source
var MergeOpts = mergo.WithTransformers(transformers{})

MergeOpts is a mergo option to merge structs

Functions

func ExecuteListQuery added in v0.7.0

func ExecuteListQuery[T any, TPointer RowPointer[T]](ctx context.Context, sql DB, query string, args ...any) ([]TPointer, error)

ExecuteListQuery runs a query and returns a list of models

func IDString

func IDString(id uint64) string

IDString returns string id

func IsNotFoundError

func IsNotFoundError(err error) bool

IsNotFoundError returns true, if error is NotFound

func Merge added in v0.5.0

func Merge(dst any, src any) error

Merge merges two structs with xdb types

func NullTime

func NullTime(val *time.Time) sql.NullTime

NullTime from *time.Time

func Open

func Open(dataSource, database string) (*sql.DB, string, string, error)

Open returns an SQL connection instance, provider name or error

func ParseUint

func ParseUint(id string) (uint64, error)

ParseUint returns id from the string

func QueryRow added in v0.6.0

func QueryRow[T any, TPointer RowPointer[T]](ctx context.Context, sql DB, query string, args ...any) (TPointer, error)

QueryRow runs a query and returns a single model

func String

func String(val *string) string

String returns string

func TimePtr

func TimePtr(val Time) *time.Time

TimePtr returns nil if time is zero, or pointer with a value

func Validate

func Validate(m any) error

Validate returns error if the model is not valid

Types

type Bool added in v0.8.0

type Bool bool

Bool represents SQL bool NULL

func (Bool) MarshalJSON added in v0.8.0

func (v Bool) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface

func (*Bool) Scan added in v0.8.0

func (v *Bool) Scan(value any) error

Scan implements the Scanner interface.

func (Bool) String added in v0.8.0

func (v Bool) String() string

func (*Bool) UnmarshalJSON added in v0.8.0

func (v *Bool) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (Bool) Value added in v0.8.0

func (v Bool) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type DB

type DB interface {
	// QueryContext executes a query that returns rows, typically a SELECT.
	// The args are for any placeholder parameters in the query.
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	// QueryRowContext executes a query that is expected to return at most one row.
	// QueryRowContext always returns a non-nil value. Errors are deferred until
	// Row's Scan method is called.
	// If the query selects no rows, the *Row's Scan will return ErrNoRows.
	// Otherwise, the *Row's Scan scans the first selected row and discards
	// the rest.
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
	// ExecContext executes a query without returning any rows.
	// The args are for any placeholder parameters in the query.
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}

DB provides interface for Db operations It's an interface accepted by Query, QueryRow and Exec methods. Both sql.DB, sql.Conn and sql.Tx can be passed as DB interface.

type Float added in v0.8.0

type Float float64

Float represents SQL float64 NULL

func (Float) MarshalJSON added in v0.8.0

func (v Float) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface

func (*Float) Scan added in v0.8.0

func (v *Float) Scan(value any) error

Scan implements the Scanner interface.

func (Float) String added in v0.8.0

func (v Float) String() string

func (*Float) UnmarshalJSON added in v0.8.0

func (v *Float) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (Float) Value added in v0.8.0

func (v Float) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type ID

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

ID defines a type to convert between internal uint64 and external string representations of ID

func MustID

func MustID(val string) ID

MustID returns ID or panics if the value is invalid

func NewID

func NewID(id uint64) ID

NewID returns ID

func ParseID

func ParseID(val string) (ID, error)

ParseID returns ID or empty if val is not valid ID

func TryParseID added in v0.8.0

func TryParseID(val string) ID

TryParseID returns ID or empty if val is not valid ID

func (ID) Invalid

func (v ID) Invalid() bool

Invalid returns if ID is invalid

func (ID) IsZero

func (v ID) IsZero() bool

IsZero returns if ID is 0

func (ID) MarshalJSON

func (v ID) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface

func (*ID) Reset

func (v *ID) Reset()

Reset the value

func (*ID) Scan

func (v *ID) Scan(value any) error

Scan implements the Scanner interface.

func (*ID) Set

func (v *ID) Set(val string) error

Set the value

func (ID) String

func (v ID) String() string

func (ID) UInt64

func (v ID) UInt64() uint64

UInt64 returns uint64 value

func (*ID) UnmarshalJSON

func (v *ID) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (ID) Valid

func (v ID) Valid() bool

Valid returns if ID is valid

func (ID) Value

func (v ID) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type ID32 added in v0.8.0

type ID32 uint32

ID32 defines a type to convert between internal uint32 and NULL values in DB

func (ID32) MarshalJSON added in v0.8.0

func (v ID32) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface

func (*ID32) Scan added in v0.8.0

func (v *ID32) Scan(value any) error

Scan implements the Scanner interface.

func (ID32) String added in v0.8.0

func (v ID32) String() string

func (*ID32) UnmarshalJSON added in v0.8.0

func (v *ID32) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (ID32) Value added in v0.8.0

func (v ID32) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type IDArray

type IDArray []ID

IDArray defines a list of IDArray

func NewIDArray added in v0.13.1

func NewIDArray(vals []uint64) IDArray

NewIDArray returns IDArray

func (IDArray) Add added in v0.13.1

func (n IDArray) Add(id ID) IDArray

Add returns new list

func (IDArray) Concat added in v0.13.1

func (n IDArray) Concat(other IDArray) IDArray

Concat returns new list

func (IDArray) List added in v0.13.1

func (n IDArray) List() []uint64

List returns list of IDs

func (*IDArray) Scan

func (n *IDArray) Scan(value any) error

Scan implements the Scanner interface for IDs

func (IDArray) Strings

func (n IDArray) Strings() []string

Strings returns string representation of IDs

func (IDArray) Value

func (n IDArray) Value() (driver.Value, error)

Value implements the driver Valuer interface for IDs

type IDGenerator

type IDGenerator interface {
	// NextID generates a next unique ID.
	NextID() ID
	IDTime(id uint64) time.Time
}

IDGenerator defines an interface to generate unique ID accross the cluster

type Int32 added in v0.8.0

type Int32 int32

Int32 represents SQL int NULL

func (Int32) MarshalJSON added in v0.8.0

func (v Int32) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface

func (*Int32) Scan added in v0.8.0

func (v *Int32) Scan(value any) error

Scan implements the Scanner interface.

func (Int32) String added in v0.8.0

func (v Int32) String() string

func (*Int32) UnmarshalJSON added in v0.8.0

func (v *Int32) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (Int32) Value added in v0.8.0

func (v Int32) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Int64 added in v0.8.1

type Int64 int64

Int64 represents SQL int64 NULL

func (Int64) MarshalJSON added in v0.8.1

func (v Int64) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface

func (*Int64) Scan added in v0.8.1

func (v *Int64) Scan(value any) error

Scan implements the Scanner interface.

func (Int64) String added in v0.8.1

func (v Int64) String() string

func (*Int64) UnmarshalJSON added in v0.8.1

func (v *Int64) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (Int64) Value added in v0.8.1

func (v Int64) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Metadata

type Metadata map[string]string

Metadata de/encodes the string map to/from a SQL string.

func (*Metadata) Merge added in v0.13.1

func (n *Metadata) Merge(m Metadata) *Metadata

Merge merges metadata

func (*Metadata) Scan

func (n *Metadata) Scan(value any) error

Scan implements the Scanner interface.

func (Metadata) Value

func (n Metadata) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type MigrationConfig

type MigrationConfig struct {
	Source         string
	ForceVersion   int
	MigrateVersion int
}

MigrationConfig defines migration configuration

type NULLString

type NULLString string

NULLString de/encodes the string a SQL string.

func (*NULLString) Scan

func (ns *NULLString) Scan(value any) error

Scan implements the Scanner interface.

func (NULLString) String added in v0.13.1

func (ns NULLString) String() string

String returns string

func (NULLString) Value

func (ns NULLString) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Provider

type Provider interface {
	IDGenerator
	DB
	Tx

	// Name returns provider name: postgres, sqlserver, etc
	Name() string

	// DB returns underlying DB connection
	DB() DB
	// Tx returns underlying DB transaction
	Tx() Tx

	// Close connection and release resources
	Close() (err error)

	BeginTx(ctx context.Context, opts *sql.TxOptions) (Provider, error)
}

Provider provides complete DB access

func NewProvider

func NewProvider(dataSource, dbName string, idGen flake.IDGenerator, migrateCfg *MigrationConfig) (Provider, error)

NewProvider creates a Provider instance

type Result

type Result[T any, TPointer RowPointer[T]] struct {
	Rows       []TPointer
	NextOffset uint32
	Limit      uint32
}

Result describes the result of a list query

func (*Result[T, RowPointer]) Execute added in v0.7.0

func (p *Result[T, RowPointer]) Execute(ctx context.Context, sql DB, query string, args ...any) error

Execute runs a query and populates the result with a list of models and the next offset, if there are more rows to fetch

type Row

type Row interface {
	// Scan copies the columns from the matched row into the values
	// pointed at by dest. See the documentation on Rows.Scan for details.
	// If more than one row matches the query,
	// Scan uses the first row and discards the rest. If no row matches
	// the query, Scan returns ErrNoRows.
	Scan(dest ...any) error
	// Err provides a way for wrapping packages to check for
	// query errors without calling Scan.
	// Err returns the error, if any, that was encountered while running the query.
	// If this error is not nil, this error will also be returned from Scan.
	Err() error
}

Row defines an interface for DB row

type RowPointer

type RowPointer[T any] interface {
	*T
	RowScanner
}

RowPointer defines a generic interface to scan a single row

type RowScanner

type RowScanner interface {
	ScanRow(rows Row) error
}

RowScanner defines an interface to scan a single row

type Rows

type Rows interface {
	io.Closer
	Row

	// Next prepares the next result row for reading with the Scan method. It
	// returns true on success, or false if there is no next result row or an error
	// happened while preparing it. Err should be consulted to distinguish between
	// the two cases.
	//
	// Every call to Scan, even the first one, must be preceded by a call to Next.
	Next() bool
	// NextResultSet prepares the next result set for reading. It reports whether
	// there is further result sets, or false if there is no further result set
	// or if there is an error advancing to it. The Err method should be consulted
	// to distinguish between the two cases.
	//
	// After calling NextResultSet, the Next method should always be called before
	// scanning. If there are further result sets they may not have rows in the result
	// set.
	NextResultSet() bool
}

Rows defines an interface for DB rows

type SQLProvider

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

SQLProvider represents SQL client instance

func New

func New(name string, db *sql.DB, idGen flake.IDGenerator) (*SQLProvider, error)

New creates a Provider instance

func (*SQLProvider) BeginTx

func (p *SQLProvider) BeginTx(ctx context.Context, _ *sql.TxOptions) (Provider, error)

BeginTx starts a transaction.

The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginTx is canceled.

The provided TxOptions is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.

func (*SQLProvider) Close

func (p *SQLProvider) Close() (err error)

Close connection and release resources

func (*SQLProvider) Commit added in v0.2.0

func (p *SQLProvider) Commit() error

func (*SQLProvider) DB

func (p *SQLProvider) DB() DB

DB returns underlying DB connection

func (*SQLProvider) ExecContext added in v0.2.0

func (p *SQLProvider) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*SQLProvider) IDTime

func (p *SQLProvider) IDTime(id uint64) time.Time

IDTime returns time when ID was generated

func (*SQLProvider) Name added in v0.6.0

func (p *SQLProvider) Name() string

Name returns provider name

func (*SQLProvider) NextID

func (p *SQLProvider) NextID() ID

NextID returns unique ID

func (*SQLProvider) QueryContext added in v0.2.0

func (p *SQLProvider) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*SQLProvider) QueryRowContext added in v0.2.0

func (p *SQLProvider) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.

func (*SQLProvider) Rollback added in v0.2.0

func (p *SQLProvider) Rollback() error

func (*SQLProvider) Tx

func (p *SQLProvider) Tx() Tx

Tx returns underlying DB transaction

type Source added in v0.2.2

type Source struct {
	Source   string
	Driver   string
	Host     string
	User     string
	Password string
	Database string
	Params   map[string]string
}

Source describes connection info

func ParseConnectionString added in v0.2.2

func ParseConnectionString(dataSource string) (*Source, error)

ParseConnectionString return parsed Source from sqlserver://username:password@host/instance?param1=value&param2=value

type Strings

type Strings []string

Strings de/encodes the string slice to/from a SQL string.

func (*Strings) Scan

func (n *Strings) Scan(value any) error

Scan implements the Scanner interface.

func (Strings) Value

func (n Strings) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Time

type Time time.Time

Time implements sql.Time functionality and always returns UTC

func FromNow

func FromNow(after time.Duration) Time

FromNow returns Time in UTC after now, with Second presicions

func FromUnixMilli

func FromUnixMilli(tm int64) Time

FromUnixMilli returns Time from Unix milliseconds elapsed since January 1, 1970 UTC.

func Now

func Now() Time

Now returns Time in UTC

func ParseTime

func ParseTime(val string) Time

ParseTime returns Time from RFC3339 format

func UTC

func UTC(t time.Time) Time

UTC returns Time in UTC,

func (Time) Add

func (ns Time) Add(after time.Duration) Time

Add returns Time in UTC after this thime, with Second presicions

func (Time) IsNil

func (ns Time) IsNil() bool

IsNil reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.

func (Time) IsZero

func (ns Time) IsZero() bool

IsZero reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.

func (Time) MarshalJSON

func (ns Time) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. The time is a quoted string in RFC 3339 format, with sub-second precision added if present.

func (Time) Ptr

func (ns Time) Ptr() *time.Time

Ptr returns pointer to Time, or nil if the time is zero

func (*Time) Scan

func (ns *Time) Scan(value any) error

Scan implements the Scanner interface.

func (Time) String

func (ns Time) String() string

String returns string in RFC3339 format, if it's Zero time, an empty string is returned

func (Time) UTC

func (ns Time) UTC() time.Time

UTC returns t with the location set to UTC.

func (Time) UnixMilli

func (ns Time) UnixMilli() int64

UnixMilli returns t as a Unix time, the number of milliseconds elapsed since January 1, 1970 UTC.

func (*Time) UnmarshalJSON

func (ns *Time) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. The time is expected to be a quoted string in RFC 3339 format.

func (Time) Value

func (ns Time) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Tx

type Tx interface {
	DB

	Commit() error
	Rollback() error
}

Tx provides interface for Tx operations

type UUID added in v0.11.0

type UUID string

UUID de/encodes the string a SQL string.

func (*UUID) Scan added in v0.11.0

func (ns *UUID) Scan(value any) error

Scan implements the Scanner interface.

func (UUID) String added in v0.13.1

func (ns UUID) String() string

String returns string

func (UUID) Value added in v0.11.0

func (ns UUID) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Validator

type Validator interface {
	// Validate returns error if the model is not valid
	Validate() error
}

Validator provides schema validation interface

Directories

Path Synopsis
cmd
internal
cli
Package cli provides CLI app and global flags
Package cli provides CLI app and global flags
cli/clisuite
Package clisuite to test CLI commands
Package clisuite to test CLI commands
cli/schema
Package schema provides CLI commands
Package schema provides CLI commands
mocks
mockschema
Package mockschema is a generated GoMock package.
Package mockschema is a generated GoMock package.
mockxdb
Package mockxdb is a generated GoMock package.
Package mockxdb is a generated GoMock package.
pkg
flake
Package flake implements Snowflake, a distributed unique ID generator inspired by Twitter's Snowflake.
Package flake implements Snowflake, a distributed unique ID generator inspired by Twitter's Snowflake.
print
Package print provides helper package to print objects.
Package print provides helper package to print objects.
Package schema provides helper package to generate schema.
Package schema provides helper package to generate schema.
Package xsql is an SQL statement builder and executor.
Package xsql is an SQL statement builder and executor.

Jump to

Keyboard shortcuts

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