sqlxt

package module
v0.0.0-...-4435bac Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2018 License: MIT Imports: 4 Imported by: 1

README

sqlxt

sqlxt is a golang package which provides a simple extension to database/sql standard package.
It's still in development, so it must improve in the future with more features and tests.

Usage

For the whole example, go to example/example.go.
For more examples, go to scanner_test.go

package main

import (
	"database/sql"
	"fmt"
	"log"
	"os"

	"github.com/avalchev94/sqlxt"
	_ "github.com/lib/pq"
)

func testMap(db *sql.DB) {
	rows, err := db.Query("SELECT * FROM newspapers")
	if err != nil {
		log.Fatalln(err)
	}

	newspapers := map[string]interface{}{}
	if err := sqlxt.NewScanner(rows).Scan(newspapers); err != nil {
		log.Fatalln(err)
	}

	fmt.Println(newspapers)
}

func testStruct(db *sql.DB) {
	type Newspaper struct {
		ID      int32  `sql:"id"`
		Title   string `sql:"title"`
		Country string `sql:"country"`
	}

	rows, err := db.Query("SELECT * FROM newspapers")
	if err != nil {
		log.Fatalln(err)
	}

	newspapers := []Newspaper{}
	if err := sqlxt.NewScanner(rows).Scan(&newspapers); err != nil {
		log.Fatalln(err)
	}

	fmt.Println(newspapers)
}

func main() {
	db, err := connectDB()
  
  ...
  ...

	testMap(db)
	testStruct(db)
}

Install

go get -u github.com/avalchev94/sqlxt

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Scanner

type Scanner struct {
	// contains filtered or unexported fields
}

Scanner is a type that will Scan your query's result.

func NewScanner

func NewScanner(rows *sql.Rows, err error) *Scanner

NewScanner is a function for creating new Scanner. Have in mind that Scanner is always successfully created, but later Scan method could fail because of problems is 'rows'.

func (*Scanner) Scan

func (s *Scanner) Scan(dest interface{}) error

Scan is the 'meat' of the package. It scan the 'rows' input into the 'dest' parameter. Dest variable could be: - primitive type(string, int, bool, interface{}) - struct (with or without 'sql' tags); - map with key(int, string, interface{}); - slice in combination with some of the above types;

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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