database

package
v0.0.0-...-76439fa Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2019 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DBConfig

type DBConfig struct {
	Driver             string `json:"driver"`
	Host               string `json:"host"`
	Port               int    `json:"port"`
	EnableSSL          bool   `json:"enable_ssl"`
	Username           string `json:"username"`
	Password           string `json:"password"`
	DBName             string `json:"db_name"`
	MaxIdleConnections int    `json:"max_idle_connections"`
	MaxOpenConnections int    `json:"max_open_connections"`
}

DBConfig defines basic db config model

func ReadConfig

func ReadConfig(path string) (*DBConfig, error)

ReadConfig defines method to read config file

type Database

type Database interface {

	// Selects one item, result will be stored in "out". It will be casted
	SelectOne(out interface{}, sql string, args ...interface{}) error

	// Selects multiple items, result will be stored in "out". It will be casted
	SelectMany(out interface{}, sql string, args ...interface{}) error

	// Queries for "SELECT <x> WHERE <y> in (...)". Got results in "out" interface (with casting)
	SelectIn(out interface{}, sql string, args ...interface{}) error

	// Queries for "SELECT <x> WHERE <y> in (...)". Got results in returning rows
	QueryIn(sql string, args ...interface{}) (*sql.Rows, error)

	// Executes an sql request, usually do not expect returning rows
	Exec(sql string, args ...interface{}) (sql.Result, error)

	// Executes an sql request, expect returning data (rows)
	ExecReturning(sql string, args ...interface{}) (*sql.Rows, error)
}

This is a basic interface for self-use database. Just in case that it might be changed to use different types of databases, we can do it with out affecting the upper level

func New

func New(config DBConfig) (Database, error)

New Creates a new psql database (most basic and traditional way)

func NewGAE

func NewGAE(config DBConfig) (Database, error)

NewGAE creates a new psql database (for Google App Engine only)

type PsqlDB

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

PsqlDB create a new db struct

func (*PsqlDB) Exec

func (d *PsqlDB) Exec(sql string, args ...interface{}) (sql.Result, error)

Exec executes query, usually non return values

func (*PsqlDB) ExecReturning

func (d *PsqlDB) ExecReturning(sql string, args ...interface{}) (*sql.Rows, error)

ExecReturning executes query, usually have return values as sql rows

func (*PsqlDB) QueryIn

func (d *PsqlDB) QueryIn(sql string, args ...interface{}) (*sql.Rows, error)

QueryIn executes "SELECT <x> WHERE <y> in (...)", result will be returned as sql rows

func (*PsqlDB) SelectIn

func (d *PsqlDB) SelectIn(out interface{}, sql string, args ...interface{}) error

SelectIn executes "SELECT <x> WHERE <y> in (...)", result in "out" (casted)

func (*PsqlDB) SelectMany

func (d *PsqlDB) SelectMany(out interface{}, sql string, args ...interface{}) error

SelectMany selects several items. result in "out" (casted)

func (*PsqlDB) SelectOne

func (d *PsqlDB) SelectOne(out interface{}, sql string, args ...interface{}) error

SelectOne selects one item. result in "out" (casted)

Directories

Path Synopsis
dbModels

Jump to

Keyboard shortcuts

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