csdb

package
v0.0.0-...-d4f462a Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2015 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package csdb implements MySQL handling and general SQL functions.

Index

Constants

View Source
const (
	// EnvDSN is the name of the environment variable
	EnvDSN string = "CS_DSN"
	// EnvDSNTest test env DSN
	EnvDSNTest string = "CS_DSN_TEST"
)
View Source
const (
	// MainTable used in SQL to refer to the main table as an alias
	MainTable = "main_table"
	// AddTable additional table
	AdditionalTable = "additional_table"
	// ScopeTable used in SQl to refer to a website scope table as an alias
	ScopeTable = "scope_table"
)

Variables

View Source
var (
	ErrDSNNotFound     = errors.New("Env var: " + EnvDSN + " not found")
	ErrDSNTestNotFound = errors.New("Env var: " + EnvDSNTest + " not found")
)
View Source
var (
	ErrTableNotFound = errors.New("Table not found")
)

Functions

func Connect

func Connect() (*sql.DB, *dbr.Connection, error)

func GetDSN

func GetDSN() (string, error)

GetDSN returns the DSN from env or an error

func GetDSNTest

func GetDSNTest() (string, error)

GetDSNTest returns the DSN from env or an error

func LoadSlice

func LoadSlice(dbrSess dbr.SessionRunner, tsr TableStructurer, ti Index, dest interface{}, cbs ...DbrSelectCb) (int, error)

LoadSlice loads the slice dest with the table structure from tsr TableStructurer and table index ti. Returns the number of loaded rows and nil or 0 and an error. Slice must be a pointer to structs.

func MustConnectTest

func MustConnectTest() *sql.DB

mustConnectTest is a helper function that creates a new database connection using environment variables.

Types

type DbrSelectCb

type DbrSelectCb func(*dbr.SelectBuilder) *dbr.SelectBuilder

type Index

type Index int

type TableStructure

type TableStructure struct {
	// Name is the table name
	Name string
	// IDFieldNames contains only primary keys
	IDFieldNames []string
	// Columns all other columns which are not primary keys
	Columns []string
}

temporary place

func NewTableStructure

func NewTableStructure(n string, IDs, c []string) *TableStructure

func (*TableStructure) AllColumnAliasQuote

func (ts *TableStructure) AllColumnAliasQuote(alias string) []string

AllColumnAliasQuote prefixes all columns with an alias and puts quotes around them. Returns a copy.

func (*TableStructure) ColumnAliasQuote

func (ts *TableStructure) ColumnAliasQuote(alias string) []string

ColumnAliasQuote prefixes non-id columns with an alias and puts quotes around them. Returns a copy.

func (*TableStructure) In

func (ts *TableStructure) In(n string) bool

In checks if column name n is a column of this table

func (*TableStructure) Select

func (ts *TableStructure) Select(dbrSess dbr.SessionRunner) (*dbr.SelectBuilder, error)

Select generates a SELECT * FROM tableName statement

func (*TableStructure) TableAliasQuote

func (ts *TableStructure) TableAliasQuote(alias string) string

remove this once the ALIAS via []string is implemented in DBR

type TableStructureSlice

type TableStructureSlice []*TableStructure

TableStructureSlice implements interface TableStructurer

func (TableStructureSlice) Len

func (m TableStructureSlice) Len() Index

Len returns the length of the slice data

func (TableStructureSlice) Name

func (m TableStructureSlice) Name(i Index) string

Name is a short hand to return a table name by given index i. Does not return an error when the table can't be found.

func (TableStructureSlice) Next

func (m TableStructureSlice) Next(i Index) bool

Next iterator function where i is the current index starting with zero. Example:

for i := Index(0); tableMap.Next(i); i++ {
	table, err := tableMap.Structure(i)
	...
}

func (TableStructureSlice) Structure

func (m TableStructureSlice) Structure(i Index) (*TableStructure, error)

Structure returns the TableStructure from a read-only map m by a giving index i.

type TableStructurer

type TableStructurer interface {
	// Structure returns the TableStructure from a read-only map m by a giving index i.
	Structure(Index) (*TableStructure, error)
	// Name is a short hand to return a table name by given index i. Does not return an error
	// when the table can't be found.
	Name(Index) string

	// Next iterator function where i is the current index starting with zero.
	// Example:
	//	for i := Index(0); tableMap.Next(i); i++ {
	//		table, err := tableMap.Structure(i)
	//		...
	//	}
	Next(Index) bool
	// Len returns the length of the underlying slice
	Len() Index
}

Jump to

Keyboard shortcuts

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