mysql

package
v0.0.0-...-1c5b680 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2019 License: MIT Imports: 6 Imported by: 0

README

  1. mysql DAO层抽象

需要弄清楚以下问题

  1. 自己的操作事件,互不阻塞,并发与有序
  2. 自己与他人的操作,互不阻塞
  3. 事务操作,提交,回滚

***************************************************************

dao共包含以下接口:
insert 插入一条数据
insertList 批量插入
update 更新
del 删除
select 查询一条数据
selectCols 自定义列查询一条数据
list 查询一组数据
listCols 自定义列查询一组数据
count 查询数据总数记录

***************************************************************


# 具体用法:
------------------------------------------------
1. insert 插入一条数据:
var testEntity = require('uw-test').TestEntity;
var testEntity = new TestEntity();
testDao.insert(client, testEntity, function(err, data){
//data返回插入数据的id
var id = data.insertId;
});

insertList 批量插入
var testEntity1 = new TestEntity();
var testEntity2 = new TestEntity();
testDao.insertList(client, [testEntity1,testEntity2], function(err, data){
//data返回空值
});
------------------------------------------------
2. update 更新:
两种使用方式
一、
testDao.update(client, {name:"aaa",lvl:2}, {id:1},function(err, data){
//data返回空值
});
二、
testDao.update(client, {name:"aaa",lvl:2}, " id = ? and name = ? and name is not null ",[1,"xxx"],function(err,
data){
//data返回空值
});
------------------------------------------------
3. del 删除:
两种使用方式
一、
testDao.del(client,{id:1},function(err, data){
//data返回空值
});
二、
testDao.del(client, " id = ? and name = ? and name is not null ",[1,"xxx"],function(err, data){
//data返回空值
});

------------------------------------------------
4. select 查询一条数据:
两种使用方式
一、
testDao.select(client,{id:1},function(err, data){
//data返回object
});
二、
testDao.select(client, " id = ? and name = ? and name is not null ",[1,"xxx"],function(err, data){
//data返回object
});

select 自定义列查询一条数据:
两种使用方式
一、
testDao.selectCols(client," name,lvl ",{id:1},function(err, data){
//data返回object,只包含name,lvl
});
二、
testDao.selectCols(client, " name,lvl ", " id = ? and name = ? and name is not null ",[1,"xxx"],function(err, data){
//data返回object,只包含name,lvl
});

------------------------------------------------
5. list 查询一组数据:
两种使用方式
一、
testDao.list(client,{lvl:10},function(err, data){
//data返回数组[object,...]
});
二、
testDao.list(client, " id = ? and name = ? and name is not null ",[1,"xxx"],function(err, data){
//data返回[object,...]
});

listCols 自定义列查询一组数据:
两种使用方式
一、
testDao.listCols(client," name,lvl ",{lvl:10},function(err, data){
//data返回[object,...],object只包含name,lvl
});
二、
testDao.listCols(client, " name,lvl ", " lvl = ? and name is not null ",[10,"xxx"],function(err, data){
//data返回[object,...],object只包含name,lvl
});

------------------------------------------------
6. count 查询数据总数记录:
两种使用方式
一、
testDao.count(client, {lvl:10}, function(err, data){
//data返回记录总数
});
二、
testDao.count(client, " lvl = ? name is not null ",[10,"xxx"],function(err, data){
//data返回记录总数
});

------------------------------------------------

  1. 没必要每一次插入操作前都要做一次存在性的查询,陷入异常检测的恶梦。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Escape

func Escape(sql string) string

Types

type Client

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

func NewClient

func NewClient(dbAlias string, conf map[string]interface{}) (*Client, error)

func (*Client) Address

func (this *Client) Address() string

func (*Client) IsReady

func (this *Client) IsReady() bool

func (*Client) Operate

func (this *Client) Operate(callback func(Client interface{}) interface{}) interface{}

func (*Client) Raw

func (this *Client) Raw() interface{}

func (*Client) ReconnDur

func (this *Client) ReconnDur() time.Duration

func (*Client) SetAddress

func (this *Client) SetAddress(v string)

func (*Client) SetReconnDur

func (this *Client) SetReconnDur(v time.Duration)

func (*Client) Start

func (this *Client) Start()

func (*Client) Stop

func (this *Client) Stop()

type DaoSource

type DaoSource struct {
	SqlExecer
	Db *sql.DB
}

type SqlExecer

type SqlExecer interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	Prepare(query string) (*sql.Stmt, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
}

Jump to

Keyboard shortcuts

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