Sin descripción

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

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).Query().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).Query().Xml() //返回查询结果的xml字符串
rows, err := db.SqlMapClient("selectAllArticle", &paramMap).Query().Json() //返回查询结果的json字符串
rows, err := db.SqlMapClient("selectAllArticle", &paramMap).QueryWithDateFormat("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).Query().Json()
rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryWithDateFormat("2006/01/02").Json()
rows, err := db.SqlTemplateClient("select.example.stpl", paramMap).QueryWithDateFormat("2006/01/02").XmlIndent("", "  ", "article")

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