From 0a3e2b0847efca9fc38cfa780781f962ee25ce9e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 17 Oct 2013 13:28:42 +0800 Subject: [PATCH] docs added iterate --- docs/QuickStart.md | 2 +- docs/QuickStartEn.md | 99 ++++++++++++++++++++++++++++++-------------- 2 files changed, 69 insertions(+), 32 deletions(-) diff --git a/docs/QuickStart.md b/docs/QuickStart.md index c2976c1b..283adbff 100644 --- a/docs/QuickStart.md +++ b/docs/QuickStart.md @@ -496,7 +496,7 @@ engine.ClearCache(new(User)) * [Sudochina](http://sudochina.com) 和对应的源代码[github.com/insionng/toropress](http://github.com/insionng/toropress) -* [VeryHour](http://veryhour.org) +* [VeryHour](http://veryhour.com) diff --git a/docs/QuickStartEn.md b/docs/QuickStartEn.md index bb80dd2c..5173bbda 100644 --- a/docs/QuickStartEn.md +++ b/docs/QuickStartEn.md @@ -1,6 +1,24 @@ -# Quick Start +Quick Start +===== -1.Create a database engine just like sql.Open, commonly you just need create once. Please notice, Create function will be deprecated, use NewEngine instead. +* [1.Create database engine](#10) +* [2.Define a struct](#20) +* [3.Create tables](#30) + * [3.1.Sync database schema](#31) +* [4.Insert, update one or more records](#40) +* [5.Get one record](#50) +* [6.Find many records](#60) +* [7.Iterate records](#70) +* [8.Delete records](#80) +* [9.Count records](#90) +* [10.Cache](#100) +* [11.Execute SQL](#110) +* [12.Advanced Usage](#120) +* [13.Mapping Rules](#130) + + +## 1.Create database engine +Create a database engine just like sql.Open, commonly you just need create once. Please notice, Create function will be deprecated, use NewEngine instead. ```Go import ( @@ -27,25 +45,21 @@ defer engine.Close() ```Go engine.ShowSQL = true ``` -1.2 If you want to use your own connection pool +1.2 Defaultly, xorm use go's connection pool. If you want to use your own connection pool, you can ```Go err = engine.SetPool(NewSimpleConnectPool()) ``` -1.3 If you want to auto sync database schema -```Go -err = engine.Sync(new(User), new(Category)) -``` - -1.4 If you want to enable cache system +1.3 If you want to enable cache system ```Go cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000) Engine.SetDefaultCacher(cacher) ``` - -2.Define a struct + + +## 2.Define a struct ```Go type User struct { @@ -56,16 +70,26 @@ type User struct { ``` 2.1.More mapping rules, please see [Mapping Rules](#mapping) - -3.When you set up your program, you can use CreateTables to create database tables. + + +## 3.Create tables +When you set up your program, you can use CreateTables to create database tables. ```Go err := engine.CreateTables(&User{}) // or err := engine.Map(&User{}, &Article{}) // err = engine.CreateAll() +``` + +3.1 If you want to auto sync database schema + +```Go +err = engine.Sync(new(User), new(Category)) ``` - -4.then, insert a struct to table, if success, User.Id will be set to id + + +## 4.Insert, Update records +then, insert a struct to table, if success, User.Id will be set to id ```Go id, err := engine.Insert(&User{Name:"lunny"}) @@ -79,8 +103,10 @@ rows, err := engine.Update(&user, &User{Id:1}) // or rows, err := engine.Where("id = ?", 1).Update(&user) // or rows, err := engine.Id(1).Update(&user) ``` - -5.Fetch a single object by user + + +## 5.Get one record +Fetch a single object by user ```Go var user = User{Id:27} @@ -90,8 +116,10 @@ has, err := engine.Get(&user) var user = User{Name:"xlw"} has, err := engine.Get(&user) ``` - -6.Fetch multipe objects into a slice or a map, use Find: + + +## 6.Find many records +Fetch multipe objects into a slice or a map, use Find: ```Go var everyone []Userinfo @@ -129,7 +157,9 @@ var tenusers []Userinfo err := engine.Cols("id", "name").Find(&tenusers) //Find only id and name ``` -7.Iterate, like find, but handle records one by one + +## 7.Iterate records +Iterate, like find, but handle records one by one ```Go err := engine.Where("age > ? or name=?)", 30, "xlw").Iterate(new(Userinfo), func(i int, bean interface{})error{ @@ -137,8 +167,10 @@ err := engine.Where("age > ? or name=?)", 30, "xlw").Iterate(new(Userinfo), func //do somthing use i and user }) ``` - -8.Delete one or more records + + +## 8.Delete one or more records +Delete one or more records 8.1 deleted by id @@ -151,20 +183,24 @@ err := engine.Id(1).Delete(&User{}) ```Go err := engine.Delete(&User{Name:"xlw"}) ``` - + + +## 9.Count records 9.Count ```Go total, err := engine.Where("id > ?", 5).Count(&User{Name:"xlw"}) ``` -10.Cache + +## 10.Cache ```Go cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000) engine.SetDefaultCacher(cacher) ``` - -## Execute SQL + + +## 11.Execute SQL Of course, SQL execution is also provided. @@ -181,9 +217,9 @@ results, err := engine.Query(sql) sql = "update userinfo set username=? where id=?" res, err := engine.Exec(sql, "xiaolun", 1) ``` - - -## Advanced Usage + + +## 12.Advanced Usage For deep usage, you should create a session. @@ -281,8 +317,9 @@ if err != nil { ``` 5.Derive mapping Please see derive.go in examples folder. - -## Mapping Rules + + +## 13.Mapping Rules 1.Struct and struct's fields name should be Pascal style, and the table and column's name default is SQL style.