sqobj

package
v1.0.51 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

README

sqlite3 objects (SQObjects)

This package provides a method forwriting, reading and deleting go objects (structures) to and from SQLite databases. Not exactly a full "Object Relational Database" but a way to reduce the amount of boilerplate code and error handling need to keep a database in syncronization with objects.

This package is part of a wider project, github.com/mutablelogic/go-sqlite. Please see the module documentation for more information.

The general method is:

  1. Use tags on your struct definition to define the database table, columns, indexes and foreign keys;
  2. Create a database connection and register the structs you want to use to syncronize with the database. You also need to register foreign key relationships;
  3. Create the tables and indexes in the database;
  4. Read, write and delete objects using the Read, Write and Delete methods. You may need to use some hook functions to handle foreign key relationships.

For example, the following definition:

type Doc struct {
	A int    `sqlite:"a,autoincrement"`
	B int    `sqlite:"b,unique"`
	C string `sqlite:"c"`
}

Will create a table with the following statement:

CREATE TABLE IF NOT EXISTS main.doc (
  a INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  b INTEGER,
  c TEXT,
  UNIQUE (b)
)

A fuller explanation of the tags and supported types is provided below. SQObjects is currently in development.

Introduction

TODO

Registering a definition

TODO

Supported scalar types

TODO

Writing objects (inserting and updating)

TODO

Reading objects (selecting)

TODO

Deleting objects

TODO

Documentation

Index

Constants

View Source
const (
	SQKeyNone stkey = iota
	SQKeySelect
	SQKeyInsert
	SQKeyDeleteRows
	SQKeyDeleteKeys
	SQKeyUpdateKeys
	SQKeyUpsertKeys
	SQKeyMax = SQKeyUpdateKeys
)

Variables

This section is empty.

Functions

func DeclType

func DeclType(t reflect.Type) string

DeclType returns the declared column type for a given field uses TEXT by default. Accepts both scalar types and pointer types

func ValueOf

func ValueOf(v interface{}) reflect.Value

ValueOf returns a struct value or nil if not valid

Types

type Class

type Class struct {
	*SQReflect
	SQSource
	// contains filtered or unexported fields
}

func MustRegisterClass

func MustRegisterClass(source SQSource, proto interface{}) *Class

MustRegisterClass registers a SQObject class, panics if an error occurs.

func RegisterClass

func RegisterClass(source SQSource, proto interface{}) (*Class, error)

RegisterClass registers a SQObject class, returns the class and any errors

func (*Class) Create

func (this *Class) Create(txn SQTransaction, schema string) error

Create creates a table, keys and prepared statements within a transaction. If the flag SQLITE_OPEN_OVERWRITE is set when creating the connection, then tables and indexes are dropped and then re-created.

func (*Class) DeleteKeys

func (c *Class) DeleteKeys(txn SQTransaction, v ...interface{}) (int, error)

Delete keys in table based on primary keys. Returns number of deleted rows

func (*Class) DeleteRows

func (c *Class) DeleteRows(txn SQTransaction, row []int64) (int, error)

Delete from the table based on rowids, returns the number of changes made

func (*Class) ForeignKey

func (this *Class) ForeignKey(parent SQClass, parentcols ...string) SQClass

ForeignKey appends a foreign key constraint, panics on error. Optionally sets the columns to refer to in the parent.

func (*Class) Insert

func (c *Class) Insert(txn SQTransaction, v ...interface{}) ([]int64, error)

Insert into a table and return rowids. If any autoincremented fields are zero valued, these are automatically set to NULL on insert

func (*Class) Proto

func (this *Class) Proto() reflect.Value

Proto returns a prototype of the class

func (*Class) Read

func (this *Class) Read(txn SQTransaction) (SQIterator, error)

Read from table and return an iterator. It is expected that Read would accept a query, including: order, limit, offset, distinct and a list of expressions

func (*Class) String

func (this *Class) String() string

func (*Class) UpdateKeys

func (c *Class) UpdateKeys(txn SQTransaction, v ...interface{}) (int, error)

Update objects by primary key, return number of updated rows

func (*Class) UpsertKeys

func (c *Class) UpsertKeys(txn SQTransaction, v ...interface{}) ([]int64, error)

func (*Class) WithForeignKey

func (this *Class) WithForeignKey(parent SQClass, parentcols ...string) error

WithForeignKey appends a foreign key constraint to the class, returns error. Optionally sets the columns to refer to in the parent.

type Iterator

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

func (*Iterator) Next

func (i *Iterator) Next() interface{}

func (*Iterator) RowId

func (i *Iterator) RowId() int64

type SQReflect

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

func NewReflect

func NewReflect(proto interface{}) (*SQReflect, error)

Return a reflection object for the given struct or nil if the argument is not a pointer to a struct or has no fields which are exported

func (*SQReflect) Column

func (this *SQReflect) Column(name string) SQColumn

Return a column definition for a given column name

func (*SQReflect) Columns

func (this *SQReflect) Columns() []SQColumn

Return column definitions

func (*SQReflect) Index

func (this *SQReflect) Index(source SQSource, name string) SQIndexView

Return an index definition for a given index name and source table

func (*SQReflect) String

func (this *SQReflect) String() string

func (*SQReflect) Table

func (this *SQReflect) Table(source SQSource, ifnotexists bool) []SQStatement

Return table and index definitions for a given source table adding IF NOT EXISTS to the table and indexes

func (*SQReflect) View added in v1.0.51

func (this *SQReflect) View(source SQSource, st SQSelect, ifnotexists bool) SQStatement

Return view definition for a given source adding IF NOT EXISTS to the view

func (*SQReflect) Virtual added in v1.0.51

func (this *SQReflect) Virtual(source SQSource, module string, ifnotexists bool, options ...string) []SQStatement

Return virtual table definition for a given source adding IF NOT EXISTS to the table, and additional options appended to the table creation statement

func (*SQReflect) WithForeignKey

func (this *SQReflect) WithForeignKey(parent SQSource, parentcols ...string) error

WithForeignKey defines a foreign key to a parent class.

type View added in v1.0.51

type View struct {
	*SQReflect
	SQSource
	// contains filtered or unexported fields
}

func MustRegisterView added in v1.0.51

func MustRegisterView(name SQSource, proto interface{}, leftjoin bool, sources ...SQClass) *View

MustRegisterView registers a SQObject view class, panics if an error occurs.

func RegisterView added in v1.0.51

func RegisterView(name SQSource, proto interface{}, leftjoin bool, sources ...SQClass) (*View, error)

RegisterView registers a SQObject view class, returns the class and any errors

func (*View) Create added in v1.0.51

func (this *View) Create(txn SQTransaction, schema string) error

Create creates a view. If the flag SQLITE_OPEN_OVERWRITE is set when creating the connection, then view is dropped and then re-created.

func (*View) Proto added in v1.0.51

func (this *View) Proto() reflect.Value

Proto returns a prototype of the class

func (*View) Select added in v1.0.51

func (this *View) Select() SQSelect

Select returns the select statement for the view

func (*View) String added in v1.0.51

func (this *View) String() string

type Virtual added in v1.0.51

type Virtual struct {
	*SQReflect
	SQSource
	// contains filtered or unexported fields
}

func MustRegisterVirtual added in v1.0.51

func MustRegisterVirtual(source SQSource, module string, proto interface{}, options ...string) *Virtual

MustRegisterVirtual registers a SQObject virtual table class, panics if an error occurs.

func RegisterVirtual added in v1.0.51

func RegisterVirtual(source SQSource, module string, proto interface{}, options ...string) (*Virtual, error)

RegisterVirtual registers a SQObject virtual table class, returns the class and any errors

func (*Virtual) Create added in v1.0.51

func (this *Virtual) Create(txn SQTransaction, schema string, options ...string) error

Create creates a virtual table. If the flag SQLITE_OPEN_OVERWRITE is set when creating the connection, then tables are dropped and then re-created.

func (*Virtual) Proto added in v1.0.51

func (this *Virtual) Proto() reflect.Value

Proto returns a prototype of the class

func (*Virtual) String added in v1.0.51

func (this *Virtual) String() string

Jump to

Keyboard shortcuts

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