Skip to content

Commit

Permalink
add day4-day7
Browse files Browse the repository at this point in the history
  • Loading branch information
geektutu committed Mar 8, 2020
1 parent 5093770 commit e06148d
Show file tree
Hide file tree
Showing 12 changed files with 858 additions and 50 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@

gorm 准备推出完全重写的 v2 版本(目前还在开发中),相对 gorm-v1 来说,xorm 的设计更容易理解,所以 geeorm 接口设计上主要参考了 xorm,一些细节实现上参考了 gorm。

- 第一天:database/sql 基础 | [Code](gee-orm/day1-database-sql)
- 第二天:对象表结构映射 | [Code](gee-orm/day2-reflect-schema)
- 第三天:插入/查询记录 | [Code](gee-orm/day3-save-query)
- 第四天:链式操作与更新删除 | [Code](gee-orm/day4-chain-operation)
- 第五天:实现钩子(Hooks) | [Code](gee-orm/day5-hooks)
- 第六天:支持事务(Transaction) | [Code](gee-orm/day6-transaction)
- 第七天:数据库迁移(Migrate) | [Code](gee-orm/day7-migrate)
- 第一天:[database/sql 基础](https://geektutu.com/post/geeorm-day1.html) | [Code](gee-orm/day1-database-sql)
- 第二天:[对象表结构映射](https://geektutu.com/post/geeorm-day2.html) | [Code](gee-orm/day2-reflect-schema)
- 第三天:[记录新增和查询](https://geektutu.com/post/geeorm-day3.html) | [Code](gee-orm/day3-save-query)
- 第四天:[链式操作与更新删除](https://geektutu.com/post/geeorm-day4.html) | [Code](gee-orm/day4-chain-operation)
- 第五天:[实现钩子(Hooks)](https://geektutu.com/post/geeorm-day5.html) | [Code](gee-orm/day5-hooks)
- 第六天:[支持事务(Transaction)](https://geektutu.com/post/geeorm-day6.html) | [Code](gee-orm/day6-transaction)
- 第七天:[数据库迁移(Migrate)](https://geektutu.com/post/geeorm-day7.html) | [Code](gee-orm/day7-migrate)

### WebAssembly 使用示例

Expand Down
13 changes: 8 additions & 5 deletions gee-orm/day5-hooks/session/hooks.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package session

import "reflect"
import (
"geeorm/log"
"reflect"
)

// Hooks constants
const (
BeforeQuery = "BeforeQuery"
AfterQuery = "AfterQuery"
BeforeUpdate = "BeforeUpdate"
AfterUpdate = "AfterUpate"
AfterUpdate = "AfterUpdate"
BeforeDelete = "BeforeDelete"
AfterDelete = "AfterDelete"
BeforeInsert = "BeforeInsert"
AfterInsert = "AfterInsert"
)

// CallMethod calls the registered hooks
func (s *Session) CallMethod(method string, value interface{}) error {
func (s *Session) CallMethod(method string, value interface{}) {
fm := reflect.ValueOf(s.RefTable().Model).MethodByName(method)
if value != nil {
fm = reflect.ValueOf(value).MethodByName(method)
Expand All @@ -24,9 +27,9 @@ func (s *Session) CallMethod(method string, value interface{}) error {
if fm.IsValid() {
if v := fm.Call(param); len(v) > 0 {
if err, ok := v[0].Interface().(error); ok {
return err
log.Error(err)
}
}
}
return nil
return
}
13 changes: 8 additions & 5 deletions gee-orm/day6-transaction/session/hooks.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package session

import "reflect"
import (
"geeorm/log"
"reflect"
)

// Hooks constants
const (
BeforeQuery = "BeforeQuery"
AfterQuery = "AfterQuery"
BeforeUpdate = "BeforeUpdate"
AfterUpdate = "AfterUpate"
AfterUpdate = "AfterUpdate"
BeforeDelete = "BeforeDelete"
AfterDelete = "AfterDelete"
BeforeInsert = "BeforeInsert"
AfterInsert = "AfterInsert"
)

// CallMethod calls the registered hooks
func (s *Session) CallMethod(method string, value interface{}) error {
func (s *Session) CallMethod(method string, value interface{}) {
fm := reflect.ValueOf(s.RefTable().Model).MethodByName(method)
if value != nil {
fm = reflect.ValueOf(value).MethodByName(method)
Expand All @@ -24,9 +27,9 @@ func (s *Session) CallMethod(method string, value interface{}) error {
if fm.IsValid() {
if v := fm.Call(param); len(v) > 0 {
if err, ok := v[0].Interface().(error); ok {
return err
log.Error(err)
}
}
}
return nil
return
}
2 changes: 1 addition & 1 deletion gee-orm/day7-migrate/geeorm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestEngine_Migrate(t *testing.T) {
defer engine.Close()
s := engine.NewSession()
_, _ = s.Raw("DROP TABLE IF EXISTS User;").Exec()
_, _ = s.Raw("CREATE TABLE User(Name text, XXX integer);").Exec()
_, _ = s.Raw("CREATE TABLE User(Name text PRIMARY KEY, XXX integer);").Exec()
_, _ = s.Raw("INSERT INTO User(`Name`) values (?), (?)", "Tom", "Sam").Exec()
engine.Migrate(&User{})

Expand Down
13 changes: 8 additions & 5 deletions gee-orm/day7-migrate/session/hooks.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package session

import "reflect"
import (
"geeorm/log"
"reflect"
)

// Hooks constants
const (
BeforeQuery = "BeforeQuery"
AfterQuery = "AfterQuery"
BeforeUpdate = "BeforeUpdate"
AfterUpdate = "AfterUpate"
AfterUpdate = "AfterUpdate"
BeforeDelete = "BeforeDelete"
AfterDelete = "AfterDelete"
BeforeInsert = "BeforeInsert"
AfterInsert = "AfterInsert"
)

// CallMethod calls the registered hooks
func (s *Session) CallMethod(method string, value interface{}) error {
func (s *Session) CallMethod(method string, value interface{}) {
fm := reflect.ValueOf(s.RefTable().Model).MethodByName(method)
if value != nil {
fm = reflect.ValueOf(value).MethodByName(method)
Expand All @@ -24,9 +27,9 @@ func (s *Session) CallMethod(method string, value interface{}) error {
if fm.IsValid() {
if v := fm.Call(param); len(v) > 0 {
if err, ok := v[0].Interface().(error); ok {
return err
log.Error(err)
}
}
}
return nil
return
}
2 changes: 1 addition & 1 deletion gee-orm/doc/geeorm-day2.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ github: https://github.com/geektutu/7days-golang

- 使用 dialect 隔离不同数据库之间的差异,便于扩展。
- 使用反射(reflect)获取任意 struct 对象的名称和字段,映射为数据中的表。
- 数据库表的创建(create)、删除(drop)。
- 数据库表的创建(create)、删除(drop)。**代码约150行**

## 1 Dialect

Expand Down
28 changes: 24 additions & 4 deletions gee-orm/doc/geeorm-day3.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: 动手写ORM框架 - GeeORM第三天 插入和查询记录
title: 动手写ORM框架 - GeeORM第三天 记录新增和查询
date: 2020-03-08 01:00:00
description: 7天用 Go语言/golang 从零实现 ORM 框架 GeeORM 教程(7 days implement golang object relational mapping framework from scratch tutorial),动手写 ORM 框架,参照 gorm, xorm 的实现。实现新增(insert)记录的功能;使用反射(reflect)将数据库的记录转换为对应的结构体实例,实现查询(select)功能。
tags:
Expand All @@ -22,7 +22,7 @@ published: false
本文是[7天用Go从零实现ORM框架GeeORM](https://geektutu.com/post/geeorm.html)的第三篇。

- 实现新增(insert)记录的功能。
- 使用反射(reflect)将数据库的记录转换为对应的结构体实例,实现查询(select)功能。
- 使用反射(reflect)将数据库的记录转换为对应的结构体实例,实现查询(select)功能。**代码约150行**

## 1 Clause 构造 SQL 语句

Expand Down Expand Up @@ -202,6 +202,26 @@ func TestClause_Build(t *testing.T) {

## 2 实现 Insert 功能

首先为 Session 添加成员变量 clause

```go
// session/raw.go
type Session struct {
db *sql.DB
dialect dialect.Dialect
refTable *schema.Schema
clause clause.Clause
sql strings.Builder
sqlVars []interface{}
}

func (s *Session) Clear() {
s.sql.Reset()
s.sqlVars = nil
s.clause = clause.Clause{}
}
```

clause 已经支持生成简单的插入(INSERT) 和 查询(SELECT) 的 SQL 语句,那么紧接着我们就可以在 session 中实现对应的功能了。

INSERT 对应的 SQL 语句一般是这样的:
Expand All @@ -216,7 +236,7 @@ INSERT INTO table_name(col1, col2, col3, ...) VALUES
在 ORM 框架中期望 Insert 的调用方式如下:

```go
s := geeorm.NewEngine().NewSession()
s := geeorm.NewEngine("sqlite3", "gee.db").NewSession()
u1 := &User{Name: "Tom", Age: 18}
u2 := &User{Name: "Sam", Age: 25}
s.Insert(u1, u2, ...)
Expand Down Expand Up @@ -282,7 +302,7 @@ func (s *Session) Insert(values ...interface{}) (int64, error) {
期望的调用方式是这样的:传入一个切片指针,查询的结果保存在切片中。

```go
s := geeorm.NewEngine().NewSession()
s := geeorm.NewEngine("sqlite3", "gee.db").NewSession()
var users []User
s.Find(&users);
```
Expand Down
Loading

0 comments on commit e06148d

Please sign in to comment.