easydb

package module
v0.0.0-...-77cbc65 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2020 License: MIT Imports: 18 Imported by: 0

README

easydb: DB adapter and SQL builder for golang

Features

  • Easy to use and expand
  • Package management using go modules
  • Current support for mysql and postgresql
  • Support for Multi Data Sources

Usage

It is recommended to use go module

Your code looks like this:

package main

import (
	"fmt"

	"github.com/jeffzhangme/easydb"
)

var mysqlConf = easydb.NewMysqlConfig(easydb.WithPassword("password"), easydb.WithSchema("testdb"))
var mysqlConf2 = easydb.NewMysqlConfig(easydb.WithHost("127.0.0.1"), easydb.WithPassword("password"), easydb.WithSchema("testdb"))

func main() {
	defer easydb.Close()
	db := easydb.GetExec(easydb.MYSQL, mysqlConf)
	db2 := easydb.GetExec(easydb.MYSQL, mysqlConf2)
	r, _ := db.Exec(
		easydb.BuildInsert("testdb").
			Table(easydb.Table{Name: "test_table"}).
			Values(easydb.Column{Name: "name", Value: "name"}))
	fmt.Println(r)
	r2, _ := db2.Exec(
		easydb.BuildQuery("testdb").
			Columns(easydb.Column{Name: "*"}).
			Tables(easydb.Table{Name: "test_table"}).
			Where(easydb.Where{Key: "name", Opt: easydb.EQ, Value: "name"}))
	fmt.Println(r2)
}

Documentation

Index

Constants

View Source
const (
	// MYSQL mysql
	MYSQL dbType = iota
	// PGSQL postgresql
	PGSQL
)
View Source
const (
	// Insert Insert
	Insert dbOptType = "INSERT INTO "
	// Delete Delete
	Delete dbOptType = "DELETE FROM "
	// Update Update
	Update dbOptType = "UPDATE "
	// Select Select
	Select dbOptType = "SELECT "
)
View Source
const (
	// COUNT select count
	COUNT queryType = " COUNT"
	// SUM select sum
	SUM queryType = " SUM"
	// MAX select max
	MAX queryType = " MAX"
	// MIN select min
	MIN queryType = " MIN"
	// AVG select avg
	AVG queryType = " AVG"
)
View Source
const (
	// EQ =
	EQ logicalOptType = " = "
	// GT >
	GT logicalOptType = " > "
	// LT <
	LT logicalOptType = " < "
	// GE >=
	GE logicalOptType = " >= "
	// LE <=
	LE logicalOptType = " <= "
	// NE <>
	NE logicalOptType = " <> "
	// LIKE like
	LIKE logicalOptType = " LIKE "
	// IN in
	IN logicalOptType = " IN "
	// IsNULL is null
	IsNULL logicalOptType = " IS NULL "
	// NotNULL is not null
	NotNULL logicalOptType = " IS NOT NULL "
)
View Source
const (
	// AND AND
	AND whereGroupType = " AND "
	// OR OR
	OR whereGroupType = " OR "
	// GroupStart (
	GroupStart whereGroupType = " ( "
	// GroupEnd )
	GroupEnd whereGroupType = " ) "
)
View Source
const (
	// DESC DESC
	DESC orderType = " DESC "
	// ASC ASC
	ASC orderType = " ASC "
)

Variables

This section is empty.

Functions

func BuildDelete

func BuildDelete(dbName ...string) iBuildDeleteReturn

BuildDelete begin

func BuildInsert

func BuildInsert(dbName ...string) iBuildInsertReturn

BuildInsert begin

func BuildQuery

func BuildQuery(dbName ...string) iBuildQueryReturn

BuildQuery begin

func BuildUpdate

func BuildUpdate(dbName ...string) iBuildUpdateReturn

BuildUpdate begin

func Close

func Close()

Close Close

func WithConnMaxLifetime

func WithConnMaxLifetime(ConnMaxLifetime int) dbConfigParam

WithConnMaxLifetime set schema option

func WithConnParams

func WithConnParams(ConnParams string) dbConfigParam

WithConnParams set schema option

func WithDataSourceName

func WithDataSourceName(DataSource string) dbConfigParam

WithDataSourceName set data source name option

func WithEnableMigrate

func WithEnableMigrate(EnableMigrate bool) dbConfigParam

WithEnableMigrate set schema option

func WithHost

func WithHost(Host string) dbConfigParam

WithHost set host option

func WithMaxIdleConns

func WithMaxIdleConns(MaxIdleConns int) dbConfigParam

WithMaxIdleConns set schema option

func WithMaxOpenConns

func WithMaxOpenConns(MaxOpenConns int) dbConfigParam

WithMaxOpenConns set schema option

func WithMigrateDir

func WithMigrateDir(MigrateDir string) dbConfigParam

WithMigrateDir set schema option

func WithPassword

func WithPassword(Password string) dbConfigParam

WithPassword set password option

func WithPort

func WithPort(Port string) dbConfigParam

WithPort set port option

func WithSchema

func WithSchema(Schema string) dbConfigParam

WithSchema set schema option

func WithUserName

func WithUserName(UserName string) dbConfigParam

WithUserName set username option

Types

type Column

type Column struct {
	Name  string
	As    string
	Value interface{}
}

Column column

type DBConfig

type DBConfig struct {
	DataSource      string
	UserName        string
	Password        string
	Host            string
	Port            string
	Schema          string
	ConnParams      string
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime int
	EnableMigrate   bool
	MigrateDir      string
}

DBConfig db config struct

func NewMysqlConfig

func NewMysqlConfig(params ...dbConfigParam) *DBConfig

NewMysqlConfig create new mysql config

func NewPgsqlConfig

func NewPgsqlConfig(params ...dbConfigParam) *DBConfig

NewPgsqlConfig create pgsql config

type DBExec

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

DBExec db executor

func GetExec

func GetExec(dbType dbType, config *DBConfig) *DBExec

GetExec get executor

func GetInst

func GetInst(dbType dbType, config *DBConfig) *DBExec

GetInst GetInst

func (*DBExec) Delete

func (p *DBExec) Delete(sqlBuilder iSQLBuilder) ([]map[string]interface{}, error)

Delete Delete

func (*DBExec) Exec

func (p *DBExec) Exec(sqlBuilder iSQLBuilder) ([]map[string]interface{}, error)

Exec Exec

func (*DBExec) Insert

func (p *DBExec) Insert(sqlBuilder iSQLBuilder) ([]map[string]interface{}, error)

Insert Insert

func (*DBExec) Select

func (p *DBExec) Select(sqlBuilder iSQLBuilder) ([]map[string]interface{}, error)

Select Select

func (*DBExec) Update

func (p *DBExec) Update(sqlBuilder iSQLBuilder) ([]map[string]interface{}, error)

Update Update

type Having

type Having struct {
	Opt   logicalOptType
	Key   string
	Value string
	Ins   []string
}

Having having

type JoinType

type JoinType string

JoinType JoinType

const (
	// LeftJoin LEFT JOIN
	LeftJoin JoinType = " LEFT JOIN "
	// RightJoin RIGHT JOIN
	RightJoin JoinType = " RIGHT JOIN "
)

type Mysql

type Mysql struct {
	*sql.DB
	*DBConfig
	// contains filtered or unexported fields
}

func (*Mysql) After

func (p *Mysql) After(optType dbOptType, rawResult []map[string]interface{}) (result interface{}, err error)

func (*Mysql) Before

func (p *Mysql) Before(optType dbOptType, sqlBuilder iSQLBuilder) (string, []interface{})

func (*Mysql) Do

func (p *Mysql) Do(optType dbOptType, sql string, val []interface{}) (result []map[string]interface{}, err error)

func (*Mysql) SqlDB

func (p *Mysql) SqlDB() *sql.DB

type On

type On struct {
	Opt   logicalOptType
	Key   string
	Value string
	Ins   []string
}

On on

type Order

type Order struct {
	Type orderType
	Key  string
}

Order order

type Pgsql

type Pgsql struct {
	Mysql
}

func (*Pgsql) After

func (p *Pgsql) After(optType dbOptType, rawResult []map[string]interface{}) (result interface{}, err error)

func (*Pgsql) Before

func (p *Pgsql) Before(optType dbOptType, sqlBuilder iSQLBuilder) (string, []interface{})

func (*Pgsql) Do

func (p *Pgsql) Do(optType dbOptType, sql string, val []interface{}) (result []map[string]interface{}, err error)

func (*Pgsql) SqlDB

func (p *Pgsql) SqlDB() *sql.DB

type QueryFunc

type QueryFunc struct {
	Type  queryType
	Names []string
	As    string
}

QueryFunc query func

type Table

type Table struct {
	Name string
	As   string
}

Table table

type Where

type Where struct {
	Opt   logicalOptType
	Key   string
	Value interface{}
	Ins   []interface{}
}

Where where

Jump to

Keyboard shortcuts

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