code tidy up

This commit is contained in:
Nash Tsai 2013-12-18 22:55:39 +08:00
parent c2fe9ee0d5
commit 9f3a211ed9
2 changed files with 24 additions and 26 deletions

View File

@ -10,27 +10,6 @@ import (
"time"
)
// !nashtsai! treat following var as interal const values, these are used for reflect.TypeOf comparision
var (
c_EMPTY_STRING = ""
c_BOOL_DEFAULT = false
c_COMPLEX64_DEFAULT = complex64(0)
c_COMPLEX128_DEFAULT = complex128(0)
c_FLOAT32_DEFAULT = float32(0)
c_FLOAT64_DEFAULT = float64(0)
c_INT64_DEFAULT = int64(0)
c_UINT64_DEFAULT = uint64(0)
c_INT32_DEFAULT = int32(0)
c_UINT32_DEFAULT = uint32(0)
c_INT16_DEFAULT = int16(0)
c_UINT16_DEFAULT = uint16(0)
c_INT8_DEFAULT = int8(0)
c_UINT8_DEFAULT = uint8(0)
c_INT_DEFAULT = int(0)
c_UINT_DEFAULT = uint(0)
c_TIME_DEFAULT time.Time = time.Unix(0, 0)
)
// statement save all the sql info for executing SQL
type Statement struct {
RefTable *Table

View File

@ -115,8 +115,27 @@ var (
uintTypes = sort.StringSlice{"*uint", "*uint16", "*uint32", "*uint8"}
)
var b byte
var tm time.Time
// !nashtsai! treat following var as interal const values, these are used for reflect.TypeOf comparision
var (
c_EMPTY_STRING string
c_BOOL_DEFAULT bool
c_BYTE_DEFAULT byte
c_COMPLEX64_DEFAULT complex64
c_COMPLEX128_DEFAULT complex128
c_FLOAT32_DEFAULT float32
c_FLOAT64_DEFAULT float64
c_INT64_DEFAULT int64
c_UINT64_DEFAULT uint64
c_INT32_DEFAULT int32
c_UINT32_DEFAULT uint32
c_INT16_DEFAULT int16
c_UINT16_DEFAULT uint16
c_INT8_DEFAULT int8
c_UINT8_DEFAULT uint8
c_INT_DEFAULT int
c_UINT_DEFAULT uint
c_TIME_DEFAULT time.Time
)
func Type2SQLType(t reflect.Type) (st SQLType) {
switch k := t.Kind(); k {
@ -131,7 +150,7 @@ func Type2SQLType(t reflect.Type) (st SQLType) {
case reflect.Complex64, reflect.Complex128:
st = SQLType{Varchar, 64, 0}
case reflect.Array, reflect.Slice, reflect.Map:
if t.Elem() == reflect.TypeOf(b) {
if t.Elem() == reflect.TypeOf(c_BYTE_DEFAULT) {
st = SQLType{Blob, 0, 0}
} else {
st = SQLType{Text, 0, 0}
@ -141,7 +160,7 @@ func Type2SQLType(t reflect.Type) (st SQLType) {
case reflect.String:
st = SQLType{Varchar, 255, 0}
case reflect.Struct:
if t == reflect.TypeOf(tm) {
if t == reflect.TypeOf(c_TIME_DEFAULT) {
st = SQLType{DateTime, 0, 0}
} else {
st = SQLType{Text, 0, 0}
@ -200,7 +219,7 @@ func SQLType2Type(st SQLType) reflect.Type {
case Bool:
return reflect.TypeOf(true)
case DateTime, Date, Time, TimeStamp, TimeStampz:
return reflect.TypeOf(tm)
return reflect.TypeOf(c_TIME_DEFAULT)
case Decimal, Numeric:
return reflect.TypeOf("")
default: