dbwrapper

package module
v0.0.0-...-f016fa8 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2018 License: MIT Imports: 5 Imported by: 1

README

License MIT

Simple databases Go wrapper

Simple databases Golang wrapper that all requests are returned as []map[string]string. Very easy but isnt cool efficient. Easy to use with golang "html/template" package.

How to use

import (
	"github.com/dblokhin/config"
	database "github.com/dblokhin/dbwrapper"
	// and others packages ...
)

func main()  {
	// load config
	app := config.NewContext(context.Background(), "config.json")
	config := config.Config(app)    // if no config <- will be panic
	dbsource := config.GetString("db.source")
	dbprefix := conf.GetString("db.prefix")

	if config.GetBool("db.enabled") {
		// connect to db
		app = database.NewContext(app, "mysql", dbsource, dbprefix)
		
		db := database.FromContext(app)
        // Settings up
        db.Exec("SET NAMES utf8")
        db.Exec("SET SESSION sql_mode = 'TRADITIONAL'")
	}
	
	// ... another place
	param1 := "NOT safe sql string"
	param1 := 15

	result := db.Query("SELECT a FROM #__b WHERE c = ?, d = ?", param1, param2)
}

Why does it panic?

The package doesn't return any errors, but it does panic. In my opinion it's good error handling way that allows easy coding & good concentrating on that.

Lets me describe this point. If you use function that returns an errors, you have to (must) check every time annoying if err != nil {}. A good way of easy coding, in my opinion, is that functions could return only the useful values or only errors, like this:

func SomeFunc() int | error

and handles values and errors separately. Directly Golang doesn't allow it, but defer & panic allow us it. Just few examples:

Before. Consider some initial function:
	// create app instance & load config
	app, err := webapp.New()
	if err != nil {
		return nil, err
	}

	app, err = config.NewContext(app, "config.json")
	if err != nil {
		return nil, err
	}

	conf, err := config.Config(app)
	if err != nil {
		return nil, err
	}
    // ...

The some caller is:

	// initiate the app
	app, err := someInitial()
	if err != nil {
		log.Println(err)
		os.Exit(someErrCode)
	}

In most cases error just means the return from function. And caller can checks the err again & return it too...

After. New nice code:
	app := webapp.New()
	app = config.NewContext(app, "config.json")
	conf := config.Config(app)
But How to handle Errors?

Error handler in caller (or may be caller of caller):

	// error handler
	defer func() {
		if err := recover(); err != nil {
			log.Println(err)
            		os.Exit(someErrCode)
		}
	}()
    
	// initiate the app
	app := someInitial()

We can manipulate error values in recover(), we can place error handlers package.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewContext

func NewContext(ctx context.Context, driver, source, prefix string) context.Context

NewContext create new context with db instance

Types

type Database

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

Database db-instance

func DB

func DB(ctx context.Context) *Database

DB return instance from context

func New

func New(driver, source, prefix string) *Database

New creates db wrapper

func NewFromDB

func NewFromDB(drv *sql.DB, prefix string) *Database

NewFromDB returns dbwrapper from active *sql.Database

func (Database) Driver

func (db Database) Driver() *sql.DB

Driver returns underlying sql.DB instance

func (Database) EscapeString

func (db Database) EscapeString(s string) string

EscapeString escapes string

func (Database) Exec

func (db Database) Exec(sql string, args ...interface{}) sql.Result

Exec executes no data SQL query

func (Database) ExecId

func (db Database) ExecId(sql string, args ...interface{}) int64

ExecId executes no data SQL query and returns inserted id

func (Database) Query

func (db Database) Query(sql string, args ...interface{}) []map[string]string

Query executes sql query wih arguments

func (Database) Result

func (db Database) Result(sql string, args ...interface{}) string

Result executes SQL query and returns a field

func (Database) Row

func (db Database) Row(sql string, args ...interface{}) map[string]string

Row executes SQL query and returns a row

Jump to

Keyboard shortcuts

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