From 16afcec0e9feb1ed9e530538074f7bd3f9b03e91 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 21 Jul 2021 15:43:37 +0800 Subject: [PATCH] Update docs --- README.md | 2 ++ README_CN.md | 2 ++ doc.go | 68 +++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 40826f13..d7217fa2 100644 --- a/README.md +++ b/README.md @@ -44,11 +44,13 @@ Drivers for Go's sql package which currently support database/sql includes: * [SQLite](https://sqlite.org) - [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3) + - [modernc.org/sqlite](https://gitlab.com/cznic/sqlite) (windows unsupported) * MsSql - [github.com/denisenkom/go-mssqldb](https://github.com/denisenkom/go-mssqldb) * Oracle + - [github.com/godror/godror)](https://github.com/godror/godror) (experiment) - [github.com/mattn/go-oci8](https://github.com/mattn/go-oci8) (experiment) ## Installation diff --git a/README_CN.md b/README_CN.md index 06706417..36930faa 100644 --- a/README_CN.md +++ b/README_CN.md @@ -43,11 +43,13 @@ v1.0.0 相对于 v0.8.2 有以下不兼容的变更: * [SQLite](https://sqlite.org) - [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3) + - [modernc.org/sqlite](https://gitlab.com/cznic/sqlite) (Windows试验性支持) * MsSql - [github.com/denisenkom/go-mssqldb](https://github.com/denisenkom/go-mssqldb) * Oracle + - [github.com/godror/godror)](https://github.com/godror/godror) (试验性支持) - [github.com/mattn/go-oci8](https://github.com/mattn/go-oci8) (试验性支持) ## 安装 diff --git a/doc.go b/doc.go index ea6a2226..d0653232 100644 --- a/doc.go +++ b/doc.go @@ -14,23 +14,30 @@ Make sure you have installed Go 1.11+ and then: Create Engine -Firstly, we should new an engine for a database +Firstly, we should create 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. +Method NewEngine's parameters are the same as sql.Open which depend drivers' implementation. +Generally, one engine for an application is enough. You can define it as a package variable. Raw Methods -XORM also support raw SQL execution: +XORM supports raw SQL execution: -1. query a SQL string, the returned results is []map[string][]byte +1. query with 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 +2. query with a SQL string, the returned results is []map[string]string + + results, err := engine.QueryString("select * from user") + +3. query with a SQL string, the returned results is []map[string]interface{} + + results, err := engine.QueryInterface("select * from user") + +4. execute with a SQL string, the returned results affected, err := engine.Exec("update user set .... where ...") @@ -77,7 +84,9 @@ There are 8 major ORM methods and many helpful methods to use to operate databas 4. Query multiple records and record by record handle, there two methods, one is Iterate, another is Rows - err := engine.Iterate(...) + err := engine.Iterate(new(User), func(i int, bean interface{}) error { + // do something + }) // SELECT * FROM user rows, err := engine.Rows(...) @@ -120,7 +129,7 @@ another is Rows Conditions The above 8 methods could use with condition methods chainable. -Attention: the above 8 methods should be the last chainable method. +Notice: the above 8 methods should be the last chainable method. 1. ID, In @@ -179,6 +188,47 @@ Attention: the above 8 methods should be the last chainable method. engine.Join("LEFT", "userdetail", "user.id=userdetail.id").Find(&users) //SELECT * FROM user LEFT JOIN userdetail ON user.id=userdetail.id +Builder + +xorm could work with xorm.io/builder directly. + +1. With Where + + var cond = builder.Eq{"a":1, "b":2} + engine.Where(cond).Find(&users) + +2. With In + + var subQuery = builder.Select("name").From("group") + engine.In("group_name", subQuery).Find(&users) + +3. With Join + + var subQuery = builder.Select("name").From("group") + engine.Join("INNER", subQuery, "group.id = user.group_id").Find(&users) + +4. With SetExprs + + var subQuery = builder.Select("name").From("group") + engine.ID(1).SetExprs("name", subQuery).Update(new(User)) + +5. With SQL + + var query = builder.Select("name").From("group") + results, err := engine.SQL(query).Find(&groups) + +6. With Query + + var query = builder.Select("name").From("group") + results, err := engine.Query(query) + results, err := engine.QueryString(query) + results, err := engine.QueryInterface(query) + +7. With Exec + + var query = builder.Insert("a, b").Into("table1").Select("b, c").From("table2") + results, err := engine.Exec(query) + More usage, please visit http://xorm.io/docs */ package xorm -- 2.40.1