structql

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

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

Go to latest
Published: Sep 20, 2021 License: MIT Imports: 8 Imported by: 0

README

StructQL

This package abstracts the connection and management of a Postgres database server using native go structs.

StructQL Logs

StructQL uses github.com/inflowml/logger for all logging. These will appear as standard out. In order to turn off StructQL logs export SQL_LOG=0.

Storage Functions

Connect

Connects to the appropriate db based on the micro-service and returns the connection structure.

func Connect(config ConnectionConfig) (*Connection, error)
Close

Closes the connection to the database, must be called when the microservice is finished using the db. Connections should only be closed when the program terminates or is killed if possible.

func (conn *Connection) Close() error
CreateTableFromObject

Create table accepts the name of the table to be created and an interface representing the table columns.

func (conn *Connection) CreateTableFromObject(table string, object interface{}) error {

A table is created out of a native go struct with StructQL tags. For example in order to create a person table with columns id, name, age, DNA the following code would be appropriate.

type Person struct {
	ID   int32  `sql:"id" typ:"SERIAL" opt:"PRIMARY KEY"`
	Name string `sql:"name"`
	Age  int32  `sql:"age"`
	DNA  []byte `sql:"dna"`
}
...
err := conn.CreateTableFromObject("person", Person{})
if err != nil {
	// Handle Error
}
...
InsertObject

InsertObject accepts a table name and an object interface and inserts it into the database

func (conn *Connection) InsertObject(table string, object interface{}) error

The object interface must be tagged with the SQL Column names. Ref the following example of an acceptable struct which corresponds to a Person database table with columns id, name, age, and dna.

type Person struct {
	ID   int32  `sql:"id" typ:"SERIAL" opt:"PRIMARY KEY"`
	Name string `sql:"name"`
	Age  int32  `sql:"age"`
	DNA  []byte `sql:"dna"`
}

In this case every time a new row is inserted a unique id will be assigned in the id column of the table. This will be automatically done by Postgres.

SelectFrom

Accepts a struct type, and table name and returns the query as a slice of given struct. Note that the fields in the given struct are the columns that are listed in the SELECT <Columns> portion of the SQL query.

func (conn *Connection) SelectFrom(object interface{}, table string) (interface{}, error) 

The following is an example of a struct type that would result in the query for Name and Age from a table

type Person struct {
	ID   int32  `sql:"id" typ:"SERIAL" opt:"PRIMARY KEY"`
	Name string `sql:"name"`
	Age  int32  `sql:"age"`
}
SelectFromWhere

Accepts a struct type, table name, and conditional and returns the query as a slice of given struct. Note that the fields in the given struct are the columns that are listed in the SELECT portion of the SQL query. Additonally the conditional must be a string using standard SQL comparisons such as age >= 50 AND name == John

func (conn *Connection) SelectFromWhere(object interface{}, table string, conditional string) (interface{}, error) 

The following is an example of a struct type that would result in the query for Name and Age from a table

type Person struct {
	ID   int32  `sql:"id" typ:"SERIAL" opt:"PRIMARY KEY"`
	Name string `sql:"name"`
	Age  int32  `sql:"age"`
}

Testing Configurations

In order to run StructQL tests a local postgres server is required. One can be installed through by running sudo install.sh in the testutils directory. Once installed run test-srv.sh. Once you are finished with the server run sudo service postgresql stop

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

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

Connection wraps the sql.DB type.

func Connect

func Connect(creds ConnectionConfig) (*Connection, error)

Connect establishes and returns a connection to the SQL database specified by the ConnectionConfig

func (*Connection) Close

func (conn *Connection) Close() error

Close closes the connection to the Database receiver.

func (*Connection) CountRows

func (conn *Connection) CountRows(table string) (int64, error)

CountRows accepts a table name and returns the number of rows in that table.

func (*Connection) CountRowsWhere

func (conn *Connection) CountRowsWhere(table string, cond string) (int64, error)

CountRowsWhere accepts a table name and condition statement and returns the number of rows in that table that meet the condition

func (*Connection) CreateTableFromObject

func (conn *Connection) CreateTableFromObject(table string, object interface{}) error

CreateTableFromObject creates an SQL table with the given name from the type of the provided object using the Connection receiver. The provided object must be a structure where:

  1. Each field is annotated with an "sql" tag. Fields may also be annotated with a "typ" or "opt" tag to override their default settings: - The "sql" tag denotes the column name (e.g., "id"). - The "typ" tag denotes the column type (e.g., "SERIAL"). - The "opt" tag denotes column constraints (e.g., "PRIMARY KEY").
  2. One field must be a 32-bit integer that corresponds to the "id" column.

func (*Connection) DeleteObject

func (conn *Connection) DeleteObject(table string, object interface{}) error

DeleteObject deletes the given object from the specified table.

func (*Connection) DropTable

func (conn *Connection) DropTable(table string) error

DropTable drops the given table from the Connection receiver.

func (*Connection) InsertObject

func (conn *Connection) InsertObject(table string, object interface{}) (int, error)

InsertObject inserts the given object into the specified table and returns the record ID of the inserted row.

func (*Connection) Lock

func (conn *Connection) Lock() error

Lock will execute the SQL BEGIN command which aids in concurrent operations Unlock must be called once the transaction is complete.

func (*Connection) OldestEntry

func (conn *Connection) OldestEntry(object interface{}, table string, timestampCol string) (interface{}, error)

OldestEntry returns the oldest row in the given table

func (*Connection) SelectForUpdate

func (conn *Connection) SelectForUpdate(object interface{}, table string, cond string, args ...interface{}) ([]interface{}, error)

SelectForUpdate is to be used in conjuction with Lock and Unlock to facilitate row locking TODO add this to standard select but use arguments instead of new function

func (*Connection) SelectFrom

func (conn *Connection) SelectFrom(object interface{}, table string) ([]interface{}, error)

SelectFrom executes a SELECT FROM query on the Connection receiver over the given object type and table.

func (*Connection) SelectFromWhere

func (conn *Connection) SelectFromWhere(object interface{}, table string, cond string, args ...interface{}) ([]interface{}, error)

SelectFromWhere executes a SELECT FROM WHERE query on the Connection receiver over the given object type, table, and conditional. Additional arguments are substituted into the conditional in a style similar to printf().

func (*Connection) Unlock

func (conn *Connection) Unlock() error

Unlock will execute the SQL END command

func (*Connection) UpdateObject

func (conn *Connection) UpdateObject(table string, object interface{}) error

UpdateObject updates the given object in the specified table.

type ConnectionConfig

type ConnectionConfig struct {
	Host     string
	Port     string
	Database string
	User     string
	Password string
	Driver   Driver
}

ConnectionConfig are required to establish a connection to a Db

type Count

type Count struct {
	Count int64 `sql:"count"`
}

Count mimics the PSQL format for Count responses in a Go Struct

type Driver

type Driver string

Driver is a custom type for all supported SQL drivers

const (
	// Postgres driver value is to be used for postgres databases
	Postgres Driver = "postgres"
	// MySQL driver value is to be used for MySql databases
	MySQL Driver = "mysql"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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