# README
SQL Database
This package provides interfaces to abstract working with the Go "database/sql" standard library. This allows services you build to unit test SQL interactions.
This README won't go into the details of each interface, as their concrete types are well documented in the Go standard library.
- ColumnType
- DB
- Row
- Rows
- Scanner (this is not in the sdtlib. If abstracts the concent of *sql.Row and *sql.Rows for scanning rows)
- Stmt
- Tx
Examples
Connect to Database
var (
err error
db sqldatabase.DB
)
if db, err = sqldatabase.Open("mysql", "localhost:3306"); err != nil {
panic(err)
}
Query
func getData() ([]DataStruct, error) {
var (
err error
ctx context.Context
cancel context.CancelFunc
rows sqldatabase.Rows
row DataStruct
)
result := make([]DataStruct, 0, 100)
ctx, cancel = context.WithTimeout(context.Background, time.Second*30)
defer cancel()
if rows, err = db.Query("SELECT * FROM stuff"); err != nil {
return result, err
}
for rows.Next() {
if row, err = convertToStruct(rows); err != nil {
return result, err
}
result = append(result, row)
}
return result, nil
}
func convertToStruct(row sqldatabase.Scanner) (DataStruct, error) {
var (
err error
value1 string
value2 int
result DataStruct
)
if err = row.Scan(&value1, &value2); err != nil {
return result, err
}
result = DataStruct{
Value1: value1,
Value2: value2,
}
return result, nil
}
DDL
func update() error {
var (
err error
ctx context.Context
cancel context.CancelFunc
)
ctx, cancel = context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
if _, err = db.ExecContext(ctx, "UPDATE stuff set a=?", 2); err != nil {
return err
}
return nil
}
# Functions
AssignScanValue reads the mapping at a row and column index, determines
the type of value, and assigns it to the provided destination variable.
GetDBContext returns a context with the timeout set to the value
configured in the application config.
LimitAndOffset returns a string used in a SQL query to limit the number
of records and set the initial offet
*/.
NullBool returns the bool value from a SQL bool.
NullDate parses a string date in the format of 2006-01-02 to time.Time.
NullDateWithFormat parses a string date in the format provided.
NullFloat returns the float value from a SQL float.
NullFloatFromString returns the float value from a SQL string.
NullInt returns the int value from a SQL int64.
NullString returns the string value from a SQL string.
NullTime returns the time.Time value of a SQL time.
Open opens a database specified by its database driver name and a driver-specific
data source name, usually consisting of at least a database name and connection
information.
Scan scans values into dest..
# Variables
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ScanMappingItem decribes a value and what kind of value it is.
# Interfaces
ColumnType contains the name and type of a column.
DB is a database handle representing a pool of zero or more underlying
connections.
Row is the result of calling QueryRow to select a single row.
Rows is the result of a query.
Scanner describes a strut that can scan columns from a record
*/.
Stmt is a prepared statement.
Tx is an in-progress database transaction.
# Type aliases
ScanMapping is a slice of a slice of ScanMappingItem structs.