godbhelper

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2020 License: MIT Imports: 14 Imported by: 0

README

GoDBHelper

A database helper for golang

Features

  • Database versioning/migrating
  • Executing prepared/named/normal statements easily with formatting strings (queries)
  • Easily switching between Databases (see Driver)
  • All sqlx functions
Driver

Usage

Use one of the following imports matching the driver you want to use.
Sqlite: github.com/mattn/go-sqlite3
Sqlite encrypt: github.com/CovenantSQL/go-sqlite3-encrypt
MySQL: github.com/go-sql-driver/mysql

Example

Connections
package main

import (
	"fmt"
	dbhelper "github.com/JojiiOfficial/GoDBHelper/"

	//_ "github.com/go-sql-driver/mysql"
	//_ "github.com/mattn/go-sqlite3"
	_ "github.com/CovenantSQL/go-sqlite3-encrypt"
)

type testUser struct {
	ID       int    `db:"id" orm:"pk,ai"`
	Username string `db:"username"`
	Pass     string `db:"password"`
}

func main() {
	db := exampleSqlite()
	if db == nil {
		return
	}
	defer db.DB.Close()

	db.Exec("CREATE TABLE user (id int, username text, password text)")
	db.Exec("INSERT INTO user (id, username, password) VALUES (1,'will', 'iamsafe')")

	var user testUser
	db.QueryRow(&user, "SELECT * FROM user")
	fmt.Println(user.ID, ":", user.Username, user.Pass)
}

func exampleMysql() *dbhelper.DBhelper {
	user := "dbUser"
	pass := "pleaseMakeItSafe"
	host := "localhost"
	port := "3306"
	database := "test"
	db, err := dbhelper.NewDBHelper(dbhelper.Mysql).Open(user, pass, host, port, database)
	if err != nil {
		fmt.Println(err.Error())
		return nil
	}
	return db
}

func exampleSqlite() *dbhelper.DBhelper {
	db, err := dbhelper.NewDBHelper(dbhelper.Sqlite).Open("test.db")
	if err != nil {
		fmt.Println(err.Error())
		return nil
	}
	return db
}

func exampleSqliteEncrypt() *dbhelper.DBhelper {
	db, err := dbhelper.NewDBHelper(dbhelper.SqliteEncrypted).Open("test.db", "passKEY")
	if err != nil {
		fmt.Println(err.Error())
		return nil
	}
	return db
}

Migrating

The following code snipped demonstrates, how your client can easily update its database to the newest version.

//db is an instance of dbhelper.DBhelper

//load sql queries from .sql file
//Queriess loaded from this function (LoadQueries) are always version 0
db.LoadQueries("chain1", "./test.sql", 0)

//Add sql queries manually
//The order specifies the execution order of the queries. So in this case, chain1 would be loaded before chain2
db.AddQueryChain(dbhelper.QueryChain{
	Order: 1,
	Name: "chain2",
	Queries: []dbhelper.SQLQuery{
		dbhelper.SQLQuery{
			VersionAdded: 0,
			QueryString:  "CREATE TABLE user (id int, username text, password text)",
		},
		dbhelper.SQLQuery{
			VersionAdded: 0,
			QueryString:  "INSERT INTO user (id, username, password) VALUES (?,?,?)",
			Params:       []string{"0", "admin", "lol123"},
		},
		//added in a later version (version 0.1)
		dbhelper.SQLQuery{
			VersionAdded: 0.1,
			QueryString:  "CREATE TABLE test1 (id int)",
		},
		//added in a later version (version 0.21)
		dbhelper.SQLQuery{
			VersionAdded: 0.21,
			QueryString:  "INSERT INTO test1 (id) VALUES (?),(?)",
			Params:       []string{"29", "1"},
		},
	},
})

//runs the update
err := db.RunUpdate()
if err != nil {
	fmt.Println("Err updating", err.Error())
}

If you add some queries in a later version, the only thing you have to do is adding a SQLQuery element to this array with a new and bigger version number. Clients wich are running on a lower version number, will run this SQL queries directly on the first run after updating (eg. git pull or a docker pull).

Documentation

Index

Constants

View Source
const (
	//Sqlite  sqlite db
	Sqlite dbsys = iota
	//SqliteEncrypted Sqlite encrypted
	SqliteEncrypted
	//Mysql mysql db
	Mysql
	//Postgres postgres db
	Postgres
)
View Source
const (
	//MysqlURIFormat formats mysql uri
	MysqlURIFormat = "%s:%s@tcp(%s:%d)/%s%s"
	//PostgresURIFormat formats mysql uri
	PostgresURIFormat = "user='%s' password='%s' host='%s' port=%d dbname='%s' %s"
)
View Source
const (
	//OrmTag orm-tag
	OrmTag = "orm"
	//DBTag db-tag
	DBTag = "db"
)

Tags

View Source
const (
	//TableDBVersion tableName for db version store
	TableDBVersion = "DBVersion"
)

Variables

View Source
var (
	//ErrDBNotSupported error if database is not supported
	ErrDBNotSupported = errors.New("Database not supported")
	//ErrPostgresURIMissingArg error if Open() mysqlDB and missing an arg
	ErrPostgresURIMissingArg = errors.New("Postgres missing argument. Use Open(username, password, address, port, database)")
	//ErrMysqlURIMissingArg error if Open() mysqlDB and missing an arg
	ErrMysqlURIMissingArg = errors.New("MYSQL missing argument. Use Open(username, password, address, port, database)")
	//ErrPortInvalid if given port is invalid
	ErrPortInvalid = errors.New("Port invalid. Port must be <= 65535 and > 0")
	//ErrSqliteEncryptMissingArg error if Open() SqliteEncrypt and missing argument
	ErrSqliteEncryptMissingArg = errors.New("SqliteEncrypt missing argument. Use Open(file, key)")
	//ErrVersionStoreTooManyVersions if VersionStore contains more than one version
	ErrVersionStoreTooManyVersions = errors.New("Too many versions stored in VersionStore")
	//ErrCantStoreVersionInDB err if running update and StoreVersionInDB=false
	ErrCantStoreVersionInDB = errors.New("Can't store Version in Database. Set StoreVersionInDB=true")

	//ErrNoStruct if the given data is no struct
	ErrNoStruct = errors.New("Data must be a struct")

	//ErrNoRowsInResultSet error if no rows in resultSet
	ErrNoRowsInResultSet = "sql: no rows in result set"
)
View Source
var NoHook = func(err error, s1, s2 string) {}

NoHook run a query without a hook

Functions

This section is empty.

Types

type DBhelper

type DBhelper struct {

	//Versions for upgrading
	//CurrentVersion the version currently running
	CurrentVersion float32
	//AvailableVersion the version which is newly added
	AvailableVersion float32

	//DBhelper data
	DB          *sqlx.DB
	Options     DBhelperOptions
	IsOpen      bool
	QueryChains []QueryChain `json:"chains"`

	ErrHookFunc    ErrHookFunc
	ErrHookOptions *ErrHookOptions

	NextErrHookFunc   ErrHookFunc
	NextErrHookOption *ErrHookOptions
	// contains filtered or unexported fields
}

DBhelper the dbhelper object

func NewDBHelper

func NewDBHelper(dbKind dbsys, bv ...bool) *DBhelper

NewDBHelper the DBhelper constructor NewDBHelper(database, debug, stopUpdateOnError, storeVersionInDB, useColors)

func (*DBhelper) AddQueryChain

func (dbhelper *DBhelper) AddQueryChain(chain QueryChain) *DBhelper

AddQueryChain adds a queryChain

func (*DBhelper) CreateTable

func (dbhelper *DBhelper) CreateTable(name string, data interface{}, additionalFields ...SQLColumn) error

CreateTable creates a table for struct Leave name empty to use the name of the struct

func (*DBhelper) Exec

func (dbhelper *DBhelper) Exec(query string, args ...interface{}) (sql.Result, error)

Exec executes command in DB

func (*DBhelper) Execf

func (dbhelper *DBhelper) Execf(queryFormat string, formatParams []string, args ...interface{}) (sql.Result, error)

Execf executes a formatted query in DB

func (*DBhelper) Insert

func (dbhelper *DBhelper) Insert(data interface{}, params ...string) (*sql.Result, error)

Insert creates a table for struct Leave name empty to use the name of the struct

func (*DBhelper) LoadQueries

func (dbhelper *DBhelper) LoadQueries(name, file string, chainOrder int) error

LoadQueries loads queries from a .sql file and executes the statements (row for row). The SQLQuery Version of the statements are 0. This is intended to initialize the database-schema

func (*DBhelper) Open

func (dbhelper *DBhelper) Open(params ...string) (*DBhelper, error)

Open db Sqlite - Open(filename) SqliteEncrypted - Open(filename, key) Mysql - Open(username, password, address, port, database) Postgres - Open(username, password, address, port, database)

func (*DBhelper) QueryRow

func (dbhelper *DBhelper) QueryRow(a interface{}, query string, args ...interface{}) error

QueryRow runs statement and fills a with db data

func (*DBhelper) QueryRowf

func (dbhelper *DBhelper) QueryRowf(a interface{}, queryf string, queryArgs []string, args ...interface{}) error

QueryRowf like QueryRow but formatted

func (*DBhelper) QueryRows

func (dbhelper *DBhelper) QueryRows(a interface{}, query string, args ...interface{}) error

QueryRows like QueryRow but for multiple rows

func (*DBhelper) QueryRowsf

func (dbhelper *DBhelper) QueryRowsf(a interface{}, query string, queryArgs []string, args ...interface{}) error

QueryRowsf like QueryRows but formatted

func (*DBhelper) RunUpdate

func (dbhelper *DBhelper) RunUpdate(options ...bool) error

RunUpdate updates new sql queries RunUpdate(fullUpdate, dropAllTables bool)

func (*DBhelper) SetErrHook

func (dbhelper *DBhelper) SetErrHook(hook ErrHookFunc, options ...ErrHookOptions)

SetErrHook sets the error hook function

func (*DBhelper) WithHook

func (dbhelper *DBhelper) WithHook(hook ErrHookFunc, options ...ErrHookOptions) *DBhelper

WithHook adds next log prefix

func (*DBhelper) WithMessage

func (dbhelper *DBhelper) WithMessage(s string) *DBhelper

WithMessage adds next log prefix

func (*DBhelper) WithOption added in v1.0.3

func (dbhelper *DBhelper) WithOption(option ErrHookOptions) *DBhelper

WithOption adds next ErrHookOption

type DBhelperOptions

type DBhelperOptions struct {
	Debug             bool
	StopUpdateOnError bool
	StoreVersionInDB  bool
	UseColors         bool
}

DBhelperOptions options for DBhelper

type ErrHookFunc

type ErrHookFunc func(error, string, string)

ErrHookFunc error hook 1. string is the query; 2. string is the hookPrefix if set

type ErrHookOptions

type ErrHookOptions struct {
	ReturnNilOnErr bool
	Prefix         string
}

ErrHookOptions options for ErrorHooks

type InitSQL

type InitSQL struct {
	Query   string
	Params  string
	FParams []string
}

InitSQL init sql obj

type QueryChain

type QueryChain struct {
	Name    string     `json:"name"`
	Order   int        `json:"order"`
	Queries []SQLQuery `json:"queries"`
}

QueryChain a list of SQL queries over time

func LoadQueries

func LoadQueries(name, file string, chainOrder int) (*QueryChain, error)

LoadQueries loads queries from a .sql file and executes the statements (row for row). The SQLQuery Version of the statements are 0. This is intended to initialize the database-schema

func NewQueryChain

func NewQueryChain(name string, order int) *QueryChain

NewQueryChain QueryChain constructor

func RestoreQueryChain

func RestoreQueryChain(file string) (*QueryChain, error)

RestoreQueryChain loads an exported queryChain from file

func (*QueryChain) ExportQueryChain

func (queryChain *QueryChain) ExportQueryChain(file string, perm os.FileMode) error

ExportQueryChain saves/exports a queryChain to a file

type SQLColumn

type SQLColumn struct {
	Name string
	Type string
}

SQLColumn a column in a table

type SQLQuery

type SQLQuery struct {
	VersionAdded float32  `json:"vs"`
	QueryString  string   `json:"query"`
	Params       []string `json:"params"`
	FqueryString string   `json:"queryf"`
	Fparams      []string `json:"fparams"`
}

SQLQuery a query

func CreateInitVersionSQL

func CreateInitVersionSQL(arg ...InitSQL) []SQLQuery

CreateInitVersionSQL creates SQLQuery[] for init version

Jump to

Keyboard shortcuts

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