1 Star 0 Fork 0

简易开源 / gom

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

gom

gom - An Easy ORM library for Golang

golang Go Report Card GitHub GoDoc

基本介绍&特性

gom是一个基于golang语言的关系型数据库ORM框架(CRUD工具库,支持事务)

目前最新版本为v2.0,于2022年4月15 发布

当前支持的数据库类型仅为 mysql及其衍生品 mariadb

数据库类型支持自定义扩展(参考factory/mysql/mysql.go)

gom是goroutine安全的(自认为的安全)

稳定性及性能

和原生查询接近的查询性能(甚至更好),增删改性能略比原生差一些。

单元测试覆盖率90%,测试比较充分,但是仍不排除还有漏网之BUG

但是逻辑覆盖率没法做到百分之百,如使用过程中如出现问题,欢迎邮件我:kmlixh@foxmail.com或者直接给PR

本地测试的结果详见迭代注记

快速入门

使用go mod的情况下:

require github.com/kmlixh/gom/v2 v2.0.0
require github.com/go-sql-driver/mysql v1.6.0 // indirect,

或者

go get github.com/kmlixh/gom/v2

一个简单的CRUD示例

package main

import (
	"github.com/kmlixh/gom/v2"
	"github.com/kmlixh/gom/v2/cnds"
	_ "github.com/kmlixh/gom/v2/factory/mysql"
	"github.com/google/uuid"
	"time"
)

var dsn = "remote:remote123@tcp(10.0.1.5)/test?charset=utf8&loc=Asia%2FShanghai&parseTime=true"

type User struct {
	Id       int64     `json:"id" gom:"@,id"`
	Pwd      string    `json:"pwd" gom:"pwd"`
	Email    string    `json:"email" gom:"email"`
	Valid    int       `json:"valid" gom:"valid"`
	NickName string    `json:"nicks" gom:"nick_name"`
	RegDate  time.Time `json:"reg_date" gom:"reg_date"`
}

var db *gom.DB

func init() {
	//Create DB ,Global
	var er error
	db, er = gom.Open("mysql", dsn, true)
	if er != nil {
		panic(er)
	}
}

func main() {
	var users []User
	//Query
	db.Where(cnds.New("name",cnds.Eq,"kmlixh")).Page(0, 100).Select(&users)
	//Update
	temp := users[0]
	temp.NickName = uuid.New().String()
	temp.RegDate = time.Now()
	db.Update(temp)
	//Delete
	db.Delete(users[1])
	tt := User{
		Pwd:      "123213",
		Email:    "1@test.com",
		Valid:    1,
		NickName: uuid.New().String(),
		RegDate:  time.Now(),
	}
	db.Insert(tt)

}

DB结构体具有的方法(函数)如下:

RawDb() 获取原生的sql.Db对象
Table(tableName string) 设置表名
Raw() *sql.Db 获取go底层的db对象
OrderBy()排序
CleanOrders清除排序
OrderByAsc
OrderByDesc
Where2
Where
Clone
Page
Count
Sum
Select
SelectByModel
First
Insert
Delete
Update
ExecuteRaw
ExecuteStatement
Begin
IsInTransaction
Commit
Rollback
DoTransaction
CleanDb

迭代注记

2022年4月15日 01:56:50

v2.0
代码几乎全部重构,你大概可以认为这是一个全新的东西,API全变了(不过也没事,之前的版本也就我一个人在用^_^自嗨锅)
代码测试覆盖率93.0%(相关的测试覆盖率结果可以看test_cover.html以及cover.out)

此处略作测试摘录证明一下我真的做过测试了:

go test  -cover -coverprofile=cover.out -coverpkg=./...

init DB.............
PASS
coverage: 93.0% of statements in ./...
ok      github.com/kmlixh/gom   9.112s

然后Benchmark也顺手写了粗糙的两个:

go test -bench="." -benchmem -run="TestNothing" 
   
init DB.............
goos: darwin
goarch: amd64
pkg: github.com/kmlixh/gom
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkBaseSelect-16                       138           8654269 ns/op          662728 B/op      10397 allocs/op
BenchmarkBaseSelectGom-16                    122           8936071 ns/op          679967 B/op      14406 allocs/op
BenchmarkDB_InsertSingle-16                   74          19828957 ns/op            5403 B/op        109 allocs/op
BenchmarkRaw_InsertSingle-16                  66          17606781 ns/op            1175 B/op         22 allocs/op
PASS
ok      github.com/kmlixh/gom   6.176s

查询的性能比原始查询是差了一些的,这个需要承认

2019年6月19日 17:44:18

v1.1.2
修复CreateSingleTable的一些bug

2019年6月15日 08:18:25

v1.1.1
修复一些bug;
增加NotIn模式

2019年5月15日 09:18:06

v1.0.8
截止1.0.8又修复了若干bug,详细请看commit

2019年4月30日 11:15:38

1.修复了大量的bug;(具体可以看提交记录)
2.改造了数据获取的方式,从原来的固定格式转换,变成了接近于数据库底层的Scanner模式的性能
3.优化了自定义类型的查询和存储

2017年6月22日 12:54:36

1.修复若干bug(具体修复哪些bug记不清了 ^_^)
2.修复Update,Insert,Delete方法传入不定参数时的bug(无法解析,或者解析不正确,使用递归解决)
3.修复Condition为空的情况下会莫名注入一个“where”进入sql语句的bug 
4.Db对象增加了一个Count函数,故名思议,用来做count的

2017年6月18日22:47:53

1.修复无法使用事务的bug
2.修改了数据库操作的一些基础逻辑,每次操作前都会进行Prepare操作,以提高一些“性能”
3.为了修复上面的bug,修改了整体的gom.Db结构
MIT License Copyright (c) 2022 李兴华 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
HTML 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/janyees/gom.git
git@gitee.com:janyees/gom.git
janyees
gom
gom
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891