sqliter

package module
v0.0.0-...-2f8950a Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2025 License: MIT Imports: 7 Imported by: 0

README

Summary

SQLiter provides a simplified interface for reading/writing flat structs to disk. That's about it.

The utility is in lean prototyping where getting 'something' working is all you need. This is something.

Usage

Init

Example usage to create an in-memory store. Use a filename to write to disk.

type MyStruct struct {
    ID    int64  `db:"id" attr:"PRIMARY KEY"`
    Value string `db:"value"`
}

db, _ := sqliter.Open(sqliter.InMemory)
db.CreateTable(MyStruct{})

Insert

Insert an instance of an object. CreateTable must first be called.

db.Insert(MyStruct{Value:"foo"})

Read

Pass a pointer to an instance, a where clause and its parameter(s).

myStruct := MyStruct{}
db.ReadOne(&myStruct, "id = ?", 1)

ReadMany

Pass a pointer to an instance, a where clause and its parameter(s). Abusing the where clause is a convienient way of working around the limited interface.

myList := []*Mystruct{}
db.ReadMany(&mylist, "value > ?", 42)
db.ReadMany(&mylist, "value > ? ORDER BY created_at LIMIT ?", 42, 10)
db.ReadMany(&mylist, "true ORDER BY created_at LIMIT ?", 42, 10)
db.ReadMany(&mylist, "")

Update

myStruct.Value = "bar"
db.Update(myStruct, "id = ?", 1)

Delete

db.Delete(MyStruct{}, "id = ?", 1)

Documentation

Index

Constants

View Source
const InMemory = ":memory:"

Variables

This section is empty.

Functions

This section is empty.

Types

type Sqliter

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

func Open

func Open(filename string) (*Sqliter, error)

Open a file or use sqliter.InMemory (":memory:")

func (*Sqliter) Close

func (s *Sqliter) Close()

Close cleans up db resources

func (*Sqliter) CreateTable

func (s *Sqliter) CreateTable(sample interface{}) error

CreateTable takes an example object (or nill pointer to its type) and runs CREATE TABLE IF NOT EXISTS with columns read from typej.

func (*Sqliter) Delete

func (s *Sqliter) Delete(sample interface{}, where string, args ...interface{}) error

Delete from the obj table all records matching the where clause

func (*Sqliter) DeleteFrom

func (s *Sqliter) DeleteFrom(table, where string, args ...interface{}) error

Delete from the specified table all records matching the where clause

func (*Sqliter) Exec

func (s *Sqliter) Exec(q string, args ...any) (sql.Result, error)

Exec SQL on the db

func (*Sqliter) Insert

func (s *Sqliter) Insert(obj interface{}) (int64, error)

Insert a record. You must first call CreateTable with the type.

func (*Sqliter) ReadMany

func (s *Sqliter) ReadMany(outPtr interface{}, where string, args ...interface{}) error

Read several records. Use where clause to specify which ones and also to inject OFFSET and LIMIT clauses.

func (*Sqliter) ReadOne

func (s *Sqliter) ReadOne(outPtr interface{}, where string, args ...interface{}) error

Read a single record. Use where clause to specify which one.

func (*Sqliter) Select

func (s *Sqliter) Select(outPtr interface{}, q string, args ...interface{}) error

Select adds a mutex lock for a threadsafe call to sqlx.Select

func (*Sqliter) TableName

func (s *Sqliter) TableName(sample interface{}) (string, error)

TableName returns the database table name used with this object

func (*Sqliter) Update

func (s *Sqliter) Update(obj interface{}, where string, args ...interface{}) error

Update modifies a record. The obj must have an attr:"PRIMARY KEY" set on a field.

func (*Sqliter) Upsert

func (s *Sqliter) Upsert(obj interface{}, where string, args ...interface{}) (int64, error)

Upsert attempts to modify an existing record before inserting. The obj string must have attr:"PRIMARY KEY" set on a field

type StructField

type StructField struct {
	Key   string
	Value interface{}
	Type  reflect.Type
	Attr  string
}

Jump to

Keyboard shortcuts

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