ksql

package module
v0.0.0-...-2f17661 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2021 License: MIT Imports: 6 Imported by: 0

README

ksql

GoDoc Build Status license

A simple extension to the golang database/sql package that facilitates getting row columns by name. The goal is to keep the existing sql package interface intact, yet allow the use of additional methods to satisfy the added functionality. This allows a seamless swap of the database/sql package with this one.

Feature highlights:

  • Internal pool of database connections that allows getting a connection by name, and not have to keep a global pointer
  • Support for queries with arbitrary number of result columns, get what you need.
  • Get the last row of results even after the Rows are closed.

Install

  go get github.com/kahoon/ksql

Usage

package main

import (
	"fmt"
	"log"

	sql "github.com/kahoon/ksql"
	_ "github.com/lib/pq"
)

// >>CHANGE<<
const (
	HOSTNAME string = "192.168.1.10"
	DATABASE        = "test"
	USER            = "postgres"
	PASSWORD        = "postgres"
)

var (
	drop   = "drop table if exists people"
	schema = "create table people (id integer not null,name text not null,married boolean not null,last_modified timestamp not null,primary key(id))"
	data   = []string{
		"insert into people values (1,'John Doe','f','1980-12-01 01:02:03')",
		"insert into people values (2,'Jane Doe','t','1999-12-01 01:02:03')",
	}
	query = "select * from people"
)

func populate() {
	db, ok := sql.Get("master")
	if !ok {
		log.Fatalf("Database doesn't exist!\n")
	}
	_, err := db.Exec(drop)
	if err != nil {
        log.Fatal(err)
    }
	_, err = db.Exec(schema)
	if err != nil {
		log.Fatal(err)
	}
	for _, value := range data {
		_, err = db.Exec(value)
		if err != nil {
			log.Fatal(err)
		}
	}
}

func main() {
	// create a connection and name it
	db, err := sql.New("master", "postgres", fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", USER, PASSWORD, HOSTNAME, DATABASE))
	if err != nil {
		log.Fatal(err)
	}
	// close all open database on exit
	defer sql.Close()
	// populate the schema
	populate()
	// query the data
	rows, err := db.Query(query)
	if err != nil {
		log.Fatalf("select failed %v", err)
	}
	for rows.Next() {
		id, err := rows.GetInteger("id")
		if err != nil {
			log.Fatal(err)
		}
		name, err := rows.GetString("name")
		if err != nil {
			log.Fatal(err)
		}
		last, err := rows.GetTime("last_modified")
		if err != nil {
			log.Fatal(err)
		}
		log.Println(id, name, last)
	}
	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}
	defer rows.Close()
	row := db.QueryRow("select * from people where id=1")
	id, err := row.GetInteger("id")
	if err != nil {
		log.Fatal(err)
	}
	name, err := row.GetString("name")
	if err != nil {
		log.Fatal(err)
	}
	log.Println(id,name)
}

Documentation

Overview

A simple extension to the golang database/sql package that facilitates getting row columns by name. The goal is to keep the existing sql package interface intact, yet allow the use of additional methods to satisfy the added functionality. This allows a seamless swap of the database/sql package with this one. Methods not list are directly inherited from database/sql

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoRows                      = sql.ErrNoRows
	ErrDupConnName                 = errors.New("ksql: duplicate database connection name")
	ErrColumnNotFound              = errors.New("ksql: column not found in result")
	ErrInvalidColumnTypeConversion = errors.New("ksql: invalid column type conversion")
)

Errors

Functions

func Close

func Close()

Close all open databases connections.

func Databases

func Databases() []string

Get list of names of open database connections

Types

type DB

type DB struct {
	*sql.DB
}

Inherit database/sql.DB

func Get

func Get(name string) (*DB, bool)

Get an open database connection by name

func New

func New(name, driver, dsn string) (*DB, error)

Open a new database connection, and save the reference by name

func NewWithDB

func NewWithDB(name string, db *sql.DB) (*DB, error)

Manage an already open database, and save the reference by name

func (*DB) Begin

func (db *DB) Begin() (*Tx, error)

func (*DB) Close

func (db *DB) Close() error

Close this database connection

func (*DB) Prepare

func (db *DB) Prepare(query string) (*Stmt, error)

func (*DB) Query

func (db *DB) Query(query string, args ...interface{}) (*Rows, error)

func (*DB) QueryRow

func (db *DB) QueryRow(query string, args ...interface{}) *Row

type Row

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

func (*Row) GetBoolean

func (r *Row) GetBoolean(column string) (bool, error)

Get the boolean value in this row by column name

func (*Row) GetDouble

func (r *Row) GetDouble(column string) (float64, error)

Get the float value in this row by column name

func (*Row) GetInteger

func (r *Row) GetInteger(column string) (int64, error)

Get the integer value in this row by column name

func (*Row) GetString

func (r *Row) GetString(column string) (string, error)

Get the string value in this row by column name

func (*Row) GetTime

func (r *Row) GetTime(column string) (time.Time, error)

Get the time.Time value in this row by column name

func (*Row) Scan

func (r *Row) Scan(dest ...interface{}) error

type Rows

type Rows struct {
	*sql.Rows
	// contains filtered or unexported fields
}

Inherit database/sql.Rows

func (*Rows) Err

func (rs *Rows) Err() error

func (*Rows) GetBoolean

func (rs *Rows) GetBoolean(column string) (bool, error)

Get the boolean value in this row by column name

func (*Rows) GetDouble

func (rs *Rows) GetDouble(column string) (float64, error)

Get the float value in this row by column name

func (*Rows) GetInteger

func (rs *Rows) GetInteger(column string) (int64, error)

Get the integer value in this row by column name

func (*Rows) GetString

func (rs *Rows) GetString(column string) (string, error)

Get the string value in this row by column name

func (*Rows) GetTime

func (rs *Rows) GetTime(column string) (time.Time, error)

Get the time.Time value in this row by column name

func (*Rows) Next

func (rs *Rows) Next() bool

type Stmt

type Stmt struct {
	*sql.Stmt
}

func (*Stmt) Query

func (s *Stmt) Query(args ...interface{}) (*Rows, error)

func (*Stmt) QueryRow

func (s *Stmt) QueryRow(args ...interface{}) *Row

type Tx

type Tx struct {
	*sql.Tx
}

func (*Tx) Prepare

func (tx *Tx) Prepare(query string) (*Stmt, error)

func (*Tx) Query

func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error)

func (*Tx) QueryRow

func (tx *Tx) QueryRow(query string, args ...interface{}) *Row

func (*Tx) Stmt

func (tx *Tx) Stmt(stmt *Stmt) *Stmt

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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