orm update docs
Showing
8 changed files
with
153 additions
and
27 deletions
| ... | @@ -33,6 +33,7 @@ more features please read the docs | ... | @@ -33,6 +33,7 @@ more features please read the docs |
| 33 | 33 | ||
| 34 | ## Changelog | 34 | ## Changelog |
| 35 | 35 | ||
| 36 | * 2013-08-19: support table auto create | ||
| 36 | * 2013-08-13: update test for database types | 37 | * 2013-08-13: update test for database types |
| 37 | * 2013-08-13: go type support, such as int8, uint8, byte, rune | 38 | * 2013-08-13: go type support, such as int8, uint8, byte, rune |
| 38 | * 2013-08-13: date / datetime timezone support very well | 39 | * 2013-08-13: date / datetime timezone support very well | ... | ... |
orm/docs/zh/Cmd.md
0 → 100644
| 1 | ## 命令模式 | ||
| 2 | |||
| 3 | 注册模型与数据库以后,调用 RunCommand 执行 orm 命令 | ||
| 4 | |||
| 5 | ```go | ||
| 6 | func main() { | ||
| 7 | // orm.RegisterModel... | ||
| 8 | // orm.RegisterDataBase... | ||
| 9 | ... | ||
| 10 | orm.RunCommand() | ||
| 11 | } | ||
| 12 | ``` | ||
| 13 | |||
| 14 | ```bash | ||
| 15 | go build main.go | ||
| 16 | ./main orm | ||
| 17 | # 直接执行可以显示帮助 | ||
| 18 | # 如果你的程序可以支持的话,直接运行 go run main.go orm 也是一样的效果 | ||
| 19 | ``` | ||
| 20 | |||
| 21 | ## 自动建表 | ||
| 22 | |||
| 23 | ```bash | ||
| 24 | ./main orm syncdb -h | ||
| 25 | Usage of orm command: syncdb: | ||
| 26 | -db="default": DataBase alias name | ||
| 27 | -force=false: drop tables before create | ||
| 28 | -v=false: verbose info | ||
| 29 | ``` | ||
| 30 | |||
| 31 | 使用 `-force=1` 可以 drop table 后再建表 | ||
| 32 | |||
| 33 | 使用 `-v` 可以查看执行的 sql 语句 | ||
| 34 | |||
| 35 | ## 打印建表SQL | ||
| 36 | |||
| 37 | ```bash | ||
| 38 | ./main orm sqlall -h | ||
| 39 | Usage of orm command: syncdb: | ||
| 40 | -db="default": DataBase alias name | ||
| 41 | ``` | ||
| 42 | |||
| 43 | 默认使用别名为 default 的数据库 |
| 1 | ## 模型定义 | 1 | ## 模型定义 |
| 2 | 2 | ||
| 3 | 复杂的模型定义不是必须的,此功能用作数据库数据转换和自动建表 | 3 | 复杂的模型定义不是必须的,此功能用作数据库数据转换和[自动建表](Cmd.md#自动建表) |
| 4 | |||
| 5 | 默认的表名使用驼峰转蛇形,比如 AuthUser -> auth_user | ||
| 6 | |||
| 7 | **自定义表名** | ||
| 8 | |||
| 9 | ```go | ||
| 10 | type User struct { | ||
| 11 | Id int | ||
| 12 | Name string | ||
| 13 | } | ||
| 14 | |||
| 15 | func (u *User) TableName() string { | ||
| 16 | return "auth_user" | ||
| 17 | } | ||
| 18 | ``` | ||
| 19 | |||
| 20 | 如果[前缀设置](Orm.md#registermodelwithprefix)为`prefix_`那么表名为:prefix_auth_user | ||
| 4 | 21 | ||
| 5 | ## Struct Tag 设置参数 | 22 | ## Struct Tag 设置参数 |
| 6 | ```go | 23 | ```go |
| 7 | orm:"null;rel(fk)" | 24 | orm:"null;rel(fk)" |
| 8 | ``` | 25 | ``` |
| 9 | 26 | ||
| 10 | 通常每个 Field 的 StructTag 里包含两种类型的设置,类似 null 的 bool 型设置,还有 类似 rel(fk) 的指定值设置,bool 型默认为 false,指定以后即表示为 true | ||
| 11 | |||
| 12 | 多个设置间使用 `;` 分隔,设置的值如果是多个,使用 `,` 分隔。 | 27 | 多个设置间使用 `;` 分隔,设置的值如果是多个,使用 `,` 分隔。 |
| 13 | 28 | ||
| 14 | #### 忽略字段 | 29 | #### 忽略字段 |
| ... | @@ -24,11 +39,13 @@ type User struct { | ... | @@ -24,11 +39,13 @@ type User struct { |
| 24 | 39 | ||
| 25 | #### auto | 40 | #### auto |
| 26 | 41 | ||
| 27 | 设置为 Autoincrement Primary Key | 42 | 当 Field 类型为 int, int32, int64 时,可以设置字段为自增健 |
| 43 | |||
| 44 | 当模型定义里没有主键时,符合上述类型且名称为 `Id` 的 Field 将被视为自增健。 | ||
| 28 | 45 | ||
| 29 | #### pk | 46 | #### pk |
| 30 | 47 | ||
| 31 | 设置为 Primary Key | 48 | 设置为主键,适用于自定义其他类型为主键 |
| 32 | 49 | ||
| 33 | #### null | 50 | #### null |
| 34 | 51 | ||
| ... | @@ -60,9 +77,12 @@ type User struct { | ... | @@ -60,9 +77,12 @@ type User struct { |
| 60 | ... | 77 | ... |
| 61 | Status int `orm:"default(1)"` | 78 | Status int `orm:"default(1)"` |
| 62 | ``` | 79 | ``` |
| 63 | #### size (string) | 80 | #### size |
| 81 | |||
| 82 | string 类型字段默认为 varchar(255) | ||
| 83 | |||
| 84 | 设置 size 以后,db type 将使用 varchar(size) | ||
| 64 | 85 | ||
| 65 | string 类型字段设置 size 以后,db type 将使用 varchar | ||
| 66 | ```go | 86 | ```go |
| 67 | Title string `orm:"size(60)"` | 87 | Title string `orm:"size(60)"` |
| 68 | ``` | 88 | ``` |
| ... | @@ -86,10 +106,18 @@ Updated time.Time `auto_now` | ... | @@ -86,10 +106,18 @@ Updated time.Time `auto_now` |
| 86 | 106 | ||
| 87 | #### type | 107 | #### type |
| 88 | 108 | ||
| 89 | 设置为 date, time.Time 字段的对应 db 类型使用 date | 109 | 设置为 date 时,time.Time 字段的对应 db 类型使用 date |
| 110 | |||
| 90 | ```go | 111 | ```go |
| 91 | Created time.Time `orm:"auto_now_add;type(date)"` | 112 | Created time.Time `orm:"auto_now_add;type(date)"` |
| 92 | ``` | 113 | ``` |
| 114 | |||
| 115 | 设置为 text 时,string 字段对应的 db 类型使用 text | ||
| 116 | |||
| 117 | ```go | ||
| 118 | Content string `orm:"type(text)"` | ||
| 119 | ``` | ||
| 120 | |||
| 93 | ## 表关系设置 | 121 | ## 表关系设置 |
| 94 | 122 | ||
| 95 | #### rel / reverse | 123 | #### rel / reverse |
| ... | @@ -174,9 +202,10 @@ type Profile struct { | ... | @@ -174,9 +202,10 @@ type Profile struct { |
| 174 | 202 | ||
| 175 | | go |mysql | 203 | | go |mysql |
| 176 | | :--- | :--- | 204 | | :--- | :--- |
| 205 | | int, int32, int64 - 设置 auto 或者名称为 `Id` 时 | integer AUTO_INCREMENT | ||
| 177 | | bool | bool | 206 | | bool | bool |
| 178 | | string - 设置 size 时 | varchar(size) | 207 | | string - 默认为 size 255 | varchar(size) |
| 179 | | string | longtext | 208 | | string - 设置 type(text) 时 | longtext |
| 180 | | time.Time - 设置 type 为 date 时 | date | 209 | | time.Time - 设置 type 为 date 时 | date |
| 181 | | time.TIme | datetime | 210 | | time.TIme | datetime |
| 182 | | byte | tinyint unsigned | 211 | | byte | tinyint unsigned |
| ... | @@ -199,9 +228,10 @@ type Profile struct { | ... | @@ -199,9 +228,10 @@ type Profile struct { |
| 199 | 228 | ||
| 200 | | go | sqlite3 | 229 | | go | sqlite3 |
| 201 | | :--- | :--- | 230 | | :--- | :--- |
| 231 | | int, int32, int64 - 设置 auto 或者名称为 `Id` 时 | integer AUTOINCREMENT | ||
| 202 | | bool | bool | 232 | | bool | bool |
| 203 | | string - 设置 size 时 | varchar(size) | 233 | | string - 默认为 size 255 | varchar(size) |
| 204 | | string | text | 234 | | string - 设置 type(text) 时 | text |
| 205 | | time.Time - 设置 type 为 date 时 | date | 235 | | time.Time - 设置 type 为 date 时 | date |
| 206 | | time.TIme | datetime | 236 | | time.TIme | datetime |
| 207 | | byte | tinyint unsigned | 237 | | byte | tinyint unsigned |
| ... | @@ -224,9 +254,10 @@ type Profile struct { | ... | @@ -224,9 +254,10 @@ type Profile struct { |
| 224 | 254 | ||
| 225 | | go | postgres | 255 | | go | postgres |
| 226 | | :--- | :--- | 256 | | :--- | :--- |
| 257 | | int, int32, int64 - 设置 auto 或者名称为 `Id` 时 | serial | ||
| 227 | | bool | bool | 258 | | bool | bool |
| 228 | | string - 设置 size 时 | varchar(size) | 259 | | string - 默认为 size 255 | varchar(size) |
| 229 | | string | text | 260 | | string - 设置 type(text) 时 | text |
| 230 | | time.Time - 设置 type 为 date 时 | date | 261 | | time.Time - 设置 type 为 date 时 | date |
| 231 | | time.TIme | timestamp with time zone | 262 | | time.TIme | timestamp with time zone |
| 232 | | byte | smallint CHECK("column" >= 0 AND "column" <= 255) | 263 | | byte | smallint CHECK("column" >= 0 AND "column" <= 255) | ... | ... |
| ... | @@ -3,7 +3,7 @@ | ... | @@ -3,7 +3,7 @@ |
| 3 | 对 object 操作简单的三个方法 Read / Insert / Update / Delete | 3 | 对 object 操作简单的三个方法 Read / Insert / Update / Delete |
| 4 | ```go | 4 | ```go |
| 5 | o := orm.NewOrm() | 5 | o := orm.NewOrm() |
| 6 | user := NewUser() | 6 | user := new(User) |
| 7 | user.Name = "slene" | 7 | user.Name = "slene" |
| 8 | 8 | ||
| 9 | fmt.Println(o.Insert(user)) | 9 | fmt.Println(o.Insert(user)) | ... | ... |
| ... | @@ -14,13 +14,13 @@ import ( | ... | @@ -14,13 +14,13 @@ import ( |
| 14 | ) | 14 | ) |
| 15 | 15 | ||
| 16 | type User struct { | 16 | type User struct { |
| 17 | Id int `orm:"auto"` // 设置为auto主键 | 17 | Id int |
| 18 | Name string | 18 | Name string |
| 19 | Profile *Profile `orm:"rel(one)"` // OneToOne relation | 19 | Profile *Profile `orm:"rel(one)"` // OneToOne relation |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | type Profile struct { | 22 | type Profile struct { |
| 23 | Id int `orm:"auto"` | 23 | Id int |
| 24 | Age int16 | 24 | Age int16 |
| 25 | User *User `orm:"reverse(one)"` // 设置反向关系(可选) | 25 | User *User `orm:"reverse(one)"` // 设置反向关系(可选) |
| 26 | } | 26 | } |
| ... | @@ -52,10 +52,10 @@ func main() { | ... | @@ -52,10 +52,10 @@ func main() { |
| 52 | o := orm.NewOrm() | 52 | o := orm.NewOrm() |
| 53 | o.Using("default") // 默认使用 default,你可以指定为其他数据库 | 53 | o.Using("default") // 默认使用 default,你可以指定为其他数据库 |
| 54 | 54 | ||
| 55 | profile := NewProfile() | 55 | profile := new(Profile) |
| 56 | profile.Age = 30 | 56 | profile.Age = 30 |
| 57 | 57 | ||
| 58 | user := NewUser() | 58 | user := new(User) |
| 59 | user.Profile = profile | 59 | user.Profile = profile |
| 60 | user.Name = "slene" | 60 | user.Name = "slene" |
| 61 | 61 | ||
| ... | @@ -98,10 +98,10 @@ orm.RegisterDriver("mymysql", orm.DR_MySQL) | ... | @@ -98,10 +98,10 @@ orm.RegisterDriver("mymysql", orm.DR_MySQL) |
| 98 | 98 | ||
| 99 | #### RegisterDataBase | 99 | #### RegisterDataBase |
| 100 | 100 | ||
| 101 | orm 必须注册一个名称为 `default` 的数据库,用以作为默认使用。 | 101 | orm 必须注册一个别名为 `default` 的数据库,作为默认使用。 |
| 102 | 102 | ||
| 103 | ```go | 103 | ```go |
| 104 | // 参数1 自定义数据库名称,用来在orm中切换数据库使用 | 104 | // 参数1 数据库的别名,用来在orm中切换数据库使用 |
| 105 | // 参数2 driverName | 105 | // 参数2 driverName |
| 106 | // 参数3 对应的链接字符串 | 106 | // 参数3 对应的链接字符串 |
| 107 | // 参数4 设置最大的空闲连接数,使用 golang 自己的连接池 | 107 | // 参数4 设置最大的空闲连接数,使用 golang 自己的连接池 |
| ... | @@ -126,12 +126,14 @@ orm 在进行 RegisterDataBase 的同时,会获取数据库使用的时区, | ... | @@ -126,12 +126,14 @@ orm 在进行 RegisterDataBase 的同时,会获取数据库使用的时区, |
| 126 | 126 | ||
| 127 | **注意:** 鉴于 Sqlite3 的设计,存取默认都为 UTC 时间 | 127 | **注意:** 鉴于 Sqlite3 的设计,存取默认都为 UTC 时间 |
| 128 | 128 | ||
| 129 | ## RegisterModel | 129 | ## 注册模型 |
| 130 | 130 | ||
| 131 | 如果使用 orm.QuerySeter 进行高级查询的话,这个是必须的。 | 131 | 如果使用 orm.QuerySeter 进行高级查询的话,这个是必须的。 |
| 132 | 132 | ||
| 133 | 反之,如果只使用 Raw 查询和 map struct,是无需这一步的。您可以去查看 [Raw SQL 查询](Raw.md) | 133 | 反之,如果只使用 Raw 查询和 map struct,是无需这一步的。您可以去查看 [Raw SQL 查询](Raw.md) |
| 134 | 134 | ||
| 135 | #### RegisterModel | ||
| 136 | |||
| 135 | 将你定义的 Model 进行注册,最佳设计是有单独的 models.go 文件,在他的 init 函数中进行注册。 | 137 | 将你定义的 Model 进行注册,最佳设计是有单独的 models.go 文件,在他的 init 函数中进行注册。 |
| 136 | 138 | ||
| 137 | 139 | ||
| ... | @@ -142,7 +144,7 @@ package main | ... | @@ -142,7 +144,7 @@ package main |
| 142 | import "github.com/astaxie/beego/orm" | 144 | import "github.com/astaxie/beego/orm" |
| 143 | 145 | ||
| 144 | type User struct { | 146 | type User struct { |
| 145 | Id int `orm:"auto"` | 147 | Id int |
| 146 | name string | 148 | name string |
| 147 | } | 149 | } |
| 148 | 150 | ||
| ... | @@ -159,6 +161,16 @@ orm.RegisterModel(new(User), new(Profile), new(Post)) | ... | @@ -159,6 +161,16 @@ orm.RegisterModel(new(User), new(Profile), new(Post)) |
| 159 | 161 | ||
| 160 | 详细的 struct 定义请查看文档 [模型定义](Models.md) | 162 | 详细的 struct 定义请查看文档 [模型定义](Models.md) |
| 161 | 163 | ||
| 164 | #### RegisterModelWithPrefix | ||
| 165 | |||
| 166 | 使用表名前缀 | ||
| 167 | |||
| 168 | ```go | ||
| 169 | orm.RegisterModelWithPrefix("prefix_", new(User)) | ||
| 170 | ``` | ||
| 171 | |||
| 172 | 创建后的表名为 prefix_user | ||
| 173 | |||
| 162 | ## ORM 接口使用 | 174 | ## ORM 接口使用 |
| 163 | 175 | ||
| 164 | 使用 orm 必然接触的 Ormer 接口,我们来熟悉一下 | 176 | 使用 orm 必然接触的 Ormer 接口,我们来熟悉一下 | ... | ... |
| ... | @@ -10,7 +10,7 @@ o := orm.NewOrm() | ... | @@ -10,7 +10,7 @@ o := orm.NewOrm() |
| 10 | qs := o.QueryTable("user") | 10 | qs := o.QueryTable("user") |
| 11 | 11 | ||
| 12 | // 也可以直接使用对象作为表名 | 12 | // 也可以直接使用对象作为表名 |
| 13 | user := NewUser() | 13 | user := new(User) |
| 14 | qs = o.QueryTable(user) // 返回 QuerySeter | 14 | qs = o.QueryTable(user) // 返回 QuerySeter |
| 15 | ``` | 15 | ``` |
| 16 | ## expr | 16 | ## expr | ... | ... |
| ... | @@ -5,7 +5,7 @@ | ... | @@ -5,7 +5,7 @@ |
| 5 | * [驱动类型设置](Orm.md#registerdriver) | 5 | * [驱动类型设置](Orm.md#registerdriver) |
| 6 | * [参数设置](Orm.md#registerdataBase) | 6 | * [参数设置](Orm.md#registerdataBase) |
| 7 | * [时区设置](Orm.md#时区设置) | 7 | * [时区设置](Orm.md#时区设置) |
| 8 | - [注册 ORM 使用的模型](Orm.md#registermodel) | 8 | - [注册模型](Orm.md#注册模型) |
| 9 | - [ORM 接口使用](Orm.md#orm-接口使用) | 9 | - [ORM 接口使用](Orm.md#orm-接口使用) |
| 10 | - [调试模式打印查询语句](Orm.md#调试模式打印查询语句) | 10 | - [调试模式打印查询语句](Orm.md#调试模式打印查询语句) |
| 11 | 2. [对象的CRUD操作](Object.md) | 11 | 2. [对象的CRUD操作](Object.md) |
| ... | @@ -19,11 +19,16 @@ | ... | @@ -19,11 +19,16 @@ |
| 19 | - [Struct Tag 设置参数](Models.md#struct-tag-设置参数) | 19 | - [Struct Tag 设置参数](Models.md#struct-tag-设置参数) |
| 20 | - [表关系设置](Models.md#表关系设置) | 20 | - [表关系设置](Models.md#表关系设置) |
| 21 | - [模型字段与数据库类型的对应](Models.md#模型字段与数据库类型的对应) | 21 | - [模型字段与数据库类型的对应](Models.md#模型字段与数据库类型的对应) |
| 22 | 7. Custom Fields | 22 | 7. [命令模式](Cmd.md) |
| 23 | 8. Faq | 23 | - [自动建表](Cmd.md#自动建表) |
| 24 | - [打印建表SQL](Cmd.md#打印建表sql) | ||
| 25 | 8. [Test ORM](Test.md) | ||
| 26 | 9. Custom Fields | ||
| 27 | 10. Faq | ||
| 24 | 28 | ||
| 25 | 29 | ||
| 26 | ### 文档更新记录 | 30 | ### 文档更新记录 |
| 27 | 31 | ||
| 32 | * 2013-08-19: 增加[自动建表](Cmd.md#自动建表)功能 | ||
| 28 | * 2013-08-13: ORM 的 [时区设置](Orm.md#时区设置) | 33 | * 2013-08-13: ORM 的 [时区设置](Orm.md#时区设置) |
| 29 | * 2013-08-13: [模型字段与数据库类型的对应](Models.md#模型字段与数据库类型的对应) 推荐的数据库对应使用的类型 | 34 | * 2013-08-13: [模型字段与数据库类型的对应](Models.md#模型字段与数据库类型的对应) 推荐的数据库对应使用的类型 | ... | ... |
orm/docs/zh/Test.md
0 → 100644
| 1 | ## Test ORM | ||
| 2 | |||
| 3 | 测试代码参见 | ||
| 4 | |||
| 5 | ```bash | ||
| 6 | models_test.go // 表定义 | ||
| 7 | orm_test.go // 测试用例 | ||
| 8 | ``` | ||
| 9 | |||
| 10 | #### MySQL | ||
| 11 | ```bash | ||
| 12 | mysql -u root -e 'create database orm_test;' | ||
| 13 | export ORM_DRIVER=mysql | ||
| 14 | export ORM_SOURCE="root:@/orm_test?charset=utf8" | ||
| 15 | go test -v github.com/astaxie/beego/orm | ||
| 16 | ``` | ||
| 17 | |||
| 18 | |||
| 19 | #### Sqlite3 | ||
| 20 | ```bash | ||
| 21 | touch /path/to/orm_test.db | ||
| 22 | export ORM_DRIVER=sqlite3 | ||
| 23 | export ORM_SOURCE=/path/to/orm_test.db | ||
| 24 | go test -v github.com/astaxie/beego/orm | ||
| 25 | ``` | ||
| 26 | |||
| 27 | |||
| 28 | #### PostgreSQL | ||
| 29 | ```bash | ||
| 30 | psql -c 'create database orm_test;' -U postgres | ||
| 31 | export ORM_DRIVER=postgres | ||
| 32 | export ORM_SOURCE="user=postgres dbname=orm_test sslmode=disable" | ||
| 33 | go test -v github.com/astaxie/beego/orm | ||
| 34 | ``` | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or sign in to post a comment