mirror of
https://github.com/tangpanqing/aorm.git
synced 2025-10-30 18:46:23 +08:00
support sqlite3
This commit is contained in:
113
builder/crud.go
113
builder/crud.go
@@ -101,36 +101,36 @@ func (ex *Builder) Insert(dest interface{}) (int64, error) {
|
|||||||
|
|
||||||
sqlStr := "INSERT INTO " + ex.tableName + " (" + strings.Join(keys, ",") + ") VALUES (" + strings.Join(place, ",") + ")"
|
sqlStr := "INSERT INTO " + ex.tableName + " (" + strings.Join(keys, ",") + ") VALUES (" + strings.Join(place, ",") + ")"
|
||||||
|
|
||||||
//如果是postgres,则转换?号到&1等
|
if ex.driverName == model.Postgres {
|
||||||
if ex.driverName == "postgres" {
|
sqlStr = convertToPostgresSql(sqlStr)
|
||||||
sqlStr = coverSql(sqlStr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//如果是mssql
|
if ex.driverName == model.Mssql {
|
||||||
if ex.driverName == "mssql" {
|
return ex.insertForMssqlOrPostgres(sqlStr+"; select ID = convert(bigint, SCOPE_IDENTITY())", paramList...)
|
||||||
rows, err := ex.LinkCommon.Query(sqlStr+"; select ID = convert(bigint, SCOPE_IDENTITY())", paramList...)
|
} else if ex.driverName == model.Postgres {
|
||||||
if err != nil {
|
return ex.insertForMssqlOrPostgres(sqlStr+" returning id", paramList...)
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
var lastInsertId1 int64
|
|
||||||
for rows.Next() {
|
|
||||||
rows.Scan(&lastInsertId1)
|
|
||||||
}
|
|
||||||
return lastInsertId1, nil
|
|
||||||
} else if ex.driverName == "postgres" {
|
|
||||||
rows, err := ex.LinkCommon.Query(sqlStr+" returning id", paramList...)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
var lastInsertId1 int64
|
|
||||||
for rows.Next() {
|
|
||||||
rows.Scan(&lastInsertId1)
|
|
||||||
}
|
|
||||||
return lastInsertId1, nil
|
|
||||||
} else {
|
} else {
|
||||||
res, err := ex.Exec(sqlStr, paramList...)
|
return ex.insertForCommon(sqlStr, paramList...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//对于Mssql,Postgres类型数据库,为了获取最后插入的id,需要改写入为查询
|
||||||
|
func (ex *Builder) insertForMssqlOrPostgres(sql string, paramList ...any) (int64, error) {
|
||||||
|
rows, err := ex.LinkCommon.Query(sql, paramList...)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var lastInsertId1 int64
|
||||||
|
for rows.Next() {
|
||||||
|
rows.Scan(&lastInsertId1)
|
||||||
|
}
|
||||||
|
return lastInsertId1, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//对于非Mssql,Postgres类型数据库,可以直接获取最后插入的id
|
||||||
|
func (ex *Builder) insertForCommon(sql string, paramList ...any) (int64, error) {
|
||||||
|
res, err := ex.Exec(sql, paramList...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@@ -141,10 +141,10 @@ func (ex *Builder) Insert(dest interface{}) (int64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return lastId, nil
|
return lastId, nil
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func coverSql(sqlStr string) string {
|
//对于Postgres数据库,不支持?占位符,支持$1,$2类型,需要做转换
|
||||||
|
func convertToPostgresSql(sqlStr string) string {
|
||||||
t := 1
|
t := 1
|
||||||
for {
|
for {
|
||||||
if strings.Index(sqlStr, "?") == -1 {
|
if strings.Index(sqlStr, "?") == -1 {
|
||||||
@@ -197,8 +197,8 @@ func (ex *Builder) InsertBatch(values interface{}) (int64, error) {
|
|||||||
|
|
||||||
sqlStr := "INSERT INTO " + ex.tableName + " (" + strings.Join(keys, ",") + ") VALUES " + strings.Join(place, ",")
|
sqlStr := "INSERT INTO " + ex.tableName + " (" + strings.Join(keys, ",") + ") VALUES " + strings.Join(place, ",")
|
||||||
|
|
||||||
if ex.driverName == "postgres" {
|
if ex.driverName == model.Postgres {
|
||||||
sqlStr = coverSql(sqlStr)
|
sqlStr = convertToPostgresSql(sqlStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := ex.Exec(sqlStr, paramList...)
|
res, err := ex.Exec(sqlStr, paramList...)
|
||||||
@@ -325,9 +325,8 @@ func (ex *Builder) GetSqlAndParams() (string, []interface{}) {
|
|||||||
|
|
||||||
sqlStr := "SELECT " + fieldStr + " FROM " + ex.tableName + joinStr + whereStr + groupStr + havingStr + orderStr + limitStr + lockStr
|
sqlStr := "SELECT " + fieldStr + " FROM " + ex.tableName + joinStr + whereStr + groupStr + havingStr + orderStr + limitStr + lockStr
|
||||||
|
|
||||||
//如果是postgres,则转换?号到&1等
|
if ex.driverName == model.Postgres {
|
||||||
if ex.driverName == "postgres" {
|
sqlStr = convertToPostgresSql(sqlStr)
|
||||||
sqlStr = coverSql(sqlStr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ex.isDebug {
|
if ex.isDebug {
|
||||||
@@ -345,9 +344,8 @@ func (ex *Builder) Update(dest interface{}) (int64, error) {
|
|||||||
whereStr, paramList := ex.handleWhere(ex.whereList, paramList)
|
whereStr, paramList := ex.handleWhere(ex.whereList, paramList)
|
||||||
sqlStr := "UPDATE " + ex.tableName + setStr + whereStr
|
sqlStr := "UPDATE " + ex.tableName + setStr + whereStr
|
||||||
|
|
||||||
//如果是postgres,则转换?号到&1等
|
if ex.driverName == model.Postgres {
|
||||||
if ex.driverName == "postgres" {
|
sqlStr = convertToPostgresSql(sqlStr)
|
||||||
sqlStr = coverSql(sqlStr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ex.ExecAffected(sqlStr, paramList...)
|
return ex.ExecAffected(sqlStr, paramList...)
|
||||||
@@ -359,9 +357,8 @@ func (ex *Builder) Delete() (int64, error) {
|
|||||||
whereStr, paramList := ex.handleWhere(ex.whereList, paramList)
|
whereStr, paramList := ex.handleWhere(ex.whereList, paramList)
|
||||||
sqlStr := "DELETE FROM " + ex.tableName + whereStr
|
sqlStr := "DELETE FROM " + ex.tableName + whereStr
|
||||||
|
|
||||||
//如果是postgres,则转换?号到&1等
|
if ex.driverName == model.Postgres {
|
||||||
if ex.driverName == "postgres" {
|
sqlStr = convertToPostgresSql(sqlStr)
|
||||||
sqlStr = coverSql(sqlStr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ex.ExecAffected(sqlStr, paramList...)
|
return ex.ExecAffected(sqlStr, paramList...)
|
||||||
@@ -370,7 +367,7 @@ func (ex *Builder) Delete() (int64, error) {
|
|||||||
// Truncate 清空记录, sqlte3不支持此操作
|
// Truncate 清空记录, sqlte3不支持此操作
|
||||||
func (ex *Builder) Truncate() (int64, error) {
|
func (ex *Builder) Truncate() (int64, error) {
|
||||||
sqlStr := "TRUNCATE TABLE " + ex.tableName
|
sqlStr := "TRUNCATE TABLE " + ex.tableName
|
||||||
if ex.driverName == "sqlite3" {
|
if ex.driverName == model.Sqlite3 {
|
||||||
sqlStr = "DELETE FROM " + ex.tableName
|
sqlStr = "DELETE FROM " + ex.tableName
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -464,6 +461,10 @@ func (ex *Builder) Increment(fieldName string, step int) (int64, error) {
|
|||||||
whereStr, paramList := ex.handleWhere(ex.whereList, paramList)
|
whereStr, paramList := ex.handleWhere(ex.whereList, paramList)
|
||||||
sqlStr := "UPDATE " + ex.tableName + " SET " + fieldName + "=" + fieldName + "+?" + whereStr
|
sqlStr := "UPDATE " + ex.tableName + " SET " + fieldName + "=" + fieldName + "+?" + whereStr
|
||||||
|
|
||||||
|
if ex.driverName == model.Postgres {
|
||||||
|
sqlStr = convertToPostgresSql(sqlStr)
|
||||||
|
}
|
||||||
|
|
||||||
return ex.ExecAffected(sqlStr, paramList...)
|
return ex.ExecAffected(sqlStr, paramList...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -474,11 +475,19 @@ func (ex *Builder) Decrement(fieldName string, step int) (int64, error) {
|
|||||||
whereStr, paramList := ex.handleWhere(ex.whereList, paramList)
|
whereStr, paramList := ex.handleWhere(ex.whereList, paramList)
|
||||||
sqlStr := "UPDATE " + ex.tableName + " SET " + fieldName + "=" + fieldName + "-?" + whereStr
|
sqlStr := "UPDATE " + ex.tableName + " SET " + fieldName + "=" + fieldName + "-?" + whereStr
|
||||||
|
|
||||||
|
if ex.driverName == model.Postgres {
|
||||||
|
sqlStr = convertToPostgresSql(sqlStr)
|
||||||
|
}
|
||||||
|
|
||||||
return ex.ExecAffected(sqlStr, paramList...)
|
return ex.ExecAffected(sqlStr, paramList...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exec 通用执行-新增,更新,删除
|
// Exec 通用执行-新增,更新,删除
|
||||||
func (ex *Builder) Exec(sqlStr string, args ...interface{}) (sql.Result, error) {
|
func (ex *Builder) Exec(sqlStr string, args ...interface{}) (sql.Result, error) {
|
||||||
|
if ex.driverName == model.Postgres {
|
||||||
|
sqlStr = convertToPostgresSql(sqlStr)
|
||||||
|
}
|
||||||
|
|
||||||
if ex.isDebug {
|
if ex.isDebug {
|
||||||
fmt.Println(sqlStr)
|
fmt.Println(sqlStr)
|
||||||
fmt.Println(args...)
|
fmt.Println(args...)
|
||||||
@@ -574,14 +583,14 @@ func (ex *Builder) whereAndHaving(where []WhereItem, paramList []any) ([]string,
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if where[i].Opt == Eq || where[i].Opt == Ne || where[i].Opt == Gt || where[i].Opt == Ge || where[i].Opt == Lt || where[i].Opt == Le {
|
if where[i].Opt == Eq || where[i].Opt == Ne || where[i].Opt == Gt || where[i].Opt == Ge || where[i].Opt == Lt || where[i].Opt == Le {
|
||||||
if ex.driverName == "sqlite3" {
|
if ex.driverName == model.Sqlite3 {
|
||||||
whereList = append(whereList, where[i].Field+" "+where[i].Opt+" "+"?")
|
whereList = append(whereList, where[i].Field+" "+where[i].Opt+" "+"?")
|
||||||
} else {
|
} else {
|
||||||
switch where[i].Val.(type) {
|
switch where[i].Val.(type) {
|
||||||
case float32:
|
case float32:
|
||||||
whereList = append(whereList, ex.getConcat(where[i].Field, "''")+" "+where[i].Opt+" "+"?")
|
whereList = append(whereList, ex.getConcatForFloat(where[i].Field, "''")+" "+where[i].Opt+" "+"?")
|
||||||
case float64:
|
case float64:
|
||||||
whereList = append(whereList, ex.getConcat(where[i].Field, "''")+" "+where[i].Opt+" "+"?")
|
whereList = append(whereList, ex.getConcatForFloat(where[i].Field, "''")+" "+where[i].Opt+" "+"?")
|
||||||
default:
|
default:
|
||||||
whereList = append(whereList, where[i].Field+" "+where[i].Opt+" "+"?")
|
whereList = append(whereList, where[i].Field+" "+where[i].Opt+" "+"?")
|
||||||
}
|
}
|
||||||
@@ -610,7 +619,7 @@ func (ex *Builder) whereAndHaving(where []WhereItem, paramList []any) ([]string,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
whereList = append(whereList, where[i].Field+" "+where[i].Opt+" "+ex.getConcat(valueStr...))
|
whereList = append(whereList, where[i].Field+" "+where[i].Opt+" "+ex.getConcatForLike(valueStr...))
|
||||||
}
|
}
|
||||||
|
|
||||||
if where[i].Opt == In || where[i].Opt == NotIn {
|
if where[i].Opt == In || where[i].Opt == NotIn {
|
||||||
@@ -701,8 +710,18 @@ func getScans(columnNameList []string, fieldNameMap map[string]int, destValue re
|
|||||||
return scans
|
return scans
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ex *Builder) getConcat(vars ...string) string {
|
func (ex *Builder) getConcatForFloat(vars ...string) string {
|
||||||
if ex.driverName == "sqlite3" {
|
if ex.driverName == model.Sqlite3 {
|
||||||
|
return strings.Join(vars, "||")
|
||||||
|
} else if ex.driverName == model.Postgres {
|
||||||
|
return vars[0]
|
||||||
|
} else {
|
||||||
|
return "CONCAT(" + strings.Join(vars, ",") + ")"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ex *Builder) getConcatForLike(vars ...string) string {
|
||||||
|
if ex.driverName == model.Sqlite3 || ex.driverName == model.Postgres {
|
||||||
return strings.Join(vars, "||")
|
return strings.Join(vars, "||")
|
||||||
} else {
|
} else {
|
||||||
return "CONCAT(" + strings.Join(vars, ",") + ")"
|
return "CONCAT(" + strings.Join(vars, ",") + ")"
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package builder
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/tangpanqing/aorm/helper"
|
"github.com/tangpanqing/aorm/helper"
|
||||||
|
"github.com/tangpanqing/aorm/model"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -97,14 +98,14 @@ func handleOrder(orderList []string) string {
|
|||||||
return " Order BY " + strings.Join(orderList, ",")
|
return " Order BY " + strings.Join(orderList, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
//拼接SQL,分页相关
|
//拼接SQL,分页相关 Postgres数据库分页数量在前偏移在后,其他数据库偏移量在前分页数量在后,另外Mssql数据库的关键词是offset...next
|
||||||
func (ex *Builder) handleLimit(offset int, pageSize int, paramList []any) (string, []any) {
|
func (ex *Builder) handleLimit(offset int, pageSize int, paramList []any) (string, []any) {
|
||||||
if 0 == pageSize {
|
if 0 == pageSize {
|
||||||
return "", paramList
|
return "", paramList
|
||||||
}
|
}
|
||||||
|
|
||||||
str := ""
|
str := ""
|
||||||
if ex.driverName == "postgres" {
|
if ex.driverName == model.Postgres {
|
||||||
paramList = append(paramList, pageSize)
|
paramList = append(paramList, pageSize)
|
||||||
paramList = append(paramList, offset)
|
paramList = append(paramList, offset)
|
||||||
|
|
||||||
@@ -114,7 +115,7 @@ func (ex *Builder) handleLimit(offset int, pageSize int, paramList []any) (strin
|
|||||||
paramList = append(paramList, pageSize)
|
paramList = append(paramList, pageSize)
|
||||||
|
|
||||||
str = " Limit ?,? "
|
str = " Limit ?,? "
|
||||||
if ex.driverName == "mssql" {
|
if ex.driverName == model.Mssql {
|
||||||
str = " offset ? rows fetch next ? rows only "
|
str = " offset ? rows fetch next ? rows only "
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func (mi *Migrator) Opinion(key string, val string) *Migrator {
|
|||||||
|
|
||||||
//ShowCreateTable 获取创建表的ddl
|
//ShowCreateTable 获取创建表的ddl
|
||||||
func (mi *Migrator) ShowCreateTable(tableName string) string {
|
func (mi *Migrator) ShowCreateTable(tableName string) string {
|
||||||
if mi.driverName == "mysql" {
|
if mi.driverName == model.Mysql {
|
||||||
me := migrate_mysql.MigrateExecutor{
|
me := migrate_mysql.MigrateExecutor{
|
||||||
DriverName: mi.driverName,
|
DriverName: mi.driverName,
|
||||||
OpinionList: mi.opinionList,
|
OpinionList: mi.opinionList,
|
||||||
@@ -69,7 +69,7 @@ func (mi *Migrator) Migrate(tableName string, dest interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (mi *Migrator) migrateCommon(tableName string, typeOf reflect.Type) {
|
func (mi *Migrator) migrateCommon(tableName string, typeOf reflect.Type) {
|
||||||
if mi.driverName == "mssql" {
|
if mi.driverName == model.Mssql {
|
||||||
me := migrate_mssql.MigrateExecutor{
|
me := migrate_mssql.MigrateExecutor{
|
||||||
DriverName: mi.driverName,
|
DriverName: mi.driverName,
|
||||||
OpinionList: mi.opinionList,
|
OpinionList: mi.opinionList,
|
||||||
@@ -80,7 +80,7 @@ func (mi *Migrator) migrateCommon(tableName string, typeOf reflect.Type) {
|
|||||||
me.MigrateCommon(tableName, typeOf)
|
me.MigrateCommon(tableName, typeOf)
|
||||||
}
|
}
|
||||||
|
|
||||||
if mi.driverName == "mysql" {
|
if mi.driverName == model.Mysql {
|
||||||
me := migrate_mysql.MigrateExecutor{
|
me := migrate_mysql.MigrateExecutor{
|
||||||
DriverName: mi.driverName,
|
DriverName: mi.driverName,
|
||||||
OpinionList: mi.opinionList,
|
OpinionList: mi.opinionList,
|
||||||
@@ -91,7 +91,7 @@ func (mi *Migrator) migrateCommon(tableName string, typeOf reflect.Type) {
|
|||||||
me.MigrateCommon(tableName, typeOf)
|
me.MigrateCommon(tableName, typeOf)
|
||||||
}
|
}
|
||||||
|
|
||||||
if mi.driverName == "sqlite3" {
|
if mi.driverName == model.Sqlite3 {
|
||||||
me := migrate_sqlite3.MigrateExecutor{
|
me := migrate_sqlite3.MigrateExecutor{
|
||||||
DriverName: mi.driverName,
|
DriverName: mi.driverName,
|
||||||
OpinionList: mi.opinionList,
|
OpinionList: mi.opinionList,
|
||||||
@@ -102,7 +102,7 @@ func (mi *Migrator) migrateCommon(tableName string, typeOf reflect.Type) {
|
|||||||
me.MigrateCommon(tableName, typeOf)
|
me.MigrateCommon(tableName, typeOf)
|
||||||
}
|
}
|
||||||
|
|
||||||
if mi.driverName == "postgres" {
|
if mi.driverName == model.Postgres {
|
||||||
me := migrate_postgres.MigrateExecutor{
|
me := migrate_postgres.MigrateExecutor{
|
||||||
DriverName: mi.driverName,
|
DriverName: mi.driverName,
|
||||||
OpinionList: mi.opinionList,
|
OpinionList: mi.opinionList,
|
||||||
|
|||||||
@@ -13,3 +13,8 @@ type OpinionItem struct {
|
|||||||
Key string
|
Key string
|
||||||
Val string
|
Val string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Mysql = "mysql"
|
||||||
|
const Mssql = "mssql"
|
||||||
|
const Postgres = "postgres"
|
||||||
|
const Sqlite3 = "sqlite3"
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/tangpanqing/aorm"
|
"github.com/tangpanqing/aorm"
|
||||||
"github.com/tangpanqing/aorm/builder"
|
"github.com/tangpanqing/aorm/builder"
|
||||||
"github.com/tangpanqing/aorm/helper"
|
"github.com/tangpanqing/aorm/helper"
|
||||||
|
"github.com/tangpanqing/aorm/model"
|
||||||
"github.com/tangpanqing/aorm/null"
|
"github.com/tangpanqing/aorm/null"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -59,10 +60,10 @@ type PersonWithArticleCount struct {
|
|||||||
|
|
||||||
func TestAll(t *testing.T) {
|
func TestAll(t *testing.T) {
|
||||||
dbList := make([]aorm.DbContent, 0)
|
dbList := make([]aorm.DbContent, 0)
|
||||||
//dbList = append(dbList, testSqlite3Connect())
|
dbList = append(dbList, testSqlite3Connect())
|
||||||
//dbList = append(dbList, testMysqlConnect())
|
dbList = append(dbList, testMysqlConnect())
|
||||||
dbList = append(dbList, testPostgresConnect())
|
dbList = append(dbList, testPostgresConnect())
|
||||||
//dbList = append(dbList, testMssqlConnect())
|
dbList = append(dbList, testMssqlConnect())
|
||||||
|
|
||||||
for i := 0; i < len(dbList); i++ {
|
for i := 0; i < len(dbList); i++ {
|
||||||
dbItem := dbList[i]
|
dbItem := dbList[i]
|
||||||
@@ -108,7 +109,7 @@ func TestAll(t *testing.T) {
|
|||||||
testExec(dbItem.DriverName, dbItem.DbLink)
|
testExec(dbItem.DriverName, dbItem.DbLink)
|
||||||
|
|
||||||
testTransaction(dbItem.DriverName, dbItem.DbLink)
|
testTransaction(dbItem.DriverName, dbItem.DbLink)
|
||||||
//testTruncate(dbItem.DriverName, dbItem.DbLink)
|
testTruncate(dbItem.DriverName, dbItem.DbLink)
|
||||||
testHelper(dbItem.DriverName, dbItem.DbLink)
|
testHelper(dbItem.DriverName, dbItem.DbLink)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -133,6 +134,11 @@ func testMysqlConnect() aorm.DbContent {
|
|||||||
panic(mysqlErr)
|
panic(mysqlErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err := mysqlContent.DbLink.Ping()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
return mysqlContent
|
return mysqlContent
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,7 +159,7 @@ func testPostgresConnect() aorm.DbContent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testMssqlConnect() aorm.DbContent {
|
func testMssqlConnect() aorm.DbContent {
|
||||||
info := fmt.Sprintf("server=%s;database=%s;user id=%s;password=%s;port=%d", "localhost", "database_name", "sa", "root", 1433)
|
info := fmt.Sprintf("server=%s;database=%s;user id=%s;password=%s;port=%d;encrypt=disable", "localhost", "database_name", "sa", "root", 1433)
|
||||||
mssqlContent, mssqlErr := aorm.Open("mssql", info)
|
mssqlContent, mssqlErr := aorm.Open("mssql", info)
|
||||||
if mssqlErr != nil {
|
if mssqlErr != nil {
|
||||||
panic(mssqlErr)
|
panic(mssqlErr)
|
||||||
@@ -167,20 +173,20 @@ func testMssqlConnect() aorm.DbContent {
|
|||||||
return mssqlContent
|
return mssqlContent
|
||||||
}
|
}
|
||||||
|
|
||||||
func testMigrate(name string, db *sql.DB) {
|
func testMigrate(driver string, db *sql.DB) {
|
||||||
//AutoMigrate
|
//AutoMigrate
|
||||||
aorm.Migrator(db).Driver(name).Opinion("ENGINE", "InnoDB").Opinion("COMMENT", "人员表").AutoMigrate(&Person{})
|
aorm.Migrator(db).Driver(driver).Opinion("ENGINE", "InnoDB").Opinion("COMMENT", "人员表").AutoMigrate(&Person{})
|
||||||
aorm.Migrator(db).Driver(name).Opinion("ENGINE", "InnoDB").Opinion("COMMENT", "文章").AutoMigrate(&Article{})
|
aorm.Migrator(db).Driver(driver).Opinion("ENGINE", "InnoDB").Opinion("COMMENT", "文章").AutoMigrate(&Article{})
|
||||||
|
|
||||||
//Migrate
|
//Migrate
|
||||||
aorm.Migrator(db).Driver(name).Opinion("ENGINE", "InnoDB").Opinion("COMMENT", "人员表").Migrate("person_1", &Person{})
|
aorm.Migrator(db).Driver(driver).Opinion("ENGINE", "InnoDB").Opinion("COMMENT", "人员表").Migrate("person_1", &Person{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func testShowCreateTable(name string, db *sql.DB) {
|
func testShowCreateTable(driver string, db *sql.DB) {
|
||||||
aorm.Migrator(db).Driver(name).ShowCreateTable("person")
|
aorm.Migrator(db).Driver(driver).ShowCreateTable("person")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testInsert(name string, db *sql.DB) int64 {
|
func testInsert(driver string, db *sql.DB) int64 {
|
||||||
obj := Person{
|
obj := Person{
|
||||||
Name: null.StringFrom("Alice"),
|
Name: null.StringFrom("Alice"),
|
||||||
Sex: null.BoolFrom(false),
|
Sex: null.BoolFrom(false),
|
||||||
@@ -191,50 +197,50 @@ func testInsert(name string, db *sql.DB) int64 {
|
|||||||
Test: null.FloatFrom(2),
|
Test: null.FloatFrom(2),
|
||||||
}
|
}
|
||||||
|
|
||||||
id, errInsert := aorm.Use(db).Debug(true).Driver(name).Insert(&obj)
|
id, errInsert := aorm.Use(db).Debug(false).Driver(driver).Insert(&obj)
|
||||||
if errInsert != nil {
|
if errInsert != nil {
|
||||||
panic(name + " testInsert " + "found err: " + errInsert.Error())
|
panic(driver + " testInsert " + "found err: " + errInsert.Error())
|
||||||
}
|
}
|
||||||
aorm.Use(db).Debug(false).Driver(name).Insert(&Article{
|
aorm.Use(db).Debug(false).Driver(driver).Insert(&Article{
|
||||||
Type: null.IntFrom(0),
|
Type: null.IntFrom(0),
|
||||||
PersonId: null.IntFrom(id),
|
PersonId: null.IntFrom(id),
|
||||||
ArticleBody: null.StringFrom("文章内容"),
|
ArticleBody: null.StringFrom("文章内容"),
|
||||||
})
|
})
|
||||||
|
|
||||||
var person Person
|
var person Person
|
||||||
err := aorm.Use(db).Table("person").Debug(true).Driver(name).WhereEq("id", id).OrderBy("id", "DESC").GetOne(&person)
|
err := aorm.Use(db).Table("person").Debug(false).Driver(driver).WhereEq("id", id).OrderBy("id", "DESC").GetOne(&person)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.Name.String != person.Name.String {
|
if obj.Name.String != person.Name.String {
|
||||||
fmt.Println("Name not match, expected: " + obj.Name.String + " ,but real is : " + person.Name.String)
|
fmt.Println(driver + ",Name not match, expected: " + obj.Name.String + " ,but real is : " + person.Name.String)
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.Sex.Bool != person.Sex.Bool {
|
if obj.Sex.Bool != person.Sex.Bool {
|
||||||
fmt.Println("Sex not match, expected: " + fmt.Sprintf("%v", obj.Sex.Bool) + " ,but real is : " + fmt.Sprintf("%v", person.Sex.Bool))
|
fmt.Println(driver + ",Sex not match, expected: " + fmt.Sprintf("%v", obj.Sex.Bool) + " ,but real is : " + fmt.Sprintf("%v", person.Sex.Bool))
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.Age.Int64 != person.Age.Int64 {
|
if obj.Age.Int64 != person.Age.Int64 {
|
||||||
fmt.Println("Age not match, expected: " + fmt.Sprintf("%v", obj.Age.Int64) + " ,but real is : " + fmt.Sprintf("%v", person.Age.Int64))
|
fmt.Println(driver + ",Age not match, expected: " + fmt.Sprintf("%v", obj.Age.Int64) + " ,but real is : " + fmt.Sprintf("%v", person.Age.Int64))
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.Type.Int64 != person.Type.Int64 {
|
if obj.Type.Int64 != person.Type.Int64 {
|
||||||
fmt.Println("Type not match, expected: " + fmt.Sprintf("%v", obj.Type.Int64) + " ,but real is : " + fmt.Sprintf("%v", person.Type.Int64))
|
fmt.Println(driver + ",Type not match, expected: " + fmt.Sprintf("%v", obj.Type.Int64) + " ,but real is : " + fmt.Sprintf("%v", person.Type.Int64))
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.Money.Float64 != person.Money.Float64 {
|
if obj.Money.Float64 != person.Money.Float64 {
|
||||||
fmt.Println(name + ",Money not match, expected: " + fmt.Sprintf("%v", obj.Money.Float64) + " ,but real is : " + fmt.Sprintf("%v", person.Money.Float64))
|
fmt.Println(driver + ",Money not match, expected: " + fmt.Sprintf("%v", obj.Money.Float64) + " ,but real is : " + fmt.Sprintf("%v", person.Money.Float64))
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.Test.Float64 != person.Test.Float64 {
|
if obj.Test.Float64 != person.Test.Float64 {
|
||||||
fmt.Println(name + ",Test not match, expected: " + fmt.Sprintf("%v", obj.Test.Float64) + " ,but real is : " + fmt.Sprintf("%v", person.Test.Float64))
|
fmt.Println(driver + ",Test not match, expected: " + fmt.Sprintf("%v", obj.Test.Float64) + " ,but real is : " + fmt.Sprintf("%v", person.Test.Float64))
|
||||||
}
|
}
|
||||||
|
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
func testInsertBatch(name string, db *sql.DB) int64 {
|
func testInsertBatch(driver string, db *sql.DB) int64 {
|
||||||
var batch []Person
|
var batch []Person
|
||||||
batch = append(batch, Person{
|
batch = append(batch, Person{
|
||||||
Name: null.StringFrom("Alice"),
|
Name: null.StringFrom("Alice"),
|
||||||
@@ -256,92 +262,92 @@ func testInsertBatch(name string, db *sql.DB) int64 {
|
|||||||
Test: null.FloatFrom(200.15987654321987654321),
|
Test: null.FloatFrom(200.15987654321987654321),
|
||||||
})
|
})
|
||||||
|
|
||||||
count, err := aorm.Use(db).Debug(true).Driver(name).InsertBatch(&batch)
|
count, err := aorm.Use(db).Debug(false).Driver(driver).InsertBatch(&batch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + " testInsertBatch " + "found err:" + err.Error())
|
panic(driver + " testInsertBatch " + "found err:" + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
func testGetOne(name string, db *sql.DB, id int64) {
|
func testGetOne(driver string, db *sql.DB, id int64) {
|
||||||
var person Person
|
var person Person
|
||||||
errFind := aorm.Use(db).Debug(false).Driver(name).OrderBy("id", "DESC").Where(&Person{Id: null.IntFrom(id)}).GetOne(&person)
|
errFind := aorm.Use(db).Debug(false).Driver(driver).OrderBy("id", "DESC").Where(&Person{Id: null.IntFrom(id)}).GetOne(&person)
|
||||||
if errFind != nil {
|
if errFind != nil {
|
||||||
panic(name + "testGetOne" + "found err")
|
panic(driver + "testGetOne" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testGetMany(name string, db *sql.DB) {
|
func testGetMany(driver string, db *sql.DB) {
|
||||||
var list []Person
|
var list []Person
|
||||||
errSelect := aorm.Use(db).Driver(name).Debug(false).Where(&Person{Type: null.IntFrom(0)}).GetMany(&list)
|
errSelect := aorm.Use(db).Driver(driver).Debug(false).Where(&Person{Type: null.IntFrom(0)}).GetMany(&list)
|
||||||
if errSelect != nil {
|
if errSelect != nil {
|
||||||
panic(name + " testGetMany " + "found err:" + errSelect.Error())
|
panic(driver + " testGetMany " + "found err:" + errSelect.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testUpdate(name string, db *sql.DB, id int64) {
|
func testUpdate(driver string, db *sql.DB, id int64) {
|
||||||
_, errUpdate := aorm.Use(db).Debug(false).Driver(name).Where(&Person{Id: null.IntFrom(id)}).Update(&Person{Name: null.StringFrom("Bob")})
|
_, errUpdate := aorm.Use(db).Debug(false).Driver(driver).Where(&Person{Id: null.IntFrom(id)}).Update(&Person{Name: null.StringFrom("Bob")})
|
||||||
if errUpdate != nil {
|
if errUpdate != nil {
|
||||||
panic(name + "testGetMany" + "found err")
|
panic(driver + "testGetMany" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testDelete(name string, db *sql.DB, id int64) {
|
func testDelete(driver string, db *sql.DB, id int64) {
|
||||||
_, errDelete := aorm.Use(db).Driver(name).Debug(false).Where(&Person{Id: null.IntFrom(id)}).Delete()
|
_, errDelete := aorm.Use(db).Driver(driver).Debug(false).Where(&Person{Id: null.IntFrom(id)}).Delete()
|
||||||
if errDelete != nil {
|
if errDelete != nil {
|
||||||
panic(name + "testDelete" + "found err")
|
panic(driver + "testDelete" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testTable(name string, db *sql.DB) {
|
func testTable(driver string, db *sql.DB) {
|
||||||
_, err := aorm.Use(db).Debug(false).Driver(name).Table("person_1").Insert(&Person{Name: null.StringFrom("Cherry")})
|
_, err := aorm.Use(db).Debug(false).Driver(driver).Table("person_1").Insert(&Person{Name: null.StringFrom("Cherry")})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + " testTable " + "found err:" + err.Error())
|
panic(driver + " testTable " + "found err:" + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSelect(name string, db *sql.DB) {
|
func testSelect(driver string, db *sql.DB) {
|
||||||
var listByFiled []Person
|
var listByFiled []Person
|
||||||
err := aorm.Use(db).Debug(false).Driver(name).Select("name,age").Where(&Person{Age: null.IntFrom(18)}).GetMany(&listByFiled)
|
err := aorm.Use(db).Debug(false).Driver(driver).Select("name,age").Where(&Person{Age: null.IntFrom(18)}).GetMany(&listByFiled)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + " testSelect " + "found err:" + err.Error())
|
panic(driver + " testSelect " + "found err:" + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSelectWithSub(name string, db *sql.DB) {
|
func testSelectWithSub(driver string, db *sql.DB) {
|
||||||
var listByFiled []PersonWithArticleCount
|
var listByFiled []PersonWithArticleCount
|
||||||
|
|
||||||
sub := aorm.Sub().Table("article").SelectCount("id", "article_count_tem").WhereRaw("person_id", "=person.id")
|
sub := aorm.Sub().Table("article").SelectCount("id", "article_count_tem").WhereRaw("person_id", "=person.id")
|
||||||
err := aorm.Use(db).Debug(false).
|
err := aorm.Use(db).Debug(false).
|
||||||
Driver(name).
|
Driver(driver).
|
||||||
SelectExp(&sub, "article_count").
|
SelectExp(&sub, "article_count").
|
||||||
Select("*").
|
Select("*").
|
||||||
Where(&Person{Age: null.IntFrom(18)}).
|
Where(&Person{Age: null.IntFrom(18)}).
|
||||||
GetMany(&listByFiled)
|
GetMany(&listByFiled)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + " testSelectWithSub " + "found err:" + err.Error())
|
panic(driver + " testSelectWithSub " + "found err:" + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testWhereWithSub(name string, db *sql.DB) {
|
func testWhereWithSub(driver string, db *sql.DB) {
|
||||||
var listByFiled []Person
|
var listByFiled []Person
|
||||||
|
|
||||||
sub := aorm.Sub().Table("article").Select("person_id").GroupBy("person_id").HavingGt("count(person_id)", 0)
|
sub := aorm.Sub().Table("article").Select("person_id").GroupBy("person_id").HavingGt("count(person_id)", 0)
|
||||||
|
|
||||||
err := aorm.Use(db).Debug(false).
|
err := aorm.Use(db).Debug(false).
|
||||||
Table("person").
|
Table("person").
|
||||||
Driver(name).
|
Driver(driver).
|
||||||
WhereIn("id", &sub).
|
WhereIn("id", &sub).
|
||||||
GetMany(&listByFiled)
|
GetMany(&listByFiled)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + " testWhereWithSub " + "found err:" + err.Error())
|
panic(driver + " testWhereWithSub " + "found err:" + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testWhere(name string, db *sql.DB) {
|
func testWhere(driver string, db *sql.DB) {
|
||||||
var listByWhere []Person
|
var listByWhere []Person
|
||||||
|
|
||||||
var where1 []builder.WhereItem
|
var where1 []builder.WhereItem
|
||||||
@@ -351,13 +357,13 @@ func testWhere(name string, db *sql.DB) {
|
|||||||
where1 = append(where1, builder.WhereItem{Field: "money", Opt: builder.Eq, Val: 100.15})
|
where1 = append(where1, builder.WhereItem{Field: "money", Opt: builder.Eq, Val: 100.15})
|
||||||
where1 = append(where1, builder.WhereItem{Field: "name", Opt: builder.Like, Val: []string{"%", "li", "%"}})
|
where1 = append(where1, builder.WhereItem{Field: "name", Opt: builder.Like, Val: []string{"%", "li", "%"}})
|
||||||
|
|
||||||
err := aorm.Use(db).Debug(true).Driver(name).Table("person").WhereArr(where1).GetMany(&listByWhere)
|
err := aorm.Use(db).Debug(false).Driver(driver).Table("person").WhereArr(where1).GetMany(&listByWhere)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + "testWhere" + "found err")
|
panic(driver + "testWhere" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testJoin(name string, db *sql.DB) {
|
func testJoin(driver string, db *sql.DB) {
|
||||||
var list2 []ArticleVO
|
var list2 []ArticleVO
|
||||||
var where2 []builder.WhereItem
|
var where2 []builder.WhereItem
|
||||||
where2 = append(where2, builder.WhereItem{Field: "o.type", Opt: builder.Eq, Val: 0})
|
where2 = append(where2, builder.WhereItem{Field: "o.type", Opt: builder.Eq, Val: 0})
|
||||||
@@ -368,13 +374,14 @@ func testJoin(name string, db *sql.DB) {
|
|||||||
Select("o.*").
|
Select("o.*").
|
||||||
Select("p.name as person_name").
|
Select("p.name as person_name").
|
||||||
WhereArr(where2).
|
WhereArr(where2).
|
||||||
|
Driver(driver).
|
||||||
GetMany(&list2)
|
GetMany(&list2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + " testWhere " + "found err " + err.Error())
|
panic(driver + " testWhere " + "found err " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testGroupBy(name string, db *sql.DB) {
|
func testGroupBy(driver string, db *sql.DB) {
|
||||||
var personAge PersonAge
|
var personAge PersonAge
|
||||||
var where []builder.WhereItem
|
var where []builder.WhereItem
|
||||||
where = append(where, builder.WhereItem{Field: "type", Opt: builder.Eq, Val: 0})
|
where = append(where, builder.WhereItem{Field: "type", Opt: builder.Eq, Val: 0})
|
||||||
@@ -384,15 +391,15 @@ func testGroupBy(name string, db *sql.DB) {
|
|||||||
Select("count(age) as age_count").
|
Select("count(age) as age_count").
|
||||||
GroupBy("age").
|
GroupBy("age").
|
||||||
WhereArr(where).
|
WhereArr(where).
|
||||||
Driver(name).
|
Driver(driver).
|
||||||
OrderBy("age", "DESC").
|
OrderBy("age", "DESC").
|
||||||
GetOne(&personAge)
|
GetOne(&personAge)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + "testGroupBy" + "found err")
|
panic(driver + "testGroupBy" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testHaving(name string, db *sql.DB) {
|
func testHaving(driver string, db *sql.DB) {
|
||||||
var listByHaving []PersonAge
|
var listByHaving []PersonAge
|
||||||
|
|
||||||
var where3 []builder.WhereItem
|
var where3 []builder.WhereItem
|
||||||
@@ -407,16 +414,16 @@ func testHaving(name string, db *sql.DB) {
|
|||||||
Select("count(age) as age_count").
|
Select("count(age) as age_count").
|
||||||
GroupBy("age").
|
GroupBy("age").
|
||||||
WhereArr(where3).
|
WhereArr(where3).
|
||||||
Driver(name).
|
Driver(driver).
|
||||||
OrderBy("age", "DESC").
|
OrderBy("age", "DESC").
|
||||||
HavingArr(having).
|
HavingArr(having).
|
||||||
GetMany(&listByHaving)
|
GetMany(&listByHaving)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + " testHaving " + "found err")
|
panic(driver + " testHaving " + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testOrderBy(name string, db *sql.DB) {
|
func testOrderBy(driver string, db *sql.DB) {
|
||||||
var listByOrder []Person
|
var listByOrder []Person
|
||||||
var where []builder.WhereItem
|
var where []builder.WhereItem
|
||||||
where = append(where, builder.WhereItem{Field: "type", Opt: builder.Eq, Val: 0})
|
where = append(where, builder.WhereItem{Field: "type", Opt: builder.Eq, Val: 0})
|
||||||
@@ -424,13 +431,14 @@ func testOrderBy(name string, db *sql.DB) {
|
|||||||
Table("person").
|
Table("person").
|
||||||
WhereArr(where).
|
WhereArr(where).
|
||||||
OrderBy("age", builder.Desc).
|
OrderBy("age", builder.Desc).
|
||||||
|
Driver(driver).
|
||||||
GetMany(&listByOrder)
|
GetMany(&listByOrder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + "testOrderBy" + "found err")
|
panic(driver + "testOrderBy" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testLimit(name string, db *sql.DB) {
|
func testLimit(driver string, db *sql.DB) {
|
||||||
var list3 []Person
|
var list3 []Person
|
||||||
var where1 []builder.WhereItem
|
var where1 []builder.WhereItem
|
||||||
where1 = append(where1, builder.WhereItem{Field: "type", Opt: builder.Eq, Val: 0})
|
where1 = append(where1, builder.WhereItem{Field: "type", Opt: builder.Eq, Val: 0})
|
||||||
@@ -438,11 +446,11 @@ func testLimit(name string, db *sql.DB) {
|
|||||||
Table("person").
|
Table("person").
|
||||||
WhereArr(where1).
|
WhereArr(where1).
|
||||||
Limit(50, 10).
|
Limit(50, 10).
|
||||||
Driver(name).
|
Driver(driver).
|
||||||
OrderBy("id", "DESC").
|
OrderBy("id", "DESC").
|
||||||
GetMany(&list3)
|
GetMany(&list3)
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
panic(name + "testLimit" + "found err")
|
panic(driver + "testLimit" + "found err")
|
||||||
}
|
}
|
||||||
|
|
||||||
var list4 []Person
|
var list4 []Person
|
||||||
@@ -452,16 +460,16 @@ func testLimit(name string, db *sql.DB) {
|
|||||||
Table("person").
|
Table("person").
|
||||||
WhereArr(where2).
|
WhereArr(where2).
|
||||||
Page(3, 10).
|
Page(3, 10).
|
||||||
Driver(name).
|
Driver(driver).
|
||||||
OrderBy("id", "DESC").
|
OrderBy("id", "DESC").
|
||||||
GetMany(&list4)
|
GetMany(&list4)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + "testPage" + "found err")
|
panic(driver + "testPage" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testLock(name string, db *sql.DB, id int64) {
|
func testLock(driver string, db *sql.DB, id int64) {
|
||||||
if name == "sqlite3" || name == "mssql" {
|
if driver == model.Sqlite3 || driver == model.Mssql {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -470,25 +478,25 @@ func testLock(name string, db *sql.DB, id int64) {
|
|||||||
Debug(false).
|
Debug(false).
|
||||||
LockForUpdate(true).
|
LockForUpdate(true).
|
||||||
Where(&Person{Id: null.IntFrom(id)}).
|
Where(&Person{Id: null.IntFrom(id)}).
|
||||||
Driver(name).
|
Driver(driver).
|
||||||
OrderBy("id", "DESC").
|
OrderBy("id", "DESC").
|
||||||
GetOne(&itemByLock)
|
GetOne(&itemByLock)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + "testLock" + "found err")
|
panic(driver + "testLock" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testIncrement(name string, db *sql.DB, id int64) {
|
func testIncrement(driver string, db *sql.DB, id int64) {
|
||||||
_, err := aorm.Use(db).Debug(false).Where(&Person{Id: null.IntFrom(id)}).Increment("age", 1)
|
_, err := aorm.Use(db).Debug(false).Where(&Person{Id: null.IntFrom(id)}).Driver(driver).Increment("age", 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + "testIncrement" + "found err")
|
panic(driver + " testIncrement " + "found err:" + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testDecrement(name string, db *sql.DB, id int64) {
|
func testDecrement(driver string, db *sql.DB, id int64) {
|
||||||
_, err := aorm.Use(db).Debug(false).Where(&Person{Id: null.IntFrom(id)}).Decrement("age", 2)
|
_, err := aorm.Use(db).Debug(false).Where(&Person{Id: null.IntFrom(id)}).Driver(driver).Decrement("age", 2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + "testDecrement" + "found err")
|
panic(driver + "testDecrement" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -519,108 +527,108 @@ func testValue(driver string, db *sql.DB, id int64) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testPluck(name string, db *sql.DB) {
|
func testPluck(driver string, db *sql.DB) {
|
||||||
|
|
||||||
var nameList []string
|
var nameList []string
|
||||||
errNameList := aorm.Use(db).Debug(false).Driver(name).OrderBy("id", "DESC").Where(&Person{Type: null.IntFrom(0)}).Limit(0, 3).Pluck("name", &nameList)
|
errNameList := aorm.Use(db).Debug(false).Driver(driver).OrderBy("id", "DESC").Where(&Person{Type: null.IntFrom(0)}).Limit(0, 3).Pluck("name", &nameList)
|
||||||
if errNameList != nil {
|
if errNameList != nil {
|
||||||
panic(name + "testPluck" + "found err")
|
panic(driver + "testPluck" + "found err")
|
||||||
}
|
}
|
||||||
|
|
||||||
var ageList []int64
|
var ageList []int64
|
||||||
errAgeList := aorm.Use(db).Debug(false).Driver(name).OrderBy("id", "DESC").Where(&Person{Type: null.IntFrom(0)}).Limit(0, 3).Pluck("age", &ageList)
|
errAgeList := aorm.Use(db).Debug(false).Driver(driver).OrderBy("id", "DESC").Where(&Person{Type: null.IntFrom(0)}).Limit(0, 3).Pluck("age", &ageList)
|
||||||
if errAgeList != nil {
|
if errAgeList != nil {
|
||||||
panic(name + "testPluck" + "found err:" + errAgeList.Error())
|
panic(driver + "testPluck" + "found err:" + errAgeList.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
var moneyList []float32
|
var moneyList []float32
|
||||||
errMoneyList := aorm.Use(db).Debug(false).Driver(name).OrderBy("id", "DESC").Where(&Person{Type: null.IntFrom(0)}).Limit(0, 3).Pluck("money", &moneyList)
|
errMoneyList := aorm.Use(db).Debug(false).Driver(driver).OrderBy("id", "DESC").Where(&Person{Type: null.IntFrom(0)}).Limit(0, 3).Pluck("money", &moneyList)
|
||||||
if errMoneyList != nil {
|
if errMoneyList != nil {
|
||||||
panic(name + "testPluck" + "found err")
|
panic(driver + "testPluck" + "found err")
|
||||||
}
|
}
|
||||||
|
|
||||||
var testList []float64
|
var testList []float64
|
||||||
errTestList := aorm.Use(db).Debug(false).Driver(name).OrderBy("id", "DESC").Where(&Person{Type: null.IntFrom(0)}).Limit(0, 3).Pluck("test", &testList)
|
errTestList := aorm.Use(db).Debug(false).Driver(driver).OrderBy("id", "DESC").Where(&Person{Type: null.IntFrom(0)}).Limit(0, 3).Pluck("test", &testList)
|
||||||
if errTestList != nil {
|
if errTestList != nil {
|
||||||
panic(name + "testPluck" + "found err")
|
panic(driver + "testPluck" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testCount(name string, db *sql.DB) {
|
func testCount(driver string, db *sql.DB) {
|
||||||
_, err := aorm.Use(db).Debug(false).Where(&Person{Age: null.IntFrom(18)}).Count("*")
|
_, err := aorm.Use(db).Debug(false).Where(&Person{Age: null.IntFrom(18)}).Driver(driver).Count("*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + "testCount" + "found err")
|
panic(driver + "testCount" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSum(name string, db *sql.DB) {
|
func testSum(driver string, db *sql.DB) {
|
||||||
_, err := aorm.Use(db).Debug(false).Where(&Person{Age: null.IntFrom(18)}).Sum("age")
|
_, err := aorm.Use(db).Debug(false).Where(&Person{Age: null.IntFrom(18)}).Driver(driver).Sum("age")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + "testSum" + "found err")
|
panic(driver + "testSum" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAvg(name string, db *sql.DB) {
|
func testAvg(driver string, db *sql.DB) {
|
||||||
_, err := aorm.Use(db).Debug(false).Where(&Person{Age: null.IntFrom(18)}).Avg("age")
|
_, err := aorm.Use(db).Debug(false).Where(&Person{Age: null.IntFrom(18)}).Driver(driver).Avg("age")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + "testAvg" + "found err")
|
panic(driver + "testAvg" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testMin(name string, db *sql.DB) {
|
func testMin(driver string, db *sql.DB) {
|
||||||
_, err := aorm.Use(db).Debug(false).Where(&Person{Age: null.IntFrom(18)}).Min("age")
|
_, err := aorm.Use(db).Debug(false).Where(&Person{Age: null.IntFrom(18)}).Driver(driver).Min("age")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + "testMin" + "found err")
|
panic(driver + "testMin" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testMax(name string, db *sql.DB) {
|
func testMax(driver string, db *sql.DB) {
|
||||||
_, err := aorm.Use(db).Debug(false).Where(&Person{Age: null.IntFrom(18)}).Max("age")
|
_, err := aorm.Use(db).Debug(false).Where(&Person{Age: null.IntFrom(18)}).Driver(driver).Max("age")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + "testMax" + "found err")
|
panic(driver + "testMax" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testExec(name string, db *sql.DB) {
|
func testExec(driver string, db *sql.DB) {
|
||||||
_, err := aorm.Use(db).Debug(false).Exec("UPDATE person SET name = ? WHERE id=?", "Bob", 3)
|
_, err := aorm.Use(db).Debug(false).Driver(driver).Exec("UPDATE person SET name = ? WHERE id=?", "Bob", 3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + "testExec" + "found err")
|
panic(driver + "testExec" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testTransaction(name string, db *sql.DB) {
|
func testTransaction(driver string, db *sql.DB) {
|
||||||
tx, _ := db.Begin()
|
tx, _ := db.Begin()
|
||||||
|
|
||||||
id, errInsert := aorm.Use(tx).Debug(false).Driver(name).Insert(&Person{
|
id, errInsert := aorm.Use(tx).Debug(false).Driver(driver).Insert(&Person{
|
||||||
Name: null.StringFrom("Alice"),
|
Name: null.StringFrom("Alice"),
|
||||||
})
|
})
|
||||||
|
|
||||||
if errInsert != nil {
|
if errInsert != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
panic(name + " testTransaction " + "found err:" + errInsert.Error())
|
panic(driver + " testTransaction " + "found err:" + errInsert.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, errCount := aorm.Use(tx).Debug(false).Where(&Person{
|
_, errCount := aorm.Use(tx).Debug(false).Driver(driver).Where(&Person{
|
||||||
Id: null.IntFrom(id),
|
Id: null.IntFrom(id),
|
||||||
}).Count("*")
|
}).Count("*")
|
||||||
if errCount != nil {
|
if errCount != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
panic(name + "testTransaction" + "found err")
|
panic(driver + "testTransaction" + "found err")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var person Person
|
var person Person
|
||||||
errPerson := aorm.Use(tx).Debug(false).Where(&Person{
|
errPerson := aorm.Use(tx).Debug(false).Where(&Person{
|
||||||
Id: null.IntFrom(id),
|
Id: null.IntFrom(id),
|
||||||
}).Driver(name).OrderBy("id", "DESC").GetOne(&person)
|
}).Driver(driver).OrderBy("id", "DESC").GetOne(&person)
|
||||||
if errPerson != nil {
|
if errPerson != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
panic(name + "testTransaction" + "found err")
|
panic(driver + "testTransaction" + "found err")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, errUpdate := aorm.Use(tx).Debug(false).Where(&Person{
|
_, errUpdate := aorm.Use(tx).Debug(false).Driver(driver).Where(&Person{
|
||||||
Id: null.IntFrom(id),
|
Id: null.IntFrom(id),
|
||||||
}).Update(&Person{
|
}).Update(&Person{
|
||||||
Name: null.StringFrom("Bob"),
|
Name: null.StringFrom("Bob"),
|
||||||
@@ -628,21 +636,21 @@ func testTransaction(name string, db *sql.DB) {
|
|||||||
|
|
||||||
if errUpdate != nil {
|
if errUpdate != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
panic(name + "testTransaction" + "found err")
|
panic(driver + "testTransaction" + "found err")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func testTruncate(name string, db *sql.DB) {
|
func testTruncate(driver string, db *sql.DB) {
|
||||||
_, err := aorm.Use(db).Debug(false).Driver(name).Table("person").Truncate()
|
_, err := aorm.Use(db).Debug(false).Driver(driver).Table("person").Truncate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + " testTruncate " + "found err")
|
panic(driver + " testTruncate " + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testHelper(name string, db *sql.DB) {
|
func testHelper(driver string, db *sql.DB) {
|
||||||
var list2 []ArticleVO
|
var list2 []ArticleVO
|
||||||
var where2 []builder.WhereItem
|
var where2 []builder.WhereItem
|
||||||
where2 = append(where2, builder.WhereItem{Field: "o.type", Opt: builder.Eq, Val: 0})
|
where2 = append(where2, builder.WhereItem{Field: "o.type", Opt: builder.Eq, Val: 0})
|
||||||
@@ -653,8 +661,9 @@ func testHelper(name string, db *sql.DB) {
|
|||||||
Select("o.*").
|
Select("o.*").
|
||||||
Select(helper.Ul("p.name as personName")).
|
Select(helper.Ul("p.name as personName")).
|
||||||
WhereArr(where2).
|
WhereArr(where2).
|
||||||
|
Driver(driver).
|
||||||
GetMany(&list2)
|
GetMany(&list2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(name + "testHelper" + "found err")
|
panic(driver + "testHelper" + "found err")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user