dal

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2020 License: MIT Imports: 5 Imported by: 0

README

DAL

DAL provides a Database Access Layer in Go for MySQL.

Requirements

  • Go 1.10 or higher
  • MySQL(4.1+) or MariaDB

Installation

This package can be installed with the go get command.

go get github.com/viemacs/dal

API Reference

Examples can be found in the test file.

A model should be initialized first with connection options.

  • Initialize a Model of MySQL.
model := Model{
    DriverName:     "mysql",
    DataSourceName: "user:password@tcp(hostIP)/database",
}
  • Write values to database.
type T struct {
     ID int `field:"id"`
}
values := []T{{1}}
if err := model.Write("tableName", values); err != nil {
	dealWithError(err)
}
  • Read records from database.
if err := model.Read("tablename", []string{"id"}, "", T{}); err != nil {
	dealWithError(err)
}
for _, v := range model.Records {
    handleRecord(v.(T))
}
  • Get version of database.
if info := model.DBInfo(); len(info) != 1 {
	dealWithError(err)
}

Features

  • supports nested structs; does not support array/slice/map as struct fields
  • supports single query; does not support multiple result sets

Program with fail with panic if the connection to database cannot be established. The caller of this module can recover properly.

Todo

  1. check type of parameter values in Write()
  2. Read function skips passing query fields, or match query results with fields of struct.
  3. Support values of nested struct in Read function.

Issue

  1. Exec accepts normal interface, but currently values are all printed into strings.

TBConfirm

  1. sumRowsAffected: are records in database changed before tx.commit() ?

Notice

  1. Values of nested struct with duplicated fields are not supported yet. During writing, the duplicated fields will generate duplicated fields in SQL. During reading, only the first one of the duplicated fields will be filled.

Documentation

Overview

Package dal provides a database access layer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Model

type Model struct {
	DriverName     string
	DataSourceName string
	BatchSize      int
	Records        []interface{}
	// contains filtered or unexported fields
}

TODO: Model.Read() is not parallel-able Field `Records` contains the latest query results. Default batch size is 4K, if record-size is 4K, the max_allowed_package 16M is reached.

func (Model) Cleanup added in v0.0.2

func (model Model) Cleanup(table, fieldTime string, tm int64) (err error)

func (Model) Create added in v0.0.4

func (model Model) Create(table string, values interface{}) (rowsAffected int64, err error)

Create does insert-ignore on the given table. The values must be a non-empty slice of the same type.

func (Model) DBInfo

func (model Model) DBInfo() (info []string)

func (*Model) Read

func (model *Model) Read(table string, columns []string, condition string, v interface{}) (err error)

func (*Model) ReadPlain added in v0.0.8

func (model *Model) ReadPlain(table string, fields []string, condition string, readType interface{}) (err error)

func (Model) SQL

func (model Model) SQL(query string) (err error)

func (Model) Update added in v0.0.4

func (model Model) Update(table string, values interface{}) (rowsAffected int64, err error)

Update does insert-update on the given table. The values must be a non-empty slice of the same type.

Jump to

Keyboard shortcuts

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