common

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: Apache-2.0 Imports: 9 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	// Service is the base application
	Service *Service
	// DB is a connection to the underlying Postgres database
	DB sql.DB
	// Engine is the underlying KwilDB engine, capable of storing and
	// executing against
	// Kuneiform schemas
	Engine Engine
}

App is an application that can modify and query the local database instance.

type Attribute added in v0.7.0

type Attribute struct {
	Type  AttributeType `json:"type"`
	Value string        `json:"value,omitempty"`
}

Attribute is a column attribute. These are constraints and default values.

func (*Attribute) Clean added in v0.7.0

func (a *Attribute) Clean() error

func (*Attribute) Copy added in v0.7.0

func (a *Attribute) Copy() *Attribute

Copy returns a copy of the attribute

type AttributeType added in v0.7.0

type AttributeType string

AttributeType is a type of attribute (e.g. PRIMARY_KEY, UNIQUE, NOT_NULL, DEFAULT, MIN, MAX, MIN_LENGTH, MAX_LENGTH)

const (
	PRIMARY_KEY AttributeType = "PRIMARY_KEY"
	UNIQUE      AttributeType = "UNIQUE"
	NOT_NULL    AttributeType = "NOT_NULL"
	DEFAULT     AttributeType = "DEFAULT"
	MIN         AttributeType = "MIN"
	MAX         AttributeType = "MAX"
	MIN_LENGTH  AttributeType = "MIN_LENGTH"
	MAX_LENGTH  AttributeType = "MAX_LENGTH" // is this kwil custom?
)

Attribute Types

func (*AttributeType) Clean added in v0.7.0

func (a *AttributeType) Clean() error

Clean validates rules about the data in the struct (naming conventions, syntax, etc.).

func (*AttributeType) IsValid added in v0.7.0

func (a *AttributeType) IsValid() bool

func (AttributeType) String added in v0.7.0

func (a AttributeType) String() string

type Column added in v0.7.0

type Column struct {
	Name       string       `json:"name"`
	Type       DataType     `json:"type"`
	Attributes []*Attribute `json:"attributes,omitempty"`
}

Column is a column in a table.

func (*Column) Clean added in v0.7.0

func (c *Column) Clean() error

func (*Column) Copy added in v0.7.0

func (c *Column) Copy() *Column

Copy returns a copy of the column

type DataType added in v0.7.0

type DataType string

DataType is a type of data (e.g. NULL, TEXT, INT, BLOB, BOOLEAN)

const (
	NULL DataType = "NULL"
	TEXT DataType = "TEXT"
	INT  DataType = "INT"
	BLOB DataType = "BLOB"
	BOOL DataType = "BOOLEAN"
)

Data types

func (*DataType) Clean added in v0.7.0

func (d *DataType) Clean() error

Clean validates rules about the data in the struct (naming conventions, syntax, etc.).

func (*DataType) IsNumeric added in v0.7.0

func (d *DataType) IsNumeric() bool

func (*DataType) IsText added in v0.7.0

func (d *DataType) IsText() bool

will check if the data type is a text type

func (*DataType) IsValid added in v0.7.0

func (d *DataType) IsValid() bool

func (DataType) String added in v0.7.0

func (d DataType) String() string

type Engine

type Engine interface {
	// CreateDataset deploys a new dataset from a schema.
	// The dataset will be owned by the caller.
	CreateDataset(ctx context.Context, tx sql.DB, schema *Schema, caller []byte) error
	// DeleteDataset deletes a dataset.
	// The caller must be the owner of the dataset.
	DeleteDataset(ctx context.Context, tx sql.DB, dbid string, caller []byte) error
	// Procedure executes a procedure in a dataset. It can be given
	// either a readwrite or readonly database transaction. If it is
	// given a read-only transaction, it will not be able to execute
	// any procedures that are not `view`.
	Procedure(ctx context.Context, tx sql.DB, options *ExecutionData) (*sql.ResultSet, error)
	// GetSchema returns the schema of a dataset.
	// It will return an error if the dataset does not exist.
	GetSchema(ctx context.Context, dbid string) (*Schema, error)
	// ListDatasets returns a list of all datasets on the network.
	ListDatasets(ctx context.Context, caller []byte) ([]*types.DatasetIdentifier, error)
	// Execute executes a SQL statement on a dataset.
	// It uses Kwil's SQL dialect.
	Execute(ctx context.Context, tx sql.DB, dbid, query string, values map[string]any) (*sql.ResultSet, error)
}

type ExecutionData

type ExecutionData struct {
	// Dataset is the DBID of the dataset that was called.
	// Even if a procedure in another dataset is called, this will
	// always be the original dataset.
	Dataset string

	// Procedure is the original procedure that was called.
	// Even if a nested procedure is called, this will always be the
	// original procedure.
	Procedure string

	// Args are the arguments that were passed to the procedure.
	// Currently these are all string or untyped nil values.
	Args []any

	// Signer is the address of public key that signed the incoming
	// transaction.
	Signer []byte

	// Caller is a string identifier for the signer.
	// It is derived from the signer's registered authenticator.
	// It is injected as a variable for usage in the query, under
	// the variable name "@caller".
	Caller string
}

ExecutionOptions is contextual data that is passed to a procedure during call / execution. It is scoped to the lifetime of a single execution.

func (*ExecutionData) Clean

func (e *ExecutionData) Clean() error

type Extension added in v0.7.0

type Extension struct {
	// Name is the name of the extension registered in the node
	Name string `json:"name"`
	// Initialization is a list of key value pairs that are used to initialize the extension
	Initialization []*ExtensionConfig `json:"initialization"`
	// Alias is the alias of the extension, which is how its instance is referred to in the schema
	Alias string `json:"alias"`
}

Extension defines what extensions the schema uses, and how they are initialized.

func (*Extension) Clean added in v0.7.0

func (e *Extension) Clean() error

Clean validates rules about the data in the struct (naming conventions, syntax, etc.).

func (*Extension) CleanMap added in v0.7.0

func (e *Extension) CleanMap() map[string]string

CleanMap returns a map of the config values for the extension. Since the Kueiform parser parses all values as strings, it cleans the single quotes from the values.

type ExtensionConfig added in v0.7.0

type ExtensionConfig struct {
	Key   string `json:"name"`
	Value string `json:"value"`
}

ExtensionConfig is a key value pair that represents a configuration value for an extension

type ForeignKey added in v0.7.0

type ForeignKey struct {
	// ChildKeys are the columns that are referencing another.
	// For example, in FOREIGN KEY (a) REFERENCES tbl2(b), "a" is the child key
	ChildKeys []string `json:"child_keys"`

	// ParentKeys are the columns that are being referred to.
	// For example, in FOREIGN KEY (a) REFERENCES tbl2(b), "b" is the parent key
	ParentKeys []string `json:"parent_keys"`

	// ParentTable is the table that holds the parent columns.
	// For example, in FOREIGN KEY (a) REFERENCES tbl2(b), "tbl2" is the parent table
	ParentTable string `json:"parent_table"`

	// Action refers to what the foreign key should do when the parent is altered.
	// This is NOT the same as a database action;
	// however sqlite's docs refer to these as actions,
	// so we should be consistent with that.
	// For example, ON DELETE CASCADE is a foreign key action
	Actions []*ForeignKeyAction `json:"actions"`
}

ForeignKey is a foreign key in a table.

func (*ForeignKey) Clean added in v0.7.0

func (f *ForeignKey) Clean() error

Clean runs a set of validations and cleans the foreign key

func (*ForeignKey) Copy added in v0.7.0

func (f *ForeignKey) Copy() *ForeignKey

Copy returns a copy of the foreign key

type ForeignKeyAction added in v0.7.0

type ForeignKeyAction struct {
	// On can be either "UPDATE" or "DELETE"
	On ForeignKeyActionOn `json:"on"`

	// Do specifies what a foreign key action should do
	Do ForeignKeyActionDo `json:"do"`
}

ForeignKeyAction is used to specify what should occur if a parent key is updated or deleted

func (*ForeignKeyAction) Clean added in v0.7.0

func (f *ForeignKeyAction) Clean() error

Clean runs a set of validations and cleans the attributes in ForeignKeyAction

func (*ForeignKeyAction) Copy added in v0.7.0

Copy returns a copy of the foreign key action

type ForeignKeyActionDo added in v0.7.0

type ForeignKeyActionDo string

ForeignKeyActionDo specifies what should be done when a foreign key action is triggered.

const (
	// DO_NO_ACTION does nothing when a parent key is altered
	DO_NO_ACTION ForeignKeyActionDo = "NO ACTION"

	// DO_RESTRICT prevents the parent key from being altered
	DO_RESTRICT ForeignKeyActionDo = "RESTRICT"

	// DO_SET_NULL sets the child key(s) to NULL
	DO_SET_NULL ForeignKeyActionDo = "SET NULL"

	// DO_SET_DEFAULT sets the child key(s) to their default values
	DO_SET_DEFAULT ForeignKeyActionDo = "SET DEFAULT"

	// DO_CASCADE updates the child key(s) or deletes the records (depending on the action type)
	DO_CASCADE ForeignKeyActionDo = "CASCADE"
)

ForeignKeyActionDo types

func (*ForeignKeyActionDo) Clean added in v0.7.0

func (f *ForeignKeyActionDo) Clean() error

Clean checks the validity or the string, and converts it to the correct case

func (*ForeignKeyActionDo) IsValid added in v0.7.0

func (f *ForeignKeyActionDo) IsValid() bool

IsValid checks if the string is a valid ForeignKeyActionDo

func (ForeignKeyActionDo) String added in v0.7.0

func (f ForeignKeyActionDo) String() string

String returns the ForeignKeyActionDo as a string

type ForeignKeyActionOn added in v0.7.0

type ForeignKeyActionOn string

ForeignKeyActionOn specifies when a foreign key action should occur. It can be either "UPDATE" or "DELETE".

const (
	// ON_UPDATE is used to specify an action should occur when a parent key is updated
	ON_UPDATE ForeignKeyActionOn = "UPDATE"
	// ON_DELETE is used to specify an action should occur when a parent key is deleted
	ON_DELETE ForeignKeyActionOn = "DELETE"
)

ForeignKeyActionOn types

func (*ForeignKeyActionOn) Clean added in v0.7.0

func (f *ForeignKeyActionOn) Clean() error

Clean checks whether the string is valid, and will convert it to the correct case.

func (*ForeignKeyActionOn) IsValid added in v0.7.0

func (f *ForeignKeyActionOn) IsValid() bool

IsValid checks whether or not the string is a valid ForeignKeyActionOn

func (ForeignKeyActionOn) String added in v0.7.0

func (f ForeignKeyActionOn) String() string

String returns the ForeignKeyActionOn as a string

type Index added in v0.7.0

type Index struct {
	Name    string    `json:"name"`
	Columns []string  `json:"columns"`
	Type    IndexType `json:"type"`
}

Index is an index on a table.

func (*Index) Clean added in v0.7.0

func (i *Index) Clean() error

Clean validates rules about the data in the struct (naming conventions, syntax, etc.).

func (*Index) Copy added in v0.7.0

func (i *Index) Copy() *Index

Copy returns a copy of the index.

type IndexType added in v0.7.0

type IndexType string

IndexType is a type of index (e.g. BTREE, UNIQUE_BTREE, PRIMARY)

const (
	// BTREE is the default index type.
	BTREE IndexType = "BTREE"
	// UNIQUE_BTREE is a unique BTREE index.
	UNIQUE_BTREE IndexType = "UNIQUE_BTREE"
	// PRIMARY is a primary index.
	// Only one primary index is allowed per table.
	// A primary index cannot exist on a table that also has a primary key.
	PRIMARY IndexType = "PRIMARY"
)

index types

func (*IndexType) Clean added in v0.7.0

func (i *IndexType) Clean() error

func (*IndexType) IsValid added in v0.7.0

func (i *IndexType) IsValid() bool

func (IndexType) String added in v0.7.0

func (i IndexType) String() string

type Modifier added in v0.7.0

type Modifier string

Modifier modifies the access to a procedure.

const (
	// View means that an action does not modify the database.
	ModifierView Modifier = "VIEW"

	// Authenticated requires that the caller is identified.
	ModifierAuthenticated Modifier = "AUTHENTICATED"

	// Owner requires that the caller is the owner of the database.
	ModifierOwner Modifier = "OWNER"
)

func (*Modifier) Clean added in v0.7.0

func (m *Modifier) Clean() error

Clean validates rules about the data in the struct (naming conventions, syntax, etc.).

func (*Modifier) IsValid added in v0.7.0

func (m *Modifier) IsValid() bool

func (Modifier) String added in v0.7.0

func (m Modifier) String() string

type Procedure added in v0.7.0

type Procedure struct {
	Name        string     `json:"name"`
	Annotations []string   `json:"annotations,omitempty"`
	Args        []string   `json:"inputs"`
	Public      bool       `json:"public"`
	Modifiers   []Modifier `json:"modifiers"`
	Statements  []string   `json:"statements"`
}

Procedure is a procedure in a database schema. These are defined by Kuneiform's `action` keyword.

func (*Procedure) Clean added in v0.7.0

func (p *Procedure) Clean() error

Clean validates rules about the data in the struct (naming conventions, syntax, etc.).

func (*Procedure) IsView added in v0.7.0

func (p *Procedure) IsView() bool

IsView returns true if the procedure has a view modifier.

type Schema added in v0.7.0

type Schema struct {
	// Name is the name of the schema given by the deployer.
	Name string `json:"name"`
	// Owner is the identifier (generally an address in bytes or public key) of the owner of the schema
	Owner      []byte       `json:"owner"`
	Extensions []*Extension `json:"extensions"`
	Tables     []*Table     `json:"tables"`
	Procedures []*Procedure `json:"procedures"`
}

Schema is a database schema that contains tables, procedures, and extensions.

func (*Schema) Clean added in v0.7.0

func (s *Schema) Clean() error

Clean validates rules about the data in the struct (naming conventions, syntax, etc.).

func (*Schema) DBID added in v0.7.0

func (s *Schema) DBID() string

type Service

type Service struct {
	// Logger is a logger for the application
	Logger log.SugaredLogger
	// ExtensionConfigs is a map of the nodes extensions and local
	// configurations.
	// It maps: extension_name -> config_key -> config_value
	ExtensionConfigs map[string]map[string]string
}

Service provides access to general application information to extensions.

type Table added in v0.7.0

type Table struct {
	Name        string        `json:"name"`
	Columns     []*Column     `json:"columns"`
	Indexes     []*Index      `json:"indexes,omitempty"`
	ForeignKeys []*ForeignKey `json:"foreign_keys"`
}

Table is a table in a database schema.

func (*Table) Clean added in v0.7.0

func (t *Table) Clean() error

Clean validates rules about the data in the struct (naming conventions, syntax, etc.).

func (*Table) Copy added in v0.7.0

func (t *Table) Copy() *Table

Copy returns a copy of the table

func (*Table) GetPrimaryKey added in v0.7.0

func (t *Table) GetPrimaryKey() ([]string, error)

GetPrimaryKey returns the names of the column(s) that make up the primary key. If there is more than one, or no primary key, an error is returned.

Directories

Path Synopsis
Package sql defines common type required by SQL database implementations and consumers.
Package sql defines common type required by SQL database implementations and consumers.

Jump to

Keyboard shortcuts

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