Merge ee574ae9b817df03391e74e0619037c55b068fa1 into 9044197ef935c0969d94cbcfba55ccb94d269bed

This commit is contained in:
Au 2018-05-02 14:39:10 +00:00 committed by GitHub
commit fee89700d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 120 additions and 31 deletions

View File

@ -1,40 +1,28 @@
# GORM
# AuOrm
基于gorm 实现对原生查询的[]map[string]string{}返回
The fantastic ORM library for Golang, aims to be developer friendly.
- 支持增删改查
- 支持事物操作
[![go report card](https://goreportcard.com/badge/github.com/jinzhu/gorm "go report card")](https://goreportcard.com/report/github.com/jinzhu/gorm)
[![wercker status](https://app.wercker.com/status/8596cace912c9947dd9c8542ecc8cb8b/s/master "wercker status")](https://app.wercker.com/project/byKey/8596cace912c9947dd9c8542ecc8cb8b)
[![Join the chat at https://gitter.im/jinzhu/gorm](https://img.shields.io/gitter/room/jinzhu/gorm.svg)](https://gitter.im/jinzhu/gorm?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Open Collective Backer](https://opencollective.com/gorm/tiers/backer/badge.svg?label=backer&color=brightgreen "Open Collective Backer")](https://opencollective.com/gorm)
[![Open Collective Sponsor](https://opencollective.com/gorm/tiers/sponsor/badge.svg?label=sponsor&color=brightgreen "Open Collective Sponsor")](https://opencollective.com/gorm)
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
[![GoDoc](https://godoc.org/github.com/jinzhu/gorm?status.svg)](https://godoc.org/github.com/jinzhu/gorm)
Installation
------------
## Overview
Use go get.
* Full-Featured ORM (almost)
* Associations (Has One, Has Many, Belongs To, Many To Many, Polymorphism)
* Hooks (Before/After Create/Save/Update/Delete/Find)
* Preloading (eager loading)
* Transactions
* Composite Primary Key
* SQL Builder
* Auto Migrations
* Logger
* Extendable, write Plugins based on GORM callbacks
* Every feature comes with tests
* Developer Friendly
go get github.com/Jetereting/gorm
## Getting Started
Then import the validator package into your own code.
* GORM Guides [http://gorm.io](http://gorm.io)
import "github.com/Jetereting/gorm"
## Contributing
示例:
```golang
datas, e := db.RawMap("select * from users where user_id=?", 123)
if e != nil {
fmt.Println("err:", e)
}
fmt.Println("datas:", datas)
```
[You can help to deliver a better GORM, check out things you can do](http://gorm.io/contribute.html)
更多示例参照: [Au-ORM 测试](https://github.com/Jetereting/gorm/blob/master/gorm_test.go)
## License
© Jinzhu, 2013~time.Now
Released under the [MIT License](https://github.com/jinzhu/gorm/blob/master/License)

66
gorm_test.go Normal file
View File

@ -0,0 +1,66 @@
package gorm
import (
"fmt"
"testing"
"github.com/Jetereting/gorm"
_ "github.com/go-sql-driver/mysql"
)
var (
db *gorm.DB
err error
)
func init() {
db, err = gorm.Open("mysql", "user:password@tcp(ip:port)/dbName?charset=utf8")
if err != nil {
fmt.Println(err)
return
}
}
// TestQuery 测试查询
func TestQuery(t *testing.T) {
datas, e := db.RawMap("select * from users where user_id=?", 123)
if e != nil {
fmt.Println("err:", e)
}
fmt.Println("datas:", datas)
}
// TestIsCanInsert 测试插入
func TestIsCanInsert(t *testing.T) {
_, e := db.RawMap("insert into users(user_id,user_name,user_tag) values (?,?,?)", 123, "testName", "testTag")
if e != nil {
fmt.Println("err:", e)
}
fmt.Println("It work!")
}
// TestIsCanUpdate 测试更新
func TestIsCanUpdate(t *testing.T) {
_, e := db.RawMap("update users set user_name=? where user_id=?", "testName2", 123)
if e != nil {
fmt.Println("err:", e)
}
fmt.Println("It work!")
}
// TestTX 测试事物
func TestTX(t *testing.T) {
tx := db.Begin()
_, e := tx.RawMap("update users set user_name=? where user_id=?", "testName3", 123)
if e != nil {
fmt.Println("err:", e)
tx.Rollback()
}
_, e = tx.RawMap("update users set user_name=? where user_id=?", "long text....long text....long text....long text....long text....long text....long text....", 123)
if e != nil {
fmt.Println("err:", e)
tx.Rollback()
}
tx.Commit()
fmt.Println("done!")
}

35
main.go
View File

@ -445,6 +445,41 @@ func (s *DB) Raw(sql string, values ...interface{}) *DB {
return s.clone().search.Raw(true).Where(sql, values...).db
}
// RawMap sql查询返回[]map[string]string类型
func (s *DB) RawMap(sqlQuery string, sqlValues ...interface{}) (result []map[string]string, err error) {
rows, err := s.Raw(sqlQuery, sqlValues...).Rows()
if err != nil {
return
}
cols, err := rows.Columns()
if err != nil {
return
}
values := make([]sql.RawBytes, len(cols))
scanArgs := make([]interface{}, len(values))
for i := range values {
scanArgs[i] = &values[i]
}
for rows.Next() {
err = rows.Scan(scanArgs...)
if err != nil {
return
}
var value string
resultC := map[string]string{}
for i, col := range values {
if col == nil {
value = "NULL"
} else {
value = string(col)
}
resultC[cols[i]] = value
}
result = append(result, resultC)
}
return
}
// Exec execute raw sql
func (s *DB) Exec(sql string, values ...interface{}) *DB {
scope := s.NewScope(nil)