mirror of https://github.com/gogs/gogs.git
vendor: update github.com/go-xorm/* (#4419)
parent
c210984b40
commit
b40dc550ed
2
gogs.go
2
gogs.go
|
@ -16,7 +16,7 @@ import (
|
|||
"github.com/gogits/gogs/pkg/setting"
|
||||
)
|
||||
|
||||
const APP_VER = "0.11.17.0606"
|
||||
const APP_VER = "0.11.17.0607"
|
||||
|
||||
func init() {
|
||||
setting.AppVer = APP_VER
|
||||
|
|
|
@ -1 +1 @@
|
|||
0.11.17.0606
|
||||
0.11.17.0607
|
|
@ -22,16 +22,21 @@ func In(col string, values ...interface{}) Cond {
|
|||
return condIn{col, values}
|
||||
}
|
||||
|
||||
func (condIn condIn) handleBlank(w Writer) error {
|
||||
_, err := fmt.Fprint(w, "0=1")
|
||||
return err
|
||||
}
|
||||
|
||||
func (condIn condIn) WriteTo(w Writer) error {
|
||||
if len(condIn.vals) <= 0 {
|
||||
return ErrNoInConditions
|
||||
return condIn.handleBlank(w)
|
||||
}
|
||||
|
||||
switch condIn.vals[0].(type) {
|
||||
case []int8:
|
||||
vals := condIn.vals[0].([]int8)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoInConditions
|
||||
return condIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -43,7 +48,7 @@ func (condIn condIn) WriteTo(w Writer) error {
|
|||
case []int16:
|
||||
vals := condIn.vals[0].([]int16)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoInConditions
|
||||
return condIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -55,7 +60,7 @@ func (condIn condIn) WriteTo(w Writer) error {
|
|||
case []int:
|
||||
vals := condIn.vals[0].([]int)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoInConditions
|
||||
return condIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -67,7 +72,7 @@ func (condIn condIn) WriteTo(w Writer) error {
|
|||
case []int32:
|
||||
vals := condIn.vals[0].([]int32)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoInConditions
|
||||
return condIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -79,7 +84,7 @@ func (condIn condIn) WriteTo(w Writer) error {
|
|||
case []int64:
|
||||
vals := condIn.vals[0].([]int64)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoInConditions
|
||||
return condIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -91,7 +96,7 @@ func (condIn condIn) WriteTo(w Writer) error {
|
|||
case []uint8:
|
||||
vals := condIn.vals[0].([]uint8)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoInConditions
|
||||
return condIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -103,7 +108,7 @@ func (condIn condIn) WriteTo(w Writer) error {
|
|||
case []uint16:
|
||||
vals := condIn.vals[0].([]uint16)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoInConditions
|
||||
return condIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -115,7 +120,7 @@ func (condIn condIn) WriteTo(w Writer) error {
|
|||
case []uint:
|
||||
vals := condIn.vals[0].([]uint)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoInConditions
|
||||
return condIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -127,7 +132,7 @@ func (condIn condIn) WriteTo(w Writer) error {
|
|||
case []uint32:
|
||||
vals := condIn.vals[0].([]uint32)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoInConditions
|
||||
return condIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -139,7 +144,7 @@ func (condIn condIn) WriteTo(w Writer) error {
|
|||
case []uint64:
|
||||
vals := condIn.vals[0].([]uint64)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoInConditions
|
||||
return condIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -151,7 +156,7 @@ func (condIn condIn) WriteTo(w Writer) error {
|
|||
case []string:
|
||||
vals := condIn.vals[0].([]string)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoInConditions
|
||||
return condIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -163,7 +168,7 @@ func (condIn condIn) WriteTo(w Writer) error {
|
|||
case []interface{}:
|
||||
vals := condIn.vals[0].([]interface{})
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoInConditions
|
||||
return condIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -193,13 +198,12 @@ func (condIn condIn) WriteTo(w Writer) error {
|
|||
return err
|
||||
}
|
||||
default:
|
||||
if len(condIn.vals) <= 0 {
|
||||
return ErrNoInConditions
|
||||
}
|
||||
|
||||
v := reflect.ValueOf(condIn.vals[0])
|
||||
if v.Kind() == reflect.Slice {
|
||||
l := v.Len()
|
||||
if l == 0 {
|
||||
return condIn.handleBlank(w)
|
||||
}
|
||||
|
||||
questionMark := strings.Repeat("?,", l)
|
||||
if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
|
|
@ -16,7 +16,12 @@ func (like Like) WriteTo(w Writer) error {
|
|||
if _, err := fmt.Fprintf(w, "%s LIKE ?", like[0]); err != nil {
|
||||
return err
|
||||
}
|
||||
// FIXME: if use other regular express, this will be failed. but for compitable, keep this
|
||||
if like[1][0] == '%' || like[1][len(like[1])-1] == '%' {
|
||||
w.Append(like[1])
|
||||
} else {
|
||||
w.Append("%" + like[1] + "%")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ package builder
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
@ -18,16 +19,21 @@ func NotIn(col string, values ...interface{}) Cond {
|
|||
return condNotIn{col, values}
|
||||
}
|
||||
|
||||
func (condNotIn condNotIn) handleBlank(w Writer) error {
|
||||
_, err := fmt.Fprint(w, "0=0")
|
||||
return err
|
||||
}
|
||||
|
||||
func (condNotIn condNotIn) WriteTo(w Writer) error {
|
||||
if len(condNotIn.vals) <= 0 {
|
||||
return ErrNoNotInConditions
|
||||
return condNotIn.handleBlank(w)
|
||||
}
|
||||
|
||||
switch condNotIn.vals[0].(type) {
|
||||
case []int8:
|
||||
vals := condNotIn.vals[0].([]int8)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoNotInConditions
|
||||
return condNotIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -39,7 +45,7 @@ func (condNotIn condNotIn) WriteTo(w Writer) error {
|
|||
case []int16:
|
||||
vals := condNotIn.vals[0].([]int16)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoNotInConditions
|
||||
return condNotIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -51,7 +57,7 @@ func (condNotIn condNotIn) WriteTo(w Writer) error {
|
|||
case []int:
|
||||
vals := condNotIn.vals[0].([]int)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoNotInConditions
|
||||
return condNotIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -63,7 +69,7 @@ func (condNotIn condNotIn) WriteTo(w Writer) error {
|
|||
case []int32:
|
||||
vals := condNotIn.vals[0].([]int32)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoNotInConditions
|
||||
return condNotIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -75,7 +81,7 @@ func (condNotIn condNotIn) WriteTo(w Writer) error {
|
|||
case []int64:
|
||||
vals := condNotIn.vals[0].([]int64)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoNotInConditions
|
||||
return condNotIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -87,7 +93,7 @@ func (condNotIn condNotIn) WriteTo(w Writer) error {
|
|||
case []uint8:
|
||||
vals := condNotIn.vals[0].([]uint8)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoNotInConditions
|
||||
return condNotIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -99,7 +105,7 @@ func (condNotIn condNotIn) WriteTo(w Writer) error {
|
|||
case []uint16:
|
||||
vals := condNotIn.vals[0].([]uint16)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoNotInConditions
|
||||
return condNotIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -111,7 +117,7 @@ func (condNotIn condNotIn) WriteTo(w Writer) error {
|
|||
case []uint:
|
||||
vals := condNotIn.vals[0].([]uint)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoNotInConditions
|
||||
return condNotIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -123,7 +129,7 @@ func (condNotIn condNotIn) WriteTo(w Writer) error {
|
|||
case []uint32:
|
||||
vals := condNotIn.vals[0].([]uint32)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoNotInConditions
|
||||
return condNotIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -135,7 +141,7 @@ func (condNotIn condNotIn) WriteTo(w Writer) error {
|
|||
case []uint64:
|
||||
vals := condNotIn.vals[0].([]uint64)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoNotInConditions
|
||||
return condNotIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -147,7 +153,7 @@ func (condNotIn condNotIn) WriteTo(w Writer) error {
|
|||
case []string:
|
||||
vals := condNotIn.vals[0].([]string)
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoNotInConditions
|
||||
return condNotIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -159,7 +165,7 @@ func (condNotIn condNotIn) WriteTo(w Writer) error {
|
|||
case []interface{}:
|
||||
vals := condNotIn.vals[0].([]interface{})
|
||||
if len(vals) <= 0 {
|
||||
return ErrNoNotInConditions
|
||||
return condNotIn.handleBlank(w)
|
||||
}
|
||||
questionMark := strings.Repeat("?,", len(vals))
|
||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
|
@ -189,12 +195,29 @@ func (condNotIn condNotIn) WriteTo(w Writer) error {
|
|||
return err
|
||||
}
|
||||
default:
|
||||
v := reflect.ValueOf(condNotIn.vals[0])
|
||||
if v.Kind() == reflect.Slice {
|
||||
l := v.Len()
|
||||
if l == 0 {
|
||||
return condNotIn.handleBlank(w)
|
||||
}
|
||||
|
||||
questionMark := strings.Repeat("?,", l)
|
||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := 0; i < l; i++ {
|
||||
w.Append(v.Index(i).Interface())
|
||||
}
|
||||
} else {
|
||||
questionMark := strings.Repeat("?,", len(condNotIn.vals))
|
||||
if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
|
||||
return err
|
||||
}
|
||||
w.Append(condNotIn.vals...)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -37,6 +37,7 @@ type Column struct {
|
|||
SetOptions map[string]int
|
||||
DisableTimeZone bool
|
||||
TimeZone *time.Location // column specified time zone
|
||||
Comment string
|
||||
}
|
||||
|
||||
func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column {
|
||||
|
@ -60,6 +61,7 @@ func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable
|
|||
IsVersion: false,
|
||||
DefaultIsEmpty: false,
|
||||
EnumOptions: make(map[string]int),
|
||||
Comment: "",
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -279,9 +279,9 @@ func (b *Base) ForUpdateSql(query string) string {
|
|||
func (b *Base) LogSQL(sql string, args []interface{}) {
|
||||
if b.logger != nil && b.logger.IsShowSQL() {
|
||||
if len(args) > 0 {
|
||||
b.logger.Info("[sql]", sql, args)
|
||||
b.logger.Infof("[SQL] %v %v", sql, args)
|
||||
} else {
|
||||
b.logger.Info("[sql]", sql)
|
||||
b.logger.Infof("[SQL] %v", sql)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ type Table struct {
|
|||
Cacher Cacher
|
||||
StoreEngine string
|
||||
Charset string
|
||||
Comment string
|
||||
}
|
||||
|
||||
func (table *Table) Columns() []*Column {
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
|
||||
Xorm is a simple and powerful ORM for Go.
|
||||
|
||||
[](https://circleci.com/gh/go-xorm/xorm/tree/master) [](https://gitter.im/go-xorm/xorm?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
||||
[](https://circleci.com/gh/go-xorm/xorm) [](https://codecov.io/gh/go-xorm/xorm)
|
||||
[](https://goreportcard.com/report/github.com/go-xorm/xorm) [](https://gitter.im/go-xorm/xorm?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
||||
|
||||
# Notice
|
||||
|
||||
|
|
|
@ -4,7 +4,8 @@
|
|||
|
||||
xorm是一个简单而强大的Go语言ORM库. 通过它可以使数据库操作非常简便。
|
||||
|
||||
[](https://circleci.com/gh/go-xorm/xorm/tree/master) [](https://gitter.im/go-xorm/xorm?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
||||
[](https://circleci.com/gh/go-xorm/xorm) [](https://codecov.io/gh/go-xorm/xorm)
|
||||
[](https://goreportcard.com/report/github.com/go-xorm/xorm) [](https://gitter.im/go-xorm/xorm?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
||||
|
||||
# 注意
|
||||
|
||||
|
|
|
@ -3,6 +3,8 @@ dependencies:
|
|||
# './...' is a relative pattern which means all subdirectories
|
||||
- go get -t -d -v ./...
|
||||
- go get -t -d -v github.com/go-xorm/tests
|
||||
- go get -u github.com/go-xorm/core
|
||||
- go get -u github.com/go-xorm/builder
|
||||
- go build -v
|
||||
|
||||
database:
|
||||
|
@ -19,7 +21,9 @@ database:
|
|||
test:
|
||||
override:
|
||||
# './...' is a relative pattern which means all subdirectories
|
||||
- go test -v -race
|
||||
- go test -v -race -db="sqlite3::mysql::postgres" -conn_str="./test.db::root:@/xorm_test::dbname=xorm_test sslmode=disable" -coverprofile=coverage.txt -covermode=atomic
|
||||
- cd /home/ubuntu/.go_workspace/src/github.com/go-xorm/tests && ./sqlite3.sh
|
||||
- cd /home/ubuntu/.go_workspace/src/github.com/go-xorm/tests && ./mysql.sh
|
||||
- cd /home/ubuntu/.go_workspace/src/github.com/go-xorm/tests && ./postgres.sh
|
||||
post:
|
||||
- bash <(curl -s https://codecov.io/bash)
|
|
@ -215,7 +215,7 @@ func (db *mssql) SqlType(c *core.Column) string {
|
|||
var res string
|
||||
switch t := c.SQLType.Name; t {
|
||||
case core.Bool:
|
||||
res = core.TinyInt
|
||||
res = core.Bit
|
||||
if strings.EqualFold(c.Default, "true") {
|
||||
c.Default = "1"
|
||||
} else {
|
||||
|
@ -250,6 +250,9 @@ func (db *mssql) SqlType(c *core.Column) string {
|
|||
case core.Uuid:
|
||||
res = core.Varchar
|
||||
c.Length = 40
|
||||
case core.TinyInt:
|
||||
res = core.TinyInt
|
||||
c.Length = 0
|
||||
default:
|
||||
res = t
|
||||
}
|
||||
|
@ -335,9 +338,15 @@ func (db *mssql) TableCheckSql(tableName string) (string, []interface{}) {
|
|||
func (db *mssql) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
|
||||
args := []interface{}{}
|
||||
s := `select a.name as name, b.name as ctype,a.max_length,a.precision,a.scale,a.is_nullable as nullable,
|
||||
replace(replace(isnull(c.text,''),'(',''),')','') as vdefault
|
||||
from sys.columns a left join sys.types b on a.user_type_id=b.user_type_id
|
||||
replace(replace(isnull(c.text,''),'(',''),')','') as vdefault,
|
||||
ISNULL(i.is_primary_key, 0)
|
||||
from sys.columns a
|
||||
left join sys.types b on a.user_type_id=b.user_type_id
|
||||
left join sys.syscomments c on a.default_object_id=c.id
|
||||
LEFT OUTER JOIN
|
||||
sys.index_columns ic ON ic.object_id = a.object_id AND ic.column_id = a.column_id
|
||||
LEFT OUTER JOIN
|
||||
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
|
||||
where a.object_id=object_id('` + tableName + `')`
|
||||
db.LogSQL(s, args)
|
||||
|
||||
|
@ -352,8 +361,8 @@ func (db *mssql) GetColumns(tableName string) ([]string, map[string]*core.Column
|
|||
for rows.Next() {
|
||||
var name, ctype, vdefault string
|
||||
var maxLen, precision, scale int
|
||||
var nullable bool
|
||||
err = rows.Scan(&name, &ctype, &maxLen, &precision, &scale, &nullable, &vdefault)
|
||||
var nullable, isPK bool
|
||||
err = rows.Scan(&name, &ctype, &maxLen, &precision, &scale, &nullable, &vdefault, &isPK)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -363,6 +372,7 @@ func (db *mssql) GetColumns(tableName string) ([]string, map[string]*core.Column
|
|||
col.Name = strings.Trim(name, "` ")
|
||||
col.Nullable = nullable
|
||||
col.Default = vdefault
|
||||
col.IsPrimaryKey = isPK
|
||||
ct := strings.ToUpper(ctype)
|
||||
if ct == "DECIMAL" {
|
||||
col.Length = precision
|
||||
|
@ -536,7 +546,6 @@ type odbcDriver struct {
|
|||
func (p *odbcDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
|
||||
kv := strings.Split(dataSourceName, ";")
|
||||
var dbName string
|
||||
|
||||
for _, c := range kv {
|
||||
vv := strings.Split(strings.TrimSpace(c), "=")
|
||||
if len(vv) == 2 {
|
||||
|
|
|
@ -299,7 +299,7 @@ func (db *mysql) TableCheckSql(tableName string) (string, []interface{}) {
|
|||
func (db *mysql) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
|
||||
args := []interface{}{db.DbName, tableName}
|
||||
s := "SELECT `COLUMN_NAME`, `IS_NULLABLE`, `COLUMN_DEFAULT`, `COLUMN_TYPE`," +
|
||||
" `COLUMN_KEY`, `EXTRA` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"
|
||||
" `COLUMN_KEY`, `EXTRA`,`COLUMN_COMMENT` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"
|
||||
db.LogSQL(s, args)
|
||||
|
||||
rows, err := db.DB().Query(s, args...)
|
||||
|
@ -314,13 +314,14 @@ func (db *mysql) GetColumns(tableName string) ([]string, map[string]*core.Column
|
|||
col := new(core.Column)
|
||||
col.Indexes = make(map[string]int)
|
||||
|
||||
var columnName, isNullable, colType, colKey, extra string
|
||||
var columnName, isNullable, colType, colKey, extra, comment string
|
||||
var colDefault *string
|
||||
err = rows.Scan(&columnName, &isNullable, &colDefault, &colType, &colKey, &extra)
|
||||
err = rows.Scan(&columnName, &isNullable, &colDefault, &colType, &colKey, &extra, &comment)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
col.Name = strings.Trim(columnName, "` ")
|
||||
col.Comment = comment
|
||||
if "YES" == isNullable {
|
||||
col.Nullable = true
|
||||
}
|
||||
|
@ -407,7 +408,7 @@ func (db *mysql) GetColumns(tableName string) ([]string, map[string]*core.Column
|
|||
|
||||
func (db *mysql) GetTables() ([]*core.Table, error) {
|
||||
args := []interface{}{db.DbName}
|
||||
s := "SELECT `TABLE_NAME`, `ENGINE`, `TABLE_ROWS`, `AUTO_INCREMENT` from " +
|
||||
s := "SELECT `TABLE_NAME`, `ENGINE`, `TABLE_ROWS`, `AUTO_INCREMENT`, `TABLE_COMMENT` from " +
|
||||
"`INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? AND (`ENGINE`='MyISAM' OR `ENGINE` = 'InnoDB' OR `ENGINE` = 'TokuDB')"
|
||||
db.LogSQL(s, args)
|
||||
|
||||
|
@ -420,14 +421,15 @@ func (db *mysql) GetTables() ([]*core.Table, error) {
|
|||
tables := make([]*core.Table, 0)
|
||||
for rows.Next() {
|
||||
table := core.NewEmptyTable()
|
||||
var name, engine, tableRows string
|
||||
var name, engine, tableRows, comment string
|
||||
var autoIncr *string
|
||||
err = rows.Scan(&name, &engine, &tableRows, &autoIncr)
|
||||
err = rows.Scan(&name, &engine, &tableRows, &autoIncr, &comment)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
table.Name = name
|
||||
table.Comment = comment
|
||||
table.StoreEngine = engine
|
||||
tables = append(tables, table)
|
||||
}
|
||||
|
|
|
@ -14,10 +14,6 @@ import (
|
|||
"github.com/go-xorm/core"
|
||||
)
|
||||
|
||||
// func init() {
|
||||
// RegisterDialect("sqlite3", &sqlite3{})
|
||||
// }
|
||||
|
||||
var (
|
||||
sqlite3ReservedWords = map[string]bool{
|
||||
"ABORT": true,
|
||||
|
@ -310,11 +306,25 @@ func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*core.Colu
|
|||
for _, colStr := range colCreates {
|
||||
reg = regexp.MustCompile(`,\s`)
|
||||
colStr = reg.ReplaceAllString(colStr, ",")
|
||||
if strings.HasPrefix(strings.TrimSpace(colStr), "PRIMARY KEY") {
|
||||
parts := strings.Split(strings.TrimSpace(colStr), "(")
|
||||
if len(parts) == 2 {
|
||||
pkCols := strings.Split(strings.TrimRight(strings.TrimSpace(parts[1]), ")"), ",")
|
||||
for _, pk := range pkCols {
|
||||
if col, ok := cols[strings.Trim(strings.TrimSpace(pk), "`")]; ok {
|
||||
col.IsPrimaryKey = true
|
||||
}
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
fields := strings.Fields(strings.TrimSpace(colStr))
|
||||
col := new(core.Column)
|
||||
col.Indexes = make(map[string]int)
|
||||
col.Nullable = true
|
||||
col.DefaultIsEmpty = true
|
||||
|
||||
for idx, field := range fields {
|
||||
if idx == 0 {
|
||||
col.Name = strings.Trim(strings.Trim(field, "`[] "), `"`)
|
||||
|
|
|
@ -8,7 +8,7 @@ Package xorm is a simple and powerful ORM for Go.
|
|||
|
||||
Installation
|
||||
|
||||
Make sure you have installed Go 1.1+ and then:
|
||||
Make sure you have installed Go 1.5+ and then:
|
||||
|
||||
go get github.com/go-xorm/xorm
|
||||
|
||||
|
@ -51,11 +51,15 @@ There are 8 major ORM methods and many helpful methods to use to operate databas
|
|||
// INSERT INTO struct1 () values ()
|
||||
// INSERT INTO struct2 () values (),(),()
|
||||
|
||||
2. Query one record from database
|
||||
2. Query one record or one variable from database
|
||||
|
||||
has, err := engine.Get(&user)
|
||||
// SELECT * FROM user LIMIT 1
|
||||
|
||||
var id int64
|
||||
has, err := engine.Table("user").Where("name = ?", name).Get(&id)
|
||||
// SELECT id FROM user WHERE name = ? LIMIT 1
|
||||
|
||||
3. Query multiple records from database
|
||||
|
||||
var sliceOfStructs []Struct
|
||||
|
@ -99,6 +103,9 @@ another is Rows
|
|||
counts, err := engine.Count(&user)
|
||||
// SELECT count(*) AS total FROM user
|
||||
|
||||
counts, err := engine.SQL("select count(*) FROM user").Count()
|
||||
// select count(*) FROM user
|
||||
|
||||
8. Sum records
|
||||
|
||||
sumFloat64, err := engine.Sum(&user, "id")
|
||||
|
|
|
@ -40,7 +40,7 @@ type Engine struct {
|
|||
showExecTime bool
|
||||
|
||||
logger core.ILogger
|
||||
TZLocation *time.Location
|
||||
TZLocation *time.Location // The timezone of the application
|
||||
DatabaseTZ *time.Location // The timezone of the database
|
||||
|
||||
disableGlobalCache bool
|
||||
|
@ -143,7 +143,6 @@ func (engine *Engine) Quote(value string) string {
|
|||
|
||||
// QuoteTo quotes string and writes into the buffer
|
||||
func (engine *Engine) QuoteTo(buf *bytes.Buffer, value string) {
|
||||
|
||||
if buf == nil {
|
||||
return
|
||||
}
|
||||
|
@ -196,6 +195,11 @@ func (engine *Engine) SetMaxIdleConns(conns int) {
|
|||
engine.db.SetMaxIdleConns(conns)
|
||||
}
|
||||
|
||||
// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
|
||||
func (engine *Engine) SetConnMaxLifetime(d time.Duration) {
|
||||
engine.db.SetConnMaxLifetime(d)
|
||||
}
|
||||
|
||||
// SetDefaultCacher set the default cacher. Xorm's default not enable cacher.
|
||||
func (engine *Engine) SetDefaultCacher(cacher core.Cacher) {
|
||||
engine.Cacher = cacher
|
||||
|
@ -781,6 +785,12 @@ func (engine *Engine) Having(conditions string) *Session {
|
|||
return session.Having(conditions)
|
||||
}
|
||||
|
||||
func (engine *Engine) unMapType(t reflect.Type) {
|
||||
engine.mutex.Lock()
|
||||
defer engine.mutex.Unlock()
|
||||
delete(engine.Tables, t)
|
||||
}
|
||||
|
||||
func (engine *Engine) autoMapType(v reflect.Value) (*core.Table, error) {
|
||||
t := v.Type()
|
||||
engine.mutex.Lock()
|
||||
|
@ -1007,6 +1017,10 @@ func (engine *Engine) mapType(v reflect.Value) (*core.Table, error) {
|
|||
col = core.NewColumn(engine.ColumnMapper.Obj2Table(t.Field(i).Name),
|
||||
t.Field(i).Name, sqlType, sqlType.DefaultLength,
|
||||
sqlType.DefaultLength2, true)
|
||||
|
||||
if fieldType.Kind() == reflect.Int64 && (strings.ToUpper(col.FieldName) == "ID" || strings.HasSuffix(strings.ToUpper(col.FieldName), ".ID")) {
|
||||
idFieldColName = col.Name
|
||||
}
|
||||
}
|
||||
if col.IsAutoIncrement {
|
||||
col.Nullable = false
|
||||
|
@ -1014,9 +1028,6 @@ func (engine *Engine) mapType(v reflect.Value) (*core.Table, error) {
|
|||
|
||||
table.AddColumn(col)
|
||||
|
||||
if fieldType.Kind() == reflect.Int64 && (strings.ToUpper(col.FieldName) == "ID" || strings.HasSuffix(strings.ToUpper(col.FieldName), ".ID")) {
|
||||
idFieldColName = col.Name
|
||||
}
|
||||
} // end for
|
||||
|
||||
if idFieldColName != "" && len(table.PrimaryKeys) == 0 {
|
||||
|
@ -1241,7 +1252,6 @@ func (engine *Engine) Sync(beans ...interface{}) error {
|
|||
return err
|
||||
}
|
||||
if index.Type == core.UniqueType {
|
||||
//isExist, err := session.isIndexExist(table.Name, name, true)
|
||||
isExist, err := session.isIndexExist2(tableName, index.Cols, true)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -1291,23 +1301,6 @@ func (engine *Engine) Sync2(beans ...interface{}) error {
|
|||
return s.Sync2(beans...)
|
||||
}
|
||||
|
||||
// Drop all mapped table
|
||||
func (engine *Engine) dropAll() error {
|
||||
session := engine.NewSession()
|
||||
defer session.Close()
|
||||
|
||||
err := session.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = session.dropAll()
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
return session.Commit()
|
||||
}
|
||||
|
||||
// CreateTables create tabls according bean
|
||||
func (engine *Engine) CreateTables(beans ...interface{}) error {
|
||||
session := engine.NewSession()
|
||||
|
@ -1348,10 +1341,11 @@ func (engine *Engine) DropTables(beans ...interface{}) error {
|
|||
return session.Commit()
|
||||
}
|
||||
|
||||
func (engine *Engine) createAll() error {
|
||||
// DropIndexes drop indexes of a table
|
||||
func (engine *Engine) DropIndexes(bean interface{}) error {
|
||||
session := engine.NewSession()
|
||||
defer session.Close()
|
||||
return session.createAll()
|
||||
return session.DropIndexes(bean)
|
||||
}
|
||||
|
||||
// Exec raw sql
|
||||
|
@ -1509,7 +1503,6 @@ func (engine *Engine) Import(r io.Reader) ([]sql.Result, error) {
|
|||
results = append(results, result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
//lastError = err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1517,49 +1510,28 @@ func (engine *Engine) Import(r io.Reader) ([]sql.Result, error) {
|
|||
return results, lastError
|
||||
}
|
||||
|
||||
// TZTime change one time to xorm time location
|
||||
func (engine *Engine) TZTime(t time.Time) time.Time {
|
||||
if !t.IsZero() { // if time is not initialized it's not suitable for Time.In()
|
||||
return t.In(engine.TZLocation)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
// NowTime return current time
|
||||
func (engine *Engine) NowTime(sqlTypeName string) interface{} {
|
||||
t := time.Now()
|
||||
return engine.FormatTime(sqlTypeName, t)
|
||||
}
|
||||
|
||||
// NowTime2 return current time
|
||||
func (engine *Engine) NowTime2(sqlTypeName string) (interface{}, time.Time) {
|
||||
t := time.Now()
|
||||
return engine.FormatTime(sqlTypeName, t), t
|
||||
}
|
||||
|
||||
// FormatTime format time
|
||||
func (engine *Engine) FormatTime(sqlTypeName string, t time.Time) (v interface{}) {
|
||||
return engine.formatTime(engine.TZLocation, sqlTypeName, t)
|
||||
return engine.formatTime(sqlTypeName, t.In(engine.DatabaseTZ)), t.In(engine.TZLocation)
|
||||
}
|
||||
|
||||
func (engine *Engine) formatColTime(col *core.Column, t time.Time) (v interface{}) {
|
||||
if col.DisableTimeZone {
|
||||
return engine.formatTime(nil, col.SQLType.Name, t)
|
||||
} else if col.TimeZone != nil {
|
||||
return engine.formatTime(col.TimeZone, col.SQLType.Name, t)
|
||||
if t.IsZero() {
|
||||
if col.Nullable {
|
||||
return nil
|
||||
}
|
||||
return engine.formatTime(engine.TZLocation, col.SQLType.Name, t)
|
||||
return ""
|
||||
}
|
||||
|
||||
func (engine *Engine) formatTime(tz *time.Location, sqlTypeName string, t time.Time) (v interface{}) {
|
||||
if engine.dialect.DBType() == core.ORACLE {
|
||||
return t
|
||||
if col.TimeZone != nil {
|
||||
return engine.formatTime(col.SQLType.Name, t.In(col.TimeZone))
|
||||
}
|
||||
if tz != nil {
|
||||
t = t.In(tz)
|
||||
} else {
|
||||
t = engine.TZTime(t)
|
||||
return engine.formatTime(col.SQLType.Name, t.In(engine.DatabaseTZ))
|
||||
}
|
||||
|
||||
// formatTime format time as column type
|
||||
func (engine *Engine) formatTime(sqlTypeName string, t time.Time) (v interface{}) {
|
||||
switch sqlTypeName {
|
||||
case core.Time:
|
||||
s := t.Format("2006-01-02 15:04:05") //time.RFC3339
|
||||
|
@ -1567,18 +1539,10 @@ func (engine *Engine) formatTime(tz *time.Location, sqlTypeName string, t time.T
|
|||
case core.Date:
|
||||
v = t.Format("2006-01-02")
|
||||
case core.DateTime, core.TimeStamp:
|
||||
if engine.dialect.DBType() == "ql" {
|
||||
v = t
|
||||
} else if engine.dialect.DBType() == "sqlite3" {
|
||||
v = t.UTC().Format("2006-01-02 15:04:05")
|
||||
} else {
|
||||
v = t.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
case core.TimeStampz:
|
||||
if engine.dialect.DBType() == core.MSSQL {
|
||||
v = t.Format("2006-01-02T15:04:05.9999999Z07:00")
|
||||
} else if engine.DriverName() == "mssql" {
|
||||
v = t
|
||||
} else {
|
||||
v = t.Format(time.RFC3339Nano)
|
||||
}
|
||||
|
|
|
@ -196,26 +196,44 @@ func isArrayValueZero(v reflect.Value) bool {
|
|||
|
||||
func int64ToIntValue(id int64, tp reflect.Type) reflect.Value {
|
||||
var v interface{}
|
||||
switch tp.Kind() {
|
||||
case reflect.Int16:
|
||||
v = int16(id)
|
||||
case reflect.Int32:
|
||||
v = int32(id)
|
||||
case reflect.Int:
|
||||
v = int(id)
|
||||
case reflect.Int64:
|
||||
v = id
|
||||
case reflect.Uint16:
|
||||
v = uint16(id)
|
||||
case reflect.Uint32:
|
||||
v = uint32(id)
|
||||
case reflect.Uint64:
|
||||
v = uint64(id)
|
||||
case reflect.Uint:
|
||||
v = uint(id)
|
||||
kind := tp.Kind()
|
||||
|
||||
if kind == reflect.Ptr {
|
||||
kind = tp.Elem().Kind()
|
||||
}
|
||||
|
||||
switch kind {
|
||||
case reflect.Int16:
|
||||
temp := int16(id)
|
||||
v = &temp
|
||||
case reflect.Int32:
|
||||
temp := int32(id)
|
||||
v = &temp
|
||||
case reflect.Int:
|
||||
temp := int(id)
|
||||
v = &temp
|
||||
case reflect.Int64:
|
||||
temp := id
|
||||
v = &temp
|
||||
case reflect.Uint16:
|
||||
temp := uint16(id)
|
||||
v = &temp
|
||||
case reflect.Uint32:
|
||||
temp := uint32(id)
|
||||
v = &temp
|
||||
case reflect.Uint64:
|
||||
temp := uint64(id)
|
||||
v = &temp
|
||||
case reflect.Uint:
|
||||
temp := uint(id)
|
||||
v = &temp
|
||||
}
|
||||
|
||||
if tp.Kind() == reflect.Ptr {
|
||||
return reflect.ValueOf(v).Convert(tp)
|
||||
}
|
||||
return reflect.ValueOf(v).Elem().Convert(tp)
|
||||
}
|
||||
|
||||
func int64ToInt(id int64, tp reflect.Type) interface{} {
|
||||
return int64ToIntValue(id, tp).Interface()
|
||||
|
@ -302,175 +320,6 @@ func sliceEq(left, right []string) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func reflect2value(rawValue *reflect.Value) (str string, err error) {
|
||||
aa := reflect.TypeOf((*rawValue).Interface())
|
||||
vv := reflect.ValueOf((*rawValue).Interface())
|
||||
switch aa.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
str = strconv.FormatInt(vv.Int(), 10)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
str = strconv.FormatUint(vv.Uint(), 10)
|
||||
case reflect.Float32, reflect.Float64:
|
||||
str = strconv.FormatFloat(vv.Float(), 'f', -1, 64)
|
||||
case reflect.String:
|
||||
str = vv.String()
|
||||
case reflect.Array, reflect.Slice:
|
||||
switch aa.Elem().Kind() {
|
||||
case reflect.Uint8:
|
||||
data := rawValue.Interface().([]byte)
|
||||
str = string(data)
|
||||
default:
|
||||
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
|
||||
}
|
||||
// time type
|
||||
case reflect.Struct:
|
||||
if aa.ConvertibleTo(core.TimeType) {
|
||||
str = vv.Convert(core.TimeType).Interface().(time.Time).Format(time.RFC3339Nano)
|
||||
} else {
|
||||
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
|
||||
}
|
||||
case reflect.Bool:
|
||||
str = strconv.FormatBool(vv.Bool())
|
||||
case reflect.Complex128, reflect.Complex64:
|
||||
str = fmt.Sprintf("%v", vv.Complex())
|
||||
/* TODO: unsupported types below
|
||||
case reflect.Map:
|
||||
case reflect.Ptr:
|
||||
case reflect.Uintptr:
|
||||
case reflect.UnsafePointer:
|
||||
case reflect.Chan, reflect.Func, reflect.Interface:
|
||||
*/
|
||||
default:
|
||||
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func value2Bytes(rawValue *reflect.Value) (data []byte, err error) {
|
||||
var str string
|
||||
str, err = reflect2value(rawValue)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
data = []byte(str)
|
||||
return
|
||||
}
|
||||
|
||||
func value2String(rawValue *reflect.Value) (data string, err error) {
|
||||
data, err = reflect2value(rawValue)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func rows2Strings(rows *core.Rows) (resultsSlice []map[string]string, err error) {
|
||||
fields, err := rows.Columns()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for rows.Next() {
|
||||
result, err := row2mapStr(rows, fields)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resultsSlice = append(resultsSlice, result)
|
||||
}
|
||||
|
||||
return resultsSlice, nil
|
||||
}
|
||||
|
||||
func rows2maps(rows *core.Rows) (resultsSlice []map[string][]byte, err error) {
|
||||
fields, err := rows.Columns()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for rows.Next() {
|
||||
result, err := row2map(rows, fields)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resultsSlice = append(resultsSlice, result)
|
||||
}
|
||||
|
||||
return resultsSlice, nil
|
||||
}
|
||||
|
||||
func row2map(rows *core.Rows, fields []string) (resultsMap map[string][]byte, err error) {
|
||||
result := make(map[string][]byte)
|
||||
scanResultContainers := make([]interface{}, len(fields))
|
||||
for i := 0; i < len(fields); i++ {
|
||||
var scanResultContainer interface{}
|
||||
scanResultContainers[i] = &scanResultContainer
|
||||
}
|
||||
if err := rows.Scan(scanResultContainers...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for ii, key := range fields {
|
||||
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
|
||||
//if row is null then ignore
|
||||
if rawValue.Interface() == nil {
|
||||
//fmt.Println("ignore ...", key, rawValue)
|
||||
continue
|
||||
}
|
||||
|
||||
if data, err := value2Bytes(&rawValue); err == nil {
|
||||
result[key] = data
|
||||
} else {
|
||||
return nil, err // !nashtsai! REVIEW, should return err or just error log?
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func row2mapStr(rows *core.Rows, fields []string) (resultsMap map[string]string, err error) {
|
||||
result := make(map[string]string)
|
||||
scanResultContainers := make([]interface{}, len(fields))
|
||||
for i := 0; i < len(fields); i++ {
|
||||
var scanResultContainer interface{}
|
||||
scanResultContainers[i] = &scanResultContainer
|
||||
}
|
||||
if err := rows.Scan(scanResultContainers...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for ii, key := range fields {
|
||||
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
|
||||
//if row is null then ignore
|
||||
if rawValue.Interface() == nil {
|
||||
//fmt.Println("ignore ...", key, rawValue)
|
||||
continue
|
||||
}
|
||||
|
||||
if data, err := value2String(&rawValue); err == nil {
|
||||
result[key] = data
|
||||
} else {
|
||||
return nil, err // !nashtsai! REVIEW, should return err or just error log?
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func txQuery2(tx *core.Tx, sqlStr string, params ...interface{}) ([]map[string]string, error) {
|
||||
rows, err := tx.Query(sqlStr, params...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
return rows2Strings(rows)
|
||||
}
|
||||
|
||||
func query2(db *core.DB, sqlStr string, params ...interface{}) ([]map[string]string, error) {
|
||||
rows, err := db.Query(sqlStr, params...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return rows2Strings(rows)
|
||||
}
|
||||
|
||||
func setColumnInt(bean interface{}, col *core.Column, t int64) {
|
||||
v, err := col.ValueOf(bean)
|
||||
if err != nil {
|
||||
|
@ -537,6 +386,10 @@ func genCols(table *core.Table, session *Session, bean interface{}, useCol bool,
|
|||
if len(fieldValue.String()) == 0 {
|
||||
continue
|
||||
}
|
||||
case reflect.Ptr:
|
||||
if fieldValue.Pointer() == 0 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
// Copyright 2017 The Xorm Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package xorm
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
zeroTime0 = "0000-00-00 00:00:00"
|
||||
zeroTime1 = "0001-01-01 00:00:00"
|
||||
)
|
||||
|
||||
func formatTime(t time.Time) string {
|
||||
return t.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
func isTimeZero(t time.Time) bool {
|
||||
return t.IsZero() || formatTime(t) == zeroTime0 ||
|
||||
formatTime(t) == zeroTime1
|
||||
}
|
|
@ -344,15 +344,6 @@ func (session *Session) row2Bean(rows *core.Rows, fields []string, fieldsCount i
|
|||
}
|
||||
}()
|
||||
|
||||
dbTZ := session.Engine.DatabaseTZ
|
||||
if dbTZ == nil {
|
||||
if session.Engine.dialect.DBType() == core.SQLITE {
|
||||
dbTZ = time.UTC
|
||||
} else {
|
||||
dbTZ = time.Local
|
||||
}
|
||||
}
|
||||
|
||||
var tempMap = make(map[string]int)
|
||||
var pk core.PK
|
||||
for ii, key := range fields {
|
||||
|
@ -528,11 +519,9 @@ func (session *Session) row2Bean(rows *core.Rows, fields []string, fieldsCount i
|
|||
}
|
||||
case reflect.Struct:
|
||||
if fieldType.ConvertibleTo(core.TimeType) {
|
||||
var tz *time.Location
|
||||
if col.TimeZone == nil {
|
||||
tz = session.Engine.TZLocation
|
||||
} else {
|
||||
tz = col.TimeZone
|
||||
dbTZ := session.Engine.DatabaseTZ
|
||||
if col.TimeZone != nil {
|
||||
dbTZ = col.TimeZone
|
||||
}
|
||||
|
||||
if rawValueType == core.TimeType {
|
||||
|
@ -548,14 +537,13 @@ func (session *Session) row2Bean(rows *core.Rows, fields []string, fieldsCount i
|
|||
t.Minute(), t.Second(), t.Nanosecond(), dbTZ)
|
||||
}
|
||||
|
||||
// !nashtsai! convert to engine location
|
||||
t = t.In(tz)
|
||||
t = t.In(session.Engine.TZLocation)
|
||||
fieldValue.Set(reflect.ValueOf(t).Convert(fieldType))
|
||||
} else if rawValueType == core.IntType || rawValueType == core.Int64Type ||
|
||||
rawValueType == core.Int32Type {
|
||||
hasAssigned = true
|
||||
|
||||
t := time.Unix(vv.Int(), 0).In(tz)
|
||||
t := time.Unix(vv.Int(), 0).In(session.Engine.TZLocation)
|
||||
fieldValue.Set(reflect.ValueOf(t).Convert(fieldType))
|
||||
} else {
|
||||
if d, ok := vv.Interface().([]uint8); ok {
|
||||
|
|
|
@ -23,6 +23,11 @@ func (session *Session) str2Time(col *core.Column, data string) (outTime time.Ti
|
|||
var x time.Time
|
||||
var err error
|
||||
|
||||
var parseLoc = session.Engine.DatabaseTZ
|
||||
if col.TimeZone != nil {
|
||||
parseLoc = col.TimeZone
|
||||
}
|
||||
|
||||
if sdata == "0000-00-00 00:00:00" ||
|
||||
sdata == "0001-01-01 00:00:00" {
|
||||
} else if !strings.ContainsAny(sdata, "- :") { // !nashtsai! has only found that mymysql driver is using this for time type column
|
||||
|
@ -30,33 +35,26 @@ func (session *Session) str2Time(col *core.Column, data string) (outTime time.Ti
|
|||
sd, err := strconv.ParseInt(sdata, 10, 64)
|
||||
if err == nil {
|
||||
x = time.Unix(sd, 0)
|
||||
// !nashtsai! HACK mymysql driver is causing Local location being change to CHAT and cause wrong time conversion
|
||||
if col.TimeZone == nil {
|
||||
x = x.In(session.Engine.TZLocation)
|
||||
} else {
|
||||
x = x.In(col.TimeZone)
|
||||
}
|
||||
session.Engine.logger.Debugf("time(0) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata)
|
||||
} else {
|
||||
session.Engine.logger.Debugf("time(0) err key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata)
|
||||
}
|
||||
} else if len(sdata) > 19 && strings.Contains(sdata, "-") {
|
||||
x, err = time.ParseInLocation(time.RFC3339Nano, sdata, session.Engine.TZLocation)
|
||||
x, err = time.ParseInLocation(time.RFC3339Nano, sdata, parseLoc)
|
||||
session.Engine.logger.Debugf("time(1) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata)
|
||||
if err != nil {
|
||||
x, err = time.ParseInLocation("2006-01-02 15:04:05.999999999", sdata, session.Engine.TZLocation)
|
||||
x, err = time.ParseInLocation("2006-01-02 15:04:05.999999999", sdata, parseLoc)
|
||||
session.Engine.logger.Debugf("time(2) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata)
|
||||
}
|
||||
if err != nil {
|
||||
x, err = time.ParseInLocation("2006-01-02 15:04:05.9999999 Z07:00", sdata, session.Engine.TZLocation)
|
||||
x, err = time.ParseInLocation("2006-01-02 15:04:05.9999999 Z07:00", sdata, parseLoc)
|
||||
session.Engine.logger.Debugf("time(3) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata)
|
||||
}
|
||||
|
||||
} else if len(sdata) == 19 && strings.Contains(sdata, "-") {
|
||||
x, err = time.ParseInLocation("2006-01-02 15:04:05", sdata, session.Engine.TZLocation)
|
||||
x, err = time.ParseInLocation("2006-01-02 15:04:05", sdata, parseLoc)
|
||||
session.Engine.logger.Debugf("time(4) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata)
|
||||
} else if len(sdata) == 10 && sdata[4] == '-' && sdata[7] == '-' {
|
||||
x, err = time.ParseInLocation("2006-01-02", sdata, session.Engine.TZLocation)
|
||||
x, err = time.ParseInLocation("2006-01-02", sdata, parseLoc)
|
||||
session.Engine.logger.Debugf("time(5) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata)
|
||||
} else if col.SQLType.Name == core.Time {
|
||||
if strings.Contains(sdata, " ") {
|
||||
|
@ -70,7 +68,7 @@ func (session *Session) str2Time(col *core.Column, data string) (outTime time.Ti
|
|||
}
|
||||
|
||||
st := fmt.Sprintf("2006-01-02 %v", sdata)
|
||||
x, err = time.ParseInLocation("2006-01-02 15:04:05", st, session.Engine.TZLocation)
|
||||
x, err = time.ParseInLocation("2006-01-02 15:04:05", st, parseLoc)
|
||||
session.Engine.logger.Debugf("time(6) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata)
|
||||
} else {
|
||||
outErr = fmt.Errorf("unsupported time format %v", sdata)
|
||||
|
@ -80,7 +78,7 @@ func (session *Session) str2Time(col *core.Column, data string) (outTime time.Ti
|
|||
outErr = fmt.Errorf("unsupported time format %v: %v", sdata, err)
|
||||
return
|
||||
}
|
||||
outTime = x
|
||||
outTime = x.In(session.Engine.TZLocation)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -588,12 +586,7 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val
|
|||
case reflect.Struct:
|
||||
if fieldType.ConvertibleTo(core.TimeType) {
|
||||
t := fieldValue.Convert(core.TimeType).Interface().(time.Time)
|
||||
if session.Engine.dialect.DBType() == core.MSSQL {
|
||||
if t.IsZero() {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
tf := session.Engine.FormatTime(col.SQLType.Name, t)
|
||||
tf := session.Engine.formatColTime(col, t)
|
||||
return tf, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -122,7 +122,10 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
|||
}
|
||||
}
|
||||
|
||||
condSQL, condArgs, _ := builder.ToSQL(session.Statement.cond.And(autoCond))
|
||||
condSQL, condArgs, err := builder.ToSQL(session.Statement.cond.And(autoCond))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
args = append(session.Statement.joinArgs, condArgs...)
|
||||
sqlStr = session.Statement.genSelectSQL(columnStr, condSQL)
|
||||
|
|
|
@ -67,7 +67,7 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
|
|||
return 0, errors.New("could not insert a empty slice")
|
||||
}
|
||||
|
||||
if err := session.Statement.setRefValue(sliceValue.Index(0)); err != nil {
|
||||
if err := session.Statement.setRefValue(reflect.ValueOf(sliceValue.Index(0).Interface())); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
|
@ -343,15 +343,26 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
|
|||
if len(exprColVals) > 0 {
|
||||
colPlaces = colPlaces + strings.Join(exprColVals, ", ")
|
||||
} else {
|
||||
if len(colPlaces) > 0 {
|
||||
colPlaces = colPlaces[0 : len(colPlaces)-2]
|
||||
}
|
||||
}
|
||||
|
||||
sqlStr := fmt.Sprintf("INSERT INTO %s (%v%v%v) VALUES (%v)",
|
||||
var sqlStr string
|
||||
if len(colPlaces) > 0 {
|
||||
sqlStr = fmt.Sprintf("INSERT INTO %s (%v%v%v) VALUES (%v)",
|
||||
session.Engine.Quote(session.Statement.TableName()),
|
||||
session.Engine.QuoteStr(),
|
||||
strings.Join(colNames, session.Engine.Quote(", ")),
|
||||
session.Engine.QuoteStr(),
|
||||
colPlaces)
|
||||
} else {
|
||||
if session.Engine.dialect.DBType() == core.MYSQL {
|
||||
sqlStr = fmt.Sprintf("INSERT INTO %s VALUES ()", session.Engine.Quote(session.Statement.TableName()))
|
||||
} else {
|
||||
sqlStr = fmt.Sprintf("INSERT INTO %s DEFAULT VALUES", session.Engine.Quote(session.Statement.TableName()))
|
||||
}
|
||||
}
|
||||
|
||||
handleAfterInsertProcessorFunc := func(bean interface{}) {
|
||||
if session.IsAutoCommit {
|
||||
|
@ -384,7 +395,6 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
|
|||
// for postgres, many of them didn't implement lastInsertId, so we should
|
||||
// implemented it ourself.
|
||||
if session.Engine.dialect.DBType() == core.ORACLE && len(table.AutoIncrement) > 0 {
|
||||
//assert table.AutoIncrement != ""
|
||||
res, err := session.query("select seq_atable.currval from dual", args...)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
|
|
@ -6,6 +6,10 @@ package xorm
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-xorm/core"
|
||||
)
|
||||
|
@ -59,6 +63,60 @@ func (session *Session) innerQuery(sqlStr string, params ...interface{}) (*core.
|
|||
return stmt, rows, nil
|
||||
}
|
||||
|
||||
func rows2maps(rows *core.Rows) (resultsSlice []map[string][]byte, err error) {
|
||||
fields, err := rows.Columns()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for rows.Next() {
|
||||
result, err := row2map(rows, fields)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resultsSlice = append(resultsSlice, result)
|
||||
}
|
||||
|
||||
return resultsSlice, nil
|
||||
}
|
||||
|
||||
func value2Bytes(rawValue *reflect.Value) (data []byte, err error) {
|
||||
var str string
|
||||
str, err = reflect2value(rawValue)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
data = []byte(str)
|
||||
return
|
||||
}
|
||||
|
||||
func row2map(rows *core.Rows, fields []string) (resultsMap map[string][]byte, err error) {
|
||||
result := make(map[string][]byte)
|
||||
scanResultContainers := make([]interface{}, len(fields))
|
||||
for i := 0; i < len(fields); i++ {
|
||||
var scanResultContainer interface{}
|
||||
scanResultContainers[i] = &scanResultContainer
|
||||
}
|
||||
if err := rows.Scan(scanResultContainers...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for ii, key := range fields {
|
||||
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
|
||||
//if row is null then ignore
|
||||
if rawValue.Interface() == nil {
|
||||
//fmt.Println("ignore ...", key, rawValue)
|
||||
continue
|
||||
}
|
||||
|
||||
if data, err := value2Bytes(&rawValue); err == nil {
|
||||
result[key] = data
|
||||
} else {
|
||||
return nil, err // !nashtsai! REVIEW, should return err or just error log?
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (session *Session) innerQuery2(sqlStr string, params ...interface{}) ([]map[string][]byte, error) {
|
||||
_, rows, err := session.innerQuery(sqlStr, params...)
|
||||
if rows != nil {
|
||||
|
@ -80,6 +138,121 @@ func (session *Session) Query(sqlStr string, paramStr ...interface{}) ([]map[str
|
|||
return session.query(sqlStr, paramStr...)
|
||||
}
|
||||
|
||||
func rows2Strings(rows *core.Rows) (resultsSlice []map[string]string, err error) {
|
||||
fields, err := rows.Columns()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for rows.Next() {
|
||||
result, err := row2mapStr(rows, fields)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resultsSlice = append(resultsSlice, result)
|
||||
}
|
||||
|
||||
return resultsSlice, nil
|
||||
}
|
||||
|
||||
func reflect2value(rawValue *reflect.Value) (str string, err error) {
|
||||
aa := reflect.TypeOf((*rawValue).Interface())
|
||||
vv := reflect.ValueOf((*rawValue).Interface())
|
||||
switch aa.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
str = strconv.FormatInt(vv.Int(), 10)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
str = strconv.FormatUint(vv.Uint(), 10)
|
||||
case reflect.Float32, reflect.Float64:
|
||||
str = strconv.FormatFloat(vv.Float(), 'f', -1, 64)
|
||||
case reflect.String:
|
||||
str = vv.String()
|
||||
case reflect.Array, reflect.Slice:
|
||||
switch aa.Elem().Kind() {
|
||||
case reflect.Uint8:
|
||||
data := rawValue.Interface().([]byte)
|
||||
str = string(data)
|
||||
default:
|
||||
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
|
||||
}
|
||||
// time type
|
||||
case reflect.Struct:
|
||||
if aa.ConvertibleTo(core.TimeType) {
|
||||
str = vv.Convert(core.TimeType).Interface().(time.Time).Format(time.RFC3339Nano)
|
||||
} else {
|
||||
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
|
||||
}
|
||||
case reflect.Bool:
|
||||
str = strconv.FormatBool(vv.Bool())
|
||||
case reflect.Complex128, reflect.Complex64:
|
||||
str = fmt.Sprintf("%v", vv.Complex())
|
||||
/* TODO: unsupported types below
|
||||
case reflect.Map:
|
||||
case reflect.Ptr:
|
||||
case reflect.Uintptr:
|
||||
case reflect.UnsafePointer:
|
||||
case reflect.Chan, reflect.Func, reflect.Interface:
|
||||
*/
|
||||
default:
|
||||
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func value2String(rawValue *reflect.Value) (data string, err error) {
|
||||
data, err = reflect2value(rawValue)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func row2mapStr(rows *core.Rows, fields []string) (resultsMap map[string]string, err error) {
|
||||
result := make(map[string]string)
|
||||
scanResultContainers := make([]interface{}, len(fields))
|
||||
for i := 0; i < len(fields); i++ {
|
||||
var scanResultContainer interface{}
|
||||
scanResultContainers[i] = &scanResultContainer
|
||||
}
|
||||
if err := rows.Scan(scanResultContainers...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for ii, key := range fields {
|
||||
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
|
||||
//if row is null then ignore
|
||||
if rawValue.Interface() == nil {
|
||||
//fmt.Println("ignore ...", key, rawValue)
|
||||
continue
|
||||
}
|
||||
|
||||
if data, err := value2String(&rawValue); err == nil {
|
||||
result[key] = data
|
||||
} else {
|
||||
return nil, err // !nashtsai! REVIEW, should return err or just error log?
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func txQuery2(tx *core.Tx, sqlStr string, params ...interface{}) ([]map[string]string, error) {
|
||||
rows, err := tx.Query(sqlStr, params...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
return rows2Strings(rows)
|
||||
}
|
||||
|
||||
func query2(db *core.DB, sqlStr string, params ...interface{}) ([]map[string]string, error) {
|
||||
rows, err := db.Query(sqlStr, params...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return rows2Strings(rows)
|
||||
}
|
||||
|
||||
// QueryString runs a raw sql and return records as []map[string]string
|
||||
func (session *Session) QueryString(sqlStr string, args ...interface{}) ([]map[string]string, error) {
|
||||
defer session.resetStatement()
|
||||
|
|
|
@ -89,24 +89,6 @@ func (session *Session) createOneTable() error {
|
|||
return err
|
||||
}
|
||||
|
||||
// to be deleted
|
||||
func (session *Session) createAll() error {
|
||||
if session.IsAutoClose {
|
||||
defer session.Close()
|
||||
}
|
||||
|
||||
for _, table := range session.Engine.Tables {
|
||||
session.Statement.RefTable = table
|
||||
session.Statement.tableName = table.Name
|
||||
err := session.createOneTable()
|
||||
session.resetStatement()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DropIndexes drop indexes
|
||||
func (session *Session) DropIndexes(bean interface{}) error {
|
||||
v := rValue(bean)
|
||||
|
@ -208,22 +190,6 @@ func (session *Session) isTableEmpty(tableName string) (bool, error) {
|
|||
return total == 0, nil
|
||||
}
|
||||
|
||||
func (session *Session) isIndexExist(tableName, idxName string, unique bool) (bool, error) {
|
||||
defer session.resetStatement()
|
||||
if session.IsAutoClose {
|
||||
defer session.Close()
|
||||
}
|
||||
var idx string
|
||||
if unique {
|
||||
idx = uniqueName(tableName, idxName)
|
||||
} else {
|
||||
idx = indexName(tableName, idxName)
|
||||
}
|
||||
sqlStr, args := session.Engine.dialect.IndexCheckSql(tableName, idx)
|
||||
results, err := session.query(sqlStr, args...)
|
||||
return len(results) > 0, err
|
||||
}
|
||||
|
||||
// find if index is exist according cols
|
||||
func (session *Session) isIndexExist2(tableName string, cols []string, unique bool) (bool, error) {
|
||||
defer session.resetStatement()
|
||||
|
@ -282,25 +248,6 @@ func (session *Session) addUnique(tableName, uqeName string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// To be deleted
|
||||
func (session *Session) dropAll() error {
|
||||
defer session.resetStatement()
|
||||
if session.IsAutoClose {
|
||||
defer session.Close()
|
||||
}
|
||||
|
||||
for _, table := range session.Engine.Tables {
|
||||
session.Statement.Init()
|
||||
session.Statement.RefTable = table
|
||||
sqlStr := session.Engine.Dialect().DropTableSql(session.Statement.TableName())
|
||||
_, err := session.exec(sqlStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Sync2 synchronize structs to database tables
|
||||
func (session *Session) Sync2(beans ...interface{}) error {
|
||||
engine := session.Engine
|
||||
|
|
|
@ -8,7 +8,7 @@ import "database/sql"
|
|||
|
||||
// Count counts the records. bean's non-empty fields
|
||||
// are conditions.
|
||||
func (session *Session) Count(bean interface{}) (int64, error) {
|
||||
func (session *Session) Count(bean ...interface{}) (int64, error) {
|
||||
defer session.resetStatement()
|
||||
if session.IsAutoClose {
|
||||
defer session.Close()
|
||||
|
@ -17,7 +17,10 @@ func (session *Session) Count(bean interface{}) (int64, error) {
|
|||
var sqlStr string
|
||||
var args []interface{}
|
||||
if session.Statement.RawSQL == "" {
|
||||
sqlStr, args = session.Statement.genCountSQL(bean)
|
||||
if len(bean) == 0 {
|
||||
return 0, ErrTableNotFound
|
||||
}
|
||||
sqlStr, args = session.Statement.genCountSQL(bean[0])
|
||||
} else {
|
||||
sqlStr = session.Statement.RawSQL
|
||||
args = session.Statement.RawParams
|
||||
|
|
|
@ -298,7 +298,19 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
|
|||
condSQL = "WHERE " + condSQL
|
||||
}
|
||||
} else if st.Engine.dialect.DBType() == core.MSSQL {
|
||||
top = fmt.Sprintf("top (%d) ", st.LimitN)
|
||||
if st.OrderStr != "" && st.Engine.dialect.DBType() == core.MSSQL &&
|
||||
table != nil && len(table.PrimaryKeys) == 1 {
|
||||
cond = builder.Expr(fmt.Sprintf("%s IN (SELECT TOP (%d) %s FROM %v%v)",
|
||||
table.PrimaryKeys[0], st.LimitN, table.PrimaryKeys[0],
|
||||
session.Engine.Quote(session.Statement.TableName()), condSQL), condArgs...)
|
||||
|
||||
condSQL, condArgs, _ = builder.ToSQL(cond)
|
||||
if len(condSQL) > 0 {
|
||||
condSQL = "WHERE " + condSQL
|
||||
}
|
||||
} else {
|
||||
top = fmt.Sprintf("TOP (%d) ", st.LimitN)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -376,7 +376,7 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
|||
if !requiredField && (t.IsZero() || !fieldValue.IsValid()) {
|
||||
continue
|
||||
}
|
||||
val = engine.FormatTime(col.SQLType.Name, t)
|
||||
val = engine.formatColTime(col, t)
|
||||
} else if nulType, ok := fieldValue.Interface().(driver.Valuer); ok {
|
||||
val, _ = nulType.Value()
|
||||
} else {
|
||||
|
@ -612,7 +612,7 @@ func buildConds(engine *Engine, table *core.Table, bean interface{},
|
|||
if !requiredField && (t.IsZero() || !fieldValue.IsValid()) {
|
||||
continue
|
||||
}
|
||||
val = engine.FormatTime(col.SQLType.Name, t)
|
||||
val = engine.formatColTime(col, t)
|
||||
} else if _, ok := reflect.New(fieldType).Interface().(core.Conversion); ok {
|
||||
continue
|
||||
} else if valNul, ok := fieldValue.Interface().(driver.Valuer); ok {
|
||||
|
@ -1278,7 +1278,7 @@ func (statement *Statement) genSelectSQL(columnStr, condSQL string) (a string) {
|
|||
}
|
||||
|
||||
// !nashtsai! REVIEW Sprintf is considered slowest mean of string concatnation, better to work with builder pattern
|
||||
a = fmt.Sprintf("SELECT %v%v%v%v%v", top, distinct, columnStr, fromStr, whereStr)
|
||||
a = fmt.Sprintf("SELECT %v%v%v%v%v", distinct, top, columnStr, fromStr, whereStr)
|
||||
if len(mssqlCondi) > 0 {
|
||||
if len(whereStr) > 0 {
|
||||
a += " AND " + mssqlCondi
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
go test -db=mssql -conn_str="server=192.168.1.58;user id=sa;password=123456;database=xorm_test"
|
|
@ -0,0 +1 @@
|
|||
go test -db=mysql -conn_str="root:@/xorm_test"
|
|
@ -0,0 +1 @@
|
|||
go test -db=postgres -conn_str="dbname=xorm_test sslmode=disable"
|
|
@ -0,0 +1 @@
|
|||
go test -db=sqlite3 -conn_str="./test.db?cache=shared&mode=rwc"
|
|
@ -17,7 +17,7 @@ import (
|
|||
|
||||
const (
|
||||
// Version show the xorm's version
|
||||
Version string = "0.6.2.0412"
|
||||
Version string = "0.6.2.0605"
|
||||
)
|
||||
|
||||
func regDrvsNDialects() bool {
|
||||
|
@ -89,6 +89,12 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
|
|||
tagHandlers: defaultTagHandlers,
|
||||
}
|
||||
|
||||
if uri.DbType == core.SQLITE {
|
||||
engine.DatabaseTZ = time.UTC
|
||||
} else {
|
||||
engine.DatabaseTZ = time.Local
|
||||
}
|
||||
|
||||
logger := NewSimpleLogger(os.Stdout)
|
||||
logger.SetLevel(core.LOG_INFO)
|
||||
engine.SetLogger(logger)
|
||||
|
|
|
@ -153,22 +153,22 @@
|
|||
"revisionTime": "2016-08-02T11:38:42Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "Fh6Svimt+QyXHbaVxgSV7qwUHL8=",
|
||||
"checksumSHA1": "9SXbj96wb1PgppBZzxMIN0axbFQ=",
|
||||
"path": "github.com/go-xorm/builder",
|
||||
"revision": "9c357861b643b7dd1023551fdf116b8d42030146",
|
||||
"revisionTime": "2017-02-16T03:03:40Z"
|
||||
"revision": "c8871c857d2555fbfbd8524f895be5386d3d8836",
|
||||
"revisionTime": "2017-05-19T03:21:30Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "1BBE89UpNp+qVvrF5oKdRLTaBJc=",
|
||||
"checksumSHA1": "GnWfEqqhYT5MsxzlB6bRESmkDvM=",
|
||||
"path": "github.com/go-xorm/core",
|
||||
"revision": "7daacb215ed03af093a72e0af32a5fe79458613d",
|
||||
"revisionTime": "2017-02-06T15:24:21Z"
|
||||
"revision": "6c9f9bf3130d143937e4adcef1cf1bb9f6899260",
|
||||
"revisionTime": "2017-05-03T12:16:46Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "Ka4hFMvc75Fb57ZNLALyYSM7CCE=",
|
||||
"checksumSHA1": "UN8r+3fuSzXJ1tXCe+M8g5eRpOI=",
|
||||
"path": "github.com/go-xorm/xorm",
|
||||
"revision": "d52a762fba17a2ed265463c1c7b608c14836eaaf",
|
||||
"revisionTime": "2017-04-20T16:02:48Z"
|
||||
"revision": "8a877636fdbbb0f7133b158fe5cde3588464b035",
|
||||
"revisionTime": "2017-06-06T06:54:59Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "1ft/4j5MFa7C9dPI9whL03HSUzk=",
|
||||
|
|
Loading…
Reference in New Issue