dbless

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2022 License: MIT Imports: 10 Imported by: 4

README

go-dbless

Travis godoc License Go Report Card cover.run

Database library with less complexity and less modeling.

Install

go get -u github.com/ddliu/go-dbless

Usage

import (
    "github.com/ddliu/go-dbless"
    _ "github.com/ddliu/go-dbless/driver/mysql"
    _ "github.com/go-sql-driver/mysql"
)
db := dbless.Open("mysql", "dsn")
Basic
  • Insert
  • Update
  • Delete
  • GetRows
  • GetRow
  • Scalar
Schema
  • List databases
  • List tables
  • List columns
Resource
package main
import github.com/ddliu/go-dbless

func main() {
    db := somedb()

    resource := dbless.Resource{
        Name: "user",
        Timestamp: true,
        DB: db,
    }

    id, err := resource.Save(dbless.Record{
        "username": "ddliu",
        "password": "123456",
    })

    user, err := record.Get(id)

    listing := resource.List(dbless.ListInput{
        Pagination: dbless.Pagination {
            PageSize: 20,
            Page: 1,
        },
    })
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsRecordNotFound

func IsRecordNotFound(err error) bool

func ListColumnsByQuery added in v0.3.0

func ListColumnsByQuery(db *DB, dbname, tableName string) ([]*sql.ColumnType, error)

func ListDatabaseByQuery added in v0.3.0

func ListDatabaseByQuery(db *DB, query string, name string, exclude []string) ([]string, error)

func ListTableByQuery added in v0.3.0

func ListTableByQuery(db *DB, query string, name string, params ...interface{}) ([]string, error)

func RegisterDriver added in v0.3.0

func RegisterDriver(driver Driver)

Types

type DB added in v0.2.0

type DB struct {
	DB     *sql.DB
	Driver Driver
	Debug  bool
}

func New added in v0.2.0

func New(driverName string, db *sql.DB) *DB

func Open added in v0.2.0

func Open(driverName, dsn string) (*DB, error)

func (*DB) Delete added in v0.2.0

func (db *DB) Delete(table string, where string, args ...interface{}) (uint64, error)

func (*DB) GetPaging added in v0.2.0

func (db *DB) GetPaging(pageSize uint, page uint, sql string, args ...interface{}) (*PagedRows, error)

func (*DB) GetRow added in v0.2.0

func (db *DB) GetRow(sql string, args ...interface{}) (Record, error)

func (*DB) GetRows added in v0.2.0

func (db *DB) GetRows(sqlStr string, args ...interface{}) ([]Record, error)

func (*DB) Insert added in v0.2.0

func (db *DB) Insert(table string, row map[string]interface{}) error

func (*DB) InsertGetID added in v0.3.0

func (db *DB) InsertGetID(table string, row map[string]interface{}) (string, error)

func (*DB) ListColumns added in v0.2.0

func (db *DB) ListColumns(dbname, tablename string) ([]*sql.ColumnType, error)

func (*DB) ListDatabases added in v0.2.0

func (db *DB) ListDatabases() ([]string, error)

func (*DB) ListTables added in v0.2.0

func (db *DB) ListTables(dbname string) ([]string, error)

func (*DB) Scalar added in v0.2.0

func (db *DB) Scalar(sql string, args ...interface{}) (uint64, error)

func (*DB) Update added in v0.2.0

func (db *DB) Update(table string, row map[string]interface{}, where string, args ...interface{}) (uint64, error)

type Driver added in v0.2.0

type Driver interface {
	Name() string
	QuoteIdentifier(string) string
	Placeholder(values []interface{}) []string
	ListDatabases(db *DB) ([]string, error)
	ListTables(db *DB, dbname string) ([]string, error)
	ListColumns(db *DB, dbname string, tablename string) ([]*sql.ColumnType, error)
	ScanReceiver(t *sql.ColumnType) (interface{}, error)
}

type DriverNotImplementedError added in v0.2.0

type DriverNotImplementedError struct {
}

func (DriverNotImplementedError) Error added in v0.2.0

type Field

type Field struct {
	Name        string `json:"name"`
	Title       string `json:"title"`
	Description string `json:"description"`
	Meta        string `json:"meta"`
}

type Filter added in v0.1.3

type Filter map[string]interface{}

func (Filter) GetWhere added in v0.1.6

func (f Filter) GetWhere(db *DB) (string, []interface{})

type ListInput

type ListInput struct {
	Pagination Pagination `json: "pagination"`
	Filter     Filter     `json:"filter"`
}

type ListOutput

type ListOutput struct {
	Pagination
	List []Record `json:"list"`
}

type NoDriverError added in v0.2.0

type NoDriverError struct {
}

func (NoDriverError) Error added in v0.2.0

func (e NoDriverError) Error() string

type PagedRows

type PagedRows struct {
	Pagination
	List []Record `json:"list"`
}

type Pagination

type Pagination struct {
	PageSize  uint `json:"page_size"`
	Page      uint `json:"page"`
	PageTotal uint `json:"page_total"`
	Total     uint `json:"total"`
}

func NewPagination

func NewPagination(pageSize, page uint) *Pagination

func (*Pagination) GetOffsetLimit

func (p *Pagination) GetOffsetLimit() (uint, uint)

func (*Pagination) SetTotal

func (p *Pagination) SetTotal(total uint)

func (*Pagination) Valid

func (p *Pagination) Valid()

type Record

type Record map[string]interface{}

func (Record) ID

func (r Record) ID() string

func (Record) Unmarshal added in v0.1.7

func (r Record) Unmarshal(input interface{}) error

type RecordNotFoundError

type RecordNotFoundError struct {
}

func (RecordNotFoundError) Error

func (e RecordNotFoundError) Error() string

type Resource

type Resource struct {
	Name      string  `json:"name"`
	Title     string  `json:"title"`
	Fields    []Field `json:"fields"`
	Timestamp bool    `json:"timestamp"`
	Meta      string  `json:"meta"`
	DB        *DB     `json:"-"`
}

func (Resource) Delete

func (r Resource) Delete(filter interface{}) error

func (Resource) Get

func (r Resource) Get(filter interface{}) (Record, error)

func (Resource) List

func (r Resource) List(input ListInput) (*ListOutput, error)

func (Resource) Save

func (r Resource) Save(record Record, filter ...Filter) (string, error)

Directories

Path Synopsis
driver
test module

Jump to

Keyboard shortcuts

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