go>> rorm>> 返回
项目作者: kodekoding

项目描述 :
Raw Query ORM - as light as raw, as easy as orm
高级语言: Go
项目地址: git://github.com/kodekoding/rorm.git
创建时间: 2019-11-16T16:57:41Z
项目社区:https://github.com/kodekoding/rorm

开源协议:

下载


Table Of Contents

Benchmarking vs other ORM

Environment:

  • MySQL 5.7
  • MacBook Pro (mid 2017)
  • Intel Core i7-7567U @ 3.5 GHz
  • 16GB 2133 MHz LPDDR3

source : https://github.com/kihamo/orm-benchmark

command : orm-benchmark -orm=all (-multi=1 default)

  1. Reports:
  2. 2000 times - Insert
  3. raw: 3.00s 1497568 ns/op 552 B/op 12 allocs/op
  4. --- rorm: 3.18s 1588970 ns/op 472 B/op 6 allocs/op ---
  5. qbs: 3.56s 1780562 ns/op 4304 B/op 104 allocs/op
  6. orm: 3.60s 1801961 ns/op 1427 B/op 36 allocs/op
  7. modl: 4.41s 2206199 ns/op 1317 B/op 28 allocs/op
  8. hood: 4.66s 2330843 ns/op 10738 B/op 155 allocs/op
  9. gorp: 4.76s 2381558 ns/op 1390 B/op 29 allocs/op
  10. xorm: 5.40s 2699078 ns/op 2562 B/op 66 allocs/op
  11. gorm: 7.97s 3985524 ns/op 7695 B/op 148 allocs/op
  12. 500 times - MultiInsert 100 row
  13. --- rorm: 1.51s 3018679 ns/op 41888 B/op 105 allocs/op ---
  14. orm: 1.52s 3037726 ns/op 103987 B/op 1529 allocs/op
  15. raw: 2.41s 4823872 ns/op 108566 B/op 810 allocs/op
  16. xorm: 2.74s 5483371 ns/op 228503 B/op 4663 allocs/op
  17. gorp: Not support multi insert
  18. hood: Not support multi insert
  19. modl: Not support multi insert
  20. gorm: Not support multi insert
  21. qbs: Not support multi insert
  22. 2000 times - Update
  23. --- rorm: 1.40s 700864 ns/op 288 B/op 5 allocs/op ---
  24. raw: 1.47s 734044 ns/op 616 B/op 14 allocs/op
  25. orm: 1.67s 836207 ns/op 1384 B/op 37 allocs/op
  26. xorm: 2.81s 1405482 ns/op 2665 B/op 100 allocs/op
  27. modl: 3.23s 1613397 ns/op 1489 B/op 36 allocs/op
  28. qbs: 3.27s 1637255 ns/op 4298 B/op 104 allocs/op
  29. gorp: 3.54s 1770792 ns/op 1536 B/op 35 allocs/op
  30. hood: 4.89s 2443941 ns/op 10733 B/op 155 allocs/op
  31. gorm: 8.99s 4494195 ns/op 18612 B/op 383 allocs/op
  32. 4000 times - Read (will be fixed asap)
  33. qbs: 2.89s 721410 ns/op 6358 B/op 176 allocs/op
  34. raw: 3.16s 789744 ns/op 1432 B/op 37 allocs/op
  35. orm: 3.48s 871127 ns/op 2610 B/op 93 allocs/op
  36. hood: 5.80s 1450978 ns/op 4020 B/op 48 allocs/op
  37. --- rorm: 5.96s 1489016 ns/op 1792 B/op 40 allocs/op ---
  38. gorm: 6.27s 1566251 ns/op 12153 B/op 239 allocs/op
  39. modl: 6.36s 1591131 ns/op 1873 B/op 45 allocs/op
  40. xorm: 6.66s 1664184 ns/op 9354 B/op 260 allocs/op
  41. gorp: 6.76s 1690332 ns/op 1872 B/op 52 allocs/op
  42. 2000 times - MultiRead limit 100 (will be fixed asap)
  43. raw: 2.28s 1139187 ns/op 34704 B/op 1320 allocs/op
  44. modl: 2.32s 1159111 ns/op 49864 B/op 1721 allocs/op
  45. orm: 2.38s 1191493 ns/op 85020 B/op 4283 allocs/op
  46. gorp: 2.49s 1243186 ns/op 63685 B/op 1909 allocs/op
  47. --- rorm: 3.75s 1875212 ns/op 40441 B/op 1536 allocs/op ---
  48. qbs: 3.75s 1875799 ns/op 165634 B/op 6428 allocs/op
  49. hood: 4.32s 2158469 ns/op 136081 B/op 6358 allocs/op
  50. xorm: 5.37s 2685240 ns/op 180061 B/op 8091 allocs/op
  51. gorm: 5.44s 2721136 ns/op 254686 B/op 6226 allocs/op

Support Database

No Database
1 MySQL
2 Postgres
3 SQL Server

Installation

  1. go get github.com/kodekoding/rorm

import to your project

  1. import "github.com/kodekoding/rorm"

Features (will be completed soon)

Feature Using Description
Select Select(cols …string) Specify the column will be query
SelectSum SelectSumn(col string) Specify the single column to be summarize
SelectAverage SelectAverage(col string) Specify the single column to average the value
SelectMax SelectMax(col string) Specify the single column to get max the value
SelectMin SelectMin(col string) Specify the single column to get min the value
SelectCount SelectCount(col string) Specify the single column to get total data that column
Where Where(col string, value interface{}, opt …string) set the condition, ex: Where(“id”, 1, “>”) -> “WHERE id > 1” Where(“name”, “test”) => “WHERE name = ‘test’”
WhereIn
WhereNotIn
WhereLike
Or
OrIn
OrNotIn
OrLike
GroupBy
Join
Limit
OrderBy
Asc
Desc

How To Use

Configure the Host

  1. // Mandatory DBConfig
  2. dbConfig := &rorm.DbConfig{
  3. Host: "localhost",
  4. Driver: "(mysql | postgres | sqlserver)",
  5. Username: "your_username",
  6. DbName: "your_dbName",
  7. }

All Property DBConfig

  1. dbConfig := &rorm.DbConfig{
  2. Host: "your_host", //mandatory
  3. Driver: "DB Driver", //mandatory
  4. Username: "dbUsername", //mandatory
  5. DbName:"database_name", //mandatory
  6. Password: "dbPass",
  7. DbScheme: "db Scheme", // for postgres scheme, default is "Public" Scheme
  8. Port: "port", //default 3306 (mysql), 5432 (postgres)
  9. Protocol: "db Protocol", //default is "tcp"
  10. DbInstance: "db Instance", // for sqlserver driver if necessary
  11. }

Init New Engine

  1. // Please make sure the variable "dbConfig" pass by reference (pointer)
  2. db, err := rorm.New(dbConfig)
  3. if err != nil {
  4. log.Fatalln("Cannot Connet to database")
  5. }
  6. log.Println("Success Connect to Database")

Init the models

  1. -- We Have a table with this structure
  2. CREATE TABLE Student (
  3. id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  4. name VARCHAR(100) NULL,
  5. address TEXT NULL,
  6. is_active BOOLEAN NULL,
  7. birth_date DATE NULL,
  8. )
  1. // Init the models (struct name MUST BE SAME with table name)
  2. type Student struct {
  3. // db tag, filled based on column name
  4. Id int `db:"id" sql:"pk"` //identify as PK column
  5. Name string `db:"name"`
  6. Address string `db:"address"`
  7. // IsActive is boolean field
  8. IsActive bool `db:"is_active"`
  9. // BirthDate is "Date" data type column field
  10. BirthDate string `db:"birth_date"`
  11. }
  12. // init student struct to variable
  13. var studentList []Student
  14. var student Student

Create New SQL Select Query

Get All Data

With SQL Raw

  1. if err := db.SQLRaw(`SELECT name, address, birth_date FROM Student [JOIN ..... ON .....]
  2. [WHERE ......... [ORDER BY ...] [LIMIT ...]]`).Get(&studentList); err != nil {
  3. log.Fatalln(err.Error())
  4. }
  5. log.Println("result is, ", studentList)

WITH Query Builder

  1. // Get All Students data
  2. if err := db.Get(&studentList); err != nil {
  3. log.Fatalln(err.Error())
  4. }
  5. log.Println("result is, ", studentList)
  1. -- it will generate :
  2. SELECT * FROM student

Get Multiple Result Data with Where Condition

  1. // Get Specific Data
  2. if err := db.Select("name, address, birth_date").Where("is_active", true).Get(&studentList); err != nil {
  3. log.Fatalln(err.Error())
  4. }
  5. log.Println("result is, ", studentList)
  1. -- it will generate: (prepared Statement)
  2. SELECT name, address, birth_date FROM student WHERE is_active = ?

  1. // Get Specific Data (other example)
  2. if err := db.Select("name", "address", "birth_date").
  3. Where("is_active", 1).WhereLike("name", "%Lorem%").
  4. Get(&studentList); err != nil {
  5. log.Fatalln(err.Error())
  6. }
  7. log.Println("result is, ", studentList)
  1. -- it will generate:
  2. SELECT name, address, birth_date FROM student WHERE is_active = ? AND name LIKE ?

Get Single Result Data with Where Condition

  1. // Get Specific Data (single Result)
  2. if err := db.Select("name, address, birth_date").Where("id", 1).Get(&student); err != nil {
  3. log.Fatalln(err.Error())
  4. }
  5. log.Println("result is, ", student)
  1. -- it will generate:
  2. SELECT name, address, birth_date FROM student WHERE id = ?

Create, Update, Delete Query

Insert

Single Insert

  1. dtStudent := Student{
  2. Name: "test",
  3. Address: "test",
  4. IsActive: 1,
  5. BirthDate: "2010-01-01",
  6. }
  7. affected, err := db.Insert(&dtStudent)
  8. if err != nil {
  9. log.Fatalln("Error When Insert")
  10. }
  11. if affected > 0 {
  12. log.Println("Success Insert")
  13. }
  1. -- it will generate : (mysql)
  2. INSERT INTO Student (name, address, is_active, birth_date) VALUES (?,?,?,?)
  3. -- prepared Values :
  4. -- ('test', 'test', 1, '2010-01-01')

Multiple Insert

  1. dtStudents := []Student{
  2. Student{
  3. Name: "test",
  4. Address: "test",
  5. IsActive: 1,
  6. BirthDate: "2010-01-01",
  7. },
  8. Student{
  9. Name: "test2",
  10. Address: "test2",
  11. IsActive: 1,
  12. BirthDate: "2010-01-02",
  13. },
  14. Student{
  15. Name: "test3",
  16. Address: "test3",
  17. IsActive: 1,
  18. BirthDate: "2010-01-03",
  19. },
  20. }
  21. affected, err := db.Insert(&dtStudent)
  22. if err != nil {
  23. log.Fatalln("Error When Insert")
  24. }
  25. if affected > 0 {
  26. log.Println("Success Insert")
  27. }
  1. -- it will generate : (mysql)
  2. INSERT INTO Student (name, address, is_active, birth_date) VALUES (?,?,?,?)
  3. -- prepared Values :
  4. -- 1. ('test', 'test', 1, '2010-01-01')
  5. -- 2. ('test2', 'test2', 1, '2010-01-02')
  6. -- 3. ('test3', 'test3', 1, '2010-01-03')

Update

All Columns

  1. dtStudent := Student{
  2. Name: "change",
  3. Address: "change",
  4. IsActive: 1,
  5. BirthDate: "2010-01-10",
  6. }
  7. affected, err := db.Where("id", 1).Update(&dtStudent)
  8. if err != nil {
  9. log.Fatalln("Error When Update")
  10. }
  11. if affected > 0 {
  12. log.Println("Success Update")
  13. }
  1. -- it will generate : (mysql)
  2. UPDATE Student SET name = ?, address = ?, is_active = ?, birth_date = ? WHERE id = ?
  3. -- prepared Values :
  4. -- ('change', 'change', 1, '2010-01-10', 1)

Specific Column

  1. dtStudent := Student{
  2. Name: "change",
  3. IsActive: 0,
  4. }
  5. // Add Function 'BindUpdateCol' and filled parameter with db column name lists
  6. affected, err := db.Where("id", 1).BindUpdateCol("name", "is_active").Update(&dtStudent)
  7. if err != nil {
  8. log.Fatalln("Error When Update")
  9. }
  10. if affected > 0 {
  11. log.Println("Success Update")
  12. }
  1. -- it will generate : (mysql)
  2. UPDATE Student SET name = ?, is_active = ? WHERE id = ?
  3. -- prepared Values :
  4. -- ('change', 0, 1)

Delete

  1. affected, err := db.Where("id", 1).Delete(&Student{})
  2. if err != nil {
  3. log.Fatalln("Error When Delete")
  4. }
  5. if affected > 0 {
  6. log.Println("Success Delete")
  7. }
  1. -- it will generate : (mysql)
  2. DELETE FROM Student WHERE id = ?
  3. -- prepared Values :
  4. -- (1)