sifdb

package module
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2023 License: MIT Imports: 6 Imported by: 0

README

sifdb

SQLite Database Wrapper for Golang using https://github.com/mattn/go-sqlite3 package

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create(table interface{}, db *sql.DB) error

Initiates a new table in the database. The table should be a struct with json tags to be able to easily unmarshal the content later. Example struct to give as table:

type User struct {
	id        int     `json:"id"` -- mandatory field to have in every struct
	username  string  `json:"username"`
	password  string  `json:"password"`
}

func Delete

func Delete(database_struct interface{}, db *sql.DB) error

func Get

func Get(database_struct interface{}, db *sql.DB) ([]string, error)

sifdb.Get is what you use to fetch from the database. It takes a struct containing what fields you want to filter on. Example program:

type User struct {
	Id       int    `json:"id"`
	Username string `json:"username"`
	Password string `json:"password"`
}
func main() {
	db := sifdb.Open("test.sqlite")
	if err := sifdb.Create(User{}, db); err != nil {
		fmt.Println(err)
		return
	}
	user := User{}
	user.Id = 2
	if user, err := getUser(user, db); err != nil {
		fmt.Println(err)
		return
	} else {
		fmt.Println(user)
	}
}
func getUser(user User, db *sql.DB) (User, error) {
	jsonString, err := sifdb.Get(user, db)
	if err != nil {
		return User{}, err
	}
	if len(jsonString) == 0 {
		return User{}, errors.New("no user found")
	}
	if len(jsonString) > 1 {
		return User{}, errors.New("found too many users")
	}
	json.Unmarshal([]byte(jsonString[0]), &user)
	return user, nil
}

func GetCustom added in v1.1.1

func GetCustom(database_struct interface{}, db *sql.DB, where_statement string) ([]string, error)

func GetLatest added in v1.1.1

func GetLatest(database_struct interface{}, amount int, db *sql.DB) ([]string, error)

func Insert

func Insert(database_struct interface{}, db *sql.DB) error

func Open

func Open(databasefile string) *sql.DB

Types

This section is empty.

Jump to

Keyboard shortcuts

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