Merge remote-tracking branch 'upstream/dev' into dev

This commit is contained in:
Nash Tsai 2014-01-09 15:41:55 +08:00
commit cf73ec9f2d
57 changed files with 6359 additions and 5840 deletions

View File

@ -6,6 +6,8 @@ import (
"strings"
"testing"
"time"
"github.com/lunny/xorm/core"
)
/*
@ -1850,7 +1852,7 @@ func testMetaInfo(engine *Engine, t *testing.T) {
for _, table := range tables {
fmt.Println(table.Name)
for _, col := range table.Columns {
for _, col := range table.Columns() {
fmt.Println(col.String(engine.dialect))
}
@ -3175,7 +3177,9 @@ func testPointerData(engine *Engine, t *testing.T) {
// using instance type should just work too
nullData2Get := NullData2{}
has, err = engine.Table("null_data").Id(nullData.Id).Get(&nullData2Get)
tableName := engine.tableMapper.Obj2Table("NullData")
has, err = engine.Table(tableName).Id(nullData.Id).Get(&nullData2Get)
if err != nil {
t.Error(err)
panic(err)
@ -3525,8 +3529,8 @@ func testNullValue(engine *Engine, t *testing.T) {
// skipped postgres test due to postgres driver doesn't read time.Time's timzezone info when stored in the db
// mysql and sqlite3 seem have done this correctly by storing datatime in UTC timezone, I think postgres driver
// prefer using timestamp with timezone to sovle the issue
if engine.DriverName != POSTGRES && engine.DriverName != MYMYSQL &&
engine.DriverName != MYSQL {
if engine.DriverName != core.POSTGRES && engine.DriverName != "mymysql" &&
engine.DriverName != core.MYSQL {
if (*nullDataGet.TimePtr).Unix() != (*nullDataUpdate.TimePtr).Unix() {
t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]:[%v]", *nullDataGet.TimePtr, *nullDataUpdate.TimePtr)))
} else {
@ -3540,7 +3544,9 @@ func testNullValue(engine *Engine, t *testing.T) {
// update to null values
nullDataUpdate = NullData{}
cnt, err = engine.Id(nullData.Id).Cols("string_ptr").Update(&nullDataUpdate)
string_ptr := engine.columnMapper.Obj2Table("StringPtr")
cnt, err = engine.Id(nullData.Id).Cols(string_ptr).Update(&nullDataUpdate)
if err != nil {
t.Error(err)
panic(err)
@ -3896,3 +3902,11 @@ func testAll3(engine *Engine, t *testing.T) {
fmt.Println("-------------- testStringPK --------------")
testStringPK(engine, t)
}
func testAllSnakeMapper(engine *Engine, t *testing.T) {
}
func testAllSameMapper(engine *Engine, t *testing.T) {
}

113
core/column.go Normal file
View File

@ -0,0 +1,113 @@
package core
import (
"fmt"
"reflect"
"strings"
)
const (
TWOSIDES = iota + 1
ONLYTODB
ONLYFROMDB
)
// database column
type Column struct {
Name string
FieldName string
SQLType SQLType
Length int
Length2 int
Nullable bool
Default string
Indexes map[string]bool
IsPrimaryKey bool
IsAutoIncrement bool
MapType int
IsCreated bool
IsUpdated bool
IsCascade bool
IsVersion bool
fieldPath []string
}
func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column {
return &Column{name, fieldName, sqlType, len1, len2, nullable, "", make(map[string]bool), false, false,
TWOSIDES, false, false, false, false, nil}
}
// generate column description string according dialect
func (col *Column) String(d Dialect) string {
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
sql += d.SqlType(col) + " "
if col.IsPrimaryKey {
sql += "PRIMARY KEY "
if col.IsAutoIncrement {
sql += d.AutoIncrStr() + " "
}
}
if col.Nullable {
sql += "NULL "
} else {
sql += "NOT NULL "
}
if col.Default != "" {
sql += "DEFAULT " + col.Default + " "
}
return sql
}
func (col *Column) StringNoPk(d Dialect) string {
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
sql += d.SqlType(col) + " "
if col.Nullable {
sql += "NULL "
} else {
sql += "NOT NULL "
}
if col.Default != "" {
sql += "DEFAULT " + col.Default + " "
}
return sql
}
// return col's filed of struct's value
func (col *Column) ValueOf(bean interface{}) (*reflect.Value, error) {
dataStruct := reflect.Indirect(reflect.ValueOf(bean))
return col.ValueOfV(&dataStruct)
}
func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error) {
var fieldValue reflect.Value
var err error
if col.fieldPath == nil {
col.fieldPath = strings.Split(col.FieldName, ".")
}
if len(col.fieldPath) == 1 {
fieldValue = dataStruct.FieldByName(col.FieldName)
} else if len(col.fieldPath) == 2 {
parentField := dataStruct.FieldByName(col.fieldPath[0])
if parentField.IsValid() {
fieldValue = parentField.FieldByName(col.fieldPath[1])
} else {
err = fmt.Errorf("field %v is not valid", col.FieldName)
}
} else {
err = fmt.Errorf("Unsupported mutliderive %v", col.FieldName)
}
if err != nil {
return nil, err
}
return &fieldValue, nil
}

8
core/converstion.go Normal file
View File

@ -0,0 +1,8 @@
package core
// Conversion is an interface. A type implements Conversion will according
// the custom method to fill into database and retrieve from database.
type Conversion interface {
FromDB([]byte) error
ToDB() ([]byte, error)
}

44
core/db.go Normal file
View File

@ -0,0 +1,44 @@
package core
import (
"database/sql"
"reflect"
)
type DB struct {
*sql.DB
}
func Open(driverName, dataSourceName string) (*DB, error) {
db, err := sql.Open(driverName, dataSourceName)
return &DB{db}, err
}
func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {
rows, err := db.DB.Query(query, args...)
return &Rows{rows}, err
}
type Rows struct {
*sql.Rows
}
func (rs *Rows) Scan(dest ...interface{}) error {
newDest := make([]interface{}, 0)
for _, s := range dest {
vv := reflect.ValueOf(s)
switch vv.Kind() {
case reflect.Ptr:
vvv := vv.Elem()
if vvv.Kind() == reflect.Struct {
for j := 0; j < vvv.NumField(); j++ {
newDest = append(newDest, vvv.FieldByIndex([]int{j}).Addr().Interface())
}
} else {
newDest = append(newDest, s)
}
}
}
return rs.Rows.Scan(newDest...)
}

53
core/db_test.go Normal file
View File

@ -0,0 +1,53 @@
package core
import (
"fmt"
"testing"
_ "github.com/mattn/go-sqlite3"
)
var (
createTableSqlite3 = "CREATE TABLE IF NOT EXISTS `user` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NULL, `title` TEXT NULL, `age` FLOAT NULL, `alias` TEXT NULL, `nick_name` TEXT NULL);"
)
type User struct {
Id int64
Name string
Title string
Age float32
Alias string
NickName string
}
func TestQuery(t *testing.T) {
db, err := Open("sqlite3", "./test.db")
if err != nil {
t.Error(err)
}
_, err = db.Exec(createTableSqlite3)
if err != nil {
t.Error(err)
}
_, err = db.Exec("insert into user (name, title, age, alias, nick_name) values (?,?,?,?,?)",
"xlw", "tester", 1.2, "lunny", "lunny xiao")
if err != nil {
t.Error(err)
}
rows, err := db.Query("select * from user")
if err != nil {
t.Error(err)
}
for rows.Next() {
var user User
err = rows.Scan(&user)
if err != nil {
t.Error(err)
}
fmt.Println(user)
}
}

144
core/dialect.go Normal file
View File

@ -0,0 +1,144 @@
package core
import (
"strings"
"time"
)
type dbType string
type Uri struct {
DbType dbType
Proto string
Host string
Port string
DbName string
User string
Passwd string
Charset string
Laddr string
Raddr string
Timeout time.Duration
}
// a dialect is a driver's wrapper
type Dialect interface {
Init(*Uri, string, string) error
URI() *Uri
DBType() dbType
SqlType(t *Column) string
SupportInsertMany() bool
QuoteStr() string
AutoIncrStr() string
SupportEngine() bool
SupportCharset() bool
IndexOnTable() bool
IndexCheckSql(tableName, idxName string) (string, []interface{})
TableCheckSql(tableName string) (string, []interface{})
ColumnCheckSql(tableName, colName string) (string, []interface{})
GetColumns(tableName string) ([]string, map[string]*Column, error)
GetTables() ([]*Table, error)
GetIndexes(tableName string) (map[string]*Index, error)
CreateTableSql(table *Table, tableName, storeEngine, charset string) string
Filters() []Filter
DriverName() string
DataSourceName() string
}
type Base struct {
dialect Dialect
driverName string
dataSourceName string
*Uri
}
func (b *Base) Init(dialect Dialect, uri *Uri, drivername, dataSourceName string) error {
b.dialect = dialect
b.driverName, b.dataSourceName = drivername, dataSourceName
b.Uri = uri
return nil
}
func (b *Base) URI() *Uri {
return b.Uri
}
func (b *Base) DBType() dbType {
return b.Uri.DbType
}
func (b *Base) DriverName() string {
return b.driverName
}
func (b *Base) DataSourceName() string {
return b.dataSourceName
}
func (b *Base) Quote(c string) string {
return b.dialect.QuoteStr() + c + b.dialect.QuoteStr()
}
func (b *Base) CreateTableSql(table *Table, tableName, storeEngine, charset string) string {
var sql string
sql = "CREATE TABLE IF NOT EXISTS "
if tableName == "" {
tableName = table.Name
}
sql += b.Quote(tableName) + " ("
pkList := table.PrimaryKeys
for _, colName := range table.ColumnsSeq() {
col := table.GetColumn(colName)
if col.IsPrimaryKey && len(pkList) == 1 {
sql += col.String(b.dialect)
} else {
sql += col.StringNoPk(b.dialect)
}
sql = strings.TrimSpace(sql)
sql += ", "
}
if len(pkList) > 1 {
sql += "PRIMARY KEY ( "
sql += strings.Join(pkList, ",")
sql += " ), "
}
sql = sql[:len(sql)-2] + ")"
if b.dialect.SupportEngine() && storeEngine != "" {
sql += " ENGINE=" + storeEngine
}
if b.dialect.SupportCharset() {
if charset == "" {
charset = b.dialect.URI().Charset
}
sql += " DEFAULT CHARSET " + charset
}
sql += ";"
return sql
}
var (
dialects = map[dbType]Dialect{}
)
func RegisterDialect(dbName dbType, dialect Dialect) {
if dialect == nil {
panic("core: Register dialect is nil")
}
if _, dup := dialects[dbName]; dup {
panic("core: Register called twice for dialect " + dbName)
}
dialects[dbName] = dialect
}
func QueryDialect(dbName dbType) Dialect {
return dialects[dbName]
}

23
core/driver.go Normal file
View File

@ -0,0 +1,23 @@
package core
type driver interface {
Parse(string, string) (*Uri, error)
}
var (
drivers = map[string]driver{}
)
func RegisterDriver(driverName string, driver driver) {
if driver == nil {
panic("core: Register driver is nil")
}
if _, dup := drivers[driverName]; dup {
panic("core: Register called twice for driver " + driverName)
}
drivers[driverName] = driver
}
func QueryDriver(driverName string) driver {
return drivers[driverName]
}

42
core/filter.go Normal file
View File

@ -0,0 +1,42 @@
package core
import "strings"
// Filter is an interface to filter SQL
type Filter interface {
Do(sql string, dialect Dialect, table *Table) string
}
// QuoteFilter filter SQL replace ` to database's own quote character
type QuoteFilter struct {
}
func (s *QuoteFilter) Do(sql string, dialect Dialect, table *Table) string {
return strings.Replace(sql, "`", dialect.QuoteStr(), -1)
}
// IdFilter filter SQL replace (id) to primary key column name
type IdFilter struct {
}
type Quoter struct {
dialect Dialect
}
func NewQuoter(dialect Dialect) *Quoter {
return &Quoter{dialect}
}
func (q *Quoter) Quote(content string) string {
return q.dialect.QuoteStr() + content + q.dialect.QuoteStr()
}
func (i *IdFilter) Do(sql string, dialect Dialect, table *Table) string {
quoter := NewQuoter(dialect)
if table != nil && len(table.PrimaryKeys) == 1 {
sql = strings.Replace(sql, "`(id)`", quoter.Quote(table.PrimaryKeys[0]), -1)
sql = strings.Replace(sql, quoter.Quote("(id)"), quoter.Quote(table.PrimaryKeys[0]), -1)
return strings.Replace(sql, "(id)", quoter.Quote(table.PrimaryKeys[0]), -1)
}
return sql
}

25
core/index.go Normal file
View File

@ -0,0 +1,25 @@
package core
const (
IndexType = iota + 1
UniqueType
)
// database index
type Index struct {
Name string
Type int
Cols []string
}
// add columns which will be composite index
func (index *Index) AddColumn(cols ...string) {
for _, col := range cols {
index.Cols = append(index.Cols, col)
}
}
// new an index
func NewIndex(name string, indexType int) *Index {
return &Index{name, indexType, make([]string, 0)}
}

94
core/table.go Normal file
View File

@ -0,0 +1,94 @@
package core
import (
"reflect"
"strings"
)
// database table
type Table struct {
Name string
Type reflect.Type
columnsSeq []string
columns map[string]*Column
Indexes map[string]*Index
PrimaryKeys []string
AutoIncrement string
Created map[string]bool
Updated string
Version string
}
func (table *Table) Columns() map[string]*Column {
return table.columns
}
func (table *Table) ColumnsSeq() []string {
return table.columnsSeq
}
func NewEmptyTable() *Table {
return &Table{columnsSeq: make([]string, 0),
columns: make(map[string]*Column),
Indexes: make(map[string]*Index),
Created: make(map[string]bool),
PrimaryKeys: make([]string, 0),
}
}
func NewTable(name string, t reflect.Type) *Table {
return &Table{Name: name, Type: t,
columnsSeq: make([]string, 0),
columns: make(map[string]*Column),
Indexes: make(map[string]*Index),
Created: make(map[string]bool),
PrimaryKeys: make([]string, 0),
}
}
func (table *Table) GetColumn(name string) *Column {
return table.columns[strings.ToLower(name)]
}
// if has primary key, return column
func (table *Table) PKColumns() []*Column {
columns := make([]*Column, 0)
for _, name := range table.PrimaryKeys {
columns = append(columns, table.GetColumn(name))
}
return columns
}
func (table *Table) AutoIncrColumn() *Column {
return table.GetColumn(table.AutoIncrement)
}
func (table *Table) VersionColumn() *Column {
return table.GetColumn(table.Version)
}
// add a column to table
func (table *Table) AddColumn(col *Column) {
table.columnsSeq = append(table.columnsSeq, col.Name)
table.columns[strings.ToLower(col.Name)] = col
if col.IsPrimaryKey {
table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
}
if col.IsAutoIncrement {
table.AutoIncrement = col.Name
}
if col.IsCreated {
table.Created[col.Name] = true
}
if col.IsUpdated {
table.Updated = col.Name
}
if col.IsVersion {
table.Version = col.Name
}
}
// add an index or an unique to table
func (table *Table) AddIndex(index *Index) {
table.Indexes[index.Name] = index
}

235
core/type.go Normal file
View File

@ -0,0 +1,235 @@
package core
import (
"reflect"
"sort"
"strings"
"time"
)
const (
POSTGRES = "postgres"
SQLITE = "sqlite3"
MYSQL = "mysql"
MSSQL = "mssql"
ORACLE = "oracle"
)
// xorm SQL types
type SQLType struct {
Name string
DefaultLength int
DefaultLength2 int
}
func (s *SQLType) IsText() bool {
return s.Name == Char || s.Name == Varchar || s.Name == TinyText ||
s.Name == Text || s.Name == MediumText || s.Name == LongText
}
func (s *SQLType) IsBlob() bool {
return (s.Name == TinyBlob) || (s.Name == Blob) ||
s.Name == MediumBlob || s.Name == LongBlob ||
s.Name == Binary || s.Name == VarBinary || s.Name == Bytea
}
var (
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"
Date = "DATE"
DateTime = "DATETIME"
Time = "TIME"
TimeStamp = "TIMESTAMP"
TimeStampz = "TIMESTAMPZ"
Decimal = "DECIMAL"
Numeric = "NUMERIC"
Real = "REAL"
Float = "FLOAT"
Double = "DOUBLE"
Binary = "BINARY"
VarBinary = "VARBINARY"
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,
Date: true,
DateTime: true,
Time: true,
TimeStamp: true,
TimeStampz: true,
Decimal: true,
Numeric: true,
Binary: true,
VarBinary: true,
Real: true,
Float: true,
Double: true,
TinyBlob: true,
Blob: true,
MediumBlob: true,
LongBlob: true,
Bytea: true,
Bool: true,
Serial: true,
BigSerial: true,
}
intTypes = sort.StringSlice{"*int", "*int16", "*int32", "*int8"}
uintTypes = sort.StringSlice{"*uint", "*uint16", "*uint32", "*uint8"}
)
// !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 {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
st = SQLType{Int, 0, 0}
case reflect.Int64, reflect.Uint64:
st = SQLType{BigInt, 0, 0}
case reflect.Float32:
st = SQLType{Float, 0, 0}
case reflect.Float64:
st = SQLType{Double, 0, 0}
case reflect.Complex64, reflect.Complex128:
st = SQLType{Varchar, 64, 0}
case reflect.Array, reflect.Slice, reflect.Map:
if t.Elem() == reflect.TypeOf(c_BYTE_DEFAULT) {
st = SQLType{Blob, 0, 0}
} else {
st = SQLType{Text, 0, 0}
}
case reflect.Bool:
st = SQLType{Bool, 0, 0}
case reflect.String:
st = SQLType{Varchar, 255, 0}
case reflect.Struct:
if t == reflect.TypeOf(c_TIME_DEFAULT) {
st = SQLType{DateTime, 0, 0}
} else {
// TODO need to handle association struct
st = SQLType{Text, 0, 0}
}
case reflect.Ptr:
st, _ = ptrType2SQLType(t)
default:
st = SQLType{Text, 0, 0}
}
return
}
func ptrType2SQLType(t reflect.Type) (st SQLType, has bool) {
has = true
switch t {
case reflect.TypeOf(&c_EMPTY_STRING):
st = SQLType{Varchar, 255, 0}
return
case reflect.TypeOf(&c_BOOL_DEFAULT):
st = SQLType{Bool, 0, 0}
case reflect.TypeOf(&c_COMPLEX64_DEFAULT), reflect.TypeOf(&c_COMPLEX128_DEFAULT):
st = SQLType{Varchar, 64, 0}
case reflect.TypeOf(&c_FLOAT32_DEFAULT):
st = SQLType{Float, 0, 0}
case reflect.TypeOf(&c_FLOAT64_DEFAULT):
st = SQLType{Double, 0, 0}
case reflect.TypeOf(&c_INT64_DEFAULT), reflect.TypeOf(&c_UINT64_DEFAULT):
st = SQLType{BigInt, 0, 0}
case reflect.TypeOf(&c_TIME_DEFAULT):
st = SQLType{DateTime, 0, 0}
case reflect.TypeOf(&c_INT_DEFAULT), reflect.TypeOf(&c_INT32_DEFAULT), reflect.TypeOf(&c_INT8_DEFAULT), reflect.TypeOf(&c_INT16_DEFAULT), reflect.TypeOf(&c_UINT_DEFAULT), reflect.TypeOf(&c_UINT32_DEFAULT), reflect.TypeOf(&c_UINT8_DEFAULT), reflect.TypeOf(&c_UINT16_DEFAULT):
st = SQLType{Int, 0, 0}
default:
has = false
}
return
}
// default sql type change to go types
func SQLType2Type(st SQLType) reflect.Type {
name := strings.ToUpper(st.Name)
switch name {
case Bit, TinyInt, SmallInt, MediumInt, Int, Integer, Serial:
return reflect.TypeOf(1)
case BigInt, BigSerial:
return reflect.TypeOf(int64(1))
case Float, Real:
return reflect.TypeOf(float32(1))
case Double:
return reflect.TypeOf(float64(1))
case Char, Varchar, TinyText, Text, MediumText, LongText:
return reflect.TypeOf("")
case TinyBlob, Blob, LongBlob, Bytea, Binary, MediumBlob, VarBinary:
return reflect.TypeOf([]byte{})
case Bool:
return reflect.TypeOf(true)
case DateTime, Date, Time, TimeStamp, TimeStampz:
return reflect.TypeOf(c_TIME_DEFAULT)
case Decimal, Numeric:
return reflect.TypeOf("")
default:
return reflect.TypeOf("")
}
}

View File

@ -1,46 +1,28 @@
package xorm
package dialects
import (
//"crypto/tls"
"database/sql"
"errors"
"fmt"
//"regexp"
"strconv"
"strings"
//"time"
. "github.com/lunny/xorm/core"
)
func init() {
RegisterDialect("mssql", &mssql{})
}
type mssql struct {
base
quoteFilter Filter
Base
}
type odbcParser struct {
}
func (p *odbcParser) parse(driverName, dataSourceName string) (*uri, error) {
kv := strings.Split(dataSourceName, ";")
var dbName string
for _, c := range kv {
vv := strings.Split(strings.TrimSpace(c), "=")
if len(vv) == 2 {
switch strings.ToLower(vv[0]) {
case "database":
dbName = vv[1]
}
}
}
if dbName == "" {
return nil, errors.New("no db name provided")
}
return &uri{dbName: dbName, dbType: MSSQL}, nil
}
func (db *mssql) Init(drivername, uri string) error {
db.quoteFilter = &QuoteFilter{}
return db.base.init(&odbcParser{}, drivername, uri)
func (db *mssql) Init(uri *Uri, drivername, dataSourceName string) error {
return db.Base.Init(db, uri, drivername, dataSourceName)
}
func (db *mssql) SqlType(c *Column) string {
@ -139,27 +121,32 @@ func (db *mssql) GetColumns(tableName string) ([]string, map[string]*Column, err
s := `select a.name as name, b.name as ctype,a.max_length,a.precision,a.scale
from sys.columns a left join sys.types b on a.user_type_id=b.user_type_id
where a.object_id=object_id('` + tableName + `')`
cnn, err := sql.Open(db.driverName, db.dataSourceName)
cnn, err := Open(db.DriverName(), db.DataSourceName())
if err != nil {
return nil, nil, err
}
defer cnn.Close()
res, err := query(cnn, s, args...)
rows, err := cnn.Query(s, args...)
if err != nil {
return nil, nil, err
}
cols := make(map[string]*Column)
colSeq := make([]string, 0)
for _, record := range res {
for rows.Next() {
var name, ctype, precision, scale string
var maxLen int
err = rows.Scan(&name, &ctype, &maxLen, &precision, &scale)
if err != nil {
return nil, nil, err
}
col := new(Column)
col.Indexes = make(map[string]bool)
for name, content := range record {
switch name {
case "name":
col.Length = maxLen
col.Name = strings.Trim(name, "` ")
col.Name = strings.Trim(string(content), "` ")
case "ctype":
ct := strings.ToUpper(string(content))
ct := strings.ToUpper(ctype)
switch ct {
case "DATETIMEOFFSET":
col.SQLType = SQLType{TimeStampz, 0, 0}
@ -168,7 +155,7 @@ where a.object_id=object_id('` + tableName + `')`
case "IMAGE":
col.SQLType = SQLType{VarBinary, 0, 0}
default:
if _, ok := sqlTypes[ct]; ok {
if _, ok := SqlTypes[ct]; ok {
col.SQLType = SQLType{ct, 0, 0}
} else {
return nil, nil, errors.New(fmt.Sprintf("unknow colType %v for %v - %v",
@ -176,14 +163,6 @@ where a.object_id=object_id('` + tableName + `')`
}
}
case "max_length":
len1, err := strconv.Atoi(strings.TrimSpace(string(content)))
if err != nil {
return nil, nil, err
}
col.Length = len1
}
}
if col.SQLType.IsText() {
if col.Default != "" {
col.Default = "'" + col.Default + "'"
@ -198,25 +177,25 @@ where a.object_id=object_id('` + tableName + `')`
func (db *mssql) GetTables() ([]*Table, error) {
args := []interface{}{}
s := `select name from sysobjects where xtype ='U'`
cnn, err := sql.Open(db.driverName, db.dataSourceName)
cnn, err := Open(db.DriverName(), db.DataSourceName())
if err != nil {
return nil, err
}
defer cnn.Close()
res, err := query(cnn, s, args...)
rows, err := cnn.Query(s, args...)
if err != nil {
return nil, err
}
tables := make([]*Table, 0)
for _, record := range res {
table := new(Table)
for name, content := range record {
switch name {
case "name":
table.Name = strings.Trim(string(content), "` ")
}
for rows.Next() {
table := NewEmptyTable()
var name string
err = rows.Scan(&name)
if err != nil {
return nil, err
}
table.Name = strings.Trim(name, "` ")
tables = append(tables, table)
}
return tables, nil
@ -238,24 +217,27 @@ INNER JOIN SYS.COLUMNS C ON IXS.OBJECT_ID=C.OBJECT_ID
AND IXCS.COLUMN_ID=C.COLUMN_ID
WHERE IXS.TYPE_DESC='NONCLUSTERED' and OBJECT_NAME(IXS.OBJECT_ID) =?
`
cnn, err := sql.Open(db.driverName, db.dataSourceName)
cnn, err := Open(db.DriverName(), db.DataSourceName())
if err != nil {
return nil, err
}
defer cnn.Close()
res, err := query(cnn, s, args...)
rows, err := cnn.Query(s, args...)
if err != nil {
return nil, err
}
indexes := make(map[string]*Index, 0)
for _, record := range res {
for rows.Next() {
var indexType int
var indexName, colName string
for name, content := range record {
switch name {
case "IS_UNIQUE":
i, err := strconv.ParseBool(string(content))
var indexName, colName, isUnique string
err = rows.Scan(&indexName, &colName, &isUnique, nil)
if err != nil {
return nil, err
}
i, err := strconv.ParseBool(isUnique)
if err != nil {
return nil, err
}
@ -265,12 +247,8 @@ WHERE IXS.TYPE_DESC='NONCLUSTERED' and OBJECT_NAME(IXS.OBJECT_ID) =?
} else {
indexType = IndexType
}
case "INDEX_NAME":
indexName = string(content)
case "COLUMN_NAME":
colName = strings.Trim(string(content), "` ")
}
}
colName = strings.Trim(colName, "` ")
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
indexName = indexName[5+len(tableName) : len(indexName)]
@ -288,3 +266,41 @@ WHERE IXS.TYPE_DESC='NONCLUSTERED' and OBJECT_NAME(IXS.OBJECT_ID) =?
}
return indexes, nil
}
func (db *mssql) CreateTablSql(table *Table, tableName, storeEngine, charset string) string {
var sql string
if tableName == "" {
tableName = table.Name
}
sql = "IF NOT EXISTS (SELECT [name] FROM sys.tables WHERE [name] = '" + tableName + "' ) CREATE TABLE"
sql += db.QuoteStr() + tableName + db.QuoteStr() + " ("
pkList := table.PrimaryKeys
for _, colName := range table.ColumnsSeq() {
col := table.GetColumn(colName)
if col.IsPrimaryKey && len(pkList) == 1 {
sql += col.String(db)
} else {
sql += col.StringNoPk(db)
}
sql = strings.TrimSpace(sql)
sql += ", "
}
if len(pkList) > 1 {
sql += "PRIMARY KEY ( "
sql += strings.Join(pkList, ",")
sql += " ), "
}
sql = sql[:len(sql)-2] + ")"
sql += ";"
return sql
}
func (db *mssql) Filters() []Filter {
return []Filter{&IdFilter{}, &QuoteFilter{}}
}

276
dialects/mysql.go Normal file
View File

@ -0,0 +1,276 @@
package dialects
import (
"crypto/tls"
"errors"
"fmt"
"strconv"
"strings"
"time"
. "github.com/lunny/xorm/core"
)
func init() {
RegisterDialect("mysql", &mysql{})
}
type mysql struct {
Base
net string
addr string
params map[string]string
loc *time.Location
timeout time.Duration
tls *tls.Config
allowAllFiles bool
allowOldPasswords bool
clientFoundRows bool
}
func (db *mysql) Init(uri *Uri, drivername, dataSourceName string) error {
return db.Base.Init(db, uri, drivername, dataSourceName)
}
func (db *mysql) SqlType(c *Column) string {
var res string
switch t := c.SQLType.Name; t {
case Bool:
res = TinyInt
case Serial:
c.IsAutoIncrement = true
c.IsPrimaryKey = true
c.Nullable = false
res = Int
case BigSerial:
c.IsAutoIncrement = true
c.IsPrimaryKey = true
c.Nullable = false
res = BigInt
case Bytea:
res = Blob
case TimeStampz:
res = Char
c.Length = 64
default:
res = t
}
var hasLen1 bool = (c.Length > 0)
var hasLen2 bool = (c.Length2 > 0)
if hasLen1 {
res += "(" + strconv.Itoa(c.Length) + ")"
} else if hasLen2 {
res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
}
return res
}
func (db *mysql) SupportInsertMany() bool {
return true
}
func (db *mysql) QuoteStr() string {
return "`"
}
func (db *mysql) SupportEngine() bool {
return true
}
func (db *mysql) AutoIncrStr() string {
return "AUTO_INCREMENT"
}
func (db *mysql) SupportCharset() bool {
return true
}
func (db *mysql) IndexOnTable() bool {
return true
}
func (db *mysql) IndexCheckSql(tableName, idxName string) (string, []interface{}) {
args := []interface{}{db.DbName, tableName, idxName}
sql := "SELECT `INDEX_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS`"
sql += " WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `INDEX_NAME`=?"
return sql, args
}
func (db *mysql) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
args := []interface{}{db.DbName, tableName, colName}
sql := "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?"
return sql, args
}
func (db *mysql) TableCheckSql(tableName string) (string, []interface{}) {
args := []interface{}{db.DbName, tableName}
sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?"
return sql, args
}
func (db *mysql) GetColumns(tableName string) ([]string, map[string]*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` = ?"
cnn, err := Open(db.DriverName(), db.DataSourceName())
if err != nil {
return nil, nil, err
}
defer cnn.Close()
rows, err := cnn.Query(s, args...)
if err != nil {
return nil, nil, err
}
cols := make(map[string]*Column)
colSeq := make([]string, 0)
for rows.Next() {
col := new(Column)
col.Indexes = make(map[string]bool)
var columnName, isNullable, colType, colKey, extra string
var colDefault *string
err = rows.Scan(&columnName, &isNullable, &colDefault, &colType, &colKey, &extra)
if err != nil {
return nil, nil, err
}
col.Name = strings.Trim(columnName, "` ")
if "YES" == isNullable {
col.Nullable = true
}
if colDefault != nil {
col.Default = *colDefault
}
cts := strings.Split(colType, "(")
var len1, len2 int
if len(cts) == 2 {
idx := strings.Index(cts[1], ")")
lens := strings.Split(cts[1][0:idx], ",")
len1, err = strconv.Atoi(strings.TrimSpace(lens[0]))
if err != nil {
return nil, nil, err
}
if len(lens) == 2 {
len2, err = strconv.Atoi(lens[1])
if err != nil {
return nil, nil, err
}
}
}
colName := cts[0]
colType = strings.ToUpper(colName)
col.Length = len1
col.Length2 = len2
if _, ok := SqlTypes[colType]; ok {
col.SQLType = SQLType{colType, len1, len2}
} else {
return nil, nil, errors.New(fmt.Sprintf("unkonw colType %v", colType))
}
if colKey == "PRI" {
col.IsPrimaryKey = true
}
if colKey == "UNI" {
//col.is
}
if extra == "auto_increment" {
col.IsAutoIncrement = true
}
if col.SQLType.IsText() {
if col.Default != "" {
col.Default = "'" + col.Default + "'"
}
}
cols[col.Name] = col
colSeq = append(colSeq, col.Name)
}
return colSeq, cols, nil
}
func (db *mysql) GetTables() ([]*Table, error) {
args := []interface{}{db.DbName}
s := "SELECT `TABLE_NAME`, `ENGINE`, `TABLE_ROWS`, `AUTO_INCREMENT` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=?"
cnn, err := Open(db.DriverName(), db.DataSourceName())
if err != nil {
return nil, err
}
defer cnn.Close()
rows, err := cnn.Query(s, args...)
if err != nil {
return nil, err
}
tables := make([]*Table, 0)
for rows.Next() {
table := NewEmptyTable()
var name, engine, tableRows string
var autoIncr *string
err = rows.Scan(&name, &engine, &tableRows, &autoIncr)
if err != nil {
return nil, err
}
table.Name = name
tables = append(tables, table)
}
return tables, nil
}
func (db *mysql) GetIndexes(tableName string) (map[string]*Index, error) {
args := []interface{}{db.DbName, tableName}
s := "SELECT `INDEX_NAME`, `NON_UNIQUE`, `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"
cnn, err := Open(db.DriverName(), db.DataSourceName())
if err != nil {
return nil, err
}
defer cnn.Close()
rows, err := cnn.Query(s, args...)
if err != nil {
return nil, err
}
indexes := make(map[string]*Index, 0)
for rows.Next() {
var indexType int
var indexName, colName, nonUnique string
err = rows.Scan(&indexName, &nonUnique, &colName)
if err != nil {
return nil, err
}
if indexName == "PRIMARY" {
continue
}
if "YES" == nonUnique || nonUnique == "1" {
indexType = IndexType
} else {
indexType = UniqueType
}
colName = strings.Trim(colName, "` ")
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
indexName = indexName[5+len(tableName) : len(indexName)]
}
var index *Index
var ok bool
if index, ok = indexes[indexName]; !ok {
index = new(Index)
index.Type = indexType
index.Name = indexName
indexes[indexName] = index
}
index.AddColumn(colName)
}
return indexes, nil
}
func (db *mysql) Filters() []Filter {
return []Filter{&IdFilter{}}
}

View File

@ -1,45 +1,24 @@
package xorm
package dialects
import (
"database/sql"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
. "github.com/lunny/xorm/core"
)
func init() {
RegisterDialect("oracle", &oracle{})
}
type oracle struct {
base
Base
}
type oracleParser struct {
}
//dataSourceName=user/password@ipv4:port/dbname
//dataSourceName=user/password@[ipv6]:port/dbname
func (p *oracleParser) parse(driverName, dataSourceName string) (*uri, error) {
db := &uri{dbType: ORACLE_OCI}
dsnPattern := regexp.MustCompile(
`^(?P<user>.*)\/(?P<password>.*)@` + // user:password@
`(?P<net>.*)` + // ip:port
`\/(?P<dbname>.*)`) // dbname
matches := dsnPattern.FindStringSubmatch(dataSourceName)
names := dsnPattern.SubexpNames()
for i, match := range matches {
switch names[i] {
case "dbname":
db.dbName = match
}
}
if db.dbName == "" {
return nil, errors.New("dbname is empty")
}
return db, nil
}
func (db *oracle) Init(drivername, uri string) error {
return db.base.init(&oracleParser{}, drivername, uri)
func (db *oracle) Init(uri *Uri, drivername, dataSourceName string) error {
return db.Base.Init(db, uri, drivername, dataSourceName)
}
func (db *oracle) SqlType(c *Column) string {
@ -119,55 +98,55 @@ func (db *oracle) GetColumns(tableName string) ([]string, map[string]*Column, er
s := "SELECT column_name,data_default,data_type,data_length,data_precision,data_scale," +
"nullable FROM USER_TAB_COLUMNS WHERE table_name = :1"
cnn, err := sql.Open(db.driverName, db.dataSourceName)
cnn, err := Open(db.DriverName(), db.DataSourceName())
if err != nil {
return nil, nil, err
}
defer cnn.Close()
res, err := query(cnn, s, args...)
rows, err := cnn.Query(s, args...)
if err != nil {
return nil, nil, err
}
defer rows.Close()
cols := make(map[string]*Column)
colSeq := make([]string, 0)
for _, record := range res {
for rows.Next() {
col := new(Column)
col.Indexes = make(map[string]bool)
for name, content := range record {
switch name {
case "column_name":
col.Name = strings.Trim(string(content), `" `)
case "data_default":
col.Default = string(content)
case "nullable":
if string(content) == "Y" {
var colName, colDefault, nullable, dataType, dataPrecision, dataScale string
var dataLen int
err = rows.Scan(&colName, &colDefault, &dataType, &dataLen, &dataPrecision,
&dataScale, &nullable)
if err != nil {
return nil, nil, err
}
col.Name = strings.Trim(colName, `" `)
col.Default = colDefault
if nullable == "Y" {
col.Nullable = true
} else {
col.Nullable = false
}
case "data_type":
ct := string(content)
switch ct {
switch dataType {
case "VARCHAR2":
col.SQLType = SQLType{Varchar, 0, 0}
case "TIMESTAMP WITH TIME ZONE":
col.SQLType = SQLType{TimeStamp, 0, 0}
col.SQLType = SQLType{TimeStampz, 0, 0}
default:
col.SQLType = SQLType{strings.ToUpper(ct), 0, 0}
}
if _, ok := sqlTypes[col.SQLType.Name]; !ok {
return nil, nil, errors.New(fmt.Sprintf("unkonw colType %v", ct))
}
case "data_length":
i, err := strconv.Atoi(string(content))
if err != nil {
return nil, nil, errors.New("retrieve length error")
}
col.Length = i
case "data_precision":
case "data_scale":
col.SQLType = SQLType{strings.ToUpper(dataType), 0, 0}
}
if _, ok := SqlTypes[col.SQLType.Name]; !ok {
return nil, nil, errors.New(fmt.Sprintf("unkonw colType %v", dataType))
}
col.Length = dataLen
if col.SQLType.IsText() {
if col.Default != "" {
col.Default = "'" + col.Default + "'"
@ -183,25 +162,24 @@ func (db *oracle) GetColumns(tableName string) ([]string, map[string]*Column, er
func (db *oracle) GetTables() ([]*Table, error) {
args := []interface{}{}
s := "SELECT table_name FROM user_tables"
cnn, err := sql.Open(db.driverName, db.dataSourceName)
cnn, err := Open(db.DriverName(), db.DataSourceName())
if err != nil {
return nil, err
}
defer cnn.Close()
res, err := query(cnn, s, args...)
rows, err := cnn.Query(s, args...)
if err != nil {
return nil, err
}
tables := make([]*Table, 0)
for _, record := range res {
table := new(Table)
for name, content := range record {
switch name {
case "table_name":
table.Name = string(content)
}
for rows.Next() {
table := NewEmptyTable()
err = rows.Scan(&table.Name)
if err != nil {
return nil, err
}
tables = append(tables, table)
}
return tables, nil
@ -209,40 +187,37 @@ func (db *oracle) GetTables() ([]*Table, error) {
func (db *oracle) GetIndexes(tableName string) (map[string]*Index, error) {
args := []interface{}{tableName}
s := "SELECT t.column_name,i.table_name,i.uniqueness,i.index_name FROM user_ind_columns t,user_indexes i " +
s := "SELECT t.column_name,i.uniqueness,i.index_name FROM user_ind_columns t,user_indexes i " +
"WHERE t.index_name = i.index_name and t.table_name = i.table_name and t.table_name =:1"
cnn, err := sql.Open(db.driverName, db.dataSourceName)
cnn, err := Open(db.DriverName(), db.DataSourceName())
if err != nil {
return nil, err
}
defer cnn.Close()
res, err := query(cnn, s, args...)
rows, err := cnn.Query(s, args...)
if err != nil {
return nil, err
}
defer rows.Close()
indexes := make(map[string]*Index, 0)
for rows.Next() {
var indexType int
var indexName, colName, uniqueness string
err = rows.Scan(&colName, &uniqueness, &indexName)
if err != nil {
return nil, err
}
indexes := make(map[string]*Index, 0)
for _, record := range res {
var indexType int
var indexName string
var colName string
indexName = strings.Trim(indexName, `" `)
for name, content := range record {
switch name {
case "index_name":
indexName = strings.Trim(string(content), `" `)
case "uniqueness":
c := string(content)
if c == "UNIQUE" {
if uniqueness == "UNIQUE" {
indexType = UniqueType
} else {
indexType = IndexType
}
case "column_name":
colName = string(content)
}
}
var index *Index
var ok bool
@ -256,3 +231,20 @@ func (db *oracle) GetIndexes(tableName string) (map[string]*Index, error) {
}
return indexes, nil
}
// PgSeqFilter filter SQL replace ?, ? ... to :1, :2 ...
type OracleSeqFilter struct {
}
func (s *OracleSeqFilter) Do(sql string, dialect Dialect, table *Table) string {
counts := strings.Count(sql, "?")
for i := 1; i <= counts; i++ {
newstr := ":" + fmt.Sprintf("%v", i)
sql = strings.Replace(sql, "?", newstr, 1)
}
return sql
}
func (db *oracle) Filters() []Filter {
return []Filter{&QuoteFilter{}, &OracleSeqFilter{}, &IdFilter{}}
}

View File

@ -1,65 +1,24 @@
package xorm
package dialects
import (
"database/sql"
"errors"
"fmt"
"strconv"
"strings"
. "github.com/lunny/xorm/core"
)
func init() {
RegisterDialect("postgres", &postgres{})
}
type postgres struct {
base
Base
}
type values map[string]string
func (vs values) Set(k, v string) {
vs[k] = v
}
func (vs values) Get(k string) (v string) {
return vs[k]
}
func errorf(s string, args ...interface{}) {
panic(fmt.Errorf("pq: %s", fmt.Sprintf(s, args...)))
}
func parseOpts(name string, o values) {
if len(name) == 0 {
return
}
name = strings.TrimSpace(name)
ps := strings.Split(name, " ")
for _, p := range ps {
kv := strings.Split(p, "=")
if len(kv) < 2 {
errorf("invalid option: %q", p)
}
o.Set(kv[0], kv[1])
}
}
type postgresParser struct {
}
func (p *postgresParser) parse(driverName, dataSourceName string) (*uri, error) {
db := &uri{dbType: POSTGRES}
o := make(values)
parseOpts(dataSourceName, o)
db.dbName = o.Get("dbname")
if db.dbName == "" {
return nil, errors.New("dbname is empty")
}
return db, nil
}
func (db *postgres) Init(drivername, uri string) error {
return db.base.init(&postgresParser{}, drivername, uri)
func (db *postgres) Init(uri *Uri, drivername, dataSourceName string) error {
return db.Base.Init(db, uri, drivername, dataSourceName)
}
func (db *postgres) SqlType(c *Column) string {
@ -153,40 +112,53 @@ func (db *postgres) GetColumns(tableName string) ([]string, map[string]*Column,
args := []interface{}{tableName}
s := "SELECT column_name, column_default, is_nullable, data_type, character_maximum_length" +
", numeric_precision, numeric_precision_radix FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = $1"
cnn, err := sql.Open(db.driverName, db.dataSourceName)
cnn, err := Open(db.DriverName(), db.DataSourceName())
if err != nil {
return nil, nil, err
}
defer cnn.Close()
res, err := query(cnn, s, args...)
rows, err := cnn.Query(s, args...)
if err != nil {
return nil, nil, err
}
cols := make(map[string]*Column)
colSeq := make([]string, 0)
for _, record := range res {
for rows.Next() {
col := new(Column)
col.Indexes = make(map[string]bool)
for name, content := range record {
switch name {
case "column_name":
col.Name = strings.Trim(string(content), `" `)
case "column_default":
if strings.HasPrefix(string(content), "nextval") {
var colName, isNullable, dataType string
var maxLenStr, colDefault, numPrecision, numRadix *string
err = rows.Scan(&colName, &colDefault, &isNullable, &dataType, &maxLenStr, &numPrecision, &numRadix)
if err != nil {
return nil, nil, err
}
var maxLen int
if maxLenStr != nil {
maxLen, err = strconv.Atoi(*maxLenStr)
if err != nil {
return nil, nil, err
}
}
col.Name = strings.Trim(colName, `" `)
if colDefault != nil {
if strings.HasPrefix(*colDefault, "nextval") {
col.IsPrimaryKey = true
} else {
col.Default = string(content)
col.Default = *colDefault
}
case "is_nullable":
if string(content) == "YES" {
}
if isNullable == "YES" {
col.Nullable = true
} else {
col.Nullable = false
}
case "data_type":
ct := string(content)
switch ct {
switch dataType {
case "character varying", "character":
col.SQLType = SQLType{Varchar, 0, 0}
case "timestamp without time zone":
@ -200,21 +172,14 @@ func (db *postgres) GetColumns(tableName string) ([]string, map[string]*Column,
case "time without time zone":
col.SQLType = SQLType{Time, 0, 0}
default:
col.SQLType = SQLType{strings.ToUpper(ct), 0, 0}
}
if _, ok := sqlTypes[col.SQLType.Name]; !ok {
return nil, nil, errors.New(fmt.Sprintf("unkonw colType %v", ct))
}
case "character_maximum_length":
i, err := strconv.Atoi(string(content))
if err != nil {
return nil, nil, errors.New("retrieve length error")
}
col.Length = i
case "numeric_precision":
case "numeric_precision_radix":
col.SQLType = SQLType{strings.ToUpper(dataType), 0, 0}
}
if _, ok := SqlTypes[col.SQLType.Name]; !ok {
return nil, nil, errors.New(fmt.Sprintf("unkonw colType %v", dataType))
}
col.Length = maxLen
if col.SQLType.IsText() {
if col.Default != "" {
col.Default = "'" + col.Default + "'"
@ -230,25 +195,25 @@ func (db *postgres) GetColumns(tableName string) ([]string, map[string]*Column,
func (db *postgres) GetTables() ([]*Table, error) {
args := []interface{}{}
s := "SELECT tablename FROM pg_tables where schemaname = 'public'"
cnn, err := sql.Open(db.driverName, db.dataSourceName)
cnn, err := Open(db.DriverName(), db.DataSourceName())
if err != nil {
return nil, err
}
defer cnn.Close()
res, err := query(cnn, s, args...)
rows, err := cnn.Query(s, args...)
if err != nil {
return nil, err
}
tables := make([]*Table, 0)
for _, record := range res {
table := new(Table)
for name, content := range record {
switch name {
case "tablename":
table.Name = string(content)
}
for rows.Next() {
table := NewEmptyTable()
var name string
err = rows.Scan(&name)
if err != nil {
return nil, err
}
table.Name = name
tables = append(tables, table)
}
return tables, nil
@ -256,39 +221,37 @@ func (db *postgres) GetTables() ([]*Table, error) {
func (db *postgres) GetIndexes(tableName string) (map[string]*Index, error) {
args := []interface{}{tableName}
s := "SELECT tablename, indexname, indexdef FROM pg_indexes WHERE schemaname = 'public' and tablename = $1"
s := "SELECT indexname, indexdef FROM pg_indexes WHERE schemaname = 'public' and tablename = $1"
cnn, err := sql.Open(db.driverName, db.dataSourceName)
cnn, err := Open(db.DriverName(), db.DataSourceName())
if err != nil {
return nil, err
}
defer cnn.Close()
res, err := query(cnn, s, args...)
rows, err := cnn.Query(s, args...)
if err != nil {
return nil, err
}
indexes := make(map[string]*Index, 0)
for _, record := range res {
for rows.Next() {
var indexType int
var indexName string
var indexName, indexdef string
var colNames []string
err = rows.Scan(&indexName, &indexdef)
if err != nil {
return nil, err
}
indexName = strings.Trim(indexName, `" `)
for name, content := range record {
switch name {
case "indexname":
indexName = strings.Trim(string(content), `" `)
case "indexdef":
c := string(content)
if strings.HasPrefix(c, "CREATE UNIQUE INDEX") {
if strings.HasPrefix(indexdef, "CREATE UNIQUE INDEX") {
indexType = UniqueType
} else {
indexType = IndexType
}
cs := strings.Split(c, "(")
cs := strings.Split(indexdef, "(")
colNames = strings.Split(cs[1][0:len(cs[1])-1], ",")
}
}
if strings.HasSuffix(indexName, "_pkey") {
continue
}
@ -307,3 +270,24 @@ func (db *postgres) GetIndexes(tableName string) (map[string]*Index, error) {
}
return indexes, nil
}
// PgSeqFilter filter SQL replace ?, ? ... to $1, $2 ...
type PgSeqFilter struct {
}
func (s *PgSeqFilter) Do(sql string, dialect Dialect, table *Table) string {
segs := strings.Split(sql, "?")
size := len(segs)
res := ""
for i, c := range segs {
if i < size-1 {
res += c + fmt.Sprintf("$%v", i+1)
}
}
res += segs[size-1]
return res
}
func (db *postgres) Filters() []Filter {
return []Filter{&IdFilter{}, &QuoteFilter{}, &PgSeqFilter{}}
}

View File

@ -1,23 +1,21 @@
package xorm
package dialects
import (
"database/sql"
"strings"
. "github.com/lunny/xorm/core"
)
func init() {
RegisterDialect("sqlite3", &sqlite3{})
}
type sqlite3 struct {
base
Base
}
type sqlite3Parser struct {
}
func (p *sqlite3Parser) parse(driverName, dataSourceName string) (*uri, error) {
return &uri{dbType: SQLITE, dbName: dataSourceName}, nil
}
func (db *sqlite3) Init(drivername, dataSourceName string) error {
return db.base.init(&sqlite3Parser{}, drivername, dataSourceName)
func (db *sqlite3) Init(uri *Uri, drivername, dataSourceName string) error {
return db.Base.Init(db, uri, drivername, dataSourceName)
}
func (db *sqlite3) SqlType(c *Column) string {
@ -89,28 +87,29 @@ func (db *sqlite3) ColumnCheckSql(tableName, colName string) (string, []interfac
func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*Column, error) {
args := []interface{}{tableName}
s := "SELECT sql FROM sqlite_master WHERE type='table' and name = ?"
cnn, err := sql.Open(db.driverName, db.dataSourceName)
cnn, err := Open(db.DriverName(), db.DataSourceName())
if err != nil {
return nil, nil, err
}
defer cnn.Close()
res, err := query(cnn, s, args...)
rows, err := cnn.Query(s, args...)
if err != nil {
return nil, nil, err
}
defer rows.Close()
var sql string
for _, record := range res {
for name, content := range record {
if name == "sql" {
sql = string(content)
}
var name string
for rows.Next() {
err = rows.Scan(&name)
if err != nil {
return nil, nil, err
}
}
nStart := strings.Index(sql, "(")
nEnd := strings.Index(sql, ")")
colCreates := strings.Split(sql[nStart+1:nEnd], ",")
nStart := strings.Index(name, "(")
nEnd := strings.Index(name, ")")
colCreates := strings.Split(name[nStart+1:nEnd], ",")
cols := make(map[string]*Column)
colSeq := make([]string, 0)
for _, colStr := range colCreates {
@ -148,24 +147,23 @@ func (db *sqlite3) GetTables() ([]*Table, error) {
args := []interface{}{}
s := "SELECT name FROM sqlite_master WHERE type='table'"
cnn, err := sql.Open(db.driverName, db.dataSourceName)
cnn, err := Open(db.DriverName(), db.DataSourceName())
if err != nil {
return nil, err
}
defer cnn.Close()
res, err := query(cnn, s, args...)
rows, err := cnn.Query(s, args...)
if err != nil {
return nil, err
}
defer rows.Close()
tables := make([]*Table, 0)
for _, record := range res {
table := new(Table)
for name, content := range record {
switch name {
case "name":
table.Name = string(content)
}
for rows.Next() {
table := NewEmptyTable()
err = rows.Scan(&table.Name)
if err != nil {
return nil, err
}
if table.Name == "sqlite_sequence" {
continue
@ -178,25 +176,30 @@ func (db *sqlite3) GetTables() ([]*Table, error) {
func (db *sqlite3) GetIndexes(tableName string) (map[string]*Index, error) {
args := []interface{}{tableName}
s := "SELECT sql FROM sqlite_master WHERE type='index' and tbl_name = ?"
cnn, err := sql.Open(db.driverName, db.dataSourceName)
cnn, err := Open(db.DriverName(), db.DataSourceName())
if err != nil {
return nil, err
}
defer cnn.Close()
res, err := query(cnn, s, args...)
rows, err := cnn.Query(s, args...)
if err != nil {
return nil, err
}
defer rows.Close()
indexes := make(map[string]*Index, 0)
for _, record := range res {
index := new(Index)
sql := string(record["sql"])
for rows.Next() {
var sql string
err = rows.Scan(&sql)
if err != nil {
return nil, err
}
if sql == "" {
continue
}
index := new(Index)
nNStart := strings.Index(sql, "INDEX")
nNEnd := strings.Index(sql, "ON")
if nNStart == -1 || nNEnd == -1 {
@ -230,3 +233,7 @@ func (db *sqlite3) GetIndexes(tableName string) (map[string]*Index, error) {
return indexes, nil
}
func (db *sqlite3) Filters() []Filter {
return []Filter{&IdFilter{}}
}

38
drivers/goracle.go Normal file
View File

@ -0,0 +1,38 @@
package drivers
import (
"errors"
"regexp"
"github.com/lunny/xorm/core"
)
func init() {
core.RegisterDriver("goracle", &goracleDriver{})
}
type goracleDriver struct {
}
func (cfg *goracleDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
db := &core.Uri{DbType: core.ORACLE}
dsnPattern := regexp.MustCompile(
`^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
`(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
`\/(?P<dbname>.*?)` + // /dbname
`(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1&paramN=valueN]
matches := dsnPattern.FindStringSubmatch(dataSourceName)
//tlsConfigRegister := make(map[string]*tls.Config)
names := dsnPattern.SubexpNames()
for i, match := range matches {
switch names[i] {
case "dbname":
db.DbName = match
}
}
if db.DbName == "" {
return nil, errors.New("dbname is empty")
}
return db, nil
}

View File

@ -1,20 +1,22 @@
package xorm
package drivers
import (
"errors"
"strings"
"time"
"github.com/lunny/xorm/core"
)
type mymysql struct {
mysql
func init() {
core.RegisterDriver("mymysql", &mymysqlDriver{})
}
type mymysqlParser struct {
type mymysqlDriver struct {
}
func (p *mymysqlParser) parse(driverName, dataSourceName string) (*uri, error) {
db := &uri{dbType: MYSQL}
func (p *mymysqlDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
db := &core.Uri{DbType: core.MYSQL}
pd := strings.SplitN(dataSourceName, "*", 2)
if len(pd) == 2 {
@ -23,9 +25,9 @@ func (p *mymysqlParser) parse(driverName, dataSourceName string) (*uri, error) {
if len(p) != 2 {
return nil, errors.New("Wrong protocol part of URI")
}
db.proto = p[0]
db.Proto = p[0]
options := strings.Split(p[1], ",")
db.raddr = options[0]
db.Raddr = options[0]
for _, o := range options[1:] {
kv := strings.SplitN(o, "=", 2)
var k, v string
@ -36,13 +38,13 @@ func (p *mymysqlParser) parse(driverName, dataSourceName string) (*uri, error) {
}
switch k {
case "laddr":
db.laddr = v
db.Laddr = v
case "timeout":
to, err := time.ParseDuration(v)
if err != nil {
return nil, err
}
db.timeout = to
db.Timeout = to
default:
return nil, errors.New("Unknown option: " + k)
}
@ -55,13 +57,9 @@ func (p *mymysqlParser) parse(driverName, dataSourceName string) (*uri, error) {
if len(dup) != 3 {
return nil, errors.New("Wrong database part of URI")
}
db.dbName = dup[0]
db.user = dup[1]
db.passwd = dup[2]
db.DbName = dup[0]
db.User = dup[1]
db.Passwd = dup[2]
return db, nil
}
func (db *mymysql) Init(drivername, uri string) error {
return db.mysql.base.init(&mymysqlParser{}, drivername, uri)
}

50
drivers/mysql.go Normal file
View File

@ -0,0 +1,50 @@
package drivers
import (
"regexp"
"strings"
"github.com/lunny/xorm/core"
)
func init() {
core.RegisterDriver("mysql", &mysqlDriver{})
}
type mysqlDriver struct {
}
func (p *mysqlDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
dsnPattern := regexp.MustCompile(
`^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
`(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
`\/(?P<dbname>.*?)` + // /dbname
`(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1&paramN=valueN]
matches := dsnPattern.FindStringSubmatch(dataSourceName)
//tlsConfigRegister := make(map[string]*tls.Config)
names := dsnPattern.SubexpNames()
uri := &core.Uri{DbType: core.MYSQL}
for i, match := range matches {
switch names[i] {
case "dbname":
uri.DbName = match
case "params":
if len(match) > 0 {
kvs := strings.Split(match, "&")
for _, kv := range kvs {
splits := strings.Split(kv, "=")
if len(splits) == 2 {
switch splits[0] {
case "charset":
uri.Charset = splits[1]
}
}
}
}
}
}
return uri, nil
}

37
drivers/oci.go Normal file
View File

@ -0,0 +1,37 @@
package drivers
import (
"errors"
"regexp"
"github.com/lunny/xorm/core"
)
func init() {
core.RegisterDriver("oci", &ociDriver{})
}
type ociDriver struct {
}
//dataSourceName=user/password@ipv4:port/dbname
//dataSourceName=user/password@[ipv6]:port/dbname
func (p *ociDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
db := &core.Uri{DbType: core.ORACLE}
dsnPattern := regexp.MustCompile(
`^(?P<user>.*)\/(?P<password>.*)@` + // user:password@
`(?P<net>.*)` + // ip:port
`\/(?P<dbname>.*)`) // dbname
matches := dsnPattern.FindStringSubmatch(dataSourceName)
names := dsnPattern.SubexpNames()
for i, match := range matches {
switch names[i] {
case "dbname":
db.DbName = match
}
}
if db.DbName == "" {
return nil, errors.New("dbname is empty")
}
return db, nil
}

34
drivers/odbc.go Normal file
View File

@ -0,0 +1,34 @@
package drivers
import (
"errors"
"strings"
"github.com/lunny/xorm/core"
)
func init() {
core.RegisterDriver("odbc", &odbcDriver{})
}
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 {
switch strings.ToLower(vv[0]) {
case "database":
dbName = vv[1]
}
}
}
if dbName == "" {
return nil, errors.New("no db name provided")
}
return &core.Uri{DbName: dbName, DbType: core.MSSQL}, nil
}

59
drivers/pq.go Normal file
View File

@ -0,0 +1,59 @@
package drivers
import (
"errors"
"fmt"
"strings"
"github.com/lunny/xorm/core"
)
func init() {
core.RegisterDriver("postgres", &pqDriver{})
}
type pqDriver struct {
}
type values map[string]string
func (vs values) Set(k, v string) {
vs[k] = v
}
func (vs values) Get(k string) (v string) {
return vs[k]
}
func errorf(s string, args ...interface{}) {
panic(fmt.Errorf("pq: %s", fmt.Sprintf(s, args...)))
}
func parseOpts(name string, o values) {
if len(name) == 0 {
return
}
name = strings.TrimSpace(name)
ps := strings.Split(name, " ")
for _, p := range ps {
kv := strings.Split(p, "=")
if len(kv) < 2 {
errorf("invalid option: %q", p)
}
o.Set(kv[0], kv[1])
}
}
func (p *pqDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
db := &core.Uri{DbType: core.POSTGRES}
o := make(values)
parseOpts(dataSourceName, o)
db.DbName = o.Get("dbname")
if db.DbName == "" {
return nil, errors.New("dbname is empty")
}
return db, nil
}

16
drivers/sqlite3.go Normal file
View File

@ -0,0 +1,16 @@
package drivers
import (
"github.com/lunny/xorm/core"
)
func init() {
core.RegisterDriver("sqlite3", &sqlite3Driver{})
}
type sqlite3Driver struct {
}
func (p *sqlite3Driver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
return &core.Uri{DbType: core.SQLITE, DbName: dataSourceName}, nil
}

174
engine.go
View File

@ -12,40 +12,10 @@ import (
"strconv"
"strings"
"sync"
"github.com/lunny/xorm/core"
)
const (
POSTGRES = "postgres"
SQLITE = "sqlite3"
MYSQL = "mysql"
MYMYSQL = "mymysql"
MSSQL = "mssql"
ORACLE_OCI = "oci8"
)
// a dialect is a driver's wrapper
type dialect interface {
Init(DriverName, DataSourceName string) error
URI() *uri
DBType() string
SqlType(t *Column) string
SupportInsertMany() bool
QuoteStr() string
AutoIncrStr() string
SupportEngine() bool
SupportCharset() bool
IndexOnTable() bool
IndexCheckSql(tableName, idxName string) (string, []interface{})
TableCheckSql(tableName string) (string, []interface{})
ColumnCheckSql(tableName, colName string) (string, []interface{})
GetColumns(tableName string) ([]string, map[string]*Column, error)
GetTables() ([]*Table, error)
GetIndexes(tableName string) (map[string]*Index, error)
}
type PK []interface{}
// Engine is the major struct of xorm, it means a database manager.
@ -56,18 +26,19 @@ type Engine struct {
TagIdentifier string
DriverName string
DataSourceName string
dialect dialect
Tables map[reflect.Type]*Table
dialect core.Dialect
Tables map[reflect.Type]*core.Table
mutex *sync.Mutex
ShowSQL bool
ShowErr bool
ShowDebug bool
ShowWarn bool
Pool IConnectPool
Filters []Filter
Filters []core.Filter
Logger io.Writer
Cacher Cacher
UseCache bool
tableCachers map[reflect.Type]Cacher
}
func (engine *Engine) SetMapper(mapper IMapper) {
@ -102,8 +73,8 @@ func (engine *Engine) Quote(sql string) string {
return engine.dialect.QuoteStr() + sql + engine.dialect.QuoteStr()
}
// A simple wrapper to dialect's SqlType method
func (engine *Engine) SqlType(c *Column) string {
// A simple wrapper to dialect's core.SqlType method
func (engine *Engine) SqlType(c *core.Column) string {
return engine.dialect.SqlType(c)
}
@ -130,13 +101,8 @@ func (engine *Engine) SetMaxIdleConns(conns int) {
// SetDefaltCacher set the default cacher. Xorm's default not enable cacher.
func (engine *Engine) SetDefaultCacher(cacher Cacher) {
if cacher == nil {
engine.UseCache = false
} else {
engine.UseCache = true
engine.Cacher = cacher
}
}
// If you has set default cacher, and you want temporilly stop use cache,
// you can use NoCache()
@ -156,7 +122,7 @@ func (engine *Engine) NoCascade() *Session {
func (engine *Engine) MapCacher(bean interface{}, cacher Cacher) {
t := rType(bean)
engine.autoMapType(t)
engine.Tables[t].Cacher = cacher
engine.tableCachers[t] = cacher
}
// OpenDB provides a interface to operate database directly.
@ -235,7 +201,7 @@ func (engine *Engine) NoAutoTime() *Session {
}
// Retrieve all tables, columns, indexes' informations from database.
func (engine *Engine) DBMetas() ([]*Table, error) {
func (engine *Engine) DBMetas() ([]*core.Table, error) {
tables, err := engine.dialect.GetTables()
if err != nil {
return nil, err
@ -246,8 +212,11 @@ func (engine *Engine) DBMetas() ([]*Table, error) {
if err != nil {
return nil, err
}
table.Columns = cols
table.ColumnsSeq = colSeq
for _, name := range colSeq {
table.AddColumn(cols[name])
}
//table.Columns = cols
//table.ColumnsSeq = colSeq
indexes, err := engine.dialect.GetIndexes(table.Name)
if err != nil {
@ -257,10 +226,10 @@ func (engine *Engine) DBMetas() ([]*Table, error) {
for _, index := range indexes {
for _, name := range index.Cols {
if col, ok := table.Columns[name]; ok {
if col := table.GetColumn(name); col != nil {
col.Indexes[index.Name] = true
} else {
return nil, fmt.Errorf("Unknown col "+name+" in indexes %v", table.Columns)
return nil, fmt.Errorf("Unknown col "+name+" in indexes %v", index)
}
}
}
@ -420,7 +389,7 @@ func (engine *Engine) Having(conditions string) *Session {
return session.Having(conditions)
}
func (engine *Engine) autoMapType(t reflect.Type) *Table {
func (engine *Engine) autoMapType(t reflect.Type) *core.Table {
engine.mutex.Lock()
defer engine.mutex.Unlock()
table, ok := engine.Tables[t]
@ -431,37 +400,31 @@ func (engine *Engine) autoMapType(t reflect.Type) *Table {
return table
}
func (engine *Engine) autoMap(bean interface{}) *Table {
func (engine *Engine) autoMap(bean interface{}) *core.Table {
t := rType(bean)
return engine.autoMapType(t)
}
func (engine *Engine) newTable() *Table {
table := &Table{}
table.Indexes = make(map[string]*Index)
table.Columns = make(map[string]*Column)
table.ColumnsSeq = make([]string, 0)
table.Created = make(map[string]bool)
table.Cacher = engine.Cacher
return table
func (engine *Engine) mapType(t reflect.Type) *core.Table {
return mappingTable(t, engine.tableMapper, engine.columnMapper, engine.dialect, engine.TagIdentifier)
}
func (engine *Engine) mapType(t reflect.Type) *Table {
table := engine.newTable()
table.Name = engine.tableMapper.Obj2Table(t.Name())
func mappingTable(t reflect.Type, tableMapper IMapper, colMapper IMapper, dialect core.Dialect, tagId string) *core.Table {
table := core.NewEmptyTable()
table.Name = tableMapper.Obj2Table(t.Name())
table.Type = t
var idFieldColName string
for i := 0; i < t.NumField(); i++ {
tag := t.Field(i).Tag
ormTagStr := tag.Get(engine.TagIdentifier)
var col *Column
ormTagStr := tag.Get(tagId)
var col *core.Column
fieldType := t.Field(i).Type
if ormTagStr != "" {
col = &Column{FieldName: t.Field(i).Name, Nullable: true, IsPrimaryKey: false,
IsAutoIncrement: false, MapType: TWOSIDES, Indexes: make(map[string]bool)}
col = &core.Column{FieldName: t.Field(i).Name, Nullable: true, IsPrimaryKey: false,
IsAutoIncrement: false, MapType: core.TWOSIDES, Indexes: make(map[string]bool)}
tags := strings.Split(ormTagStr, " ")
if len(tags) > 0 {
@ -470,25 +433,24 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
}
if (strings.ToUpper(tags[0]) == "EXTENDS") &&
(fieldType.Kind() == reflect.Struct) {
parentTable := engine.mapType(fieldType)
for name, col := range parentTable.Columns {
parentTable := mappingTable(fieldType, tableMapper, colMapper, dialect, tagId)
for _, col := range parentTable.Columns() {
col.FieldName = fmt.Sprintf("%v.%v", fieldType.Name(), col.FieldName)
table.Columns[strings.ToLower(name)] = col
table.ColumnsSeq = append(table.ColumnsSeq, name)
table.AddColumn(col)
}
table.PrimaryKeys = parentTable.PrimaryKeys
continue
}
var indexType int
var indexName string
for j, key := range tags {
k := strings.ToUpper(key)
switch {
case k == "<-":
col.MapType = ONLYFROMDB
col.MapType = core.ONLYFROMDB
case k == "->":
col.MapType = ONLYTODB
col.MapType = core.ONLYTODB
case k == "PK":
col.IsPrimaryKey = true
col.Nullable = false
@ -506,15 +468,15 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
case k == "UPDATED":
col.IsUpdated = true
case strings.HasPrefix(k, "INDEX(") && strings.HasSuffix(k, ")"):
indexType = IndexType
indexType = core.IndexType
indexName = k[len("INDEX")+1 : len(k)-1]
case k == "INDEX":
indexType = IndexType
indexType = core.IndexType
case strings.HasPrefix(k, "UNIQUE(") && strings.HasSuffix(k, ")"):
indexName = k[len("UNIQUE")+1 : len(k)-1]
indexType = UniqueType
indexType = core.UniqueType
case k == "UNIQUE":
indexType = UniqueType
indexType = core.UniqueType
case k == "NOTNULL":
col.Nullable = false
case k == "NOT":
@ -525,10 +487,10 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
}
} else if strings.Contains(k, "(") && strings.HasSuffix(k, ")") {
fs := strings.Split(k, "(")
if _, ok := sqlTypes[fs[0]]; !ok {
if _, ok := core.SqlTypes[fs[0]]; !ok {
continue
}
col.SQLType = SQLType{fs[0], 0, 0}
col.SQLType = core.SQLType{fs[0], 0, 0}
fs2 := strings.Split(fs[1][0:len(fs[1])-1], ",")
if len(fs2) == 2 {
col.Length, _ = strconv.Atoi(fs2[0])
@ -537,17 +499,17 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
col.Length, _ = strconv.Atoi(fs2[0])
}
} else {
if _, ok := sqlTypes[k]; ok {
col.SQLType = SQLType{k, 0, 0}
if _, ok := core.SqlTypes[k]; ok {
col.SQLType = core.SQLType{k, 0, 0}
} else if key != col.Default {
col.Name = key
}
}
engine.SqlType(col)
dialect.SqlType(col)
}
}
if col.SQLType.Name == "" {
col.SQLType = Type2SQLType(fieldType)
col.SQLType = core.Type2SQLType(fieldType)
}
if col.Length == 0 {
col.Length = col.SQLType.DefaultLength
@ -556,9 +518,9 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
col.Length2 = col.SQLType.DefaultLength2
}
if col.Name == "" {
col.Name = engine.columnMapper.Obj2Table(t.Field(i).Name)
col.Name = colMapper.Obj2Table(t.Field(i).Name)
}
if indexType == IndexType {
if indexType == core.IndexType {
if indexName == "" {
indexName = col.Name
}
@ -566,12 +528,12 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
index.AddColumn(col.Name)
col.Indexes[index.Name] = true
} else {
index := NewIndex(indexName, IndexType)
index := core.NewIndex(indexName, core.IndexType)
index.AddColumn(col.Name)
table.AddIndex(index)
col.Indexes[index.Name] = true
}
} else if indexType == UniqueType {
} else if indexType == core.UniqueType {
if indexName == "" {
indexName = col.Name
}
@ -579,7 +541,7 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
index.AddColumn(col.Name)
col.Indexes[index.Name] = true
} else {
index := NewIndex(indexName, UniqueType)
index := core.NewIndex(indexName, core.UniqueType)
index.AddColumn(col.Name)
table.AddIndex(index)
col.Indexes[index.Name] = true
@ -587,10 +549,9 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
}
}
} else {
sqlType := Type2SQLType(fieldType)
col = &Column{engine.columnMapper.Obj2Table(t.Field(i).Name), t.Field(i).Name, sqlType,
sqlType.DefaultLength, sqlType.DefaultLength2, true, "", make(map[string]bool), false, false,
TWOSIDES, false, false, false, false}
sqlType := core.Type2SQLType(fieldType)
col = core.NewColumn(colMapper.Obj2Table(t.Field(i).Name), t.Field(i).Name, sqlType,
sqlType.DefaultLength, sqlType.DefaultLength2, true)
}
if col.IsAutoIncrement {
col.Nullable = false
@ -604,7 +565,7 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
}
if idFieldColName != "" && len(table.PrimaryKeys) == 0 {
col := table.Columns[strings.ToLower(idFieldColName)]
col := table.GetColumn(idFieldColName)
col.IsPrimaryKey = true
col.IsAutoIncrement = true
col.Nullable = false
@ -666,6 +627,13 @@ func (engine *Engine) CreateUniques(bean interface{}) error {
return session.CreateUniques(bean)
}
func (engine *Engine) getCacher(t reflect.Type) Cacher {
if cacher, ok := engine.tableCachers[t]; ok {
return cacher
}
return engine.Cacher
}
// If enabled cache, clear the cache bean
func (engine *Engine) ClearCacheBean(bean interface{}, id int64) error {
t := rType(bean)
@ -673,9 +641,10 @@ func (engine *Engine) ClearCacheBean(bean interface{}, id int64) error {
return errors.New("error params")
}
table := engine.autoMap(bean)
if table.Cacher != nil {
table.Cacher.ClearIds(table.Name)
table.Cacher.DelBean(table.Name, id)
cacher := engine.getCacher(t)
if cacher != nil {
cacher.ClearIds(table.Name)
cacher.DelBean(table.Name, id)
}
return nil
}
@ -688,9 +657,10 @@ func (engine *Engine) ClearCache(beans ...interface{}) error {
return errors.New("error params")
}
table := engine.autoMap(bean)
if table.Cacher != nil {
table.Cacher.ClearIds(table.Name)
table.Cacher.ClearBeans(table.Name)
cacher := engine.getCacher(t)
if cacher != nil {
cacher.ClearIds(table.Name)
cacher.ClearBeans(table.Name)
}
}
return nil
@ -730,7 +700,7 @@ func (engine *Engine) Sync(beans ...interface{}) error {
return err
}
} else {
for _, col := range table.Columns {
for _, col := range table.Columns() {
session := engine.NewSession()
session.Statement.RefTable = table
defer session.Close()
@ -753,7 +723,7 @@ func (engine *Engine) Sync(beans ...interface{}) error {
session := engine.NewSession()
session.Statement.RefTable = table
defer session.Close()
if index.Type == UniqueType {
if index.Type == core.UniqueType {
//isExist, err := session.isIndexExist(table.Name, name, true)
isExist, err := session.isIndexExist2(table.Name, index.Cols, true)
if err != nil {
@ -768,7 +738,7 @@ func (engine *Engine) Sync(beans ...interface{}) error {
return err
}
}
} else if index.Type == IndexType {
} else if index.Type == core.IndexType {
isExist, err := session.isIndexExist2(table.Name, index.Cols, false)
if err != nil {
return err

View File

@ -1,49 +0,0 @@
package xorm
import (
"fmt"
"strings"
)
// Filter is an interface to filter SQL
type Filter interface {
Do(sql string, session *Session) string
}
// PgSeqFilter filter SQL replace ?, ? ... to $1, $2 ...
type PgSeqFilter struct {
}
func (s *PgSeqFilter) Do(sql string, session *Session) string {
segs := strings.Split(sql, "?")
size := len(segs)
res := ""
for i, c := range segs {
if i < size-1 {
res += c + fmt.Sprintf("$%v", i+1)
}
}
res += segs[size-1]
return res
}
// QuoteFilter filter SQL replace ` to database's own quote character
type QuoteFilter struct {
}
func (s *QuoteFilter) Do(sql string, session *Session) string {
return strings.Replace(sql, "`", session.Engine.QuoteStr(), -1)
}
// IdFilter filter SQL replace (id) to primary key column name
type IdFilter struct {
}
func (i *IdFilter) Do(sql string, session *Session) string {
if session.Statement.RefTable != nil && len(session.Statement.RefTable.PrimaryKeys) == 1 {
sql = strings.Replace(sql, "`(id)`", session.Engine.Quote(session.Statement.RefTable.PrimaryKeys[0]), -1)
sql = strings.Replace(sql, session.Engine.Quote("(id)"), session.Engine.Quote(session.Statement.RefTable.PrimaryKeys[0]), -1)
return strings.Replace(sql, "(id)", session.Engine.Quote(session.Statement.RefTable.PrimaryKeys[0]), -1)
}
return sql
}

344
mysql.go
View File

@ -1,344 +0,0 @@
package xorm
import (
"crypto/tls"
"database/sql"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
type uri struct {
dbType string
proto string
host string
port string
dbName string
user string
passwd string
charset string
laddr string
raddr string
timeout time.Duration
}
type parser interface {
parse(driverName, dataSourceName string) (*uri, error)
}
type mysqlParser struct {
}
func (p *mysqlParser) parse(driverName, dataSourceName string) (*uri, error) {
dsnPattern := regexp.MustCompile(
`^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
`(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
`\/(?P<dbname>.*?)` + // /dbname
`(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1&paramN=valueN]
matches := dsnPattern.FindStringSubmatch(dataSourceName)
//tlsConfigRegister := make(map[string]*tls.Config)
names := dsnPattern.SubexpNames()
uri := &uri{dbType: MYSQL}
for i, match := range matches {
switch names[i] {
case "dbname":
uri.dbName = match
case "params":
if len(match) > 0 {
kvs := strings.Split(match, "&")
for _, kv := range kvs {
splits := strings.Split(kv, "=")
if len(splits) == 2 {
switch splits[0] {
case "charset":
uri.charset = splits[1]
}
}
}
}
}
}
return uri, nil
}
type base struct {
parser parser
driverName string
dataSourceName string
*uri
}
func (b *base) init(parser parser, drivername, dataSourceName string) (err error) {
b.parser = parser
b.driverName, b.dataSourceName = drivername, dataSourceName
b.uri, err = b.parser.parse(b.driverName, b.dataSourceName)
return
}
func (b *base) URI() *uri {
return b.uri
}
func (b *base) DBType() string {
return b.uri.dbType
}
type mysql struct {
base
net string
addr string
params map[string]string
loc *time.Location
timeout time.Duration
tls *tls.Config
allowAllFiles bool
allowOldPasswords bool
clientFoundRows bool
}
func (db *mysql) Init(drivername, uri string) error {
return db.base.init(&mysqlParser{}, drivername, uri)
}
func (db *mysql) SqlType(c *Column) string {
var res string
switch t := c.SQLType.Name; t {
case Bool:
res = TinyInt
case Serial:
c.IsAutoIncrement = true
c.IsPrimaryKey = true
c.Nullable = false
res = Int
case BigSerial:
c.IsAutoIncrement = true
c.IsPrimaryKey = true
c.Nullable = false
res = BigInt
case Bytea:
res = Blob
case TimeStampz:
res = Char
c.Length = 64
default:
res = t
}
var hasLen1 bool = (c.Length > 0)
var hasLen2 bool = (c.Length2 > 0)
if hasLen1 {
res += "(" + strconv.Itoa(c.Length) + ")"
} else if hasLen2 {
res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
}
return res
}
func (db *mysql) SupportInsertMany() bool {
return true
}
func (db *mysql) QuoteStr() string {
return "`"
}
func (db *mysql) SupportEngine() bool {
return true
}
func (db *mysql) AutoIncrStr() string {
return "AUTO_INCREMENT"
}
func (db *mysql) SupportCharset() bool {
return true
}
func (db *mysql) IndexOnTable() bool {
return true
}
func (db *mysql) IndexCheckSql(tableName, idxName string) (string, []interface{}) {
args := []interface{}{db.dbName, tableName, idxName}
sql := "SELECT `INDEX_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS`"
sql += " WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `INDEX_NAME`=?"
return sql, args
}
func (db *mysql) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
args := []interface{}{db.dbName, tableName, colName}
sql := "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?"
return sql, args
}
func (db *mysql) TableCheckSql(tableName string) (string, []interface{}) {
args := []interface{}{db.dbName, tableName}
sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?"
return sql, args
}
func (db *mysql) GetColumns(tableName string) ([]string, map[string]*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` = ?"
cnn, err := sql.Open(db.driverName, db.dataSourceName)
if err != nil {
return nil, nil, err
}
defer cnn.Close()
res, err := query(cnn, s, args...)
if err != nil {
return nil, nil, err
}
cols := make(map[string]*Column)
colSeq := make([]string, 0)
for _, record := range res {
col := new(Column)
col.Indexes = make(map[string]bool)
for name, content := range record {
switch name {
case "COLUMN_NAME":
col.Name = strings.Trim(string(content), "` ")
case "IS_NULLABLE":
if "YES" == string(content) {
col.Nullable = true
}
case "COLUMN_DEFAULT":
// add ''
col.Default = string(content)
case "COLUMN_TYPE":
cts := strings.Split(string(content), "(")
var len1, len2 int
if len(cts) == 2 {
idx := strings.Index(cts[1], ")")
lens := strings.Split(cts[1][0:idx], ",")
len1, err = strconv.Atoi(strings.TrimSpace(lens[0]))
if err != nil {
return nil, nil, err
}
if len(lens) == 2 {
len2, err = strconv.Atoi(lens[1])
if err != nil {
return nil, nil, err
}
}
}
colName := cts[0]
colType := strings.ToUpper(colName)
col.Length = len1
col.Length2 = len2
if _, ok := sqlTypes[colType]; ok {
col.SQLType = SQLType{colType, len1, len2}
} else {
return nil, nil, errors.New(fmt.Sprintf("unkonw colType %v", colType))
}
case "COLUMN_KEY":
key := string(content)
if key == "PRI" {
col.IsPrimaryKey = true
}
if key == "UNI" {
//col.is
}
case "EXTRA":
extra := string(content)
if extra == "auto_increment" {
col.IsAutoIncrement = true
}
}
}
if col.SQLType.IsText() {
if col.Default != "" {
col.Default = "'" + col.Default + "'"
}
}
cols[col.Name] = col
colSeq = append(colSeq, col.Name)
}
return colSeq, cols, nil
}
func (db *mysql) GetTables() ([]*Table, error) {
args := []interface{}{db.dbName}
s := "SELECT `TABLE_NAME`, `ENGINE`, `TABLE_ROWS`, `AUTO_INCREMENT` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=?"
cnn, err := sql.Open(db.driverName, db.dataSourceName)
if err != nil {
return nil, err
}
defer cnn.Close()
res, err := query(cnn, s, args...)
if err != nil {
return nil, err
}
tables := make([]*Table, 0)
for _, record := range res {
table := new(Table)
for name, content := range record {
switch name {
case "TABLE_NAME":
table.Name = strings.Trim(string(content), "` ")
case "ENGINE":
}
}
tables = append(tables, table)
}
return tables, nil
}
func (db *mysql) GetIndexes(tableName string) (map[string]*Index, error) {
args := []interface{}{db.dbName, tableName}
s := "SELECT `INDEX_NAME`, `NON_UNIQUE`, `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"
cnn, err := sql.Open(db.driverName, db.dataSourceName)
if err != nil {
return nil, err
}
defer cnn.Close()
res, err := query(cnn, s, args...)
if err != nil {
return nil, err
}
indexes := make(map[string]*Index, 0)
for _, record := range res {
var indexType int
var indexName, colName string
for name, content := range record {
switch name {
case "NON_UNIQUE":
if "YES" == string(content) || string(content) == "1" {
indexType = IndexType
} else {
indexType = UniqueType
}
case "INDEX_NAME":
indexName = string(content)
case "COLUMN_NAME":
colName = strings.Trim(string(content), "` ")
}
}
if indexName == "PRIMARY" {
continue
}
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
indexName = indexName[5+len(tableName) : len(indexName)]
}
var index *Index
var ok bool
if index, ok = indexes[indexName]; !ok {
index = new(Index)
index.Type = indexType
index.Name = indexName
indexes[indexName] = index
}
index.AddColumn(colName)
}
return indexes, nil
}

View File

@ -35,6 +35,30 @@ func TestMysql(t *testing.T) {
testAll3(engine, t)
}
func TestMysqlSameMapper(t *testing.T) {
err := mysqlDdlImport()
if err != nil {
t.Error(err)
return
}
engine, err := NewEngine("mysql", "root:@/xorm_test3?charset=utf8")
defer engine.Close()
if err != nil {
t.Error(err)
return
}
engine.ShowSQL = showTestSql
engine.ShowErr = showTestSql
engine.ShowWarn = showTestSql
engine.ShowDebug = showTestSql
engine.SetMapper(SameMapper{})
testAll(engine, t)
testAll2(engine, t)
testAll3(engine, t)
}
func TestMysqlWithCache(t *testing.T) {
err := mysqlDdlImport()
if err != nil {

View File

@ -41,7 +41,7 @@ func newRows(session *Session, bean interface{}) (*Rows, error) {
}
for _, filter := range rows.session.Engine.Filters {
sql = filter.Do(sql, session)
sql = filter.Do(sql, session.Engine.dialect, rows.session.Statement.RefTable)
}
rows.session.Engine.LogSQL(sql)

View File

@ -9,6 +9,8 @@ import (
"strconv"
"strings"
"time"
"github.com/lunny/xorm/core"
)
// Struct Session keep a pointer to sql.DB and provides all execution of all
@ -108,7 +110,7 @@ func (session *Session) After(closures func(interface{})) *Session {
return session
}
// Method Table can input a string or pointer to struct for special a table to operate.
// Method core.Table can input a string or pointer to struct for special a table to operate.
func (session *Session) Table(tableNameOrBean interface{}) *Session {
session.Statement.Table(tableNameOrBean)
return session
@ -354,13 +356,10 @@ func (session *Session) scanMapIntoStruct(obj interface{}, objMap map[string][]b
}
table := session.Engine.autoMapType(rType(obj))
var col *core.Column
for key, data := range objMap {
key = strings.ToLower(key)
var col *Column
var ok bool
if col, ok = table.Columns[key]; !ok {
session.Engine.LogWarn(fmt.Sprintf("table %v's has not column %v. %v", table.Name, key, table.ColumnsSeq))
if col = table.GetColumn(key); col == nil {
session.Engine.LogWarn(fmt.Sprintf("table %v's has not column %v. %v", table.Name, key, table.Columns()))
continue
}
@ -410,7 +409,7 @@ func (session *Session) innerExec(sqlStr string, args ...interface{}) (sql.Resul
func (session *Session) exec(sqlStr string, args ...interface{}) (sql.Result, error) {
for _, filter := range session.Engine.Filters {
sqlStr = filter.Do(sqlStr, session)
sqlStr = filter.Do(sqlStr, session.Engine.dialect, session.Statement.RefTable)
}
session.Engine.LogSQL(sqlStr)
@ -596,14 +595,14 @@ func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interf
return false, ErrCacheFailed
}
for _, filter := range session.Engine.Filters {
sqlStr = filter.Do(sqlStr, session)
sqlStr = filter.Do(sqlStr, session.Engine.dialect, session.Statement.RefTable)
}
newsql := session.Statement.convertIdSql(sqlStr)
if newsql == "" {
return false, ErrCacheFailed
}
cacher := session.Statement.RefTable.Cacher
cacher := session.Engine.getCacher(session.Statement.RefTable.Type)
tableName := session.Statement.TableName()
session.Engine.LogDebug("[xorm:cacheGet] find sql:", newsql, args)
ids, err := getCacheSql(cacher, tableName, newsql, args)
@ -679,7 +678,7 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in
}
for _, filter := range session.Engine.Filters {
sqlStr = filter.Do(sqlStr, session)
sqlStr = filter.Do(sqlStr, session.Engine.dialect, session.Statement.RefTable)
}
newsql := session.Statement.convertIdSql(sqlStr)
@ -688,7 +687,7 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in
}
table := session.Statement.RefTable
cacher := table.Cacher
cacher := session.Engine.getCacher(t)
ids, err := getCacheSql(cacher, session.Statement.TableName(), newsql, args)
if err != nil {
//session.Engine.LogError(err)
@ -892,7 +891,7 @@ func (session *Session) Get(bean interface{}) (bool, error) {
args = session.Statement.RawParams
}
if session.Statement.RefTable.Cacher != nil && session.Statement.UseCache {
if cacher := session.Engine.getCacher(session.Statement.RefTable.Type); cacher != nil && session.Statement.UseCache {
has, err := session.cacheGet(bean, sqlStr, args...)
if err != ErrCacheFailed {
return has, err
@ -1003,7 +1002,7 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
}
sliceElementType := sliceValue.Type().Elem()
var table *Table
var table *core.Table
if session.Statement.RefTable == nil {
if sliceElementType.Kind() == reflect.Ptr {
if sliceElementType.Elem().Kind() == reflect.Struct {
@ -1045,7 +1044,7 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
args = session.Statement.RawParams
}
if table.Cacher != nil &&
if cacher := session.Engine.getCacher(table.Type); cacher != nil &&
session.Statement.UseCache &&
!session.Statement.IsDistinct {
err = session.cacheFind(sliceElementType, sqlStr, rowsSlicePtr, args...)
@ -1092,23 +1091,39 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
fieldsCount := len(fields)
for rawRows.Next() {
var newValue reflect.Value
var newElemFunc func() reflect.Value
if sliceElementType.Kind() == reflect.Ptr {
newValue = reflect.New(sliceElementType.Elem())
} else {
newValue = reflect.New(sliceElementType)
newElemFunc = func() reflect.Value {
return reflect.New(sliceElementType.Elem())
}
} else {
newElemFunc = func() reflect.Value {
return reflect.New(sliceElementType)
}
}
var sliceValueSetFunc func(*reflect.Value)
if sliceValue.Kind() == reflect.Slice {
if sliceElementType.Kind() == reflect.Ptr {
sliceValueSetFunc = func(newValue *reflect.Value) {
sliceValue.Set(reflect.Append(sliceValue, reflect.ValueOf(newValue.Interface())))
}
} else {
sliceValueSetFunc = func(newValue *reflect.Value) {
sliceValue.Set(reflect.Append(sliceValue, reflect.Indirect(reflect.ValueOf(newValue.Interface()))))
}
}
}
for rawRows.Next() {
var newValue reflect.Value = newElemFunc()
if sliceValueSetFunc != nil {
err := session.row2Bean(rawRows, fields, fieldsCount, newValue.Interface())
if err != nil {
return err
}
if sliceValue.Kind() == reflect.Slice {
if sliceElementType.Kind() == reflect.Ptr {
sliceValue.Set(reflect.Append(sliceValue, reflect.ValueOf(newValue.Interface())))
} else {
sliceValue.Set(reflect.Append(sliceValue, reflect.Indirect(reflect.ValueOf(newValue.Interface()))))
}
sliceValueSetFunc(&newValue)
}
}
} else {
@ -1236,9 +1251,9 @@ func (session *Session) isIndexExist2(tableName string, cols []string, unique bo
for _, index := range indexes {
if sliceEq(index.Cols, cols) {
if unique {
return index.Type == UniqueType, nil
return index.Type == core.UniqueType, nil
} else {
return index.Type == IndexType, nil
return index.Type == core.IndexType, nil
}
}
}
@ -1256,7 +1271,7 @@ func (session *Session) addColumn(colName string) error {
}
//fmt.Println(session.Statement.RefTable)
col := session.Statement.RefTable.Columns[strings.ToLower(colName)]
col := session.Statement.RefTable.GetColumn(colName)
sql, args := session.Statement.genAddColumnStr(col)
_, err = session.exec(sql, args...)
return err
@ -1345,34 +1360,25 @@ func row2map(rows *sql.Rows, fields []string) (resultsMap map[string][]byte, err
return result, nil
}
func (session *Session) getField(dataStruct *reflect.Value, key string, table *Table) *reflect.Value {
func (session *Session) getField(dataStruct *reflect.Value, key string, table *core.Table) *reflect.Value {
var col *core.Column
if col = table.GetColumn(key); col == nil {
session.Engine.LogWarn(fmt.Sprintf("table %v's has not column %v. %v", table.Name, key, table.Columns()))
return nil
}
key = strings.ToLower(key)
if _, ok := table.Columns[key]; !ok {
session.Engine.LogWarn(fmt.Sprintf("table %v's has not column %v. %v", table.Name, key, table.ColumnsSeq))
fieldValue, err := col.ValueOfV(dataStruct)
if err != nil {
session.Engine.LogError(err)
return nil
}
col := table.Columns[key]
fieldName := col.FieldName
fieldPath := strings.Split(fieldName, ".")
var fieldValue reflect.Value
if len(fieldPath) > 2 {
session.Engine.LogError("Unsupported mutliderive", fieldName)
return nil
} else if len(fieldPath) == 2 {
parentField := dataStruct.FieldByName(fieldPath[0])
if parentField.IsValid() {
fieldValue = parentField.FieldByName(fieldPath[1])
}
} else {
fieldValue = dataStruct.FieldByName(fieldName)
}
if !fieldValue.IsValid() || !fieldValue.CanSet() {
session.Engine.LogWarn("table %v's column %v is not valid or cannot set",
table.Name, key)
return nil
}
return &fieldValue
return fieldValue
}
func (session *Session) row2Bean(rows *sql.Rows, fields []string, fieldsCount int, bean interface{}) error {
@ -1395,7 +1401,6 @@ func (session *Session) row2Bean(rows *sql.Rows, fields []string, fieldsCount in
for ii, key := range fields {
if fieldValue := session.getField(&dataStruct, key, table); fieldValue != nil {
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
//if row is null then ignore
@ -1404,7 +1409,7 @@ func (session *Session) row2Bean(rows *sql.Rows, fields []string, fieldsCount in
continue
}
if structConvert, ok := fieldValue.Addr().Interface().(Conversion); ok {
if structConvert, ok := fieldValue.Addr().Interface().(core.Conversion); ok {
if data, err := value2Bytes(&rawValue); err == nil {
structConvert.FromDB(data)
} else {
@ -1634,7 +1639,7 @@ func (session *Session) row2Bean(rows *sql.Rows, fields []string, fieldsCount in
if !hasAssigned {
data, err := value2Bytes(&rawValue)
if err == nil {
session.bytes2Value(table.Columns[key], fieldValue, data)
session.bytes2Value(table.GetColumn(key), fieldValue, data)
} else {
session.Engine.LogError(err.Error())
}
@ -1647,7 +1652,7 @@ func (session *Session) row2Bean(rows *sql.Rows, fields []string, fieldsCount in
func (session *Session) queryPreprocess(sqlStr *string, paramStr ...interface{}) {
for _, filter := range session.Engine.Filters {
*sqlStr = filter.Do(*sqlStr, session)
*sqlStr = filter.Do(*sqlStr, session.Engine.dialect, session.Statement.RefTable)
}
session.Engine.LogSQL(*sqlStr)
@ -1763,7 +1768,7 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
colNames := make([]string, 0)
colMultiPlaces := make([]string, 0)
var args = make([]interface{}, 0)
cols := make([]*Column, 0)
cols := make([]*core.Column, 0)
for i := 0; i < size; i++ {
elemValue := sliceValue.Index(i).Interface()
@ -1781,12 +1786,12 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
// --
if i == 0 {
for _, col := range table.Columns {
for _, col := range table.Columns() {
fieldValue := reflect.Indirect(reflect.ValueOf(elemValue)).FieldByName(col.FieldName)
if col.IsAutoIncrement && fieldValue.Int() == 0 {
continue
}
if col.MapType == ONLYFROMDB {
if col.MapType == core.ONLYFROMDB {
continue
}
if session.Statement.ColumnStr != "" {
@ -1814,7 +1819,7 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
if col.IsAutoIncrement && fieldValue.Int() == 0 {
continue
}
if col.MapType == ONLYFROMDB {
if col.MapType == core.ONLYFROMDB {
continue
}
if session.Statement.ColumnStr != "" {
@ -1853,7 +1858,7 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
return 0, err
}
if table.Cacher != nil && session.Statement.UseCache {
if cacher := session.Engine.getCacher(table.Type); cacher != nil && session.Statement.UseCache {
session.cacheInsert(session.Statement.TableName())
}
@ -1904,7 +1909,7 @@ func (session *Session) InsertMulti(rowsSlicePtr interface{}) (int64, error) {
return session.innerInsertMulti(rowsSlicePtr)
}
func (session *Session) byte2Time(col *Column, data []byte) (outTime time.Time, outErr error) {
func (session *Session) byte2Time(col *core.Column, data []byte) (outTime time.Time, outErr error) {
sdata := strings.TrimSpace(string(data))
var x time.Time
var err error
@ -1929,7 +1934,7 @@ func (session *Session) byte2Time(col *Column, data []byte) (outTime time.Time,
x, err = time.Parse("2006-01-02 15:04:05", sdata)
} else if len(sdata) == 10 && sdata[4] == '-' && sdata[7] == '-' {
x, err = time.Parse("2006-01-02", sdata)
} else if col.SQLType.Name == Time {
} else if col.SQLType.Name == core.Time {
if strings.Contains(sdata, " ") {
ssd := strings.Split(sdata, " ")
sdata = ssd[1]
@ -1937,7 +1942,7 @@ func (session *Session) byte2Time(col *Column, data []byte) (outTime time.Time,
sdata = strings.TrimSpace(sdata)
//fmt.Println(sdata)
if session.Engine.dialect.DBType() == MYSQL && len(sdata) > 8 {
if session.Engine.dialect.DBType() == core.MYSQL && len(sdata) > 8 {
sdata = sdata[len(sdata)-8:]
}
//fmt.Println(sdata)
@ -1957,8 +1962,8 @@ func (session *Session) byte2Time(col *Column, data []byte) (outTime time.Time,
}
// convert a db data([]byte) to a field value
func (session *Session) bytes2Value(col *Column, fieldValue *reflect.Value, data []byte) error {
if structConvert, ok := fieldValue.Addr().Interface().(Conversion); ok {
func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value, data []byte) error {
if structConvert, ok := fieldValue.Addr().Interface().(core.Conversion); ok {
return structConvert.FromDB(data)
}
@ -2018,8 +2023,8 @@ func (session *Session) bytes2Value(col *Column, fieldValue *reflect.Value, data
var x int64
var err error
// for mysql, when use bit, it returned \x01
if col.SQLType.Name == Bit &&
session.Engine.dialect.DBType() == MYSQL {
if col.SQLType.Name == core.Bit &&
session.Engine.dialect.DBType() == core.MYSQL {
if len(data) == 1 {
x = int64(data[0])
} else {
@ -2199,7 +2204,7 @@ func (session *Session) bytes2Value(col *Column, fieldValue *reflect.Value, data
var x int64
var err error
// for mysql, when use bit, it returned \x01
if col.SQLType.Name == Bit &&
if col.SQLType.Name == core.Bit &&
strings.Contains(session.Engine.DriverName, "mysql") {
if len(data) == 1 {
x = int64(data[0])
@ -2225,7 +2230,7 @@ func (session *Session) bytes2Value(col *Column, fieldValue *reflect.Value, data
var x1 int64
var err error
// for mysql, when use bit, it returned \x01
if col.SQLType.Name == Bit &&
if col.SQLType.Name == core.Bit &&
strings.Contains(session.Engine.DriverName, "mysql") {
if len(data) == 1 {
x = int(data[0])
@ -2254,7 +2259,7 @@ func (session *Session) bytes2Value(col *Column, fieldValue *reflect.Value, data
var x1 int64
var err error
// for mysql, when use bit, it returned \x01
if col.SQLType.Name == Bit &&
if col.SQLType.Name == core.Bit &&
strings.Contains(session.Engine.DriverName, "mysql") {
if len(data) == 1 {
x = int32(data[0])
@ -2283,7 +2288,7 @@ func (session *Session) bytes2Value(col *Column, fieldValue *reflect.Value, data
var x1 int64
var err error
// for mysql, when use bit, it returned \x01
if col.SQLType.Name == Bit &&
if col.SQLType.Name == core.Bit &&
strings.Contains(session.Engine.DriverName, "mysql") {
if len(data) == 1 {
x = int8(data[0])
@ -2312,7 +2317,7 @@ func (session *Session) bytes2Value(col *Column, fieldValue *reflect.Value, data
var x1 int64
var err error
// for mysql, when use bit, it returned \x01
if col.SQLType.Name == Bit &&
if col.SQLType.Name == core.Bit &&
strings.Contains(session.Engine.DriverName, "mysql") {
if len(data) == 1 {
x = int16(data[0])
@ -2345,9 +2350,9 @@ func (session *Session) bytes2Value(col *Column, fieldValue *reflect.Value, data
}
// convert a field value of a struct to interface for put into db
func (session *Session) value2Interface(col *Column, fieldValue reflect.Value) (interface{}, error) {
func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Value) (interface{}, error) {
if fieldValue.CanAddr() {
if fieldConvert, ok := fieldValue.Addr().Interface().(Conversion); ok {
if fieldConvert, ok := fieldValue.Addr().Interface().(core.Conversion); ok {
data, err := fieldConvert.ToDB()
if err != nil {
return 0, err
@ -2384,19 +2389,19 @@ func (session *Session) value2Interface(col *Column, fieldValue reflect.Value) (
case reflect.Struct:
if fieldType == reflect.TypeOf(c_TIME_DEFAULT) {
t := fieldValue.Interface().(time.Time)
if session.Engine.dialect.DBType() == MSSQL {
if session.Engine.dialect.DBType() == core.MSSQL {
if t.IsZero() {
return nil, nil
}
}
if col.SQLType.Name == Time {
if col.SQLType.Name == core.Time {
//s := fieldValue.Interface().(time.Time).Format("2006-01-02 15:04:05 -0700")
s := fieldValue.Interface().(time.Time).Format(time.RFC3339)
return s[11:19], nil
} else if col.SQLType.Name == Date {
} else if col.SQLType.Name == core.Date {
return fieldValue.Interface().(time.Time).Format("2006-01-02"), nil
} else if col.SQLType.Name == TimeStampz {
if session.Engine.dialect.DBType() == MSSQL {
} else if col.SQLType.Name == core.TimeStampz {
if session.Engine.dialect.DBType() == core.MSSQL {
tf := t.Format("2006-01-02T15:04:05.9999999Z07:00")
return tf, nil
}
@ -2470,7 +2475,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
}
// --
colNames, args, err := table.genCols(session, bean, false, false)
colNames, args, err := genCols(table, session, bean, false, false)
if err != nil {
return 0, err
}
@ -2519,7 +2524,7 @@ 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.DriverName != POSTGRES || table.AutoIncrement == "" {
if session.Engine.DriverName != core.POSTGRES || table.AutoIncrement == "" {
res, err := session.exec(sqlStr, args...)
if err != nil {
return 0, err
@ -2527,13 +2532,15 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
handleAfterInsertProcessorFunc(bean)
}
if table.Cacher != nil && session.Statement.UseCache {
if cacher := session.Engine.getCacher(table.Type); cacher != nil && session.Statement.UseCache {
session.cacheInsert(session.Statement.TableName())
}
if table.Version != "" && session.Statement.checkVersion {
verValue := table.VersionColumn().ValueOf(bean)
if verValue.IsValid() && verValue.CanSet() {
verValue, err := table.VersionColumn().ValueOf(bean)
if err != nil {
session.Engine.LogError(err)
} else if verValue.IsValid() && verValue.CanSet() {
verValue.SetInt(1)
}
}
@ -2548,8 +2555,12 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
return res.RowsAffected()
}
aiValue := table.AutoIncrColumn().ValueOf(bean)
if !aiValue.IsValid() /*|| aiValue.Int() != 0*/ || !aiValue.CanSet() {
aiValue, err := table.AutoIncrColumn().ValueOf(bean)
if err != nil {
session.Engine.LogError(err)
}
if aiValue == nil || !aiValue.IsValid() /*|| aiValue.Int() != 0*/ || !aiValue.CanSet() {
return res.RowsAffected()
}
@ -2580,13 +2591,15 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
handleAfterInsertProcessorFunc(bean)
}
if table.Cacher != nil && session.Statement.UseCache {
if cacher := session.Engine.getCacher(table.Type); cacher != nil && session.Statement.UseCache {
session.cacheInsert(session.Statement.TableName())
}
if table.Version != "" && session.Statement.checkVersion {
verValue := table.VersionColumn().ValueOf(bean)
if verValue.IsValid() && verValue.CanSet() {
verValue, err := table.VersionColumn().ValueOf(bean)
if err != nil {
session.Engine.LogError(err)
} else if verValue.IsValid() && verValue.CanSet() {
verValue.SetInt(1)
}
}
@ -2601,8 +2614,12 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
return 1, err
}
aiValue := table.AutoIncrColumn().ValueOf(bean)
if !aiValue.IsValid() /*|| aiValue. != 0*/ || !aiValue.CanSet() {
aiValue, err := table.AutoIncrColumn().ValueOf(bean)
if err != nil {
session.Engine.LogError(err)
}
if aiValue == nil || !aiValue.IsValid() /*|| aiValue. != 0*/ || !aiValue.CanSet() {
return 1, nil
}
@ -2659,9 +2676,9 @@ func (statement *Statement) convertUpdateSql(sqlStr string) (string, string) {
//TODO: for postgres only, if any other database?
var paraStr string
if statement.Engine.dialect.DBType() == POSTGRES {
if statement.Engine.dialect.DBType() == core.POSTGRES {
paraStr = "$"
} else if statement.Engine.dialect.DBType() == MSSQL {
} else if statement.Engine.dialect.DBType() == core.MSSQL {
paraStr = ":"
}
@ -2687,7 +2704,7 @@ func (session *Session) cacheInsert(tables ...string) error {
}
table := session.Statement.RefTable
cacher := table.Cacher
cacher := session.Engine.getCacher(table.Type)
for _, t := range tables {
session.Engine.LogDebug("cache clear:", t)
@ -2707,7 +2724,7 @@ func (session *Session) cacheUpdate(sqlStr string, args ...interface{}) error {
return ErrCacheFailed
}
for _, filter := range session.Engine.Filters {
newsql = filter.Do(newsql, session)
newsql = filter.Do(newsql, session.Engine.dialect, session.Statement.RefTable)
}
session.Engine.LogDebug("[xorm:cacheUpdate] new sql", oldhead, newsql)
@ -2721,7 +2738,7 @@ func (session *Session) cacheUpdate(sqlStr string, args ...interface{}) error {
}
}
table := session.Statement.RefTable
cacher := table.Cacher
cacher := session.Engine.getCacher(table.Type)
tableName := session.Statement.TableName()
session.Engine.LogDebug("[xorm:cacheUpdate] get cache sql", newsql, args[nStart:])
ids, err := getCacheSql(cacher, tableName, newsql, args[nStart:])
@ -2777,14 +2794,18 @@ func (session *Session) cacheUpdate(sqlStr string, args ...interface{}) error {
return ErrCacheFailed
}
if col, ok := table.Columns[strings.ToLower(colName)]; ok {
fieldValue := col.ValueOf(bean)
if col := table.GetColumn(colName); col != nil {
fieldValue, err := col.ValueOf(bean)
if err != nil {
session.Engine.LogError(err)
} else {
session.Engine.LogDebug("[xorm:cacheUpdate] set bean field", bean, colName, fieldValue.Interface())
if col.IsVersion && session.Statement.checkVersion {
fieldValue.SetInt(fieldValue.Int() + 1)
} else {
fieldValue.Set(reflect.ValueOf(args[idx]))
}
}
} else {
session.Engine.LogError("[xorm:cacheUpdate] ERROR: column %v is not table %v's",
colName, table.Name)
@ -2820,7 +2841,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
var colNames []string
var args []interface{}
var table *Table
var table *core.Table
// handle before update processors
for _, closure := range session.beforeClosures {
@ -2840,7 +2861,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
colNames, args = buildConditions(session.Engine, table, bean, false, false,
false, false, session.Statement.allUseBool, session.Statement.boolColumnMap)
} else {
colNames, args, err = table.genCols(session, bean, true, true)
colNames, args, err = genCols(table, session, bean, true, true)
if err != nil {
return 0, err
}
@ -2917,7 +2938,12 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
session.Engine.Quote(table.Version)+" = "+session.Engine.Quote(table.Version)+" + 1",
condition)
condiArgs = append(condiArgs, table.VersionColumn().ValueOf(bean).Interface())
verValue, err := table.VersionColumn().ValueOf(bean)
if err != nil {
return 0, err
}
condiArgs = append(condiArgs, verValue.Interface())
} else {
if condition != "" {
condition = "WHERE " + condition
@ -2946,10 +2972,10 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
return 0, err
}
if table.Cacher != nil && session.Statement.UseCache {
if cacher := session.Engine.getCacher(t); cacher != nil && session.Statement.UseCache {
//session.cacheUpdate(sqlStr, args...)
table.Cacher.ClearIds(session.Statement.TableName())
table.Cacher.ClearBeans(session.Statement.TableName())
cacher.ClearIds(session.Statement.TableName())
cacher.ClearBeans(session.Statement.TableName())
}
// handle after update processors
@ -2990,7 +3016,7 @@ func (session *Session) cacheDelete(sqlStr string, args ...interface{}) error {
}
for _, filter := range session.Engine.Filters {
sqlStr = filter.Do(sqlStr, session)
sqlStr = filter.Do(sqlStr, session.Engine.dialect, session.Statement.RefTable)
}
newsql := session.Statement.convertIdSql(sqlStr)
@ -2998,7 +3024,7 @@ func (session *Session) cacheDelete(sqlStr string, args ...interface{}) error {
return ErrCacheFailed
}
cacher := session.Statement.RefTable.Cacher
cacher := session.Engine.getCacher(session.Statement.RefTable.Type)
tableName := session.Statement.TableName()
ids, err := getCacheSql(cacher, tableName, newsql, args)
if err != nil {
@ -3090,7 +3116,7 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
args = append(session.Statement.Params, args...)
if table.Cacher != nil && session.Statement.UseCache {
if cacher := session.Engine.getCacher(session.Statement.RefTable.Type); cacher != nil && session.Statement.UseCache {
session.cacheDelete(sqlStr, args...)
}

View File

@ -8,11 +8,13 @@ import (
"encoding/json"
"strings"
"time"
"github.com/lunny/xorm/core"
)
// statement save all the sql info for executing SQL
type Statement struct {
RefTable *Table
RefTable *core.Table
Engine *Engine
Start int
LimitN int
@ -64,7 +66,7 @@ func (statement *Statement) Init() {
statement.RawSQL = ""
statement.RawParams = make([]interface{}, 0)
statement.BeanArgs = make([]interface{}, 0)
statement.UseCache = statement.Engine.UseCache
statement.UseCache = true
statement.UseAutoTime = true
statement.IsDistinct = false
statement.allUseBool = false
@ -237,13 +239,13 @@ func (statement *Statement) Table(tableNameOrBean interface{}) *Statement {
}*/
// Auto generating conditions according a struct
func buildConditions(engine *Engine, table *Table, bean interface{},
func buildConditions(engine *Engine, table *core.Table, bean interface{},
includeVersion bool, includeUpdated bool, includeNil bool, includeAutoIncr bool, allUseBool bool,
boolColumnMap map[string]bool) ([]string, []interface{}) {
colNames := make([]string, 0)
var args = make([]interface{}, 0)
for _, col := range table.Columns {
for _, col := range table.Columns() {
if !includeVersion && col.IsVersion {
continue
}
@ -255,10 +257,16 @@ func buildConditions(engine *Engine, table *Table, bean interface{},
}
//
//fmt.Println(engine.dialect.DBType(), Text)
if engine.dialect.DBType() == MSSQL && col.SQLType.Name == Text {
if engine.dialect.DBType() == core.MSSQL && col.SQLType.Name == core.Text {
continue
}
fieldValue := col.ValueOf(bean)
fieldValuePtr, err := col.ValueOf(bean)
if err != nil {
engine.LogError(err)
continue
}
fieldValue := *fieldValuePtr
fieldType := reflect.TypeOf(fieldValue.Interface())
requiredField := false
@ -323,10 +331,10 @@ func buildConditions(engine *Engine, table *Table, bean interface{},
continue
}
var str string
if col.SQLType.Name == Time {
if col.SQLType.Name == core.Time {
s := t.UTC().Format("2006-01-02 15:04:05")
val = s[11:19]
} else if col.SQLType.Name == Date {
} else if col.SQLType.Name == core.Date {
str = t.Format("2006-01-02")
val = str
} else {
@ -510,7 +518,7 @@ func (statement *Statement) Distinct(columns ...string) *Statement {
func (statement *Statement) Cols(columns ...string) *Statement {
newColumns := col2NewCols(columns...)
for _, nc := range newColumns {
statement.columnMap[nc] = true
statement.columnMap[strings.ToLower(nc)] = true
}
statement.ColumnStr = statement.Engine.Quote(strings.Join(newColumns, statement.Engine.Quote(", ")))
return statement
@ -521,7 +529,7 @@ func (statement *Statement) UseBool(columns ...string) *Statement {
if len(columns) > 0 {
newColumns := col2NewCols(columns...)
for _, nc := range newColumns {
statement.boolColumnMap[nc] = true
statement.boolColumnMap[strings.ToLower(nc)] = true
}
} else {
statement.allUseBool = true
@ -533,7 +541,7 @@ func (statement *Statement) UseBool(columns ...string) *Statement {
func (statement *Statement) Omit(columns ...string) {
newColumns := col2NewCols(columns...)
for _, nc := range newColumns {
statement.columnMap[nc] = false
statement.columnMap[strings.ToLower(nc)] = false
}
statement.OmitStr = statement.Engine.Quote(strings.Join(newColumns, statement.Engine.Quote(", ")))
}
@ -584,13 +592,13 @@ func (statement *Statement) Having(conditions string) *Statement {
func (statement *Statement) genColumnStr() string {
table := statement.RefTable
colNames := make([]string, 0)
for _, col := range table.Columns {
for _, col := range table.Columns() {
if statement.OmitStr != "" {
if _, ok := statement.columnMap[col.Name]; ok {
if _, ok := statement.columnMap[strings.ToLower(col.Name)]; ok {
continue
}
}
if col.MapType == ONLYTODB {
if col.MapType == core.ONLYTODB {
continue
}
colNames = append(colNames, statement.Engine.Quote(statement.TableName())+"."+statement.Engine.Quote(col.Name))
@ -599,54 +607,8 @@ func (statement *Statement) genColumnStr() string {
}
func (statement *Statement) genCreateTableSQL() string {
var sql string
if statement.Engine.dialect.DBType() == MSSQL {
sql = "IF NOT EXISTS (SELECT [name] FROM sys.tables WHERE [name] = '" + statement.TableName() + "' ) CREATE TABLE"
} else {
sql = "CREATE TABLE IF NOT EXISTS "
}
sql += statement.Engine.Quote(statement.TableName()) + " ("
pkList := []string{}
for _, colName := range statement.RefTable.ColumnsSeq {
col := statement.RefTable.Columns[strings.ToLower(colName)]
if col.IsPrimaryKey {
pkList = append(pkList, col.Name)
}
}
statement.Engine.LogDebug("len:", len(pkList))
for _, colName := range statement.RefTable.ColumnsSeq {
col := statement.RefTable.Columns[strings.ToLower(colName)]
if col.IsPrimaryKey && len(pkList) == 1 {
sql += col.String(statement.Engine.dialect)
} else {
sql += col.stringNoPk(statement.Engine.dialect)
}
sql = strings.TrimSpace(sql)
sql += ", "
}
if len(pkList) > 1 {
sql += "PRIMARY KEY ( "
sql += strings.Join(pkList, ",")
sql += " ), "
}
sql = sql[:len(sql)-2] + ")"
if statement.Engine.dialect.SupportEngine() && statement.StoreEngine != "" {
sql += " ENGINE=" + statement.StoreEngine
}
if statement.Engine.dialect.SupportCharset() {
if statement.Charset != "" {
sql += " DEFAULT CHARSET " + statement.Charset
} else if statement.Engine.dialect.URI().charset != "" {
sql += " DEFAULT CHARSET " + statement.Engine.dialect.URI().charset
}
}
sql += ";"
return sql
return statement.Engine.dialect.CreateTableSql(statement.RefTable, statement.AltTableName,
statement.StoreEngine, statement.Charset)
}
func indexName(tableName, idxName string) string {
@ -658,7 +620,7 @@ func (s *Statement) genIndexSQL() []string {
tbName := s.TableName()
quote := s.Engine.Quote
for idxName, index := range s.RefTable.Indexes {
if index.Type == IndexType {
if index.Type == core.IndexType {
sql := fmt.Sprintf("CREATE INDEX %v ON %v (%v);", quote(indexName(tbName, idxName)),
quote(tbName), quote(strings.Join(index.Cols, quote(","))))
sqls = append(sqls, sql)
@ -676,7 +638,7 @@ func (s *Statement) genUniqueSQL() []string {
tbName := s.TableName()
quote := s.Engine.Quote
for idxName, unique := range s.RefTable.Indexes {
if unique.Type == UniqueType {
if unique.Type == core.UniqueType {
sql := fmt.Sprintf("CREATE UNIQUE INDEX %v ON %v (%v);", quote(uniqueName(tbName, idxName)),
quote(tbName), quote(strings.Join(unique.Cols, quote(","))))
sqls = append(sqls, sql)
@ -689,9 +651,9 @@ func (s *Statement) genDelIndexSQL() []string {
var sqls []string = make([]string, 0)
for idxName, index := range s.RefTable.Indexes {
var rIdxName string
if index.Type == UniqueType {
if index.Type == core.UniqueType {
rIdxName = uniqueName(s.TableName(), idxName)
} else if index.Type == IndexType {
} else if index.Type == core.IndexType {
rIdxName = indexName(s.TableName(), idxName)
}
sql := fmt.Sprintf("DROP INDEX %v", s.Engine.Quote(rIdxName))
@ -704,7 +666,7 @@ func (s *Statement) genDelIndexSQL() []string {
}
func (s *Statement) genDropSQL() string {
if s.Engine.dialect.DBType() == MSSQL {
if s.Engine.dialect.DBType() == core.MSSQL {
return "IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N'" +
s.TableName() + "') and OBJECTPROPERTY(id, N'IsUserTable') = 1) " +
"DROP TABLE " + s.Engine.Quote(s.TableName()) + ";"
@ -731,7 +693,7 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{})
return statement.genSelectSql(columnStr), append(statement.Params, statement.BeanArgs...)
}
func (s *Statement) genAddColumnStr(col *Column) (string, []interface{}) {
func (s *Statement) genAddColumnStr(col *core.Column) (string, []interface{}) {
quote := s.Engine.Quote
sql := fmt.Sprintf("ALTER TABLE %v ADD COLUMN %v;", quote(s.TableName()),
col.String(s.Engine.dialect))
@ -804,13 +766,14 @@ func (statement *Statement) genSelectSql(columnStr string) (a string) {
if statement.OrderStr != "" {
a = fmt.Sprintf("%v ORDER BY %v", a, statement.OrderStr)
}
if statement.Engine.dialect.DBType() != MSSQL {
if statement.Engine.dialect.DBType() != core.MSSQL {
if statement.Start > 0 {
a = fmt.Sprintf("%v LIMIT %v OFFSET %v", a, statement.LimitN, statement.Start)
} else if statement.LimitN > 0 {
a = fmt.Sprintf("%v LIMIT %v", a, statement.LimitN)
}
} else {
//TODO: for mssql, should handler limit.
/*SELECT * FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY id desc) as row FROM "userinfo"
) a WHERE row > [start] and row <= [start+limit] order by id desc*/
@ -823,11 +786,12 @@ func (statement *Statement) processIdParam() {
if statement.IdParam != nil {
i := 0
colCnt := len(statement.RefTable.ColumnsSeq)
columns := statement.RefTable.ColumnsSeq()
colCnt := len(columns)
for _, elem := range *(statement.IdParam) {
for ; i < colCnt; i++ {
colName := statement.RefTable.ColumnsSeq[i]
col := statement.RefTable.Columns[strings.ToLower(colName)]
colName := columns[i]
col := statement.RefTable.GetColumn(colName)
if col.IsPrimaryKey {
statement.And(fmt.Sprintf("%v=?", col.Name), elem)
i++
@ -840,8 +804,8 @@ func (statement *Statement) processIdParam() {
// as empty string for now, so this will result sql exec failed instead of unexpected
// false update/delete
for ; i < colCnt; i++ {
colName := statement.RefTable.ColumnsSeq[i]
col := statement.RefTable.Columns[strings.ToLower(colName)]
colName := columns[i]
col := statement.RefTable.GetColumn(colName)
if col.IsPrimaryKey {
statement.And(fmt.Sprintf("%v=?", col.Name), "")
}

409
table.go
View File

@ -2,120 +2,12 @@ package xorm
import (
"reflect"
"sort"
"strings"
"time"
"github.com/lunny/xorm/core"
)
// xorm SQL types
type SQLType struct {
Name string
DefaultLength int
DefaultLength2 int
}
func (s *SQLType) IsText() bool {
return s.Name == Char || s.Name == Varchar || s.Name == TinyText ||
s.Name == Text || s.Name == MediumText || s.Name == LongText
}
func (s *SQLType) IsBlob() bool {
return (s.Name == TinyBlob) || (s.Name == Blob) ||
s.Name == MediumBlob || s.Name == LongBlob ||
s.Name == Binary || s.Name == VarBinary || s.Name == Bytea
}
const ()
var (
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"
Date = "DATE"
DateTime = "DATETIME"
Time = "TIME"
TimeStamp = "TIMESTAMP"
TimeStampz = "TIMESTAMPZ"
Decimal = "DECIMAL"
Numeric = "NUMERIC"
Real = "REAL"
Float = "FLOAT"
Double = "DOUBLE"
Binary = "BINARY"
VarBinary = "VARBINARY"
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,
Date: true,
DateTime: true,
Time: true,
TimeStamp: true,
TimeStampz: true,
Decimal: true,
Numeric: true,
Binary: true,
VarBinary: true,
Real: true,
Float: true,
Double: true,
TinyBlob: true,
Blob: true,
MediumBlob: true,
LongBlob: true,
Bytea: true,
Bool: true,
Serial: true,
BigSerial: true,
}
intTypes = sort.StringSlice{"*int", "*int16", "*int32", "*int8"}
uintTypes = sort.StringSlice{"*uint", "*uint16", "*uint32", "*uint8"}
)
// !nashtsai! treat following var as interal const values, these are used for reflect.TypeOf comparision
var (
c_EMPTY_STRING string
c_BOOL_DEFAULT bool
@ -137,290 +29,28 @@ var (
c_TIME_DEFAULT time.Time
)
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:
st = SQLType{Int, 0, 0}
case reflect.Int64, reflect.Uint64:
st = SQLType{BigInt, 0, 0}
case reflect.Float32:
st = SQLType{Float, 0, 0}
case reflect.Float64:
st = SQLType{Double, 0, 0}
case reflect.Complex64, reflect.Complex128:
st = SQLType{Varchar, 64, 0}
case reflect.Array, reflect.Slice, reflect.Map:
if t.Elem() == reflect.TypeOf(c_BYTE_DEFAULT) {
st = SQLType{Blob, 0, 0}
} else {
st = SQLType{Text, 0, 0}
}
case reflect.Bool:
st = SQLType{Bool, 0, 0}
case reflect.String:
st = SQLType{Varchar, 255, 0}
case reflect.Struct:
if t == reflect.TypeOf(c_TIME_DEFAULT) {
st = SQLType{DateTime, 0, 0}
} else {
// TODO need to handle association struct
st = SQLType{Text, 0, 0}
}
case reflect.Ptr:
st, _ = ptrType2SQLType(t)
default:
st = SQLType{Text, 0, 0}
}
return
}
func ptrType2SQLType(t reflect.Type) (st SQLType, has bool) {
has = true
switch t {
case reflect.TypeOf(&c_EMPTY_STRING):
st = SQLType{Varchar, 255, 0}
return
case reflect.TypeOf(&c_BOOL_DEFAULT):
st = SQLType{Bool, 0, 0}
case reflect.TypeOf(&c_COMPLEX64_DEFAULT), reflect.TypeOf(&c_COMPLEX128_DEFAULT):
st = SQLType{Varchar, 64, 0}
case reflect.TypeOf(&c_FLOAT32_DEFAULT):
st = SQLType{Float, 0, 0}
case reflect.TypeOf(&c_FLOAT64_DEFAULT):
st = SQLType{Double, 0, 0}
case reflect.TypeOf(&c_INT64_DEFAULT), reflect.TypeOf(&c_UINT64_DEFAULT):
st = SQLType{BigInt, 0, 0}
case reflect.TypeOf(&c_TIME_DEFAULT):
st = SQLType{DateTime, 0, 0}
case reflect.TypeOf(&c_INT_DEFAULT), reflect.TypeOf(&c_INT32_DEFAULT), reflect.TypeOf(&c_INT8_DEFAULT), reflect.TypeOf(&c_INT16_DEFAULT), reflect.TypeOf(&c_UINT_DEFAULT), reflect.TypeOf(&c_UINT32_DEFAULT), reflect.TypeOf(&c_UINT8_DEFAULT), reflect.TypeOf(&c_UINT16_DEFAULT):
st = SQLType{Int, 0, 0}
default:
has = false
}
return
}
// default sql type change to go types
func SQLType2Type(st SQLType) reflect.Type {
name := strings.ToUpper(st.Name)
switch name {
case Bit, TinyInt, SmallInt, MediumInt, Int, Integer, Serial:
return reflect.TypeOf(1)
case BigInt, BigSerial:
return reflect.TypeOf(int64(1))
case Float, Real:
return reflect.TypeOf(float32(1))
case Double:
return reflect.TypeOf(float64(1))
case Char, Varchar, TinyText, Text, MediumText, LongText:
return reflect.TypeOf("")
case TinyBlob, Blob, LongBlob, Bytea, Binary, MediumBlob, VarBinary:
return reflect.TypeOf([]byte{})
case Bool:
return reflect.TypeOf(true)
case DateTime, Date, Time, TimeStamp, TimeStampz:
return reflect.TypeOf(c_TIME_DEFAULT)
case Decimal, Numeric:
return reflect.TypeOf("")
default:
return reflect.TypeOf("")
}
}
const (
IndexType = iota + 1
UniqueType
)
// database index
type Index struct {
Name string
Type int
Cols []string
}
// add columns which will be composite index
func (index *Index) AddColumn(cols ...string) {
for _, col := range cols {
index.Cols = append(index.Cols, col)
}
}
// new an index
func NewIndex(name string, indexType int) *Index {
return &Index{name, indexType, make([]string, 0)}
}
const (
TWOSIDES = iota + 1
ONLYTODB
ONLYFROMDB
)
// database column
type Column struct {
Name string
FieldName string
SQLType SQLType
Length int
Length2 int
Nullable bool
Default string
Indexes map[string]bool
IsPrimaryKey bool
IsAutoIncrement bool
MapType int
IsCreated bool
IsUpdated bool
IsCascade bool
IsVersion bool
}
// generate column description string according dialect
func (col *Column) String(d dialect) string {
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
sql += d.SqlType(col) + " "
if col.IsPrimaryKey {
sql += "PRIMARY KEY "
if col.IsAutoIncrement {
sql += d.AutoIncrStr() + " "
}
}
if col.Nullable {
sql += "NULL "
} else {
sql += "NOT NULL "
}
if col.Default != "" {
sql += "DEFAULT " + col.Default + " "
}
return sql
}
func (col *Column) stringNoPk(d dialect) string {
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
sql += d.SqlType(col) + " "
if col.Nullable {
sql += "NULL "
} else {
sql += "NOT NULL "
}
if col.Default != "" {
sql += "DEFAULT " + col.Default + " "
}
return sql
}
// return col's filed of struct's value
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
}
// database table
type Table struct {
Name string
Type reflect.Type
ColumnsSeq []string
Columns map[string]*Column
Indexes map[string]*Index
PrimaryKeys []string
AutoIncrement string
Created map[string]bool
Updated string
Version string
Cacher Cacher
}
/*
func NewTable(name string, t reflect.Type) *Table {
return &Table{Name: name, Type: t,
ColumnsSeq: make([]string, 0),
Columns: make(map[string]*Column),
Indexes: make(map[string]*Index),
Created: make(map[string]bool),
}
}*/
// if has primary key, return column
func (table *Table) PKColumns() []*Column {
columns := make([]*Column, 0)
for _, name := range table.PrimaryKeys {
columns = append(columns, table.Columns[strings.ToLower(name)])
}
return columns
}
func (table *Table) AutoIncrColumn() *Column {
return table.Columns[strings.ToLower(table.AutoIncrement)]
}
func (table *Table) VersionColumn() *Column {
return table.Columns[strings.ToLower(table.Version)]
}
// add a column to table
func (table *Table) AddColumn(col *Column) {
table.ColumnsSeq = append(table.ColumnsSeq, col.Name)
table.Columns[strings.ToLower(col.Name)] = col
if col.IsPrimaryKey {
table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
}
if col.IsAutoIncrement {
table.AutoIncrement = col.Name
}
if col.IsCreated {
table.Created[col.Name] = true
}
if col.IsUpdated {
table.Updated = col.Name
}
if col.IsVersion {
table.Version = col.Name
}
}
// add an index or an unique to table
func (table *Table) AddIndex(index *Index) {
table.Indexes[index.Name] = index
}
func (table *Table) genCols(session *Session, bean interface{}, useCol bool, includeQuote bool) ([]string, []interface{}, error) {
func genCols(table *core.Table, session *Session, bean interface{}, useCol bool, includeQuote bool) ([]string, []interface{}, error) {
colNames := make([]string, 0)
args := make([]interface{}, 0)
for _, col := range table.Columns {
for _, col := range table.Columns() {
lColName := strings.ToLower(col.Name)
if useCol && !col.IsVersion && !col.IsCreated && !col.IsUpdated {
if _, ok := session.Statement.columnMap[col.Name]; !ok {
if _, ok := session.Statement.columnMap[lColName]; !ok {
continue
}
}
if col.MapType == ONLYFROMDB {
if col.MapType == core.ONLYFROMDB {
continue
}
fieldValue := col.ValueOf(bean)
fieldValuePtr, err := col.ValueOf(bean)
if err != nil {
session.Engine.LogError(err)
continue
}
fieldValue := *fieldValuePtr
if col.IsAutoIncrement {
switch fieldValue.Type().Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
@ -439,12 +69,12 @@ func (table *Table) genCols(session *Session, bean interface{}, useCol bool, inc
}
if session.Statement.ColumnStr != "" {
if _, ok := session.Statement.columnMap[col.Name]; !ok {
if _, ok := session.Statement.columnMap[lColName]; !ok {
continue
}
}
if session.Statement.OmitStr != "" {
if _, ok := session.Statement.columnMap[col.Name]; ok {
if _, ok := session.Statement.columnMap[lColName]; ok {
continue
}
}
@ -469,10 +99,3 @@ func (table *Table) genCols(session *Session, bean interface{}, useCol bool, inc
}
return colNames, args, nil
}
// Conversion is an interface. A type implements Conversion will according
// the custom method to fill into database and retrieve from database.
type Conversion interface {
FromDB([]byte) error
ToDB() ([]byte, error)
}

51
xorm.go
View File

@ -7,6 +7,10 @@ import (
"reflect"
"runtime"
"sync"
"github.com/lunny/xorm/core"
_ "github.com/lunny/xorm/dialects"
_ "github.com/lunny/xorm/drivers"
)
const (
@ -20,39 +24,38 @@ func close(engine *Engine) {
// new a db manager according to the parameter. Currently support four
// drivers
func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
engine := &Engine{DriverName: driverName,
DataSourceName: dataSourceName, Filters: make([]Filter, 0)}
engine.SetMapper(SnakeMapper{})
if driverName == SQLITE {
engine.dialect = &sqlite3{}
} else if driverName == MYSQL {
engine.dialect = &mysql{}
} else if driverName == POSTGRES {
engine.dialect = &postgres{}
engine.Filters = append(engine.Filters, &PgSeqFilter{})
engine.Filters = append(engine.Filters, &QuoteFilter{})
} else if driverName == MYMYSQL {
engine.dialect = &mymysql{}
} else if driverName == "odbc" {
engine.dialect = &mssql{quoteFilter: &QuoteFilter{}}
engine.Filters = append(engine.Filters, &QuoteFilter{})
} else if driverName == ORACLE_OCI {
engine.dialect = &oracle{}
engine.Filters = append(engine.Filters, &QuoteFilter{})
} else {
driver := core.QueryDriver(driverName)
if driver == nil {
return nil, errors.New(fmt.Sprintf("Unsupported driver name: %v", driverName))
}
err := engine.dialect.Init(driverName, dataSourceName)
uri, err := driver.Parse(driverName, dataSourceName)
if err != nil {
return nil, err
}
engine.Tables = make(map[reflect.Type]*Table)
dialect := core.QueryDialect(uri.DbType)
if dialect == nil {
return nil, errors.New(fmt.Sprintf("Unsupported dialect type: %v", uri.DbType))
}
err = dialect.Init(uri, driverName, dataSourceName)
if err != nil {
return nil, err
}
engine := &Engine{DriverName: driverName,
DataSourceName: dataSourceName, dialect: dialect,
tableCachers: make(map[reflect.Type]Cacher)}
engine.SetMapper(SnakeMapper{})
engine.Filters = dialect.Filters()
engine.Tables = make(map[reflect.Type]*core.Table)
engine.mutex = &sync.Mutex{}
engine.TagIdentifier = "xorm"
engine.Filters = append(engine.Filters, &IdFilter{})
engine.Logger = os.Stdout
//engine.Pool = NewSimpleConnectPool()