sqltk

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

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

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 10 Imported by: 12 modules, 12 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

sqltk

Functions for simple SQL queries and commands.

Try to make SQL actions more easily.

Install:

go get -v github.com/topxeq/sqltk

Usage:

See the test file(sqltk_test.go) for details.

or in short:

dbT, errT := sqltk.ConnectDB("goracle", dbConnectString)

tk.CheckErrf("failed to connect: %v", errT)

defer dbT.Close()

insertIDT, rowsAffectedT, errT := sqltk.ExecV(dbT, `DROP TABLE TXTEST`)

tk.Plvsr(insertIDT, rowsAffectedT, errT)

createStatementT := `
CREATE TABLE TXTEST (
	ID NUMBER(10),
	USER_NAME VARCHAR(256),
	CODE NUMBER,
	UPDATE_TIME DATE
)
`

insertIDT, rowsAffectedT, errT = sqltk.ExecV(dbT, createStatementT)

tk.CheckErrf("failed to create DB: %v", errT)

tk.Plvsr(insertIDT, rowsAffectedT)

insertIDT, rowsAffectedT, errT = sqltk.ExecV(dbT, `insert into TXTEST (ID, USER_NAME, CODE, UPDATE_TIME) values(:a1, :a2, :a3, TO_DATE(:a4 ,'yyyy-mm-dd hh24:mi:ss'))`, 1, "abc", 1.2, "2019-04-22 15:30:00")

tk.CheckErrf("failed to insert record: %v", errT)

tk.Plvsr(insertIDT, rowsAffectedT)

tk.Plvsr(sqltk.ExecV(dbT, `insert into TXTEST (ID, USER_NAME, CODE, UPDATE_TIME) values(:a1, :a2, :a3, TO_DATE(:a4 ,'yyyy-mm-dd hh24:mi:ss'))`, 1, "abc", 1.2, "2019-04-22 15:30:00"))

resultSetT, errT := sqltk.QueryDBNS(dbT, "select * from TXTEST")

tk.CheckErrf("failed to query: %v", errT)

tk.Plvsr(resultSetT)

tk.Plvsr(sqltk.QueryDBNS(dbT, "select count(*) from TXTEST"))

tk.Plvsr(sqltk.QueryDBI(dbT, "select * from TXTEST"))

tk.Plvsr(sqltk.QueryDBNS(dbT, "select * from TXTEST where ID=?", 3))

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BeginTransX = SqlTKX.BeginTransX
View Source
var CloseDBX = SqlTKX.CloseDBX
View Source
var CommitX = SqlTKX.CommitX
View Source
var ConnectDB = SqlTKX.ConnectDB
View Source
var ConnectDBNoPing = SqlTKX.ConnectDBNoPing
View Source
var ConnectDBX = SqlTKX.ConnectDBX
View Source
var ExecDBX = SqlTKX.ExecDBX
View Source
var ExecV = SqlTKX.ExecV
View Source
var FormatSQLValue = SqlTKX.FormatSQLValue
View Source
var GetVersion = SqlTKX.GetVersion
View Source
var ListToSQLList = SqlTKX.ListToSQLList
View Source
var NewSqlTK = SqlTKX.NewSqlTK
View Source
var OneColumnRecordsToArray = SqlTKX.OneColumnRecordsToArray
View Source
var OneLineRecordToMap = SqlTKX.OneLineRecordToMap
View Source
var PrepareX = SqlTKX.PrepareX
View Source
var QueryCountX = SqlTKX.QueryCountX
View Source
var QueryDBCount = SqlTKX.QueryDBCount
View Source
var QueryDBFloat = SqlTKX.QueryDBFloat
View Source
var QueryDBI = SqlTKX.QueryDBI
View Source
var QueryDBIX = SqlTKX.QueryDBIX
View Source
var QueryDBMapArrayX = SqlTKX.QueryDBMapArrayX
View Source
var QueryDBMapX = SqlTKX.QueryDBMapX
View Source
var QueryDBNS = SqlTKX.QueryDBNS
View Source
var QueryDBNSS = SqlTKX.QueryDBNSS
View Source
var QueryDBNSSF = SqlTKX.QueryDBNSSF
View Source
var QueryDBNSV = SqlTKX.QueryDBNSV
View Source
var QueryDBOrderedX = SqlTKX.QueryDBOrderedX
View Source
var QueryDBRecsIX = SqlTKX.QueryDBRecsIX
View Source
var QueryDBRecsX = SqlTKX.QueryDBRecsX
View Source
var QueryDBS = SqlTKX.QueryDBS
View Source
var QueryDBString = SqlTKX.QueryDBString
View Source
var QueryDBX = SqlTKX.QueryDBX
View Source
var QueryFloatX = SqlTKX.QueryFloatX
View Source
var QueryStringX = SqlTKX.QueryStringX
View Source
var RecordsToMapArray = SqlTKX.RecordsToMapArray
View Source
var RecordsToMapArrayMap = SqlTKX.RecordsToMapArrayMap
View Source
var SqlTKX = &SqlTK{Version: versionG}

Functions

This section is empty.

Types

type OrderedMap

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

OrderedMap 是一个保持插入顺序的有序 Map。

func NewOrderedMap

func NewOrderedMap() *OrderedMap

NewOrderedMap 创建一个新的 OrderedMap。

func (*OrderedMap) Delete

func (om *OrderedMap) Delete(key interface{})

Delete 删除指定键。

func (*OrderedMap) Get

func (om *OrderedMap) Get(key interface{}) (interface{}, bool)

Get 获取键对应的值。

func (*OrderedMap) GetByIndex

func (om *OrderedMap) GetByIndex(idxA int) (interface{}, bool)

GetByIndex 按索引获取值。

func (*OrderedMap) GetItems

func (om *OrderedMap) GetItems() []interface{}

GetItems 按插入顺序返回所有键值对。

func (*OrderedMap) GetKeyByIndex

func (om *OrderedMap) GetKeyByIndex(idxA int) (interface{}, bool)

GetKeyByIndex 按索引获取键。

func (*OrderedMap) GetKeys

func (om *OrderedMap) GetKeys() []interface{}

GetKeys 按插入顺序返回所有键。

func (*OrderedMap) GetString

func (om *OrderedMap) GetString(key interface{}) string

GetString 获取键对应的字符串值。

func (*OrderedMap) GetStringKeys

func (om *OrderedMap) GetStringKeys() []string

GetStringKeys 按插入顺序返回所有字符串键。

func (*OrderedMap) GetValues

func (om *OrderedMap) GetValues() []interface{}

GetValues 按插入顺序返回所有值。

func (*OrderedMap) HasKey

func (om *OrderedMap) HasKey(key interface{}) bool

HasKey 判断键是否存在。

func (*OrderedMap) Len

func (om *OrderedMap) Len() int

Len 返回条目数量。

func (*OrderedMap) MarshalJSON

func (om *OrderedMap) MarshalJSON() ([]byte, error)

MarshalJSON 按插入顺序序列化为 JSON 对象。

func (*OrderedMap) Set

func (om *OrderedMap) Set(key interface{}, value interface{})

Set 设置键值对,如果键已存在则更新其值。

func (*OrderedMap) ToMap

func (om *OrderedMap) ToMap() map[string]interface{}

ToMap 转换为 map[string]interface{}。

func (*OrderedMap) UnmarshalJSON

func (om *OrderedMap) UnmarshalJSON(data []byte) error

UnmarshalJSON 按文档顺序解析 JSON 对象为键值对。

type OrderedMapPair

type OrderedMapPair struct {
	Key   interface{}
	Value interface{}
	// contains filtered or unexported fields
}

OrderedMapPair 是 OrderedMap 中保存的一对键值。The element is a pointer to the list element.

type SqlTK

type SqlTK struct {
	Version string
}

func (*SqlTK) BeginTransX

func (pA *SqlTK) BeginTransX(dbA *sql.DB) interface{}

func (*SqlTK) CloseDBX

func (pA *SqlTK) CloseDBX(dbA *sql.DB) error

func (*SqlTK) CommitX

func (pA *SqlTK) CommitX(transA *sql.Tx, sqlA string) interface{}

func (*SqlTK) ConnectDB

func (pA *SqlTK) ConnectDB(driverStrA string, connectStrA string) (*sql.DB, error)

ConnectDB connected the database, don't forget to close it(probably by defer function)

func (*SqlTK) ConnectDBNoPing

func (pA *SqlTK) ConnectDBNoPing(driverStrA string, connectStrA string) (*sql.DB, error)

ConnectDBNoPing connected the database(with no ping action), don't forget to close it(probably by defer function)

func (*SqlTK) ConnectDBX

func (pA *SqlTK) ConnectDBX(driverStrA string, connectStrA string) interface{}

func (*SqlTK) ExecDBX

func (pA *SqlTK) ExecDBX(dbA *sql.DB, sqlStrA string, argsA ...interface{}) interface{}

func (*SqlTK) ExecV

func (pA *SqlTK) ExecV(dbA *sql.DB, sqlStrA string, argsA ...interface{}) (int64, int64, error)

ExecV execute SQL statement, get the results(insert id and rows afftected), passing parameters is supported as well.

func (*SqlTK) FormatSQLValue

func (pA *SqlTK) FormatSQLValue(strA string) string

FormatSQLValue equivalent to strings.Replace(strA, "'", "”")

func (*SqlTK) GetVersion

func (pA *SqlTK) GetVersion() string

func (*SqlTK) ListToSQLList

func (pA *SqlTK) ListToSQLList(vA interface{}) string

func (*SqlTK) NewSqlTK

func (pA *SqlTK) NewSqlTK() *SqlTK

func (*SqlTK) OneColumnRecordsToArray

func (pA *SqlTK) OneColumnRecordsToArray(recsA [][]string) []string

OneColumnRecordsToArray convert SQL result in [][]string (several lines, first is the header, only one column per line) to []string

func (*SqlTK) OneLineRecordToMap

func (pA *SqlTK) OneLineRecordToMap(recA [][]string) map[string]string

OneLineRecordToMap convert SQL result in [][]string (2 lines, first is the header) to map[string]string

func (*SqlTK) PrepareX

func (pA *SqlTK) PrepareX(transA *sql.Tx, sqlA string) interface{}

func (*SqlTK) QueryCountX

func (pA *SqlTK) QueryCountX(dbA *sql.DB, sqlStrA string, argsA ...interface{}) interface{}

func (*SqlTK) QueryDBCount

func (pA *SqlTK) QueryDBCount(dbA *sql.DB, sqlStrA string, argsA ...interface{}) (int, error)

QueryDBCount execute a SQL query for count(select count(*)), -1 indicates error, can handle null values, passing parameters is supported as well. Also used to get a single int result from SQL query.

func (*SqlTK) QueryDBFloat

func (pA *SqlTK) QueryDBFloat(dbA *sql.DB, sqlStrA string, argsA ...interface{}) (float64, error)

QueryDBFloat execute a SQL query for get a single float value, can handle null values, passing parameters is supported as well.

func (*SqlTK) QueryDBI

func (pA *SqlTK) QueryDBI(dbA *sql.DB, sqlStrA string, argsA ...interface{}) ([][]interface{}, error)

QueryDBI execute a SQL query and return result set(first row will be the column names), all values will be interface{} type, passing parameters is supported as well.

func (*SqlTK) QueryDBIX

func (pA *SqlTK) QueryDBIX(dbA *sql.DB, sqlStrA string, argsA ...interface{}) interface{}

QueryDBIX execute a SQL query and return result set as []map[string]interface{} (each row is a map with column names as keys), values keep original types ([]byte converted to string), passing parameters is supported as well.

func (*SqlTK) QueryDBMapArrayX

func (pA *SqlTK) QueryDBMapArrayX(dbA *sql.DB, sqlStrA string, idA string, argsA ...interface{}) interface{}

func (*SqlTK) QueryDBMapX

func (pA *SqlTK) QueryDBMapX(dbA *sql.DB, sqlStrA string, idA string, argsA ...interface{}) interface{}

func (*SqlTK) QueryDBNS

func (pA *SqlTK) QueryDBNS(dbA *sql.DB, sqlStrA string, argsA ...interface{}) ([][]string, error)

QueryDBNS execute a SQL query and return result set(first row will be the column names), all values will be string type, can handle null values, passing parameters is supported as well.

func (*SqlTK) QueryDBNSS

func (pA *SqlTK) QueryDBNSS(dbA *sql.DB, sqlStrA string, argsA ...interface{}) ([][]string, error)

QueryDBNSS execute a SQL query and return result set(first row will be the column names), all values will be string type(ensure for some DBs, such as MYSQL with uf8_general_ci encoding), can handle null values, passing parameters is supported as well.

func (*SqlTK) QueryDBNSSF

func (pA *SqlTK) QueryDBNSSF(dbA *sql.DB, sqlStrA string, argsA ...interface{}) ([][]string, error)

QueryDBNSSF the same as QueryDBNSS, but use special format on float values, format with argument floatFormatA(i.e. %1.2f etc).

func (*SqlTK) QueryDBNSV

func (pA *SqlTK) QueryDBNSV(dbA *sql.DB, sqlStrA string, argsA ...interface{}) ([][]string, error)

QueryDBNSV execute a SQL query and return result set(first row will be the column names), all values will be string type(ensure for some DBs, such as MYSQL with uf8_general_ci encoding), can handle null values, passing parameters is supported as well.

func (*SqlTK) QueryDBOrderedX

func (pA *SqlTK) QueryDBOrderedX(dbA *sql.DB, sqlStrA string, argsA ...interface{}) interface{}

func (*SqlTK) QueryDBRecsIX

func (pA *SqlTK) QueryDBRecsIX(dbA *sql.DB, sqlStrA string, argsA ...interface{}) interface{}

QueryDBRecsIX execute a SQL query and return result set as [][]interface{} (first row will be the column names), values keep original types ([]byte converted to string), passing parameters is supported as well.

func (*SqlTK) QueryDBRecsX

func (pA *SqlTK) QueryDBRecsX(dbA *sql.DB, sqlStrA string, argsA ...interface{}) interface{}

func (*SqlTK) QueryDBS

func (pA *SqlTK) QueryDBS(dbA *sql.DB, sqlStrA string, argsA ...interface{}) ([][]string, error)

QueryDBS execute a SQL query and return result set(first row will be the column names), all values will be string type, cannot handle null values, passing parameters is supported as well.

func (*SqlTK) QueryDBString

func (pA *SqlTK) QueryDBString(dbA *sql.DB, sqlStrA string, argsA ...interface{}) (string, error)

QueryDBString execute a SQL query for a one string result, can handle null values, passing parameters is supported as well.

func (*SqlTK) QueryDBX

func (pA *SqlTK) QueryDBX(dbA *sql.DB, sqlStrA string, argsA ...interface{}) interface{}

func (*SqlTK) QueryFloatX

func (pA *SqlTK) QueryFloatX(dbA *sql.DB, sqlStrA string, argsA ...interface{}) interface{}

func (*SqlTK) QueryStringX

func (pA *SqlTK) QueryStringX(dbA *sql.DB, sqlStrA string, argsA ...interface{}) interface{}

func (*SqlTK) RecordsToMapArray

func (pA *SqlTK) RecordsToMapArray(recA [][]string) []map[string]string

RecordsToMapArray convert SQL result in [][]string (multi lines, first is the header) to []map[string]string

func (*SqlTK) RecordsToMapArrayMap

func (pA *SqlTK) RecordsToMapArrayMap(recA [][]string, keyA string) map[string][]map[string]string

Jump to

Keyboard shortcuts

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