-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go
88 lines (73 loc) · 2.44 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package orm
import (
"database/sql/driver"
"encoding/json"
"time"
)
// Index 索引信息
type Index map[string]interface{}
// ArgTrans 开启事务的参数
type ArgTrans struct {
Level string // 事务级别
Timeout time.Duration // 超时回滚
}
// ArgObjects 获取Objects的定制参数
type ArgObjects struct {
LogLevel int // 日志级别
}
// Model 模型定义
type Model interface {
String() string // 表名
Objects() Objects // 返回object对象
ObjectsWith(opt *ArgObjects) Objects // 返回object对象
// 通过tag来定义索引:
// unique索引 Name string `sorm:"unique"`
// index索引 Name string `sorm:"index"`
// TODO: 全文索引 Name string `sorm:"text"`
Ensure(st interface{}) error // 通过struct的tag来 添加字段,确认索引
// 确认字段存在
EnsureColumn(st interface{}) error
// 确认索引存在
EnsureIndex(index Index) error
// 事务
Begin() (Trans, error) // 事务开始
BeginWith(opt *ArgTrans) (Trans, error) // 指定事务开始
Commit(Trans) error // 阶段二提交
Rollback(Trans) error // 回滚操作
AutoTrans(Trans) error // 依据Trans内是否储存有错误来自动决定回滚或提交
// 表的读写锁
Lock()
Unlock()
RLock()
RUnlock()
// 直接执行数据库语句
Exec(query string, args ...interface{}) (result Result, err error)
Select(dest interface{}, query string, args ...interface{}) (err error)
// 删表
Drop() error
// other
With(opt *ArgModel) Model // 返回一个新的model
}
// Result summarizes an executed SQL command. 结果信息
type Result interface {
// LastInsertId returns the integer generated by the database
// in response to a command. Typically this will be from an
// "auto increment" column when inserting a new row. Not all
// databases support this feature, and the syntax of such
// statements varies.
LastInsertId() (int64, error)
// RowsAffected returns the number of rows affected by an
// update, insert, or delete. Not every database or database
// driver may support this.
RowsAffected() (int64, error)
}
// TypeJSONValue 通用json内联字段
type TypeJSONValue struct{}
// Value SQL规范
func (m TypeJSONValue) Value() (driver.Value, error) {
return json.Marshal(m)
}
// Scan SQL规范
func (m *TypeJSONValue) Scan(src interface{}) (err error) {
return json.Unmarshal(src.([]byte), m)
}