sqlz

package
v0.0.0-...-175522e Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2025 License: MIT Imports: 9 Imported by: 0

README

SQLz

Connect
package main

import (
	"github.com/GuoTengda1993/dora/sqlz"
)

func main(){
	db, err := sqlz.NewDB("mysql", "user", "pw", "127.0.0.1:3306", "database1", "utf8")
	if err != nil {
		panic(err)
	}
	// do something...
	db.Close()
}
Select
package main

import (
	"fmt"
	"github.com/GuoTengda1993/dora/sqlz"
)

type User struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

type UserList struct {
	Data []User
}

const TABLE = "users"

func main() {
	db, err := sqlz.NewDB("mysql", "user", "pw", "127.0.0.1:3306", "database1", "utf8")
	if err != nil {
		panic(err)
	}

	x := &User{}
	w1 := map[string]interface{}{
		"name": "test",
	}
	_, err = db.Table(TABLE).Select(nil).Where(w1).First(x)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("x: %+v", x)
	
	xList := &UserList{}
	w2 := map[string]interface{}{
		"age >": 18,
	}
	_, err = db.Table(TABLE).Select(nil).Where(w2).All(xList.Data)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("x: %+v", xList.Data)

	db.Close()
}

Documentation

Index

Constants

View Source
const (
	DEFAULT = 0
	SELECT  = 1
	UPDATE  = 2
	INSERT  = 3
	DELETE  = 4
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DBClient

type DBClient struct {
	DB           *sql.DB
	Info         *Info
	Tx           *sql.Tx
	AutoRollback bool
}

func NewDB

func NewDB(driverName, username, password, host, dbName, charset string) (*DBClient, error)

func NewDbByConn

func NewDbByConn(db *sql.DB) *DBClient

func (*DBClient) All

func (t *DBClient) All(result interface{}) ([]map[string]interface{}, error)

func (*DBClient) Begin

func (t *DBClient) Begin(autoRollback bool) error

func (*DBClient) Close

func (t *DBClient) Close()

func (*DBClient) Commit

func (t *DBClient) Commit() error

func (*DBClient) Delete

func (t *DBClient) Delete() *DBClient

func (*DBClient) Do

func (t *DBClient) Do() (int64, error)

func (*DBClient) First

func (t *DBClient) First(result interface{}) (map[string]interface{}, error)

func (*DBClient) Insert

func (t *DBClient) Insert(data interface{}) *DBClient

func (*DBClient) Limit

func (t *DBClient) Limit(limit uint64) *DBClient

func (*DBClient) Offset

func (t *DBClient) Offset(offset uint64) *DBClient

func (*DBClient) OrderBy

func (t *DBClient) OrderBy(col string, asc bool) *DBClient

func (*DBClient) RawExec

func (t *DBClient) RawExec(sql string, args ...any) *DBClient

func (*DBClient) RawQuery

func (t *DBClient) RawQuery(sql string, args ...any) *DBClient

func (*DBClient) Select

func (t *DBClient) Select(columns []string, distinct bool) *DBClient

func (*DBClient) SetConnAndTime

func (t *DBClient) SetConnAndTime(maxOpenConns, maxIdleConns int, maxLifeTime, maxIdleTime time.Duration)

func (*DBClient) Table

func (t *DBClient) Table(table string) *DBClient

func (*DBClient) Update

func (t *DBClient) Update(data interface{}) *DBClient

func (*DBClient) Where

func (t *DBClient) Where(where map[string]interface{}) *DBClient

type Info

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

Jump to

Keyboard shortcuts

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