godbs

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2019 License: MIT Imports: 8 Imported by: 0

README

Godbs

Sample go database helper.

1.step1 install

go get github.com/easedot/godbs

2.step2 import

import (
    "database/sql"
    "log"
    "time"
    "github.com/easedot/godbs"
)

3.step3 init database connection

type Article struct {
    ID        int64 
    Title     string
    Content   string
    UpdatedAt time.Time
    CreatedAt time.Time
}
dsn := "user:password@tcp(0.0.0.0:3306)/article"
dbConn, err := sql.Open("mysql", dsn)
if err != nil  {
    log.Println(err)
}
defer dbConn.Close()	     
db := godbs.NewHelper(dbConn, false)        

4.step4 sample

4.1 query

var articles []Article
q := Article{Title: "test_title", Content: "test_content"}
err = db.Query(&q, &articles)
if err != nil {
    ...
}
for _, article := range articles {
    log.Printf("%+v\n", article)
}

4.2 find

var transId int64 = 7
article := Article{ID: transId}
if err := db.Find(&article); err != nil {
    ...
}
log.Printf("%+v\n", article)

4.3 create

createTitle := "test create"
c := Article{Title: createTitle, Content: "jhh test 2"}
if err := db.Create(&c); err != nil {
    ...
}

4.3 trans update

err = db.WithTrans(
    func(tx *DbHelper) error {
        err:= tx.Update(u)
        return err
    },
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DbHelper

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

func NewHelper

func NewHelper(db *sql.DB, debug bool) DbHelper

func (*DbHelper) Close added in v1.0.2

func (e *DbHelper) Close() error

func (*DbHelper) Create

func (e *DbHelper) Create(m interface{}) (err error)

func (*DbHelper) Delete

func (e *DbHelper) Delete(m interface{}) (err error)

func (*DbHelper) Find

func (e *DbHelper) Find(m interface{}) (err error)

func (*DbHelper) MapToStruct added in v1.0.1

func (e *DbHelper) MapToStruct(m map[string]interface{}) interface{}

func (*DbHelper) Query

func (e *DbHelper) Query(m interface{}, outSlice interface{}) (err error)

func (*DbHelper) SqlMap added in v1.0.1

func (e *DbHelper) SqlMap(query string) ([]map[string]string, error)

func (*DbHelper) SqlSlice added in v1.0.1

func (e *DbHelper) SqlSlice(query string) ([][]string, error)

func (*DbHelper) SqlStructMap added in v1.0.1

func (e *DbHelper) SqlStructMap(where string, outMap interface{}) (err error)

func (*DbHelper) SqlStructSlice added in v1.0.1

func (e *DbHelper) SqlStructSlice(where string, outSlice interface{}) (err error)

func (*DbHelper) StructToMap added in v1.0.1

func (e *DbHelper) StructToMap(m interface{}) map[string]interface{}

func (*DbHelper) Update

func (e *DbHelper) Update(m interface{}) (err error)

func (*DbHelper) WithTrans

func (e *DbHelper) WithTrans(block TransFunc) error

type TransFunc

type TransFunc func(tx *DbHelper) error

type Transaction

type Transaction interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	Prepare(query string) (*sql.Stmt, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
}

For sql.DB and sql.Tx Replace

Jump to

Keyboard shortcuts

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