connector

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package connector: connect to MySQL, execute raw sql statements, return raw execution result or error.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connector

type Connector struct {
	DSN      string
	Host     string
	Port     int
	Username string
	Password string
	DbName   string
	// contains filtered or unexported fields
}

Connector: connect to MySQL, execute raw sql statements, return raw execution result or error.

func NewConnector

func NewConnector(host string, port int, username string, password string, dbname string) (*Connector, error)

NewConnector: create Connector. CREATE DATABASE IF NOT EXISTS dbname + USE dbname when dbname != ""

func (*Connector) Close

func (conn *Connector) Close()

func (*Connector) ExecSQL

func (conn *Connector) ExecSQL(sql string) *Result

Connector.ExecSQL: execute sql, return *Result.

func (*Connector) InitDB

func (conn *Connector) InitDB() error

Connector.InitDB:

DROP DATABASE IF EXISTS Connector.DbName
CREATE DATABASE Connector.DbName
USE Connector.DbName

type Result

type Result struct {
	ColumnNames []string
	ColumnTypes []string
	Rows        [][]string
	Err         error
	Time        time.Duration // total time
}

Result:

query result, for example:

+-----+------+------+
| 1+2 | ID   | NAME | -> ColumnNames: 1+2,    ID,  NAME
+-----+------+------+ -> ColumnTypes: BIGINT, INT, TEXT
|   3 |    1 | H    | -> Rows[0]:     3,      1,   H
|   3 |    2 | Z    | -> Rows[1]:     3,      2,   Z
|   3 |    3 | Y    | -> Rows[2]:     3,      3,   Y
+-----+------+------+

or error, for example:

Err: ERROR 1054 (42S22): Unknown column 'T' in 'field list'

note that:

len(ColumnNames) = len(ColumnTypes) = len(Rows[i]);

if the statement is not SELECT, then the ColumnNames, ColumnTypes and Rows are empty

func (*Result) FlatRows

func (result *Result) FlatRows() []string

Result.FlatRows: [["1","2"],["3","4"]] -> ["1,2", "3,4"]

func (*Result) GetErrorCode

func (result *Result) GetErrorCode() (int, error)

func (*Result) IsEmpty

func (result *Result) IsEmpty() bool

Result.IsEmpty: if the result is empty

func (*Result) ToString

func (result *Result) ToString() string

Jump to

Keyboard shortcuts

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