Git Product home page Git Product logo

orm's Introduction

中文 README

orm (Simple data to object mapping functions combing with sql.Rows)

Original Intention

  • database/sql package, sql.Rows returned by Db.Query, according to Rows.Scan, the instance code is as following:
rows, err := db.Query("SELECT ...")
defer rows.Close()
for rows.Next() {
    var id int
    var name string
    err = rows.Scan(&id, &name)
}
err = rows.Err()
...

In actual coding, we expect as following much more:

rows, err := db.Query("SELECT ...")
defer rows.Close()
var d []*stru
err = Rows2Strus(rows, &d)

This is a simple object mapping technology which makes our code much easier.

Functions

  • Providing usages under the following four conditions:

Rows2Strus, sql.Rows mapping to struct slice

Rows2Stru, sql.Rows mapping to struct,like db.QueryRow

Rows2Cnts, sql.Rows mapping to int slice

Rows2Cnt, sql.Rows mapping to int,like using select count(1) ...

  • Supporting tag:orm, as following:
type Demo struct {
    Id int
    DemoName string `orm:"demo_name"` // mapping to demo_name
}
  • Supporting anonymous member, as following:
type C struct {
    Id int
}
type P struct {
    C  // mapping to id field
    Name string
}

demo

orm_test.go


中文

orm (配合sql.Rows使用的超简单数据到对象映射功能函数)

实现初衷

  • database/sql包,Db.Query返回的sql.Rows,通过Rows.Scan方式示例代码如下:
rows, err := db.Query("SELECT ...")
defer rows.Close()
for rows.Next() {
    var id int
    var name string
    err = rows.Scan(&id, &name)
}
err = rows.Err()
...

但实际项目场景里,我们更想这样:

rows, err := db.Query("SELECT ...")
defer rows.Close()
var d []*stru
err = Rows2Strus(rows, &d)

这就是一种简单的对象映射,通过转为对象的方式,我们的代码更方便处理了

功能

  • 一共提供四种场景的使用方法:

Rows2Strus, sql.Rows转为struct slice

Rows2Stru, sql.Rows转为struct,等同db.QueryRow

Rows2Cnts, sql.Rows转为int slice

Rows2Cnt, sql.Rows转为int,用于select count(1)操作

  • 支持tag: orm,如下:
type Demo struct {
    Id int
    DemoName string `orm:"demo_name"` // 映射成demo_name字段
}
  • 支持匿名成员,如下:
type C struct {
    Id int
}
type P struct {
    C  // 映射成id字段
    Name string
}

demo

orm_test.go

orm's People

Contributors

simplejia avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.