mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
5.1 KiB
185 lines
5.1 KiB
8 years ago
|
// Copyright 2013 - 2016 The XORM Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a BSD
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
/*
|
||
|
|
||
|
Package xorm is a simple and powerful ORM for Go.
|
||
|
|
||
|
Installation
|
||
|
|
||
7 years ago
|
Make sure you have installed Go 1.6+ and then:
|
||
8 years ago
|
|
||
5 years ago
|
go get xorm.io/xorm
|
||
8 years ago
|
|
||
|
Create Engine
|
||
|
|
||
|
Firstly, we should new an engine for a database
|
||
|
|
||
|
engine, err := xorm.NewEngine(driverName, dataSourceName)
|
||
|
|
||
|
Method NewEngine's parameters is the same as sql.Open. It depends
|
||
|
drivers' implementation.
|
||
|
Generally, one engine for an application is enough. You can set it as package variable.
|
||
|
|
||
|
Raw Methods
|
||
|
|
||
8 years ago
|
XORM also support raw SQL execution:
|
||
8 years ago
|
|
||
|
1. query a SQL string, the returned results is []map[string][]byte
|
||
|
|
||
|
results, err := engine.Query("select * from user")
|
||
|
|
||
|
2. execute a SQL string, the returned results
|
||
|
|
||
|
affected, err := engine.Exec("update user set .... where ...")
|
||
|
|
||
|
ORM Methods
|
||
|
|
||
8 years ago
|
There are 8 major ORM methods and many helpful methods to use to operate database.
|
||
8 years ago
|
|
||
8 years ago
|
1. Insert one or multiple records to database
|
||
8 years ago
|
|
||
|
affected, err := engine.Insert(&struct)
|
||
|
// INSERT INTO struct () values ()
|
||
|
affected, err := engine.Insert(&struct1, &struct2)
|
||
|
// INSERT INTO struct1 () values ()
|
||
|
// INSERT INTO struct2 () values ()
|
||
|
affected, err := engine.Insert(&sliceOfStruct)
|
||
|
// INSERT INTO struct () values (),(),()
|
||
|
affected, err := engine.Insert(&struct1, &sliceOfStruct2)
|
||
|
// INSERT INTO struct1 () values ()
|
||
|
// INSERT INTO struct2 () values (),(),()
|
||
|
|
||
7 years ago
|
2. Query one record or one variable from database
|
||
8 years ago
|
|
||
|
has, err := engine.Get(&user)
|
||
|
// SELECT * FROM user LIMIT 1
|
||
|
|
||
7 years ago
|
var id int64
|
||
|
has, err := engine.Table("user").Where("name = ?", name).Get(&id)
|
||
|
// SELECT id FROM user WHERE name = ? LIMIT 1
|
||
|
|
||
8 years ago
|
3. Query multiple records from database
|
||
|
|
||
8 years ago
|
var sliceOfStructs []Struct
|
||
|
err := engine.Find(&sliceOfStructs)
|
||
8 years ago
|
// SELECT * FROM user
|
||
|
|
||
8 years ago
|
var mapOfStructs = make(map[int64]Struct)
|
||
|
err := engine.Find(&mapOfStructs)
|
||
|
// SELECT * FROM user
|
||
|
|
||
|
var int64s []int64
|
||
|
err := engine.Table("user").Cols("id").Find(&int64s)
|
||
|
// SELECT id FROM user
|
||
|
|
||
8 years ago
|
4. Query multiple records and record by record handle, there two methods, one is Iterate,
|
||
|
another is Rows
|
||
|
|
||
|
err := engine.Iterate(...)
|
||
|
// SELECT * FROM user
|
||
|
|
||
|
rows, err := engine.Rows(...)
|
||
|
// SELECT * FROM user
|
||
|
defer rows.Close()
|
||
|
bean := new(Struct)
|
||
|
for rows.Next() {
|
||
|
err = rows.Scan(bean)
|
||
|
}
|
||
|
|
||
|
5. Update one or more records
|
||
|
|
||
7 years ago
|
affected, err := engine.ID(...).Update(&user)
|
||
8 years ago
|
// UPDATE user SET ...
|
||
|
|
||
8 years ago
|
6. Delete one or more records, Delete MUST has condition
|
||
8 years ago
|
|
||
|
affected, err := engine.Where(...).Delete(&user)
|
||
|
// DELETE FROM user Where ...
|
||
|
|
||
|
7. Count records
|
||
|
|
||
|
counts, err := engine.Count(&user)
|
||
|
// SELECT count(*) AS total FROM user
|
||
|
|
||
7 years ago
|
counts, err := engine.SQL("select count(*) FROM user").Count()
|
||
|
// select count(*) FROM user
|
||
|
|
||
8 years ago
|
8. Sum records
|
||
|
|
||
|
sumFloat64, err := engine.Sum(&user, "id")
|
||
|
// SELECT sum(id) from user
|
||
|
|
||
|
sumFloat64s, err := engine.Sums(&user, "id1", "id2")
|
||
|
// SELECT sum(id1), sum(id2) from user
|
||
|
|
||
|
sumInt64s, err := engine.SumsInt(&user, "id1", "id2")
|
||
|
// SELECT sum(id1), sum(id2) from user
|
||
|
|
||
8 years ago
|
Conditions
|
||
|
|
||
8 years ago
|
The above 8 methods could use with condition methods chainable.
|
||
|
Attention: the above 8 methods should be the last chainable method.
|
||
8 years ago
|
|
||
8 years ago
|
1. ID, In
|
||
8 years ago
|
|
||
8 years ago
|
engine.ID(1).Get(&user) // for single primary key
|
||
8 years ago
|
// SELECT * FROM user WHERE id = 1
|
||
8 years ago
|
engine.ID(core.PK{1, 2}).Get(&user) // for composite primary keys
|
||
8 years ago
|
// SELECT * FROM user WHERE id1 = 1 AND id2 = 2
|
||
|
engine.In("id", 1, 2, 3).Find(&users)
|
||
|
// SELECT * FROM user WHERE id IN (1, 2, 3)
|
||
8 years ago
|
engine.In("id", []int{1, 2, 3}).Find(&users)
|
||
8 years ago
|
// SELECT * FROM user WHERE id IN (1, 2, 3)
|
||
|
|
||
|
2. Where, And, Or
|
||
|
|
||
|
engine.Where().And().Or().Find()
|
||
|
// SELECT * FROM user WHERE (.. AND ..) OR ...
|
||
|
|
||
|
3. OrderBy, Asc, Desc
|
||
|
|
||
|
engine.Asc().Desc().Find()
|
||
|
// SELECT * FROM user ORDER BY .. ASC, .. DESC
|
||
|
engine.OrderBy().Find()
|
||
|
// SELECT * FROM user ORDER BY ..
|
||
|
|
||
|
4. Limit, Top
|
||
|
|
||
|
engine.Limit().Find()
|
||
|
// SELECT * FROM user LIMIT .. OFFSET ..
|
||
|
engine.Top(5).Find()
|
||
|
// SELECT TOP 5 * FROM user // for mssql
|
||
|
// SELECT * FROM user LIMIT .. OFFSET 0 //for other databases
|
||
|
|
||
8 years ago
|
5. SQL, let you custom SQL
|
||
8 years ago
|
|
||
|
var users []User
|
||
8 years ago
|
engine.SQL("select * from user").Find(&users)
|
||
8 years ago
|
|
||
|
6. Cols, Omit, Distinct
|
||
|
|
||
|
var users []*User
|
||
|
engine.Cols("col1, col2").Find(&users)
|
||
|
// SELECT col1, col2 FROM user
|
||
|
engine.Cols("col1", "col2").Where().Update(user)
|
||
|
// UPDATE user set col1 = ?, col2 = ? Where ...
|
||
|
engine.Omit("col1").Find(&users)
|
||
|
// SELECT col2, col3 FROM user
|
||
|
engine.Omit("col1").Insert(&user)
|
||
|
// INSERT INTO table (non-col1) VALUES ()
|
||
|
engine.Distinct("col1").Find(&users)
|
||
|
// SELECT DISTINCT col1 FROM user
|
||
|
|
||
|
7. Join, GroupBy, Having
|
||
|
|
||
|
engine.GroupBy("name").Having("name='xlw'").Find(&users)
|
||
|
//SELECT * FROM user GROUP BY name HAVING name='xlw'
|
||
|
engine.Join("LEFT", "userdetail", "user.id=userdetail.id").Find(&users)
|
||
|
//SELECT * FROM user LEFT JOIN userdetail ON user.id=userdetail.id
|
||
|
|
||
|
More usage, please visit http://xorm.io/docs
|
||
|
*/
|
||
|
package xorm
|