Skip to content
This repository was archived by the owner on Jun 14, 2021. It is now read-only.

Commit

Permalink
Move statement as a sub package (#1564)
Browse files Browse the repository at this point in the history
Fix test

Fix bug

Move statement as a sub package

Reviewed-on: https://gitea.com/xorm/xorm/pulls/1564
  • Loading branch information
lunny committed Feb 28, 2020
1 parent f63b42f commit 2b62dc5
Show file tree
Hide file tree
Showing 50 changed files with 1,985 additions and 1,782 deletions.
2 changes: 1 addition & 1 deletion context_cache.go → contexts/context_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm
package contexts

// ContextCache is the interface that operates the cache data.
type ContextCache interface {
Expand Down
5 changes: 5 additions & 0 deletions dialects/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Dialect interface {
DBType() DBType
SQLType(*schemas.Column) string
FormatBytes(b []byte) string
DefaultSchema() string

DriverName() string
DataSourceName() string
Expand Down Expand Up @@ -103,6 +104,10 @@ func (b *Base) SetLogger(logger log.Logger) {
b.logger = logger
}

func (b *Base) DefaultSchema() string {
return ""
}

func (b *Base) Init(db *core.DB, dialect Dialect, uri *URI, drivername, dataSourceName string) error {
b.db, b.dialect, b.uri = db, dialect, uri
b.driverName, b.dataSourceName = drivername, dataSourceName
Expand Down
4 changes: 4 additions & 0 deletions dialects/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,10 @@ func (db *postgres) Init(d *core.DB, uri *URI, drivername, dataSourceName string
return nil
}

func (db *postgres) DefaultSchema() string {
return PostgresPublicSchema
}

func (db *postgres) SQLType(c *schemas.Column) string {
var res string
switch t := c.SQLType.Name; t {
Expand Down
90 changes: 90 additions & 0 deletions dialects/table_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2015 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 dialects

import (
"fmt"
"reflect"
"strings"

"xorm.io/xorm/internal/utils"
"xorm.io/xorm/names"
)

// TableNameWithSchema will add schema prefix on table name if possible
func TableNameWithSchema(dialect Dialect, tableName string) string {
// Add schema name as prefix of table name.
// Only for postgres database.
if dialect.URI().Schema != "" &&
dialect.URI().Schema != dialect.DefaultSchema() &&
strings.Index(tableName, ".") == -1 {
return fmt.Sprintf("%s.%s", dialect.URI().Schema, tableName)
}
return tableName
}

// TableNameNoSchema returns table name with given tableName
func TableNameNoSchema(dialect Dialect, mapper names.Mapper, tableName interface{}) string {
quote := dialect.Quoter().Quote
switch tableName.(type) {
case []string:
t := tableName.([]string)
if len(t) > 1 {
return fmt.Sprintf("%v AS %v", quote(t[0]), quote(t[1]))
} else if len(t) == 1 {
return quote(t[0])
}
case []interface{}:
t := tableName.([]interface{})
l := len(t)
var table string
if l > 0 {
f := t[0]
switch f.(type) {
case string:
table = f.(string)
case names.TableName:
table = f.(names.TableName).TableName()
default:
v := utils.ReflectValue(f)
t := v.Type()
if t.Kind() == reflect.Struct {
table = names.GetTableName(mapper, v)
} else {
table = quote(fmt.Sprintf("%v", f))
}
}
}
if l > 1 {
return fmt.Sprintf("%v AS %v", quote(table), quote(fmt.Sprintf("%v", t[1])))
} else if l == 1 {
return quote(table)
}
case names.TableName:
return tableName.(names.TableName).TableName()
case string:
return tableName.(string)
case reflect.Value:
v := tableName.(reflect.Value)
return names.GetTableName(mapper, v)
default:
v := utils.ReflectValue(tableName)
t := v.Type()
if t.Kind() == reflect.Struct {
return names.GetTableName(mapper, v)
}
return quote(fmt.Sprintf("%v", tableName))
}
return ""
}

// FullTableName returns table name with quote and schema according parameter
func FullTableName(dialect Dialect, mapper names.Mapper, bean interface{}, includeSchema ...bool) string {
tbName := TableNameNoSchema(dialect, mapper, bean)
if len(includeSchema) > 0 && includeSchema[0] && !utils.IsSubQuery(tbName) {
tbName = TableNameWithSchema(dialect, tbName)
}
return tbName
}
12 changes: 7 additions & 5 deletions engine_table_test.go → dialects/table_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm
package dialects

import (
"testing"

"xorm.io/xorm/names"

"github.com/stretchr/testify/assert"
)

Expand All @@ -20,9 +22,9 @@ func (mcc *MCC) TableName() string {
return "mcc"
}

func TestTableName1(t *testing.T) {
assert.NoError(t, prepareEngine())
func TestFullTableName(t *testing.T) {
dialect := QueryDialect("mysql")

assert.EqualValues(t, "mcc", testEngine.TableName(new(MCC)))
assert.EqualValues(t, "mcc", testEngine.TableName("mcc"))
assert.EqualValues(t, "mcc", FullTableName(dialect, names.SnakeMapper{}, &MCC{}))
assert.EqualValues(t, "mcc", FullTableName(dialect, names.SnakeMapper{}, "mcc"))
}
49 changes: 49 additions & 0 deletions dialects/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2015 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 dialects

import (
"time"

"xorm.io/xorm/schemas"
)

// FormatTime format time as column type
func FormatTime(dialect Dialect, sqlTypeName string, t time.Time) (v interface{}) {
switch sqlTypeName {
case schemas.Time:
s := t.Format("2006-01-02 15:04:05") // time.RFC3339
v = s[11:19]
case schemas.Date:
v = t.Format("2006-01-02")
case schemas.DateTime, schemas.TimeStamp, schemas.Varchar: // !DarthPestilane! format time when sqlTypeName is schemas.Varchar.
v = t.Format("2006-01-02 15:04:05")
case schemas.TimeStampz:
if dialect.DBType() == schemas.MSSQL {
v = t.Format("2006-01-02T15:04:05.9999999Z07:00")
} else {
v = t.Format(time.RFC3339Nano)
}
case schemas.BigInt, schemas.Int:
v = t.Unix()
default:
v = t
}
return
}

func FormatColumnTime(dialect Dialect, defaultTimeZone *time.Location, col *schemas.Column, t time.Time) (v interface{}) {
if t.IsZero() {
if col.Nullable {
return nil
}
return ""
}

if col.TimeZone != nil {
return FormatTime(dialect, col.SQLType.Name, t.In(col.TimeZone))
}
return FormatTime(dialect, col.SQLType.Name, t.In(defaultTimeZone))
}
Loading

0 comments on commit 2b62dc5

Please sign in to comment.