2013-07-03 03:49:29 +00:00
|
|
|
// Copyright 2013 The XORM Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package xorm provides is a simple and powerful ORM for Go. It makes your
|
|
|
|
// database operation simple.
|
|
|
|
|
2013-05-06 08:01:17 +00:00
|
|
|
package xorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2013-06-04 08:56:59 +00:00
|
|
|
//"strconv"
|
2013-05-19 05:25:52 +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 05:24:38 +00:00
|
|
|
Bit = SQLType{"BIT", 0, 0}
|
2013-06-04 08:56:59 +00:00
|
|
|
TinyInt = SQLType{"TINYINT", 0, 0}
|
|
|
|
SmallInt = SQLType{"SMALLINT", 0, 0}
|
|
|
|
MediumInt = SQLType{"MEDIUMINT", 0, 0}
|
2013-08-08 05:24:38 +00:00
|
|
|
Int = SQLType{"INT", 0, 0}
|
|
|
|
Integer = SQLType{"INTEGER", 0, 0}
|
2013-06-04 08:56:59 +00:00
|
|
|
BigInt = SQLType{"BIGINT", 0, 0}
|
2013-08-08 05:24:38 +00:00
|
|
|
|
|
|
|
Char = SQLType{"CHAR", 0, 0}
|
|
|
|
Varchar = SQLType{"VARCHAR", 64, 0}
|
|
|
|
TinyText = SQLType{"TINYTEXT", 0, 0}
|
|
|
|
Text = SQLType{"TEXT", 0, 0}
|
|
|
|
MediumText = SQLType{"MEDIUMTEXT", 0, 0}
|
|
|
|
LongText = SQLType{"LONGTEXT", 0, 0}
|
|
|
|
Binary = SQLType{"BINARY", 0, 0}
|
|
|
|
VarBinary = SQLType{"VARBINARY", 0, 0}
|
|
|
|
|
|
|
|
Date = SQLType{"DATE", 0, 0}
|
2013-06-04 08:56:59 +00:00
|
|
|
DateTime = SQLType{"DATETIME", 0, 0}
|
2013-08-08 05:24:38 +00:00
|
|
|
Time = SQLType{"TIME", 0, 0}
|
2013-06-04 08:56:59 +00:00
|
|
|
TimeStamp = SQLType{"TIMESTAMP", 0, 0}
|
2013-08-08 05:24:38 +00:00
|
|
|
|
|
|
|
Decimal = SQLType{"DECIMAL", 26, 2}
|
|
|
|
Numeric = SQLType{"NUMERIC", 0, 0}
|
|
|
|
|
|
|
|
Real = SQLType{"REAL", 0, 0}
|
|
|
|
Float = SQLType{"FLOAT", 0, 0}
|
|
|
|
Double = SQLType{"DOUBLE", 0, 0}
|
|
|
|
//Money = SQLType{"MONEY", 0, 0}
|
|
|
|
|
|
|
|
TinyBlob = SQLType{"TINYBLOB", 0, 0}
|
|
|
|
Blob = SQLType{"BLOB", 0, 0}
|
|
|
|
MediumBlob = SQLType{"MEDIUMBLOB", 0, 0}
|
|
|
|
LongBlob = SQLType{"LONGBLOB", 0, 0}
|
|
|
|
Bytea = SQLType{"BYTEA", 0, 0}
|
|
|
|
|
|
|
|
Bool = SQLType{"BOOL", 0, 0}
|
|
|
|
|
|
|
|
Serial = SQLType{"SERIAL", 0, 0}
|
|
|
|
BigSerial = SQLType{"BIGSERIAL", 0, 0}
|
2013-05-06 08:01:17 +00:00
|
|
|
)
|
|
|
|
|
2013-06-04 08:56:59 +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 {
|
2013-06-04 08:56:59 +00:00
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
|
2013-05-06 08:01:17 +00:00
|
|
|
st = Int
|
2013-06-04 08:56:59 +00:00
|
|
|
case reflect.Int64, reflect.Uint64:
|
|
|
|
st = BigInt
|
|
|
|
case reflect.Float32:
|
|
|
|
st = Float
|
|
|
|
case reflect.Float64:
|
|
|
|
st = Double
|
|
|
|
case reflect.Complex64, reflect.Complex128:
|
|
|
|
st = Varchar
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
if t.Elem() == reflect.TypeOf(b) {
|
|
|
|
st = Blob
|
|
|
|
}
|
2013-05-06 08:01:17 +00:00
|
|
|
case reflect.Bool:
|
2013-06-04 08:56:59 +00:00
|
|
|
st = TinyInt
|
2013-05-06 08:01:17 +00:00
|
|
|
case reflect.String:
|
|
|
|
st = Varchar
|
|
|
|
case reflect.Struct:
|
2013-06-04 08:56:59 +00:00
|
|
|
if t == reflect.TypeOf(tm) {
|
|
|
|
st = DateTime
|
2013-05-06 08:01:17 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
st = Varchar
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-24 04:08:58 +00:00
|
|
|
const (
|
|
|
|
TWOSIDES = iota + 1
|
|
|
|
ONLYTODB
|
|
|
|
ONLYFROMDB
|
|
|
|
)
|
|
|
|
|
2013-07-27 04:24:38 +00:00
|
|
|
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
|
2013-07-27 04:24:38 +00:00
|
|
|
UniqueType int
|
|
|
|
UniqueName string
|
|
|
|
IndexType int
|
|
|
|
IndexName string
|
2013-05-06 08:01:17 +00:00
|
|
|
IsPrimaryKey bool
|
|
|
|
IsAutoIncrement bool
|
2013-06-24 04:08:58 +00:00
|
|
|
MapType int
|
2013-05-06 08:01:17 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 05:24:38 +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-05-06 08:01:17 +00:00
|
|
|
type Table struct {
|
|
|
|
Name string
|
|
|
|
Type reflect.Type
|
|
|
|
Columns map[string]Column
|
2013-07-27 04:24:38 +00:00
|
|
|
Indexes map[string][]string
|
|
|
|
Uniques map[string][]string
|
2013-05-06 08:01:17 +00:00
|
|
|
PrimaryKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (table *Table) PKColumn() Column {
|
|
|
|
return table.Columns[table.PrimaryKey]
|
|
|
|
}
|
2013-06-25 17:02:22 +00:00
|
|
|
|
|
|
|
type Conversion interface {
|
|
|
|
FromDB([]byte) error
|
|
|
|
ToDB() ([]byte, error)
|
|
|
|
}
|