No Description

xormplus 1601c637cd bug fixed for empty struct when update 9 years ago
docs cb5fc920a7 . 10 years ago
test 2b491784b7 update test case 9 years ago
.gitignore 52a602da39 Initial commit 10 years ago
LICENSE 52a602da39 Initial commit 10 years ago
README.md 7054eada51 Update README.md 10 years ago
cover.out c93a5bbd96 初始化 10 years ago
engine.go 858078832d serious extends bug fixed & correct logger file path 9 years ago
engineplus.go 8bb4299b35 bug fixed 9 years ago
error.go c93a5bbd96 初始化 10 years ago
goracle_driver.go c93a5bbd96 初始化 10 years ago
helpers.go 1601c637cd bug fixed for empty struct when update 9 years ago
helpersplus.go 858078832d serious extends bug fixed & correct logger file path 9 years ago
logger.go 858078832d serious extends bug fixed & correct logger file path 9 years ago
lru_cacher.go c93a5bbd96 初始化 10 years ago
memroy_store.go c93a5bbd96 初始化 10 years ago
mssql_dialect.go 8bb4299b35 bug fixed 9 years ago
mymysql_driver.go 8bb4299b35 bug fixed 9 years ago
mysql_dialect.go 8bb4299b35 bug fixed 9 years ago
mysql_driver.go 8bb4299b35 bug fixed 9 years ago
oci8_driver.go 8bb4299b35 bug fixed 9 years ago
odbc_driver.go 8bb4299b35 bug fixed 9 years ago
oracle_dialect.go 8bb4299b35 bug fixed 9 years ago
pg_reserved.txt c93a5bbd96 初始化 10 years ago
postgres_dialect.go 8bb4299b35 bug fixed 9 years ago
pq_driver.go 8bb4299b35 bug fixed 9 years ago
processors.go a85984d0c5 Added feature for storing lastSQL query on session 10 years ago
rows.go a85984d0c5 Added feature for storing lastSQL query on session 10 years ago
session.go 8bb4299b35 bug fixed 9 years ago
sessionplus.go 858078832d serious extends bug fixed & correct logger file path 9 years ago
sqlite3_dialect.go 858078832d serious extends bug fixed & correct logger file path 9 years ago
sqlite3_driver.go c93a5bbd96 初始化 10 years ago
sqlmap.go 9f264f3857 sqlmap和sqltemplate的根目录除可在配置文件配置,也可以在程序初始化之前代码指定,代码指定优先级高于配置 10 years ago
sqltemplate.go 9f264f3857 sqlmap和sqltemplate的根目录除可在配置文件配置,也可以在程序初始化之前代码指定,代码指定优先级高于配置 10 years ago
statement.go 1601c637cd bug fixed for empty struct when update 9 years ago
syslogger.go 858078832d serious extends bug fixed & correct logger file path 9 years ago
types.go 6e437f24db some comments, refactors improvements 9 years ago
xorm.go 1601c637cd bug fixed for empty struct when update 9 years ago
xormplus.go db2466f3a7 修改SqlMap和SqlTemplate初始化方式 10 years ago

README.md

xorm

###优化xorm的查询API,并提供类似ibatis的配置文件及动态SQL功能,支持AcitveRecord操作

go get -u github.com/xormplus/xorm

测试用例测试结果

var err error
db, err = xorm.NewPostgreSQL("postgres://postgres:root@localhost:5432/testdb?sslmode=disable")

db.SqlMap.SqlMapRootDir="./sql/oracle" //SqlMap配置文件存根目录,可代码指定,可在配置文件中配置,代码指定优先级高于配置
db.SqlTemplate.SqlTemplateRootDir="./sql/oracle" //SqlTemplate配置文件存根目录,可代码指定,可在配置文件中配置,代码指定优先级高于配置
if err != nil {
	t.Fatal(err)
}

err = db.InitSqlMap() //初始化SqlMap配置,可选功能,如应用中无需使用SqlMap,可无需初始化
if err != nil {
	t.Fatal(err)
}
err = db.InitSqlTemplate() //初始化动态SQL模板配置,可选功能,如应用中无需使用SqlTemplate,可无需初始化
if err != nil {
	t.Fatal(err)
}

####db.InitSqlMap()过程

  • 如指定db.SqlMap.SqlMapRootDir,则err = db.InitSqlMap()按指定目录遍历SqlMapRootDir所配置的目录及其子目录下的所有xml配置文件(配置文件样例
  • 如未指定db.SqlMap.SqlMapRootDir,err = db.InitSqlMap()则读取程序所在目下的sql/xormcfg.ini配置文件(样例)中的SqlMapRootDir配置项,遍历SqlMapRootDir所配置的目录及其子目录下的所有xml配置文件(配置文件样例
  • 解析所有配置SqlMap的xml配置文件

####db.InitSqlTemplate()过程

  • 如指定db.SqlTemplate.SqlTemplateRootDir,err = db.InitSqlTemplate()按指定目录遍历SqlTemplateRootDir所配置的目录及其子目录下的所有stpl模板文件(模板文件样例
  • 如指未定db.SqlTemplate.SqlTemplateRootDir,err = db.InitSqlTemplate()则读取程序所在目下的sql/xormcfg.ini配置文件(样例)中的SqlTemplateRootDir配置项,遍历SqlTemplateRootDir所配置的目录及其子目录下的所有stpl模板文件(模板文件样例
  • 解析stpl模板文件

###支持类似这样的链式读取数据操作

sql := "select id,title,createdatetime,content from article where id = ?"
rows, err := db.Sql(sql, 2).Query().Json() //返回查询结果的json字符串
rows, err := db.Sql("sql", 2).QueryWithDateFormat("20060102").Json() //返回查询结果的json字符串,并支持格式化日期
rows, err := db.Sql("sql", 2).QueryWithDateFormat("20060102").Xml() //返回查询结果的xml字符串,并支持格式化日期

id := db.Sql(sql, 2).Query().Result[0]["id"] //返回查询结果的第一条数据的id列的值
title := db.Sql(sql, 2).Query().Result[0]["title"]
createdatetime := db.Sql(sql, 2).Query().Result[0]["createdatetime"]
content := db.Sql(sql, 2).Query().Result[0]["content"]

articles := make([]Article, 0)
xml,err := db.Where("id=?", 6).Find(&articles).Xml() //返回查询结果的xml字符串
json,err := db.Where("id=?", 6).Find(&articles).Json() //返回查询结果的json字符串

sql := "select id,title,createdatetime,content from article where id = ?id and userid=?userid"
paramMap := map[string]interface{}{"id": 6, "userid": 1} //支持参数使用map存放
rows, err := db.Sql(sql, &paramMap).QueryByParamMap().XmlIndent("", "  ", "article")

###支持SqlMap配置,配置文件样例

<sqlMap>
	<sql id="selectAllArticle">
		select id,title,createdatetime,content 
		from article where id in (?1,?2)
	</sql>
	<sql id="selectStudentById1">
		select * from article where id=?id
	</sql>
</sqlMap>
paramMap := map[string]interface{}{"1": 2, "2": 5} //支持参数使用map存放
rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryByParamMap().Xml() //返回查询结果的xml字符串
rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryByParamMap().Json() //返回查询结果的json字符串
rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryByParamMapWithDateFormat("2006/01/02").XmlIndent("", "  ", "article") //返回查询结果格式化的xml字符串,并支持格式化日期

###提供动态SQL支持,使用pongo2模板引擎 例如配置文件名:select.example.stpl

配置样例内容如下:

select id,userid,title,createdatetime,content 
from article where  
{% if count>1%}
id=?id
{% else%}
userid=?userid
{% endif %}
paramMap := map[string]interface{}{"id": 2, "userid": 3, "count": 1}
rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMap().Json()
rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMapWithDateFormat("2006/01/02").Json()
rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryByParamMapWithDateFormat("2006/01/02").XmlIndent("", "  ", "article")

###讨论 请加入QQ群:280360085 进行讨论。API设计相关建议可联系本人QQ:50892683