Golang ORM Comparison

Source: Internet
Author: User
Tags connection pooling mssql

Introduction to the main Golang ORM

The current more mainstream/active ORM has Gorm, Xorm, gorose, etc.

Xorm

    • Document

      • GitHub
      • Document
      • Godoc
    • Supported databases are: MySQL, Mymysql, Postgres, TIDB, SQLite, MSSQL, Oracle
    • Transactional support
    • Chained API

      has, err := engine.Where("name = ?", name).Desc("id").Get(&user)err := engine.Where(builder.NotIn("a", 1, 2).And(builder.In("b", "c", "d", "e"))).Find(&users)
    • Supports native SQL operations
    • Query cache
    • Code can be generated based on database inversion
    • Cascade Load
    • Provide SQL statement log output
    • Support Batch Query processing

      // 每次处理100条// SELECT * FROM user Limit 0, 100// SELECT * FROM user Limit 101, 100err := engine.BufferSize(100).Iterate(&User{Name:name}, func(idx int, bean interface{}) error {    user := bean.(*User)    return nil})
    • Automated read/write separation/master-Slave
dataSourceNameSlice := []string{masterDataSourceName, slave1DataSourceName, slave2DataSourceName}engineGroup, err := xorm.NewEngineGroup(driverName, dataSourceNameSlice)

Gorm

    • Document

      • GitHub
      • Gorm
    • Hook mechanism (Before/after create/save/update/delete/find)
    • Object relationship has one, have many, belongs to, many to many, polymorphism
    • Unloading
    • Supports native SQL operations
    • Transactional
    • Chained API

      tx := db.Where("name = ?", "jinzhu").Where("age = ?", 20).Find(&users)
    • Supported databases are: MySQL, postgre, sqlite, SQL Server
    • Query operations

      // Get first record, order by primary keydb.First(&user)//// SELECT * FROM users ORDER BY id LIMIT 1;// plain sqldb.Where("name = ? AND age >= ?", "jinzhu", "22").Find(&users)// mapdb.Where(&User{Name: "jinzhu", Age: 20}).First(&user)//// SELECT * FROM users WHERE name = "jinzhu" AND age = 20 LIMIT 1;

Gorose

    • Document

      • GitHub
      • Document
    • Supported databases are: MySQL, Postgres, SQLite, MSSQL, Oracle
    • Chained API
    • Connect multiple databases and switch at the same time
    • Supports native SQL operations
    • Support Batch Query processing
    • Transactional

      User.Fields("id, name").Where("id",">",2).Chunk(2, func(data []map[string]interface{}) {    // for _,item := range data {    //     fmt.Println(item)    // }    fmt.Println(data)})

upper/db

There is not much support for both NoSQL and SQL, which is one of them (the other is beedb, which has not been updated for four years). Upper/db encapsulates multiple databases and provides a unified interface for CRUD.

    • Document

      • GitHub
      • Document or Tour
    • Supported databases are: PostgreSQL, MySQL, SQLite, MSSQL, QL, and MongoDB.
    • It is not supported to generate DCL operations such as database tables based on database classes, only DQL,DML
    • Same as most ORM frameworks, provides connection pooling
    • Support for RDBMS transactional
sess, err := postgresql.Open(settings)if err != nil {    log.Fatalf("db.Open(): %q\n", err)}defer sess.Close()var books []Bookerr = sess.Collection("books").Find().All(&books)

Summarize

    • Similarity of

      • The databases supported by each ORM are basically the same (supported by the mainstream database)
      • Support transactional, chain-like queries, etc.
    • Difference

      • Xorm, gorose support batch query processing
      • Xorm supports master-slave read-write separation
      • Gorm supports hot loading
      • Gorose facilitates switching across multiple databases
      • Document Comprehensiveness Gorm>xorm>gorose
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.