暫無描述

xormplus 5c08def0b4 Fixed driver-name logging 10 年之前
docs cb5fc920a7 . 11 年之前
test fe399adfb0 update Query and QueryWithDateFormat function 10 年之前
.gitignore 52a602da39 Initial commit 11 年之前
LICENSE 52a602da39 Initial commit 11 年之前
README.md 657075cf99 update 10 年之前
cover.out c93a5bbd96 初始化 11 年之前
engine.go 5c08def0b4 Fixed driver-name logging 10 年之前
engineplus.go a293180a44 bug fixed 10 年之前
error.go c93a5bbd96 初始化 11 年之前
goracle_driver.go c93a5bbd96 初始化 11 年之前
helpers.go aa15fd9816 bug fixed 10 年之前
helpersplus.go 858078832d serious extends bug fixed & correct logger file path 10 年之前
logger.go 858078832d serious extends bug fixed & correct logger file path 10 年之前
lru_cacher.go c93a5bbd96 初始化 11 年之前
memroy_store.go c93a5bbd96 初始化 11 年之前
mssql_dialect.go 8bb4299b35 bug fixed 10 年之前
mymysql_driver.go 8bb4299b35 bug fixed 10 年之前
mysql_dialect.go 8bb4299b35 bug fixed 10 年之前
mysql_driver.go 8bb4299b35 bug fixed 10 年之前
oci8_driver.go 8bb4299b35 bug fixed 10 年之前
odbc_driver.go 8bb4299b35 bug fixed 10 年之前
oracle_dialect.go 8bb4299b35 bug fixed 10 年之前
pg_reserved.txt c93a5bbd96 初始化 11 年之前
postgres_dialect.go 8bb4299b35 bug fixed 10 年之前
pq_driver.go 8bb4299b35 bug fixed 10 年之前
processors.go a85984d0c5 Added feature for storing lastSQL query on session 10 年之前
rows.go a85984d0c5 Added feature for storing lastSQL query on session 10 年之前
session.go b893234e54 bug fixed 10 年之前
sessionplus.go b893234e54 bug fixed 10 年之前
sqlite3_dialect.go 858078832d serious extends bug fixed & correct logger file path 10 年之前
sqlite3_driver.go c93a5bbd96 初始化 11 年之前
sqlmap.go 9f264f3857 sqlmap和sqltemplate的根目录除可在配置文件配置,也可以在程序初始化之前代码指定,代码指定优先级高于配置 11 年之前
sqltemplate.go 9f264f3857 sqlmap和sqltemplate的根目录除可在配置文件配置,也可以在程序初始化之前代码指定,代码指定优先级高于配置 11 年之前
statement.go b893234e54 bug fixed 10 年之前
syslogger.go 858078832d serious extends bug fixed & correct logger file path 10 年之前
types.go 3ebf129b7b update import package 10 年之前
xorm.go b893234e54 bug fixed 10 年之前
xormplus.go db2466f3a7 修改SqlMap和SqlTemplate初始化方式 11 年之前

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