dataq

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2022 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
  • TABLE - The table name.
  • COL - The field name, also can be a function and with Tag RAW.
  • INDEX - This field is an index.
  • ASNULL - As NULL value.
  • 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.
  • 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.
  • JSONMERGEPATCH - Use JSON_MERGE_PATCH function to update the field.
  • OMIT - This field will be ignored in 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"

Variables

View Source
var ConfigParseDateTimeFormat = "2006-01-02 15:04:05.000"

Functions

func ClearValue

func ClearValue(v interface{})

Types

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) Model

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

Model returns a qStat object with default method SQLSelect

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 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
	// 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) 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) 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 ...

Jump to

Keyboard shortcuts

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