query

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2022 License: MIT Imports: 4 Imported by: 1

README

Query is a tiny package for executing select-like queries and converting sql.Rows into slice of map[string]string. The new map is allocated for each row. Keys are column names as seen in sql.

Install

Use go modules and just

import "github.com/rogozhka/query"

or

go get -u github.com/rogozhka/query

Example

package main

import (
	"github.com/rogozhka/query"

	"database/sql"
	"fmt"
	"log"
	
	_ "github.com/go-sql-driver/mysql"
)

func main() {

	db, err := sql.Open("mysql",
		"username:pass@tcp(hostname)/database_name")
	if err != nil {
		panic(err)
	}
	if db == nil {
		panic(fmt.Errorf("db is nil"))
	}
	defer db.Close()

	// fetch all the rows into slice of map[string]string
	rows, err := query.Fetch("SELECT * FROM `example_table`", db)
	if err != nil {
		panic(err)
	}

	for _, row := range rows {
		for colName, val := range row {
			log.Printf("%s:%s", colName, val)
		}
	}

	// fetch only 1 row
	// in real project you should use LIMIT 0,1
	// but there is limited version of method ;)
	rows2, err := query.FetchLimited("SELECT * FROM `example_table`", 1, db)
	if err != nil {
		panic(err)
	}

	if len(rows2) > 0 {
		for colName, val := range rows2[0] {
			log.Printf("%s:%s", colName, val)
		}
	}
}

Documentation

Overview

Package query is a tiny package for executing select-like queries and converting sql.Rows into slice of map[string]string The new map[string]string is allocated for each row. Keys are column names as seen in sql.

Index

Constants

This section is empty.

Variables

View Source
var ErrConvertion = errors.New("cannot convert")
View Source
var ErrResult = errors.New("result")

Functions

func Fetch

func Fetch(strQuery string, db *sql.DB) ([]map[string]string, error)

Fetch is used to execute sql and convert expected rows to slice of map[string]string with keys as a column names.

func FetchContext added in v1.2.0

func FetchContext(ctx context.Context, strQuery string, db *sql.DB) ([]map[string]string, error)

FetchContext is used to execute sql and convert expected rows to slice of map[string]string with keys as a column names.

func FetchLimited

func FetchLimited(strQuery string, rowsLimit uint64, db *sql.DB) ([]map[string]string, error)

FetchLimited is used to execute sql query and convert expected rows to array of map[string]string with keys as a column names; no more than rowsLimit elements OR unlimited if 0.

func FetchLimitedContext added in v1.2.0

func FetchLimitedContext(ctx context.Context, query string, maxRows uint64, db *sql.DB) ([]map[string]string, error)

FetchLimitedContext is used to execute sql query and convert expected rows to array of map[string]string with keys as a column names; no more than rowsLimit elements OR unlimited if 0.

func NewQueryWrap added in v1.1.0

func NewQueryWrap(db *sql.DB) *queryWrap

func NewQueryWrapVoid added in v1.1.0

func NewQueryWrapVoid(db *sql.DB) *queryWrapVoid

NewQueryWrapVoid created noop wrapper for testing.

func Scan

func Scan(rows *sql.Rows) ([]map[string]string, error)

Scan converts already executed query result into a slice of Row with keys as a column names.

func ScanContext added in v1.2.0

func ScanContext(ctx context.Context, rows *sql.Rows) ([]map[string]string, error)

func ScanLimited

func ScanLimited(rows *sql.Rows, rowsLimit uint64) ([]map[string]string, error)

ScanLimited converts already executed query result into a slice of Row with keys as a column names; no more than rowsLimit elements OR unlimited if 0.

func ScanLimitedContext added in v1.2.0

func ScanLimitedContext(ctx context.Context, rows *sql.Rows, rowsLimit uint64) ([]map[string]string, error)

ScanLimitedContext converts already executed query result into a slice of Row with keys as a column names; no more than rowsLimit elements OR unlimited if 0.

func Select

func Select(strQuery string, db *sql.DB) ([]map[string]string, error)

Select is a Fetch alias.

Types

This section is empty.

Jump to

Keyboard shortcuts

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