dataq

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 10 Imported by: 0

README

dataq(uelle) for MySQL

A light-weight SQL builder of MySQL for Go

Telegram

Features

Mapping struct with "Query Entity"
  • To execute query == manipulate with struct

    The table definition:

    CREATE TABLE IF NOT EXISTS Person (
    	ID INT(5) AUTO_INCREMENT,
    	NAME VARCHAR(50) DEFAULT '',
    	AGE TINYINT(2) DEFAULT 0,
    	PROFILE JSON,
    	CREATED DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    	PRIMARY KEY (ID)
    ) ENGINE=InnoDB;
    

    The struct definition:

    type Person struct {
    	ID      int64  `INDEX:"" COL:"ID" TABLE:"Person"`
    	Name    string `COL:"NAME" ALT:""`
    	Age     int    `COL:"AGE"`
    	Profile string `COL:"PROFILE" ALT:"{}" WHERE:"PROFILE<>\"\""`
    }
    

    Init the connection of database:

    db, err := Open("<dns here>")
    checkErr(err, t)
    defer db.Close()
    

    Init the struct with data to INSERT:

    p1 := Persion{
    	Name: "Mike",
    	Age: 18,
    }
    
    model := db.Model(p1)
    resInsert := model.Insert()
    
  • Fix "where condition" or "join condition" in struct's Tag

  • Support dynamic "where condition"

Mini Doc

Attention
  • WHERE <ID> IN (,,,,) will be replaced to WHERE <ID> IN (?,?,?,?,?)
Tags
Tag Description
TABLE The table name.
TABLEAS The table part of field when select.
TABLEALIAS The table alias.
COL The field name, also can be a function and with Tag RAW.
COLAS The field alias, also can be a function and with Tag RAW.
INDEX This field is an index.
INIT If the value of the field is empty (0 for number, '' for string, etc.), and the field is a JSON field, at least the field will be create in the JSON document.
COUNTON The value of COUNT(x) function.
ASNULL As NULL value.
ASCLEAR [Update Only] The value used as to empty the field.
ALT Alternative value.
WHERE The fixed part of WHERE clause.
JOIN The fixed part of JOIN clause.
JSON The column is assigned to a key in the json field, e.g. JSON:"Data.name" will be transfered as Data->>'$.name' in SELECT statement.
JSONCAST The column will be applied CAST(? AS JSON) in SELECT statement.
JSONMERGEPRESERVE [Update Only] Use JSON_MERGE_PRESERVE function to update the field.
JSONMERGEPATCH [Update Only] Use JSON_MERGE_PATCH function to update the field.
JSONARRAYAPPEND [Update Only] Use JSON_ARRAY_APPEND function to update the field.
OMIT This field will be ignored in query.
PASSUPDATE This field will be ignored in update query.
NOFROM [Query only!] No FROM clause will be generated.
RAW [Query only!] Will query with what the Tag COL has.
SCHEMAF [CreateTable only!] the define string for the field.
SCHEMAT [CreateTable only!] the define string for the table.
SELF <Field>=<Field><SELF> (not for JOSN datatype)

Documentation

Index

Constants

View Source
const ConfigAsNullDateTimeFormat = "0001-01-01 00:00:00.000"
View Source
const ConfigMySQLDateTimeFormat = "2006-01-02 15:04:05.000"
View Source
const LockForShare = "SHARE"
View Source
const LockForUpdate = "UPDATE"
View Source
const LockForUpdateNoWait = "UPDATE NOWAIT"
View Source
const LockForUpdateSkipLocked = "UPDATE SKIP LOCKED"

Variables

View Source
var (
	DefaultConnMaxIdleTime = 5 * time.Minute
	DefaultConnMaxLifetime = 5 * time.Minute
	MaxIdleConns           = 128
	MaxOpenConns           = 128
)
View Source
var ConfigParseDateTimeFormat = "2006-01-02 15:04:05.000"

Functions

func ClearValue

func ClearValue(v interface{})

Types

type Config added in v0.1.3

type Config struct {
	DebugLvl        int
	ConnMaxIdleTime time.Duration
	ConnMaxLifetime time.Duration
	MaxIdleConns    int
	MaxOpenConns    int
}

type QBool added in v0.1.4

type QBool struct {
	Value bool
	Valid bool
}

func InitQBool added in v0.1.8

func InitQBool(value bool) QBool

func NewQBool added in v0.1.8

func NewQBool(value bool) *QBool

func (QBool) MarshalBinary added in v0.1.4

func (t QBool) MarshalBinary() ([]byte, error)

func (QBool) MarshalJSON added in v0.1.4

func (t QBool) MarshalJSON() ([]byte, error)

func (*QBool) Set added in v0.1.5

func (t *QBool) Set(value bool) *QBool

Set the value and force to update

func (*QBool) UnmarshalBinary added in v0.1.4

func (t *QBool) UnmarshalBinary(data []byte) error

func (*QBool) UnmarshalJSON added in v0.1.4

func (t *QBool) UnmarshalJSON(data []byte) error

type QData

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

QData Database Connection

func Open

func Open(args ...interface{}) (dbc *QData, err error)

Open MySQL Connection Open(connectionString, debugLevel)

func (*QData) Begin

func (c *QData) Begin() *QData

Begin returns a transaction handler

func (*QData) Close

func (dbc *QData) Close() error

Close MySQL Connection

func (*QData) Commit

func (c *QData) Commit() error

Commit will do as it named

func (*QData) DBName

func (dbc *QData) DBName() string

DBName return Name of Database

func (*QData) DBStat

func (dbc *QData) DBStat() sql.DBStats

DBStat return sql.DBStats

func (*QData) ExecUnsafe added in v0.2.1

func (c *QData) ExecUnsafe(query string, args ...interface{}) (sql.Result, error)

func (*QData) FinAfterFuncOK added in v0.0.8

func (c *QData) FinAfterFuncOK(txFunc func() error) error

func (*QData) FinDefaultCommit added in v0.0.8

func (c *QData) FinDefaultCommit() error

func (*QData) FinDefaultRollback added in v0.0.8

func (c *QData) FinDefaultRollback() error

func (*QData) Model

func (dbc *QData) Model(model interface{}) *QStat

Model returns a qStat object with default method SQLSelect

func (*QData) QueryRowUnsafe added in v0.2.1

func (c *QData) QueryRowUnsafe(query string, args ...interface{}) (row *sql.Row)

func (*QData) QueryUnsafe added in v0.2.3

func (c *QData) QueryUnsafe(query string, args ...interface{}) (*sql.Rows, error)

func (*QData) Rollback

func (c *QData) Rollback() error

Rollback will do as it named

func (*QData) SQLDB

func (dbc *QData) SQLDB() QInterface

SQLDB return *sql.DB

type QFloat64 added in v0.1.4

type QFloat64 struct {
	Value float64
	Valid bool
}

func InitQFloat64 added in v0.1.8

func InitQFloat64(value float64) QFloat64

func NewQFloat64 added in v0.1.8

func NewQFloat64(value float64) *QFloat64

func (QFloat64) MarshalBinary added in v0.1.4

func (t QFloat64) MarshalBinary() ([]byte, error)

func (QFloat64) MarshalJSON added in v0.1.4

func (t QFloat64) MarshalJSON() ([]byte, error)

func (*QFloat64) Set added in v0.1.5

func (t *QFloat64) Set(value float64) *QFloat64

Set the value and force to update

func (*QFloat64) UnmarshalBinary added in v0.1.4

func (t *QFloat64) UnmarshalBinary(data []byte) error

func (*QFloat64) UnmarshalJSON added in v0.1.4

func (t *QFloat64) UnmarshalJSON(data []byte) error

type QInt added in v0.1.6

type QInt struct {
	Value int
	Valid bool
}

func InitQInt added in v0.1.8

func InitQInt(value int) QInt

func NewQInt added in v0.1.8

func NewQInt(value int) *QInt

func (QInt) MarshalBinary added in v0.1.6

func (t QInt) MarshalBinary() ([]byte, error)

func (QInt) MarshalJSON added in v0.1.6

func (t QInt) MarshalJSON() ([]byte, error)

func (*QInt) Set added in v0.1.6

func (t *QInt) Set(value int) *QInt

Set the value and force to update

func (*QInt) UnmarshalBinary added in v0.1.6

func (t *QInt) UnmarshalBinary(data []byte) error

func (*QInt) UnmarshalJSON added in v0.1.6

func (t *QInt) UnmarshalJSON(data []byte) error

type QInterface

type QInterface 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
	Stats() sql.DBStats
	Begin() (*sql.Tx, error)
	SetMaxOpenConns(n int)
	SetMaxIdleConns(n int)
	SetConnMaxLifetime(d time.Duration)
}

QInterface Interface

type QResult

type QResult struct {
	AffectedRows int64
	LastInsertId int64
	ReturnedRows int64
	Warning      uint
	Error        error
}

QResult encapsulates the result of the SQL query

func (*QResult) String

func (re *QResult) String() string

type QStat

type QStat struct {
	Variables   map[string]string
	Method      qMethod
	Filters     []qClause
	GroupS      string
	HavingS     string
	OrderS      string
	RowLimit    int
	BeginOffset int
	BatchMode   bool
	LockFor     string
	// contains filtered or unexported fields
}

QStat ... Support multiple value states

func (*QStat) AppendBatchValue

func (stat *QStat) AppendBatchValue(val map[string]interface{}) *QStat

AppendBatchValue append the maps-type value when batch mode enabled

func (*QStat) BatchInsert

func (stat *QStat) BatchInsert() *QResult

BatchInsert executes the multiply value insert `AffectedRows` returns the number of rows inserted `LastInsertId` returns the PK of first inserted row

func (*QStat) BatchUpdate

func (stat *QStat) BatchUpdate() *QResult

BatchUpdate executes the CASE-WHEN-THEN update Fieldname case sensitive

func (*QStat) ClearWhere

func (stat *QStat) ClearWhere() *QStat

ClearWhere ...

func (*QStat) Count

func (stat *QStat) Count() *QResult

Count the number of rows in result set

func (*QStat) CreateTable

func (stat *QStat) CreateTable() *QResult

CreateTable creates a table defined by qStruct each field must have `SCHEMA` tag

func (*QStat) Delete

func (stat *QStat) Delete() *QResult

Delete return *QResult

func (*QStat) Exec

func (stat *QStat) Exec() *QResult

Exec the query

func (*QStat) ExecUnsafe added in v0.0.3

func (stat *QStat) ExecUnsafe(query string, args ...interface{}) (sql.Result, error)

func (*QStat) FreeLength

func (stat *QStat) FreeLength(it bool) *QStat

func (*QStat) GroupBy

func (stat *QStat) GroupBy(keys string) *QStat

GroupBy ...

func (*QStat) Having

func (stat *QStat) Having(having string) *QStat

Having ...

func (*QStat) IgnoreNullAt added in v0.0.2

func (stat *QStat) IgnoreNullAt(i int) *QStat

IgnoreNullAt sets the i-th (start from 0)

func (*QStat) IndexWith

func (stat *QStat) IndexWith(i int) *QStat

IndexWith sets the i-th (start from 0) Field as Index

func (*QStat) Insert

func (stat *QStat) Insert() *QResult

Insert return *QResult

func (*QStat) Join

func (stat *QStat) Join(joins ...string) *QStat

Join ...

func (*QStat) Limit

func (stat *QStat) Limit(limit int) *QStat

Limit the query LIMIT row_count

func (*QStat) Model

func (stat *QStat) Model(model interface{}) *QStat

Model changes the model

func (*QStat) Offset

func (stat *QStat) Offset(offset int) *QStat

Offset the query OFFSET offset

func (*QStat) OrderBy

func (stat *QStat) OrderBy(order string) *QStat

OrderBy ...

func (*QStat) PrepareNext

func (stat *QStat) PrepareNext(it bool) *QStat

PrepareNext will prepare the next sql query

func (*QStat) Query

func (stat *QStat) Query() *QResult

Query return *QResult

func (*QStat) QueryLockFor added in v0.0.3

func (stat *QStat) QueryLockFor(lockType string) *QStat

func (*QStat) QueryRowUnsafe added in v0.0.3

func (stat *QStat) QueryRowUnsafe(query string, args ...interface{}) (row *sql.Row)

func (*QStat) Scope

func (stat *QStat) Scope(fn func(*QStat) *QStat) *QStat

func (*QStat) Self

func (stat *QStat) Self(n int, param string) *QStat

func (*QStat) Set

func (stat *QStat) Set(template string, vals ...interface{}) *QStat

func (*QStat) SetBatchMode

func (stat *QStat) SetBatchMode(val bool) *QStat

SetBatchMode is the Setter of the BatchMode

func (*QStat) SetModel

func (stat *QStat) SetModel(model interface{}) *QStat

SetModel will only analyse the model without query to database

func (*QStat) SetOnDuplicateKeyUpdateNCol

func (stat *QStat) SetOnDuplicateKeyUpdateNCol(val bool, colDefine map[string]interface{}) *QStat

SetOnDuplicateKeyUpdateNCol is the Setter of the OnDuplicateKeyUpdate and DuplicateKeyUpdateCol colDefin := "<col>": "<col_define>" will be handled as RAW

func (*QStat) String

func (stat *QStat) String() string

func (*QStat) Table

func (stat *QStat) Table(table string) *QStat

Table setter Also overwrite the $T0 variable

func (*QStat) TableOfFields

func (stat *QStat) TableOfFields(table string) *QStat

TableOfFields changes all the `Table` of EACH FIELD when its original `Table` is equal to $T0

func (*QStat) TableSchema

func (stat *QStat) TableSchema(defs ...string) *QStat

func (*QStat) Update

func (stat *QStat) Update() *QResult

Update return *QResult

func (*QStat) Variable

func (stat *QStat) Variable(key, value string) *QStat

func (*QStat) Where

func (stat *QStat) Where(operator, template string, vals ...interface{}) *QStat

Where ...

type QString added in v0.1.4

type QString struct {
	Value string
	Valid bool
}

func InitQString added in v0.1.8

func InitQString(value string) QString

func NewQString added in v0.1.8

func NewQString(value string) *QString

func (QString) MarshalBinary added in v0.1.4

func (t QString) MarshalBinary() ([]byte, error)

func (QString) MarshalJSON added in v0.1.4

func (t QString) MarshalJSON() ([]byte, error)

func (*QString) Set added in v0.1.5

func (t *QString) Set(value string) *QString

Set the value and force to update

func (*QString) UnmarshalBinary added in v0.1.4

func (t *QString) UnmarshalBinary(data []byte) error

func (*QString) UnmarshalJSON added in v0.1.4

func (t *QString) UnmarshalJSON(data []byte) error

type QTime added in v0.1.4

type QTime struct {
	Value time.Time
	Valid bool
}

func InitQTime added in v0.1.8

func InitQTime(value time.Time) QTime

func NewQTime added in v0.1.8

func NewQTime(value time.Time) *QTime

func (QTime) MarshalBinary added in v0.1.4

func (t QTime) MarshalBinary() ([]byte, error)

func (QTime) MarshalJSON added in v0.1.4

func (t QTime) MarshalJSON() ([]byte, error)

func (*QTime) Set added in v0.1.5

func (t *QTime) Set(value time.Time) *QTime

Set the value and force to update

func (*QTime) UnmarshalBinary added in v0.1.4

func (t *QTime) UnmarshalBinary(data []byte) error

func (*QTime) UnmarshalJSON added in v0.1.4

func (t *QTime) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

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