sqlconvenient

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

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

Go to latest
Published: Aug 15, 2018 License: MIT Imports: 3 Imported by: 0

README

This package exports two functions

SqlExec(db *sql.DB, query string, params ...interface{})

SqlQuery(db *sql.DB, rettype interface{}, query string, params ...interface{})

and allows querying/executing sql operations in just 2-3 lines, resulting in less boilerplate. Example:

var e Entry
var query = "select * from testtable"
return sqlconvenient.SqlQuery(db, &e, query)

See a more in depth example in _example/ and documentation over here.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SqlExec

func SqlExec(db *sql.DB, query string, params ...interface{}) error

Prepares the query string by inserting given parameters and then executes the query. For example used for SQL insert operations.

func SqlQuery

func SqlQuery(db *sql.DB, rettype interface{}, query string, params ...interface{}) []interface{}

Passes params into query string, executes the query and returns a slice of type rettype disguised as interface. This is useful when the real type of the slice isn't actually needed. For example passing the result into a json encoder works fine even with this interface slice. If need be, one can convert the results in 3 lines into a slice with correct type:

realslice := make([]REALTYPE, len(results))
for i, val := range results {
	realslice[i] = val.(REALTYPE)
}

This is used for SQL select operations. rettype can not only be a struct, but also a simple type such as string or int.

Example usage:

var p Product
result := sqlQueryStruct(&p, "...")

B E W A R E This uses reflect, thus is inefficient and purely for convenience.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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