dbdt

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: MIT Imports: 12 Imported by: 0

README

dbdt

Sometimes you don't need object relational management.

Sometimes you just want to store data in a base.

This package is probably not suitable for production.

This is database duct tape.


dbdt is just a couple of thin wrappers around database/sql and a SQLite driver.


Really Quick Start

package main

import (
	"fmt"

	"github.com/Angus-Warman/dbdt"
)

func main() {
	key := "key"
	value := "data to store"

	dbdt.SetValue(key, value)

	// some time later...

	retValue := dbdt.GetValue(key)

	fmt.Println(retValue)
}

Quick Start

package main

import (
	"fmt"

	"github.com/Angus-Warman/dbdt"
)

type Customer struct {
	ID    int
	Name  string
	Email string
}

func main() {
	dbdt.CreateTable[Customer]()

	alice := Customer{0, "Alice", "[email protected]"}

	dbdt.Add(alice)
	// dbdt.Insert(&alice) // If you need to use the automatic ROWID, use DBInsert

	newCustomers := []Customer{
		{0, "Bob", ""},
		{106, "Carol", "[email protected]"}, // specific ID number
	}

	dbdt.AddAll(newCustomers)

	customers, _ := dbdt.GetAll[Customer]()

	fmt.Println(customers)

	validCustomers, _ := dbdt.FindAll[Customer]("SELECT * FROM Customers WHERE Email IS NOT NULL AND Email != ''")

	fmt.Println(validCustomers)
}

Start

package main

import (
	"fmt"

	"github.com/Angus-Warman/dbdt"
)

func main() {
	dbdt.SetActiveFolder("/path/to/your/data") // Otherwise, current directory

	dbdt.SetActiveDB("name_of_database.db") // Otherwise, "data.db"
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ActiveDBPath added in v0.0.2

func ActiveDBPath() string

func Add

func Add[T any](entity T) error

func AddAllDB added in v0.0.5

func AddAllDB[T any](entities []T) error

func AddDB added in v0.0.5

func AddDB[T any](db *sql.DB, entity T) error

func CreateTable

func CreateTable[T any]() error

func CreateTableDB added in v0.0.5

func CreateTableDB[T any](db *sql.DB) error

func Exec

func Exec(query string, args ...any) error

func ExecDB added in v0.0.5

func ExecDB(db *sql.DB, query string, args ...any) error

func FindAll

func FindAll[T any](query string, args ...any) ([]T, error)

func FindAllDB added in v0.0.5

func FindAllDB[T any](db *sql.DB, query string, args ...any) ([]T, error)

func Get

func Get[T any](id any) (T, error)

func GetAll

func GetAll[T any]() ([]T, error)

func GetAllDB added in v0.0.5

func GetAllDB[T any](db *sql.DB) ([]T, error)

func GetColumn

func GetColumn[T any](query string, args ...any) ([]T, error)

func GetColumnDB added in v0.0.5

func GetColumnDB[T any](db *sql.DB, query string, args ...any) ([]T, error)

func GetDB added in v0.0.5

func GetDB[T any](db *sql.DB, id any) (T, error)

func GetRow

func GetRow(query string, args ...any) (map[string]any, error)

func GetRowDB added in v0.0.5

func GetRowDB(db *sql.DB, query string, args ...any) (map[string]any, error)

func GetRows

func GetRows(query string, args ...any) ([]map[string]any, error)

func GetRowsDB added in v0.0.5

func GetRowsDB(db *sql.DB, query string, args ...any) ([]map[string]any, error)

func GetSingle

func GetSingle[T any](query string, args ...any) (T, error)

func GetSingleDB added in v0.0.5

func GetSingleDB[T any](db *sql.DB, query string, args ...any) (T, error)

func GetTableName added in v0.0.5

func GetTableName(entityType reflect.Type) string

func GetValue

func GetValue(key string) string

func Insert

func Insert[T any](entity *T) error

func InsertAll

func InsertAll[T any](entities []*T) error

func InsertAllDB added in v0.0.5

func InsertAllDB[T any](db *sql.DB, entities []*T) error

func InsertDB added in v0.0.5

func InsertDB[T any](db *sql.DB, entity *T) error

func OpenActiveDB added in v0.0.5

func OpenActiveDB() (*sql.DB, error)

func OpenDB

func OpenDB(dbPath string) (*sql.DB, error)

func SetActiveDB

func SetActiveDB(dbName string)

func SetActiveDBPath

func SetActiveDBPath(dbPath string)

func SetActiveFolder

func SetActiveFolder(folder string)

func SetValue

func SetValue(key string, value string)

func Update

func Update[T any](entity T) error

func UpdateAll

func UpdateAll[T any](db *sql.DB, entities []T) error

func UpdateDB added in v0.0.5

func UpdateDB[T any](db *sql.DB, entity T) error

Types

type DBWatcher added in v0.0.2

type DBWatcher struct {
	Callbacks []func()
	// contains filtered or unexported fields
}

func CreateDBWatcher added in v0.0.2

func CreateDBWatcher(dbPath string) (*DBWatcher, error)

func (*DBWatcher) AddCallback added in v0.0.2

func (watcher *DBWatcher) AddCallback(callback func())

func (*DBWatcher) Start added in v0.0.2

func (watcher *DBWatcher) Start()

type Grid

type Grid struct {
	Columns []string
	Rows    [][]any
}

func GetGrid

func GetGrid(query string, args ...any) (Grid, error)

func GetGridDB added in v0.0.5

func GetGridDB(db *sql.DB, query string, args ...any) (Grid, error)

Jump to

Keyboard shortcuts

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