sql

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2020 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package sql is an extension to standard library "database/sql.DB" that provide common functionality across DBMS.

Index

Constants

View Source
const (
	DriverNameMysql    = "mysql"
	DriverNamePostgres = "postgres"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	DB         *sql.DB
	DriverName string
	TableNames []string // List of tables in database.
}

Client provide a wrapper for generic database instance.

func New

func New(driverName string, db *sql.DB) (cl *Client, err error)

New wrap a database client to provide additional methods.

func (*Client) FetchTableNames

func (cl *Client) FetchTableNames() (tableNames []string, err error)

FetchTableNames return the table names in current database schema sorted in ascending order.

func (*Client) TruncateTable

func (cl *Client) TruncateTable(tableName string) (err error)

TruncateTable truncate all data on table `tableName`.

type Row

type Row map[string]interface{}

Row represent a single row in table.

func (Row) ExtractSQLFields

func (row Row) ExtractSQLFields() (
	names, holders []string, values []interface{},
)

ExtractSQLFields extract the column's name, column place holder (default is "?"), and column values; as slices.

type Session

type Session interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	Prepare(query string) (*sql.Stmt, error)
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}

Session is an interface that represent both sql.DB and sql.Tx.

type Table

type Table struct {
	Name       string // Table name, required.
	PrimaryKey string // Primary key of table, optional.
	Rows       []Row  // The row or data in the table, optional.
}

Table represent a tuple or table in database.

A table has Name, PrimaryKey, and list of Row.

func (*Table) Insert

func (table *Table) Insert(tx *sql.Tx) (ids []int64, err error)

Insert all rows into table, one by one.

On success, it will return list of ID, if table has primary key.

Jump to

Keyboard shortcuts

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