sqobj

package
v1.0.32 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

SQLite 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.

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

This section is empty.

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 MustRegisterClass

func MustRegisterClass(source SQSource, proto interface{}) SQClass

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

func NewIterator

func NewIterator(class *sqclass, rs SQRows) *sqiterator

func RegisterClass

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

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

func ValueOf

func ValueOf(v interface{}) reflect.Value

ValueOf returns a struct value or nil if not valid

Types

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) WithForeignKey

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

WithForeignKey defines a foreign key to a parent class.

Jump to

Keyboard shortcuts

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