sqlz

package module
v0.0.0-...-cc8f96a Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2020 License: MIT Imports: 3 Imported by: 1

README

sqlz

用于go 中 统计sql情况:统计select,Insert,delete,update,次数,对应操作表的次数。

statistics the SQL count of the: select, Insert, delete, update... and the opration table.

Usage

improt (
	"encoding/json"
	"testing"
	"time"

	"sqlz"
)

func TestSQLZ(t *testing.T) {
	tcases := []struct {
		desc       string
		query      string
	}{
		{"create table sql.", "create table t1(id int, val int)"},
		{"select sql.", "select * from t1 where id = 2"},
		{"select sql.", "select * from t1,t2 where id = 2"T},
		{"insert sql.", "inSERT INTO t1(id, val) values(123,456),(678,90)"},
		{"delete sql.", "delete from t1 where id=3"},
		{"update sql.", "update t1 set val=333 where id=1"},
		{"drop table sql.", "drop table t1"},
		{"show tables", "show tables"},
		{"error sql", "error sql"},
		{"unknow sql", "set @@a=1"},
	}

	sqlz.StartZ()
	for _, tcase := range tcases {
		sqlz.Z(tcase.query)
	}

	time.Sleep(time.Second)

	st := sqlz.Status()
	byt, err := json.Marshal(st)
	if err != nil {
		t.Error(err.Error())
	} else {
		println(string(byt))
	}

	sqlz.StopZ()
}

Ouput

{"End": "2014-11-11T17:41:39.0728559+08:00",
 "Error": 1,
 "Opration": {"CREATE": {"TableCount": {"t1": 1}},
              "DELETE": {"TableCount": {"t1": 1}},
              "DROP": {"TableCount": {"t1": 1}},
              "INSERT": {"TableCount": {"t1": 1}},
              "SELECT": {"TableCount": {"t1": 2, "t2": 1}},
              "SHOW": {"TableCount": {"tables": 1}},
              "UNKNOW_SQL": {"TableCount": {"nil": 1}},
              "UPDATE": {"TableCount": {"t1": 1}}},
 "Start": "2014-11-11T17:41:38.0727987+08:00",
 "Success": 9,
 "Waiting": 0}

Status:

Stable

but nut support drop tables tb1,tb2 ... SQL. Just statistics the tb1, not include tb2.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StartZ

func StartZ()

----------------------------- 开启统计功能,可随时开启

func StopZ

func StopZ()

func Z

func Z(query string) bool

Types

type Count

type Count struct {
	// Table      string
	// Count      int64
	TableCount map[string]int64 //key is table name.
}

func NewCount

func NewCount() *Count

type SQL_Type

type SQL_Type string
const (
	ERROR_SQL SQL_Type = "ERROR_SQL"
	UNKNOW    SQL_Type = "UNKNOW_SQL"
	SELECT    SQL_Type = "SELECT"
	INSERT    SQL_Type = "INSERT"
	UPDATE    SQL_Type = "UPDATE"
	DELETE    SQL_Type = "DELETE"
	SHOW      SQL_Type = "SHOW"
	CREATE    SQL_Type = "CREATE"
	RENAME    SQL_Type = "RENAME"
	ALTER     SQL_Type = "ALTER"
	DROP      SQL_Type = "DROP"
)

func ParseQuery

func ParseQuery(query string) (action SQL_Type, table string)

type StatusResult

type StatusResult struct {
	Opration map[SQL_Type]*Count
	Error    int
	Success  int
	Waiting  int
	Start    time.Time
	End      time.Time
	// contains filtered or unexported fields
}

func Status

func Status() StatusResult

Directories

Path Synopsis
sqltypes
Package sqltypes implements interfaces and types that represent SQL values.
Package sqltypes implements interfaces and types that represent SQL values.

Jump to

Keyboard shortcuts

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