Help us shape the future of Go! Take the 2026 Go Developer Survey

query

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2019 License: MIT Imports: 2 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

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

This section is empty.

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 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 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

func Scan

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

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

func ScanLimited

func ScanLimited(dbRows *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 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