borm

package module
v0.0.0-...-ce6676e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2023 License: MIT Imports: 1 Imported by: 0

README

borm

a small ORM for golang, simultaneously enhancing traditional access methods

We are trying to create a very simple orm framework using the go language that can support traditional database operations and orm patterns. The goal of borm is easy to use and modify in your project.

getting borm

  1. go get github.com/buffalo-x/borm
  2. download zip file and unzip to directoty github.com/buffalo-x/borm

starting to use borm

  1. borm.Init() should be called in your project

  2. connect to a database, taking MySQL as an example.
    dsn := "root:XXX@tcp(127.0.0.1:3307)/db"
    sqlDb, err := sql.Open("mysql", dsn)

  3. Add default database connection to datasource
    bds.SetDs("default", sqlDb, "root", "","127.0.0.1",3306, "db", "","mysql", "version info", "more info")
    Here, "mysql" is dsType
    More connections can be added to the datasource, and they can be accessed by function
    bds.GetDs(name string)(*Datasource)
    DS with the name "default" should generally exist, otherwise it will increase code complexity. Because, if there is no explicit selection of ds, many functions default to using the ds with name of "default".

using bmod method

  1. Define the go structure of a database table

    type TestTable struct {
    	Id          int64  `borm:"primaryKey;autoIncrement"`
    	Code        string `borm:"column:code;"`
    	Name        string
    	Pwd         string    `borm:"excluded"`
    	ColInt      int       `borm:"excluded"`
    	ColDecimal  float64   `borm:"type:decimal(20,2)"`
    	ColDatetime string    `borm:"column:col_datetime;autoCreateTime"`
    	ColDate     time.Time `borm:"excluded"`
    	ColTs       time.Time `borm:"autoUpdateTime;"`
    }
    

    The accepted datatype is among [string int int8 int16 int32 int64 float32 float64 bool time.Time]

    borm tags :

    • primaryKey : this is a primary key column
    • autoIncrement : only primary key has this tag and field datatype must be int64
    • readonly : display data only
    • excluded : not handled
    • column : the real table column name in database
    • autoCreateTime : the time when record created, server's time
    • autoUpdateTime : the time when record updated, server's time
    • type : datatype constraints
  2. Query table rows
    var rs []TestTable
    modDb := bmod.Where("id>?",1).Query(&rs)

    If everything is normal, you can see the data in rs. You can also use
    err := bmod.Query(&rs).Error() to find if error occured or not.

  3. Query one table row
    _var row TestTable
    modDb := bmod.Where("id=1").Query(&row)

  4. About func Where
    func (modDb *ModDB) Where(sqlStr string, args ...interface{}) (retModDb *ModDB),this func will create a *ModDB and set its where condition
    for example
    modDb := bmod.Where("code=? and name=?","z01","jim").Query(&row)

  5. Specify a datasource
    func Db(dsName ...string) (retModDb *ModDB)
    We can use it in this way.
    modDb := bmod.Db("ds1").Query(&rs)
    This will choose a datasource with name of "ds1",then query.

  6. Create a database record
    tb := &TestTable{Code: "aa01"}
    modDb := bmod.Create(tb)

    You can also choose a datasource
    modDb := bmod.Db("ds1").Create(tb)

  7. Use Transaction
    func Tx(tx *sql.Tx, dsName ...string) (retModDb *ModDB)
    We can use it in this way.
    Firstly, you should create a *sql.Tx object. Then,
    modDb := bmod.Tx(tx,"ds1").Create(tb)
    This will create record using tx

  8. Save a database record
    tb := &TestTable{Code: "aa01",name:"mike"}\ modDb := bmod.Save(tb)

  9. Delete a database record
    modDb := bmod.Delete(tb)

  10. Use Count func
    var ct int
    modDb := bmod.Model(tb).Where("1=1").Count(&ct)

  11. Update one column
    bmod.Model(tb).Update("name","cat")
    This will update the name column using table primary key
    bmod.Model(tb).Where("id=20").Update("name","cat")
    This will update the name column where column id=20

  12. Update more than one columns
    bmod.Model(tb).UpdateColumns(bsql.CV{"name": "cat", "code": "z002"})
    bsql.CV is a map[string]interface{}

  13. Use bsql.Expr
    bmod.Model(tb).Update("col_int",bsql.Expr("col_int+?", 1))
    This means\ update test_table set col_int=col_int+1

  14. Use sql.Rows as Output
    func (modDb *ModDB) Rows(columnList ...string) (*sql.Rows, error)
    Youcan use
    _bmod.Model(tb).Rows()
    or
    _bmod.Model(tb).Where("id>1").Rows("name,code")

  15. Use sql.Row as Output
    func (modDb *ModDB) Row(columnList ...string) (*sql.Row, error)
    Youcan use
    _bmod.Model(tb).Where("id>1").Row()
    or
    _bmod.Model(tb).Where("id>?",2).Row("name,code")

using bgen method

We also have a traditional way to deal with database.

  1. Query table rows
    var rs bsql.Rows
    genMod := bgen.Sql("select * from test_table where id>?", 1).Query(&rs)

    If everything is normal, you can see the data in rs. You can also use
    err := bgen.Sql("select * from test_table where id>?", 1).Query(&rs).Error()
    to find if error occured or not.

    bsql.Rows has the following definition,all data being stored as string.

    type Rows struct {
       Count    int
       Data     [][]string
       ColsMap  map[string]int
       ColsName []string
    }
    
  2. Query table row
    var rs bsql.Row
    genMod := bgen.Sql("select * from test_table where id>?", 1).First(&rs)

    bsql.Row has the following definition,all data being stored as string.

    type Row struct {
       Data     []string
       ColsMap  map[string]int
       ColsName []string
    }
    
  3. Specify a datasource
    genMod := bgen.Db("ds1").Sql("select * from test_table where id>?", 1).First(&rs)
    This will choose a datasource with name of "ds1"

  4. Use Transaction func Tx(tx *sql.Tx, dsName ...string) (retModDb *ModDB)
    We can use it in this way.
    Firstly, you should create a *sql.Tx object. Then,
    genMod := bmod.Tx(tx,"ds1").Sql("select * from test_table where id>?", 1).First(&rs)
    This will query using tx

  5. Query a value
    var ct int
    bgen.Sql("select count(*) from test_table").Value(&ct)

    If your query return more then one rows, the first row will be used.
    bgen.Sql("select id from test_table").Value(&ct)

  6. Use sql.Rows as Output
    rows,err:=bgen.Sql("select * from test_table").Rows()

  7. Use sql.Row as Output
    row,err:=bgen.Sql("select * from test_table").Row()

  8. Exec sql
    genMod:= bgen.Sql("insert into test_table(name,code) values(?,?)","mike","z11").Exec()
    This will execute one insert sql. The following code has the same result.
    args := []interface{}{"mike", "z11"}
    db1 := bgen.Sql("insert into test_table(name,code) values(?,?)",args).Exec()

    We can create args like the following:
    args := [][]interface{}{{"susan", "z01"}, {"jose", "z02"}}
    db1 := bgen.Sql("insert into test_table(name,code) values(?,?)",args).Exec()

    This will execute two insert sqls.

  9. Exec batch sqls
    func (genDb *GenDB) Batch(sqls []bsql.BatchSql) (retGenDb *GenDB)
    This will execute sqls in []bsql.BatchSql.
    bsql.BatchSql is a struct\

    type BatchSql struct {
    	Sql  string
    	Args []interface{}
    }
    
  10. Create record
    bgen.Create("test_table", bsql.CV{"code": "33", "name": nil, "col_int": 1})

  11. Update one column
    bgen.Sql("id=?", 23).Update("test_table", "name", "mike")
    this will update one column

  12. Update more columns
    bgen.Sql("id=?", 23).
    UpdateColumns("test_table",
    bsql.CV{"code": "33", "name": "cccs", "col_int":\ bsql.Expr("col_int+1")})

multiple database sqls

  1. prepare sql option data
    You can organize sqls like this. id-》dbType+dbVersion-》sql
    sqlMap := map[string]map[string]string{
        "id1": {
    	    "mysql": "select id,now() from test_table",
    	    "mssql": "select id,getdate() from test_table",
    	    "default": "select id,now() from test_table",
    	},
    	"id2":{
    		"mysql": "select id,left(name,5) from test_table",
    		"mssql": "select id,substring(name,0,5) from test_table",
    	},    
    }
    
  • "id1" and "id2" are ids
  • "mysql" and "mssql" are dbTypes
  • "default" is the option when no matchs
  1. add the option sql data to global map\
    _mdb.AddDsSqlOptionMap("test",sqlMap)
    Here, "test" is called a category.

  2. use the option data
    var rs bsql.Rows
    bgen.Sql("opt^^test,id1^^demo sql").Query(&rs)

    This will use the ds with name "default" to locate sql in group "id1" of category "test".
    If no match found, "default": "select id,now() from test_table" will be returned.

    You can use like this to specify a datasource:
    bgen.Db("ds1").Sql("opt^^test,id1^^demo sql").Query(&rs)

    This also works in .Where function.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init()

Types

This section is empty.

Directories

Path Synopsis
mdb

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL