From f1e2372a56bc21ee7dc50371424b86dc740bc7e6 Mon Sep 17 00:00:00 2001 From: slene Date: Fri, 9 Aug 2013 13:53:04 +0800 Subject: [PATCH] orm update docs about debug log queries --- orm/README.md | 23 ++++++++++++++++++++ orm/docs/zh/Orm.md | 43 ++++++++++++++++++++++++++++++++++++++ orm/docs/zh/README.md | 1 + orm/docs/zh/Transaction.md | 14 +++++++++++++ 4 files changed, 81 insertions(+) diff --git a/orm/README.md b/orm/README.md index b612672d..62eb8094 100644 --- a/orm/README.md +++ b/orm/README.md @@ -107,6 +107,29 @@ if err != nil { ``` +#### Debug Log Queries + +In development env, you can simple use + +```go +func main() { + orm.Debug = true +... +``` + +enable log queries. + +output include all queries, such as exec / prepare / transaction. + +like this: + +```go +[ORM] - 2013-08-09 13:18:16 - [Queries/default] - [ db.Exec / 0.4ms] - [INSERT INTO `user` (`name`) VALUES (?)] - `slene` +... +``` + +note: not recommend use this in product env. + ## Docs more details and examples in docs and test diff --git a/orm/docs/zh/Orm.md b/orm/docs/zh/Orm.md index db02d481..0030ac0b 100644 --- a/orm/docs/zh/Orm.md +++ b/orm/docs/zh/Orm.md @@ -198,3 +198,46 @@ fmt.Println(dr.Name() == "db2") // true fmt.Println(dr.Type() == orm.DR_Sqlite) // true ``` + +## 调试模式打印查询语句 + +简单的设置 Debug 为 true 打印查询的语句 + +可能存在性能问题,不建议使用在产品模式 + +```go +func main() { + orm.Debug = true +... +``` + +默认使用 os.Stderr 输出日志信息 + +改变输出到你自己的 io.Writer + +```go +var w io.Writer +... +// 设置为你的 io.Writer +... +orm.DebugLog = orm.NewLog(w) +``` + +日志格式 + +```go +[ORM] - 时间 - [Queries/数据库名] - [执行操作/执行时间] - [SQL语句] - 使用标点 `,` 分隔的参数列表 - 打印遇到的错误 +``` + +```go +[ORM] - 2013-08-09 13:18:16 - [Queries/default] - [ db.Exec / 0.4ms] - [INSERT INTO `user` (`name`) VALUES (?)] - `slene` +[ORM] - 2013-08-09 13:18:16 - [Queries/default] - [ db.Exec / 0.5ms] - [UPDATE `user` SET `name` = ? WHERE `id` = ?] - `astaxie`, `14` +[ORM] - 2013-08-09 13:18:16 - [Queries/default] - [db.QueryRow / 0.4ms] - [SELECT `id`, `name` FROM `user` WHERE `id` = ?] - `14` +[ORM] - 2013-08-09 13:18:16 - [Queries/default] - [ db.Exec / 0.4ms] - [INSERT INTO `post` (`user_id`,`title`,`content`) VALUES (?, ?, ?)] - `14`, `beego orm`, `powerful amazing` +[ORM] - 2013-08-09 13:18:16 - [Queries/default] - [ db.Query / 0.4ms] - [SELECT T1.`name` `User__Name`, T0.`user_id` `User`, T1.`id` `User__Id` FROM `post` T0 INNER JOIN `user` T1 ON T1.`id` = T0.`user_id` WHERE T0.`id` = ? LIMIT 1000] - `68` +[ORM] - 2013-08-09 13:18:16 - [Queries/default] - [ db.Exec / 0.4ms] - [DELETE FROM `user` WHERE `id` = ?] - `14` +[ORM] - 2013-08-09 13:18:16 - [Queries/default] - [ db.Query / 0.3ms] - [SELECT T0.`id` FROM `post` T0 WHERE T0.`user_id` IN (?) ] - `14` +[ORM] - 2013-08-09 13:18:16 - [Queries/default] - [ db.Exec / 0.4ms] - [DELETE FROM `post` WHERE `id` IN (?)] - `68` +``` + +日志内容包括 **所有的数据库操作**,事务,Prepare,等 diff --git a/orm/docs/zh/README.md b/orm/docs/zh/README.md index e7d80cc5..305e19a8 100644 --- a/orm/docs/zh/README.md +++ b/orm/docs/zh/README.md @@ -4,6 +4,7 @@ 1. [Orm 使用方法](Orm.md) - [数据库的设置](Orm.md#数据库的设置) - [ORM 接口使用](Orm.md#orm-接口使用) + - [调试模式打印查询语句](Orm.md#调试模式打印查询语句) 2. [对象的CRUD操作](Object.md) 3. [高级查询](Query.md) - [使用的表达式语法](Query.md#expr) diff --git a/orm/docs/zh/Transaction.md b/orm/docs/zh/Transaction.md index 2742529c..51172c09 100644 --- a/orm/docs/zh/Transaction.md +++ b/orm/docs/zh/Transaction.md @@ -1,3 +1,17 @@ ## 事务处理 +orm 可以简单的进行事务操作 +```go +o := NewOrm() +err := o.Begin() +// 事务处理过程 +... +... +// 此过程中的所有使用 o Ormer 对象的查询都在事务处理范围内 +if SomeError { + err = o.Rollback() +} else { + err = o.Commit() +} +```