orm docs update
Showing
5 changed files
with
168 additions
and
30 deletions
| 1 | # beego orm | 1 | # beego orm |
| 2 | 2 | ||
| 3 | a powerful orm framework | 3 | [](https://drone.io/github.com/astaxie/beego/latest) |
| 4 | |||
| 5 | A powerful orm framework for go. | ||
| 6 | |||
| 7 | It is heavily influenced by Django ORM, SQLAlchemy. | ||
| 4 | 8 | ||
| 5 | now, beta, unstable, may be changing some api make your app build failed. | 9 | now, beta, unstable, may be changing some api make your app build failed. |
| 6 | 10 | ||
| ... | @@ -14,12 +18,25 @@ Passed all test, but need more feedback. | ... | @@ -14,12 +18,25 @@ Passed all test, but need more feedback. |
| 14 | 18 | ||
| 15 | **Features:** | 19 | **Features:** |
| 16 | 20 | ||
| 17 | ... | 21 | * full go type support |
| 22 | * easy for usage, simple CRUD operation | ||
| 23 | * auto join with relation table | ||
| 24 | * cross DataBase compatible query | ||
| 25 | * Raw SQL query / mapper without orm model | ||
| 26 | * full test keep stable and strong | ||
| 27 | |||
| 28 | more features please read the docs | ||
| 18 | 29 | ||
| 19 | **Install:** | 30 | **Install:** |
| 20 | 31 | ||
| 21 | go get github.com/astaxie/beego/orm | 32 | go get github.com/astaxie/beego/orm |
| 22 | 33 | ||
| 34 | ## Changelog | ||
| 35 | |||
| 36 | * 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: date / datetime timezone support very well | ||
| 39 | |||
| 23 | ## Quick Start | 40 | ## Quick Start |
| 24 | 41 | ||
| 25 | #### Simple Usage | 42 | #### Simple Usage |
| ... | @@ -143,5 +160,3 @@ more details and examples in docs and test | ... | @@ -143,5 +160,3 @@ more details and examples in docs and test |
| 143 | - some unrealized api | 160 | - some unrealized api |
| 144 | - examples | 161 | - examples |
| 145 | - docs | 162 | - docs |
| 146 | |||
| 147 | ## | ... | ... |
| ... | @@ -164,27 +164,91 @@ type Profile struct { | ... | @@ -164,27 +164,91 @@ type Profile struct { |
| 164 | ``` | 164 | ``` |
| 165 | 165 | ||
| 166 | 166 | ||
| 167 | ## Struct Field 类型与数据库的对应 | 167 | ## 模型字段与数据库类型的对应 |
| 168 | 168 | ||
| 169 | 现在 orm 支持下面的字段形式 | 169 | 在此列出 orm 推荐的对应数据库类型,自动建表功能也会以此为标准。 |
| 170 | 170 | ||
| 171 | | go type | field type | mysql type | 171 | 默认所有的字段都是 **NOT NULL** |
| 172 | | :--- | :--- | :--- | 172 | |
| 173 | | bool | TypeBooleanField | tinyint | 173 | #### MySQL |
| 174 | | string | TypeCharField | varchar | 174 | |
| 175 | | string | TypeTextField | longtext | 175 | | go |mysql |
| 176 | | time.Time | TypeDateField | date | 176 | | :--- | :--- |
| 177 | | time.TIme | TypeDateTimeField | datetime | 177 | | bool | bool |
| 178 | | int16 |TypeSmallIntegerField | int(4) | 178 | | string - 设置 size 时 | varchar(size) |
| 179 | | int, int32 |TypeIntegerField | int(11) | 179 | | string | longtext |
| 180 | | int64 |TypeBigIntegerField | bigint(20) | 180 | | time.Time - 设置 type 为 date 时 | date |
| 181 | | uint, uint16 |TypePositiveSmallIntegerField | int(4) unsigned | 181 | | time.TIme | datetime |
| 182 | | uint32 |TypePositiveIntegerField | int(11) unsigned | 182 | | byte | tinyint unsigned |
| 183 | | uint64 |TypePositiveBigIntegerField | bigint(20) unsigned | 183 | | rune | integer |
| 184 | | float32, float64 | TypeFloatField | double | 184 | | int | integer |
| 185 | | float32, float64 | TypeDecimalField | double(digits, decimals) | 185 | | int8 | tinyint |
| 186 | 186 | | int16 | smallint | |
| 187 | 关系型的字段,其字段类型取决于对应的主键。 | 187 | | int32 | integer |
| 188 | | int64 | bigint | ||
| 189 | | uint | integer unsigned | ||
| 190 | | uint8 | tinyint unsigned | ||
| 191 | | uint16 | smallint unsigned | ||
| 192 | | uint32 | integer unsigned | ||
| 193 | | uint64 | bigint unsigned | ||
| 194 | | float32 | double precision | ||
| 195 | | float64 | double precision | ||
| 196 | | float64 - 设置 digits, decimals 时 | numeric(digits, decimals) | ||
| 197 | |||
| 198 | #### Sqlite3 | ||
| 199 | |||
| 200 | | go | sqlite3 | ||
| 201 | | :--- | :--- | ||
| 202 | | bool | bool | ||
| 203 | | string - 设置 size 时 | varchar(size) | ||
| 204 | | string | text | ||
| 205 | | time.Time - 设置 type 为 date 时 | date | ||
| 206 | | time.TIme | datetime | ||
| 207 | | byte | tinyint unsigned | ||
| 208 | | rune | integer | ||
| 209 | | int | integer | ||
| 210 | | int8 | tinyint | ||
| 211 | | int16 | smallint | ||
| 212 | | int32 | integer | ||
| 213 | | int64 | bigint | ||
| 214 | | uint | integer unsigned | ||
| 215 | | uint8 | tinyint unsigned | ||
| 216 | | uint16 | smallint unsigned | ||
| 217 | | uint32 | integer unsigned | ||
| 218 | | uint64 | bigint unsigned | ||
| 219 | | float32 | real | ||
| 220 | | float64 | real | ||
| 221 | | float64 - 设置 digits, decimals 时 | decimal | ||
| 222 | |||
| 223 | #### PostgreSQL | ||
| 224 | |||
| 225 | | go | postgres | ||
| 226 | | :--- | :--- | ||
| 227 | | bool | bool | ||
| 228 | | string - 设置 size 时 | varchar(size) | ||
| 229 | | string | text | ||
| 230 | | time.Time - 设置 type 为 date 时 | date | ||
| 231 | | time.TIme | timestamp with time zone | ||
| 232 | | byte | smallint CHECK("column" >= 0 AND "column" <= 255) | ||
| 233 | | rune | integer | ||
| 234 | | int | integer | ||
| 235 | | int8 | smallint CHECK("column" >= -127 AND "column" <= 128) | ||
| 236 | | int16 | smallint | ||
| 237 | | int32 | integer | ||
| 238 | | int64 | bigint | ||
| 239 | | uint | bigint CHECK("column" >= 0) | ||
| 240 | | uint8 | smallint CHECK("column" >= 0 AND "column" <= 255) | ||
| 241 | | uint16 | integer CHECK("column" >= 0) | ||
| 242 | | uint32 | bigint CHECK("column" >= 0) | ||
| 243 | | uint64 | bigint CHECK("column" >= 0) | ||
| 244 | | float32 | double precision | ||
| 245 | | float64 | double precision | ||
| 246 | | float64 - 设置 digits, decimals 时 | numeric(digits, decimals) | ||
| 247 | |||
| 248 | |||
| 249 | ## 关系型字段 | ||
| 250 | |||
| 251 | 其字段类型取决于对应的主键。 | ||
| 188 | 252 | ||
| 189 | * RelForeignKey | 253 | * RelForeignKey |
| 190 | * RelOneToOne | 254 | * RelOneToOne | ... | ... |
| ... | @@ -80,7 +80,7 @@ import ( | ... | @@ -80,7 +80,7 @@ import ( |
| 80 | 80 | ||
| 81 | #### RegisterDriver | 81 | #### RegisterDriver |
| 82 | 82 | ||
| 83 | 三种数据库类型 | 83 | 三种默认数据库类型 |
| 84 | 84 | ||
| 85 | ```go | 85 | ```go |
| 86 | orm.DR_MySQL | 86 | orm.DR_MySQL |
| ... | @@ -93,7 +93,7 @@ orm.DR_Postgres | ... | @@ -93,7 +93,7 @@ orm.DR_Postgres |
| 93 | // 参数2 数据库类型 | 93 | // 参数2 数据库类型 |
| 94 | // 这个用来设置 driverName 对应的数据库类型 | 94 | // 这个用来设置 driverName 对应的数据库类型 |
| 95 | // mysql / sqlite3 / postgres 这三种是默认已经注册过的,所以可以无需设置 | 95 | // mysql / sqlite3 / postgres 这三种是默认已经注册过的,所以可以无需设置 |
| 96 | orm.RegisterDriver("mysql", orm.DR_MySQL) | 96 | orm.RegisterDriver("mymysql", orm.DR_MySQL) |
| 97 | ``` | 97 | ``` |
| 98 | 98 | ||
| 99 | #### RegisterDataBase | 99 | #### RegisterDataBase |
| ... | @@ -108,6 +108,56 @@ orm 必须注册一个名称为 `default` 的数据库,用以作为默认使 | ... | @@ -108,6 +108,56 @@ orm 必须注册一个名称为 `default` 的数据库,用以作为默认使 |
| 108 | orm.RegisterDataBase("default", "mysql", "root:root@/orm_test?charset=utf8", 30) | 108 | orm.RegisterDataBase("default", "mysql", "root:root@/orm_test?charset=utf8", 30) |
| 109 | ``` | 109 | ``` |
| 110 | 110 | ||
| 111 | #### 时区设置 | ||
| 112 | |||
| 113 | orm 默认使用 time.Local 本地时区 | ||
| 114 | |||
| 115 | * 作用于 orm 自动创建的时间 | ||
| 116 | * 从数据库中取回的时间转换成 orm 本地时间 | ||
| 117 | |||
| 118 | 如果需要的话,你也可以进行更改 | ||
| 119 | |||
| 120 | ```go | ||
| 121 | // 设置为 UTC 时间 | ||
| 122 | orm.DefaultTimeLoc = time.UTC | ||
| 123 | ``` | ||
| 124 | |||
| 125 | orm 在进行 RegisterDataBase 的同时,会获取数据库使用的时区,然后在 time.Time 类型存取的时做相应转换,以匹配时间系统,从而保证时间不会出错。 | ||
| 126 | |||
| 127 | **注意:** 鉴于 Sqlite3 的设计,存取默认都为 UTC 时间 | ||
| 128 | |||
| 129 | ## RegisterModel | ||
| 130 | |||
| 131 | 如果使用 orm.QuerySeter 进行高级查询的话,这个是必须的。 | ||
| 132 | |||
| 133 | 反之,如果只使用 Raw 查询和 map struct,是无需这一步的。您可以去查看 [Raw SQL 查询](Raw.md) | ||
| 134 | |||
| 135 | 将你定义的 Model 进行注册,最佳设计是有单独的 models.go 文件,在他的 init 函数中进行注册。 | ||
| 136 | |||
| 137 | |||
| 138 | 迷你版 models.go | ||
| 139 | ```go | ||
| 140 | package main | ||
| 141 | |||
| 142 | import "github.com/astaxie/beego/orm" | ||
| 143 | |||
| 144 | type User struct { | ||
| 145 | Id int `orm:"auto"` | ||
| 146 | name string | ||
| 147 | } | ||
| 148 | |||
| 149 | func init(){ | ||
| 150 | orm.RegisterModel(new(User)) | ||
| 151 | } | ||
| 152 | ``` | ||
| 153 | |||
| 154 | RegisterModel 也可以同时注册多个 model | ||
| 155 | |||
| 156 | ```go | ||
| 157 | orm.RegisterModel(new(User), new(Profile), new(Post)) | ||
| 158 | ``` | ||
| 159 | |||
| 160 | |||
| 111 | ## ORM 接口使用 | 161 | ## ORM 接口使用 |
| 112 | 162 | ||
| 113 | 使用 orm 必然接触的 Ormer 接口,我们来熟悉一下 | 163 | 使用 orm 必然接触的 Ormer 接口,我们来熟悉一下 | ... | ... |
| ... | @@ -15,7 +15,7 @@ qs = o.QueryTable(user) // 返回 QuerySeter | ... | @@ -15,7 +15,7 @@ qs = o.QueryTable(user) // 返回 QuerySeter |
| 15 | ``` | 15 | ``` |
| 16 | ## expr | 16 | ## expr |
| 17 | 17 | ||
| 18 | QuerySeter 中用于描述字段和 sql 操作符使用简单的 expr 查询方法 | 18 | QuerySeter 中用于描述字段和 sql 操作符,使用简单的 expr 查询方法 |
| 19 | 19 | ||
| 20 | 字段组合的前后顺序依照表的关系,比如 User 表拥有 Profile 的外键,那么对 User 表查询对应的 Profile.Age 为条件,则使用 `Profile__Age` 注意,字段的分隔符号使用双下划线 `__`,除了描述字段, expr 的尾部可以增加操作符以执行对应的 sql 操作。比如 `Profile__Age__gt` 代表 Profile.Age > 18 的条件查询。 | 20 | 字段组合的前后顺序依照表的关系,比如 User 表拥有 Profile 的外键,那么对 User 表查询对应的 Profile.Age 为条件,则使用 `Profile__Age` 注意,字段的分隔符号使用双下划线 `__`,除了描述字段, expr 的尾部可以增加操作符以执行对应的 sql 操作。比如 `Profile__Age__gt` 代表 Profile.Age > 18 的条件查询。 |
| 21 | 21 | ... | ... |
| 1 | ## 文档目录 | 1 | ## 文档目录 |
| 2 | 2 | ||
| 3 | |||
| 4 | 1. [Orm 使用方法](Orm.md) | 3 | 1. [Orm 使用方法](Orm.md) |
| 5 | - [数据库的设置](Orm.md#数据库的设置) | 4 | - [数据库的设置](Orm.md#数据库的设置) |
| 5 | * [驱动类型设置](Orm.md#registerdriver) | ||
| 6 | * [参数设置](Orm.md#registerdataBase) | ||
| 7 | * [时区设置](Orm.md#时区设置) | ||
| 8 | - [注册 ORM 使用的模型](Orm.md#registermodel) | ||
| 6 | - [ORM 接口使用](Orm.md#orm-接口使用) | 9 | - [ORM 接口使用](Orm.md#orm-接口使用) |
| 7 | - [调试模式打印查询语句](Orm.md#调试模式打印查询语句) | 10 | - [调试模式打印查询语句](Orm.md#调试模式打印查询语句) |
| 8 | 2. [对象的CRUD操作](Object.md) | 11 | 2. [对象的CRUD操作](Object.md) |
| ... | @@ -15,6 +18,12 @@ | ... | @@ -15,6 +18,12 @@ |
| 15 | 6. [模型定义](Models.md) | 18 | 6. [模型定义](Models.md) |
| 16 | - [Struct Tag 设置参数](Models.md#struct-tag-设置参数) | 19 | - [Struct Tag 设置参数](Models.md#struct-tag-设置参数) |
| 17 | - [表关系设置](Models.md#表关系设置) | 20 | - [表关系设置](Models.md#表关系设置) |
| 18 | - [Struct Field 类型与数据库的对应](Models.md#struct-field-类型与数据库的对应) | 21 | - [模型字段与数据库类型的对应](Models.md#模型字段与数据库类型的对应) |
| 19 | 7. Custom Fields | 22 | 7. Custom Fields |
| 20 | 8. Faq | 23 | 8. Faq |
| 24 | |||
| 25 | |||
| 26 | ### 文档更新记录 | ||
| 27 | |||
| 28 | * 2013-08-13: ORM 的 [时区设置](Orm.md#时区设置) | ||
| 29 | * 2013-08-13: [模型字段与数据库类型的对应](Models.md#模型字段与数据库类型的对应) 推荐的数据库对应使用的类型 | ... | ... |
-
Please register or sign in to post a comment