SoFunction
Updated on 2025-03-03

Detailed explanation of the example of implementing go ORM database operation as convenient as php

introduction

Many people have transferred from PHP. I wonder if you have discovered that the orm in the Go world is not as useful as the orm in PHP. In this article, we will seriously discuss this issue and will propose solutions later.

PHP convenience

For example, if you want to implement an association query, in PHP, you just need to constantly use the arrow function.

$users = DB::table('users')->whereIn('id', [1, 2, 3])->orderBy('name', 'desc')->get();

The above code simply implements query operations on a table, and arranges the query results in reverse order in name, which is very simple

But doing the same job is more troublesome in Go

Go trouble

If you use the native query of go, you need to write the following code

rows, err := ("select * from users where id in (?,?,?) order by name desc", 1, 2, 3)

Basically, you need to hand-written a complete sql statement, all manually

What! It’s not that we can’t write SQL statements by hand, but the key is that there is no need to write them by hand, right?

Full handwriting may bring two problems

  • Some students may not be familiar with SQL grammar and are prone to syntax errors.
  • Some students may not write SQL seriously and are prone to spelling errors, especially when there are many conditions and many placeholders.

If you use orm tools like gorm, you might write this

("id in (?)", []int{1,2,3}).Order("create_time desc").Find(&users)

Obviously, compared to native ones, it is better. You don’t need to write keywords such as select, but the core problem is still not solved, and you still need to write id in (?) and so on. It was equivalent to being fully handmade before, but now it is semi-handed and semi-automatic.

In my example, there are only one condition. In actual business, there will be many query conditions and the number is not certain. This semi-automatic method is still not very good.

Solution

Since there is a problem, there is a solution. Obviously, the ideal solution is to keep it consistent with php. So can go do this?

The answer is undoubtedly, yes.

Here we recommend a new database operation library, which can easily complete such work

tangpanqing/aorm: Operate Database So Easy For GoLang Developer ()

It has some significant characteristics

  • Concise code, high performance
  • Support MySQL, MsSQL, Postgres, Sqlite3 databases
  • Supports null value query
  • Support automatic migration
  • Support SQL stitching

Let's look at the specific use, just the operation

(db).Table("users").WhereIn("id", []int{1,2,3}).OrderBy("name","desc").GetMany(&users)

Comparison of how to write php

$users = DB::table('users')->whereIn('id', [1, 2, 3])->orderBy('name', 'desc')->get();

It can't be said to be exactly the same, it's just exactly the same, right?

Let’s take a look at what to do if the query conditions are not sure?

This is a common problem encountered in list query. The number of data transmitted from the front end is not certain. We need to increase or decrease different conditions according to different data, and then generate different SQL and query different results.

    var listByWhere []Person
    var where1 []
    where1 = append(where1, {Field: "type", Opt: , Val: 0})
    where1 = append(where1, {Field: "age", Opt: , Val: []int{18, 20}})
    where1 = append(where1, {Field: "money", Opt: , Val: []float64{100.1, 200.9}})
    where1 = append(where1, {Field: "money", Opt: , Val: 100.15})
    where1 = append(where1, {Field: "name", Opt: , Val: []string{"%", "li", "%"}})
    (db).Debug(true).Table("person").WhereArr(where1).GetMany(&listByWhere)
    for i := 0; i < len(listByWhere); i++ {
        (listByWhere[i])
    }

As mentioned above, you can define a slice (array), then increase and decrease the items in this array based on the information passed by the front end, and finally pass this query array to aorm for the final query, and finally get the result.

As can be seen from the above example, like PHP, you just need to enter the key field name, and this is enough for the key data. The aorm library will automatically complete the other SQL keywords and the splicing of multiple conditions. Perfectly solve grammar error problems and spelling error problems.

How about it, is it fragrant?

Written at the end

The aorm library is very useful and brings a general development experience of php to go engineers. I recommend you to use it quickly.

tangpanqing/aorm: Operate Database So Easy For GoLang Developer ()

The above is a detailed explanation of the detailed explanation of the example of implementing the Go ORM database operation as convenient as PHP. For more information about go ORM database operation, please pay attention to my other related articles!