xorm/table.go

240 lines
4.4 KiB
Go
Raw Normal View History

2013-05-06 08:01:17 +00:00
package xorm
import (
"reflect"
//"strconv"
2013-08-08 16:03:33 +00:00
"strings"
2013-05-06 08:01:17 +00:00
"time"
)
type SQLType struct {
Name string
DefaultLength int
DefaultLength2 int
}
var (
2013-08-08 16:03:33 +00:00
Bit = "BIT"
TinyInt = "TINYINT"
SmallInt = "SMALLINT"
MediumInt = "MEDIUMINT"
Int = "INT"
Integer = "INTEGER"
BigInt = "BIGINT"
Char = "CHAR"
Varchar = "VARCHAR"
TinyText = "TINYTEXT"
Text = "TEXT"
MediumText = "MEDIUMTEXT"
LongText = "LONGTEXT"
Binary = "BINARY"
VarBinary = "VARBINARY"
Date = "DATE"
DateTime = "DATETIME"
Time = "TIME"
TimeStamp = "TIMESTAMP"
Decimal = "DECIMAL"
Numeric = "NUMERIC"
Real = "REAL"
Float = "FLOAT"
Double = "DOUBLE"
TinyBlob = "TINYBLOB"
Blob = "BLOB"
MediumBlob = "MEDIUMBLOB"
LongBlob = "LONGBLOB"
Bytea = "BYTEA"
Bool = "BOOL"
Serial = "SERIAL"
BigSerial = "BIGSERIAL"
sqlTypes = map[string]bool{
Bit: true,
TinyInt: true,
SmallInt: true,
MediumInt: true,
Int: true,
Integer: true,
BigInt: true,
Char: true,
Varchar: true,
TinyText: true,
Text: true,
MediumText: true,
LongText: true,
Binary: true,
VarBinary: true,
Date: true,
DateTime: true,
Time: true,
TimeStamp: true,
Decimal: true,
Numeric: true,
Real: true,
Float: true,
Double: true,
TinyBlob: true,
Blob: true,
MediumBlob: true,
LongBlob: true,
Bytea: true,
Bool: true,
Serial: true,
BigSerial: true,
}
2013-05-06 08:01:17 +00:00
)
var b byte
var tm time.Time
2013-05-06 08:01:17 +00:00
func Type2SQLType(t reflect.Type) (st SQLType) {
switch k := t.Kind(); k {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
2013-08-08 16:03:33 +00:00
st = SQLType{Int, 0, 0}
case reflect.Int64, reflect.Uint64:
2013-08-08 16:03:33 +00:00
st = SQLType{BigInt, 0, 0}
case reflect.Float32:
2013-08-08 16:03:33 +00:00
st = SQLType{Float, 0, 0}
case reflect.Float64:
2013-08-08 16:03:33 +00:00
st = SQLType{Double, 0, 0}
case reflect.Complex64, reflect.Complex128:
2013-08-08 16:03:33 +00:00
st = SQLType{Varchar, 64, 0}
case reflect.Array, reflect.Slice:
if t.Elem() == reflect.TypeOf(b) {
2013-08-08 16:03:33 +00:00
st = SQLType{Blob, 0, 0}
2013-09-01 02:37:46 +00:00
} else {
st = SQLType{Text, 0, 0}
}
2013-05-06 08:01:17 +00:00
case reflect.Bool:
2013-08-08 16:03:33 +00:00
st = SQLType{Bool, 0, 0}
2013-05-06 08:01:17 +00:00
case reflect.String:
2013-09-01 02:37:46 +00:00
st = SQLType{Varchar, 255, 0}
2013-05-06 08:01:17 +00:00
case reflect.Struct:
if t == reflect.TypeOf(tm) {
2013-08-08 16:03:33 +00:00
st = SQLType{DateTime, 0, 0}
2013-05-06 08:01:17 +00:00
}
default:
2013-09-01 02:37:46 +00:00
st = SQLType{Varchar, 255, 0}
2013-05-06 08:01:17 +00:00
}
return
}
const (
TWOSIDES = iota + 1
ONLYTODB
ONLYFROMDB
)
const (
NONEINDEX = iota
SINGLEINDEX
UNIONINDEX
)
const (
NONEUNIQUE = iota
SINGLEUNIQUE
UNIONUNIQUE
)
2013-05-06 08:01:17 +00:00
type Column struct {
Name string
FieldName string
SQLType SQLType
Length int
Length2 int
Nullable bool
Default string
UniqueType int
UniqueName string
IndexType int
IndexName string
2013-05-06 08:01:17 +00:00
IsPrimaryKey bool
IsAutoIncrement bool
MapType int
2013-05-06 08:01:17 +00:00
}
func (col *Column) String(engine *Engine) string {
sql := engine.Quote(col.Name) + " "
sql += engine.SqlType(col) + " "
if col.IsPrimaryKey {
sql += "PRIMARY KEY "
}
if col.IsAutoIncrement {
sql += engine.AutoIncrStr() + " "
}
if col.Nullable {
sql += "NULL "
} else {
sql += "NOT NULL "
}
if col.Default != "" {
sql += "DEFAULT " + col.Default + " "
}
return sql
}
2013-08-08 16:03:33 +00:00
func (col *Column) ValueOf(bean interface{}) reflect.Value {
var fieldValue reflect.Value
if strings.Contains(col.FieldName, ".") {
fields := strings.Split(col.FieldName, ".")
if len(fields) > 2 {
return reflect.ValueOf(nil)
}
fieldValue = reflect.Indirect(reflect.ValueOf(bean)).FieldByName(fields[0])
fieldValue = fieldValue.FieldByName(fields[1])
} else {
fieldValue = reflect.Indirect(reflect.ValueOf(bean)).FieldByName(col.FieldName)
}
return fieldValue
}
2013-05-06 08:01:17 +00:00
type Table struct {
Name string
Type reflect.Type
2013-09-01 03:01:10 +00:00
ColumnsSeq []string
2013-08-08 16:03:33 +00:00
Columns map[string]*Column
Indexes map[string][]string
Uniques map[string][]string
2013-05-06 08:01:17 +00:00
PrimaryKey string
}
2013-08-08 16:03:33 +00:00
func (table *Table) PKColumn() *Column {
2013-05-06 08:01:17 +00:00
return table.Columns[table.PrimaryKey]
}
2013-09-01 03:01:10 +00:00
func (table *Table) AddColumn(col *Column) {
table.ColumnsSeq = append(table.ColumnsSeq, col.Name)
table.Columns[col.Name] = col
}
func NewTable() *Table {
table := &Table{Indexes: map[string][]string{}, Uniques: map[string][]string{}}
table.Columns = make(map[string]*Column)
table.ColumnsSeq = make([]string, 0)
return table
}
type Conversion interface {
FromDB([]byte) error
ToDB() ([]byte, error)
}