Merge remote-tracking branch 'upstream/dev' into dev
This commit is contained in:
commit
cf73ec9f2d
24
base_test.go
24
base_test.go
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/lunny/xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -1850,7 +1852,7 @@ func testMetaInfo(engine *Engine, t *testing.T) {
|
||||||
|
|
||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
fmt.Println(table.Name)
|
fmt.Println(table.Name)
|
||||||
for _, col := range table.Columns {
|
for _, col := range table.Columns() {
|
||||||
fmt.Println(col.String(engine.dialect))
|
fmt.Println(col.String(engine.dialect))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -3175,7 +3177,9 @@ func testPointerData(engine *Engine, t *testing.T) {
|
||||||
// using instance type should just work too
|
// using instance type should just work too
|
||||||
nullData2Get := NullData2{}
|
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 {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(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
|
// 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
|
// 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
|
// prefer using timestamp with timezone to sovle the issue
|
||||||
if engine.DriverName != POSTGRES && engine.DriverName != MYMYSQL &&
|
if engine.DriverName != core.POSTGRES && engine.DriverName != "mymysql" &&
|
||||||
engine.DriverName != MYSQL {
|
engine.DriverName != core.MYSQL {
|
||||||
if (*nullDataGet.TimePtr).Unix() != (*nullDataUpdate.TimePtr).Unix() {
|
if (*nullDataGet.TimePtr).Unix() != (*nullDataUpdate.TimePtr).Unix() {
|
||||||
t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]:[%v]", *nullDataGet.TimePtr, *nullDataUpdate.TimePtr)))
|
t.Error(errors.New(fmt.Sprintf("inserted value unmatch: [%v]:[%v]", *nullDataGet.TimePtr, *nullDataUpdate.TimePtr)))
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -3540,7 +3544,9 @@ func testNullValue(engine *Engine, t *testing.T) {
|
||||||
// update to null values
|
// update to null values
|
||||||
nullDataUpdate = NullData{}
|
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 {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
@ -3896,3 +3902,11 @@ func testAll3(engine *Engine, t *testing.T) {
|
||||||
fmt.Println("-------------- testStringPK --------------")
|
fmt.Println("-------------- testStringPK --------------")
|
||||||
testStringPK(engine, t)
|
testStringPK(engine, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testAllSnakeMapper(engine *Engine, t *testing.T) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func testAllSameMapper(engine *Engine, t *testing.T) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -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...)
|
||||||
|
}
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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]
|
||||||
|
}
|
||||||
|
|
@ -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]
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -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)}
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -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("")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,46 +1,28 @@
|
||||||
package xorm
|
package dialects
|
||||||
|
|
||||||
import (
|
import (
|
||||||
//"crypto/tls"
|
//"crypto/tls"
|
||||||
"database/sql"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
//"regexp"
|
//"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
//"time"
|
//"time"
|
||||||
|
|
||||||
|
. "github.com/lunny/xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterDialect("mssql", &mssql{})
|
||||||
|
}
|
||||||
|
|
||||||
type mssql struct {
|
type mssql struct {
|
||||||
base
|
Base
|
||||||
quoteFilter Filter
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type odbcParser struct {
|
func (db *mssql) Init(uri *Uri, drivername, dataSourceName string) error {
|
||||||
}
|
return db.Base.Init(db, uri, drivername, dataSourceName)
|
||||||
|
|
||||||
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) SqlType(c *Column) string {
|
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
|
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
|
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 + `')`
|
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 {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
defer cnn.Close()
|
defer cnn.Close()
|
||||||
res, err := query(cnn, s, args...)
|
|
||||||
|
rows, err := cnn.Query(s, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
cols := make(map[string]*Column)
|
cols := make(map[string]*Column)
|
||||||
colSeq := make([]string, 0)
|
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 := new(Column)
|
||||||
col.Indexes = make(map[string]bool)
|
col.Indexes = make(map[string]bool)
|
||||||
for name, content := range record {
|
col.Length = maxLen
|
||||||
switch name {
|
col.Name = strings.Trim(name, "` ")
|
||||||
case "name":
|
|
||||||
|
|
||||||
col.Name = strings.Trim(string(content), "` ")
|
ct := strings.ToUpper(ctype)
|
||||||
case "ctype":
|
|
||||||
ct := strings.ToUpper(string(content))
|
|
||||||
switch ct {
|
switch ct {
|
||||||
case "DATETIMEOFFSET":
|
case "DATETIMEOFFSET":
|
||||||
col.SQLType = SQLType{TimeStampz, 0, 0}
|
col.SQLType = SQLType{TimeStampz, 0, 0}
|
||||||
|
|
@ -168,7 +155,7 @@ where a.object_id=object_id('` + tableName + `')`
|
||||||
case "IMAGE":
|
case "IMAGE":
|
||||||
col.SQLType = SQLType{VarBinary, 0, 0}
|
col.SQLType = SQLType{VarBinary, 0, 0}
|
||||||
default:
|
default:
|
||||||
if _, ok := sqlTypes[ct]; ok {
|
if _, ok := SqlTypes[ct]; ok {
|
||||||
col.SQLType = SQLType{ct, 0, 0}
|
col.SQLType = SQLType{ct, 0, 0}
|
||||||
} else {
|
} else {
|
||||||
return nil, nil, errors.New(fmt.Sprintf("unknow colType %v for %v - %v",
|
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.SQLType.IsText() {
|
||||||
if col.Default != "" {
|
if col.Default != "" {
|
||||||
col.Default = "'" + col.Default + "'"
|
col.Default = "'" + col.Default + "'"
|
||||||
|
|
@ -198,25 +177,25 @@ where a.object_id=object_id('` + tableName + `')`
|
||||||
func (db *mssql) GetTables() ([]*Table, error) {
|
func (db *mssql) GetTables() ([]*Table, error) {
|
||||||
args := []interface{}{}
|
args := []interface{}{}
|
||||||
s := `select name from sysobjects where xtype ='U'`
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer cnn.Close()
|
defer cnn.Close()
|
||||||
res, err := query(cnn, s, args...)
|
rows, err := cnn.Query(s, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tables := make([]*Table, 0)
|
tables := make([]*Table, 0)
|
||||||
for _, record := range res {
|
for rows.Next() {
|
||||||
table := new(Table)
|
table := NewEmptyTable()
|
||||||
for name, content := range record {
|
var name string
|
||||||
switch name {
|
err = rows.Scan(&name)
|
||||||
case "name":
|
if err != nil {
|
||||||
table.Name = strings.Trim(string(content), "` ")
|
return nil, err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
table.Name = strings.Trim(name, "` ")
|
||||||
tables = append(tables, table)
|
tables = append(tables, table)
|
||||||
}
|
}
|
||||||
return tables, nil
|
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
|
AND IXCS.COLUMN_ID=C.COLUMN_ID
|
||||||
WHERE IXS.TYPE_DESC='NONCLUSTERED' and OBJECT_NAME(IXS.OBJECT_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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer cnn.Close()
|
defer cnn.Close()
|
||||||
res, err := query(cnn, s, args...)
|
rows, err := cnn.Query(s, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
indexes := make(map[string]*Index, 0)
|
indexes := make(map[string]*Index, 0)
|
||||||
for _, record := range res {
|
for rows.Next() {
|
||||||
var indexType int
|
var indexType int
|
||||||
var indexName, colName string
|
var indexName, colName, isUnique string
|
||||||
for name, content := range record {
|
|
||||||
switch name {
|
err = rows.Scan(&indexName, &colName, &isUnique, nil)
|
||||||
case "IS_UNIQUE":
|
if err != nil {
|
||||||
i, err := strconv.ParseBool(string(content))
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
i, err := strconv.ParseBool(isUnique)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -265,12 +247,8 @@ WHERE IXS.TYPE_DESC='NONCLUSTERED' and OBJECT_NAME(IXS.OBJECT_ID) =?
|
||||||
} else {
|
} else {
|
||||||
indexType = IndexType
|
indexType = IndexType
|
||||||
}
|
}
|
||||||
case "INDEX_NAME":
|
|
||||||
indexName = string(content)
|
colName = strings.Trim(colName, "` ")
|
||||||
case "COLUMN_NAME":
|
|
||||||
colName = strings.Trim(string(content), "` ")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
|
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
|
||||||
indexName = indexName[5+len(tableName) : len(indexName)]
|
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
|
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{}}
|
||||||
|
}
|
||||||
|
|
@ -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{}}
|
||||||
|
}
|
||||||
|
|
@ -1,45 +1,24 @@
|
||||||
package xorm
|
package dialects
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
. "github.com/lunny/xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterDialect("oracle", &oracle{})
|
||||||
|
}
|
||||||
|
|
||||||
type oracle struct {
|
type oracle struct {
|
||||||
base
|
Base
|
||||||
}
|
}
|
||||||
|
|
||||||
type oracleParser struct {
|
func (db *oracle) Init(uri *Uri, drivername, dataSourceName string) error {
|
||||||
}
|
return db.Base.Init(db, uri, drivername, dataSourceName)
|
||||||
|
|
||||||
//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) SqlType(c *Column) string {
|
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," +
|
s := "SELECT column_name,data_default,data_type,data_length,data_precision,data_scale," +
|
||||||
"nullable FROM USER_TAB_COLUMNS WHERE table_name = :1"
|
"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 {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
defer cnn.Close()
|
defer cnn.Close()
|
||||||
res, err := query(cnn, s, args...)
|
rows, err := cnn.Query(s, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
cols := make(map[string]*Column)
|
cols := make(map[string]*Column)
|
||||||
colSeq := make([]string, 0)
|
colSeq := make([]string, 0)
|
||||||
for _, record := range res {
|
for rows.Next() {
|
||||||
col := new(Column)
|
col := new(Column)
|
||||||
col.Indexes = make(map[string]bool)
|
col.Indexes = make(map[string]bool)
|
||||||
for name, content := range record {
|
|
||||||
switch name {
|
var colName, colDefault, nullable, dataType, dataPrecision, dataScale string
|
||||||
case "column_name":
|
var dataLen int
|
||||||
col.Name = strings.Trim(string(content), `" `)
|
|
||||||
case "data_default":
|
err = rows.Scan(&colName, &colDefault, &dataType, &dataLen, &dataPrecision,
|
||||||
col.Default = string(content)
|
&dataScale, &nullable)
|
||||||
case "nullable":
|
if err != nil {
|
||||||
if string(content) == "Y" {
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
col.Name = strings.Trim(colName, `" `)
|
||||||
|
col.Default = colDefault
|
||||||
|
|
||||||
|
if nullable == "Y" {
|
||||||
col.Nullable = true
|
col.Nullable = true
|
||||||
} else {
|
} else {
|
||||||
col.Nullable = false
|
col.Nullable = false
|
||||||
}
|
}
|
||||||
case "data_type":
|
|
||||||
ct := string(content)
|
switch dataType {
|
||||||
switch ct {
|
|
||||||
case "VARCHAR2":
|
case "VARCHAR2":
|
||||||
col.SQLType = SQLType{Varchar, 0, 0}
|
col.SQLType = SQLType{Varchar, 0, 0}
|
||||||
case "TIMESTAMP WITH TIME ZONE":
|
case "TIMESTAMP WITH TIME ZONE":
|
||||||
col.SQLType = SQLType{TimeStamp, 0, 0}
|
col.SQLType = SQLType{TimeStampz, 0, 0}
|
||||||
default:
|
default:
|
||||||
col.SQLType = SQLType{strings.ToUpper(ct), 0, 0}
|
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", 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":
|
|
||||||
}
|
}
|
||||||
|
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.SQLType.IsText() {
|
||||||
if col.Default != "" {
|
if col.Default != "" {
|
||||||
col.Default = "'" + 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) {
|
func (db *oracle) GetTables() ([]*Table, error) {
|
||||||
args := []interface{}{}
|
args := []interface{}{}
|
||||||
s := "SELECT table_name FROM user_tables"
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer cnn.Close()
|
defer cnn.Close()
|
||||||
res, err := query(cnn, s, args...)
|
rows, err := cnn.Query(s, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tables := make([]*Table, 0)
|
tables := make([]*Table, 0)
|
||||||
for _, record := range res {
|
for rows.Next() {
|
||||||
table := new(Table)
|
table := NewEmptyTable()
|
||||||
for name, content := range record {
|
err = rows.Scan(&table.Name)
|
||||||
switch name {
|
if err != nil {
|
||||||
case "table_name":
|
return nil, err
|
||||||
table.Name = string(content)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tables = append(tables, table)
|
tables = append(tables, table)
|
||||||
}
|
}
|
||||||
return tables, nil
|
return tables, nil
|
||||||
|
|
@ -209,40 +187,37 @@ func (db *oracle) GetTables() ([]*Table, error) {
|
||||||
|
|
||||||
func (db *oracle) GetIndexes(tableName string) (map[string]*Index, error) {
|
func (db *oracle) GetIndexes(tableName string) (map[string]*Index, error) {
|
||||||
args := []interface{}{tableName}
|
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"
|
"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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer cnn.Close()
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
indexes := make(map[string]*Index, 0)
|
indexName = strings.Trim(indexName, `" `)
|
||||||
for _, record := range res {
|
|
||||||
var indexType int
|
|
||||||
var indexName string
|
|
||||||
var colName string
|
|
||||||
|
|
||||||
for name, content := range record {
|
if uniqueness == "UNIQUE" {
|
||||||
switch name {
|
|
||||||
case "index_name":
|
|
||||||
indexName = strings.Trim(string(content), `" `)
|
|
||||||
case "uniqueness":
|
|
||||||
c := string(content)
|
|
||||||
if c == "UNIQUE" {
|
|
||||||
indexType = UniqueType
|
indexType = UniqueType
|
||||||
} else {
|
} else {
|
||||||
indexType = IndexType
|
indexType = IndexType
|
||||||
}
|
}
|
||||||
case "column_name":
|
|
||||||
colName = string(content)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var index *Index
|
var index *Index
|
||||||
var ok bool
|
var ok bool
|
||||||
|
|
@ -256,3 +231,20 @@ func (db *oracle) GetIndexes(tableName string) (map[string]*Index, error) {
|
||||||
}
|
}
|
||||||
return indexes, nil
|
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{}}
|
||||||
|
}
|
||||||
|
|
@ -1,65 +1,24 @@
|
||||||
package xorm
|
package dialects
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
. "github.com/lunny/xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterDialect("postgres", &postgres{})
|
||||||
|
}
|
||||||
|
|
||||||
type postgres struct {
|
type postgres struct {
|
||||||
base
|
Base
|
||||||
}
|
}
|
||||||
|
|
||||||
type values map[string]string
|
func (db *postgres) Init(uri *Uri, drivername, dataSourceName string) error {
|
||||||
|
return db.Base.Init(db, uri, drivername, dataSourceName)
|
||||||
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) SqlType(c *Column) string {
|
func (db *postgres) SqlType(c *Column) string {
|
||||||
|
|
@ -153,40 +112,53 @@ func (db *postgres) GetColumns(tableName string) ([]string, map[string]*Column,
|
||||||
args := []interface{}{tableName}
|
args := []interface{}{tableName}
|
||||||
s := "SELECT column_name, column_default, is_nullable, data_type, character_maximum_length" +
|
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"
|
", numeric_precision, numeric_precision_radix FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = $1"
|
||||||
|
cnn, err := Open(db.DriverName(), db.DataSourceName())
|
||||||
cnn, err := sql.Open(db.driverName, db.dataSourceName)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
defer cnn.Close()
|
defer cnn.Close()
|
||||||
res, err := query(cnn, s, args...)
|
rows, err := cnn.Query(s, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
cols := make(map[string]*Column)
|
cols := make(map[string]*Column)
|
||||||
colSeq := make([]string, 0)
|
colSeq := make([]string, 0)
|
||||||
for _, record := range res {
|
|
||||||
|
for rows.Next() {
|
||||||
col := new(Column)
|
col := new(Column)
|
||||||
col.Indexes = make(map[string]bool)
|
col.Indexes = make(map[string]bool)
|
||||||
for name, content := range record {
|
var colName, isNullable, dataType string
|
||||||
switch name {
|
var maxLenStr, colDefault, numPrecision, numRadix *string
|
||||||
case "column_name":
|
err = rows.Scan(&colName, &colDefault, &isNullable, &dataType, &maxLenStr, &numPrecision, &numRadix)
|
||||||
col.Name = strings.Trim(string(content), `" `)
|
if err != nil {
|
||||||
case "column_default":
|
return nil, nil, err
|
||||||
if strings.HasPrefix(string(content), "nextval") {
|
}
|
||||||
|
|
||||||
|
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
|
col.IsPrimaryKey = true
|
||||||
} else {
|
} else {
|
||||||
col.Default = string(content)
|
col.Default = *colDefault
|
||||||
}
|
}
|
||||||
case "is_nullable":
|
}
|
||||||
if string(content) == "YES" {
|
|
||||||
|
if isNullable == "YES" {
|
||||||
col.Nullable = true
|
col.Nullable = true
|
||||||
} else {
|
} else {
|
||||||
col.Nullable = false
|
col.Nullable = false
|
||||||
}
|
}
|
||||||
case "data_type":
|
|
||||||
ct := string(content)
|
switch dataType {
|
||||||
switch ct {
|
|
||||||
case "character varying", "character":
|
case "character varying", "character":
|
||||||
col.SQLType = SQLType{Varchar, 0, 0}
|
col.SQLType = SQLType{Varchar, 0, 0}
|
||||||
case "timestamp without time zone":
|
case "timestamp without time zone":
|
||||||
|
|
@ -200,21 +172,14 @@ func (db *postgres) GetColumns(tableName string) ([]string, map[string]*Column,
|
||||||
case "time without time zone":
|
case "time without time zone":
|
||||||
col.SQLType = SQLType{Time, 0, 0}
|
col.SQLType = SQLType{Time, 0, 0}
|
||||||
default:
|
default:
|
||||||
col.SQLType = SQLType{strings.ToUpper(ct), 0, 0}
|
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", 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":
|
|
||||||
}
|
}
|
||||||
|
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.SQLType.IsText() {
|
||||||
if col.Default != "" {
|
if col.Default != "" {
|
||||||
col.Default = "'" + 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) {
|
func (db *postgres) GetTables() ([]*Table, error) {
|
||||||
args := []interface{}{}
|
args := []interface{}{}
|
||||||
s := "SELECT tablename FROM pg_tables where schemaname = 'public'"
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer cnn.Close()
|
defer cnn.Close()
|
||||||
res, err := query(cnn, s, args...)
|
rows, err := cnn.Query(s, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tables := make([]*Table, 0)
|
tables := make([]*Table, 0)
|
||||||
for _, record := range res {
|
for rows.Next() {
|
||||||
table := new(Table)
|
table := NewEmptyTable()
|
||||||
for name, content := range record {
|
var name string
|
||||||
switch name {
|
err = rows.Scan(&name)
|
||||||
case "tablename":
|
if err != nil {
|
||||||
table.Name = string(content)
|
return nil, err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
table.Name = name
|
||||||
tables = append(tables, table)
|
tables = append(tables, table)
|
||||||
}
|
}
|
||||||
return tables, nil
|
return tables, nil
|
||||||
|
|
@ -256,39 +221,37 @@ func (db *postgres) GetTables() ([]*Table, error) {
|
||||||
|
|
||||||
func (db *postgres) GetIndexes(tableName string) (map[string]*Index, error) {
|
func (db *postgres) GetIndexes(tableName string) (map[string]*Index, error) {
|
||||||
args := []interface{}{tableName}
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer cnn.Close()
|
defer cnn.Close()
|
||||||
res, err := query(cnn, s, args...)
|
rows, err := cnn.Query(s, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
indexes := make(map[string]*Index, 0)
|
indexes := make(map[string]*Index, 0)
|
||||||
for _, record := range res {
|
for rows.Next() {
|
||||||
var indexType int
|
var indexType int
|
||||||
var indexName string
|
var indexName, indexdef string
|
||||||
var colNames []string
|
var colNames []string
|
||||||
|
err = rows.Scan(&indexName, &indexdef)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
indexName = strings.Trim(indexName, `" `)
|
||||||
|
|
||||||
for name, content := range record {
|
if strings.HasPrefix(indexdef, "CREATE UNIQUE INDEX") {
|
||||||
switch name {
|
|
||||||
case "indexname":
|
|
||||||
indexName = strings.Trim(string(content), `" `)
|
|
||||||
case "indexdef":
|
|
||||||
c := string(content)
|
|
||||||
if strings.HasPrefix(c, "CREATE UNIQUE INDEX") {
|
|
||||||
indexType = UniqueType
|
indexType = UniqueType
|
||||||
} else {
|
} else {
|
||||||
indexType = IndexType
|
indexType = IndexType
|
||||||
}
|
}
|
||||||
cs := strings.Split(c, "(")
|
cs := strings.Split(indexdef, "(")
|
||||||
colNames = strings.Split(cs[1][0:len(cs[1])-1], ",")
|
colNames = strings.Split(cs[1][0:len(cs[1])-1], ",")
|
||||||
}
|
|
||||||
}
|
|
||||||
if strings.HasSuffix(indexName, "_pkey") {
|
if strings.HasSuffix(indexName, "_pkey") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
@ -307,3 +270,24 @@ func (db *postgres) GetIndexes(tableName string) (map[string]*Index, error) {
|
||||||
}
|
}
|
||||||
return indexes, nil
|
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{}}
|
||||||
|
}
|
||||||
|
|
@ -1,23 +1,21 @@
|
||||||
package xorm
|
package dialects
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
. "github.com/lunny/xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterDialect("sqlite3", &sqlite3{})
|
||||||
|
}
|
||||||
|
|
||||||
type sqlite3 struct {
|
type sqlite3 struct {
|
||||||
base
|
Base
|
||||||
}
|
}
|
||||||
|
|
||||||
type sqlite3Parser struct {
|
func (db *sqlite3) Init(uri *Uri, drivername, dataSourceName string) error {
|
||||||
}
|
return db.Base.Init(db, uri, drivername, dataSourceName)
|
||||||
|
|
||||||
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) SqlType(c *Column) string {
|
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) {
|
func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*Column, error) {
|
||||||
args := []interface{}{tableName}
|
args := []interface{}{tableName}
|
||||||
s := "SELECT sql FROM sqlite_master WHERE type='table' and name = ?"
|
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 {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
defer cnn.Close()
|
defer cnn.Close()
|
||||||
res, err := query(cnn, s, args...)
|
|
||||||
|
rows, err := cnn.Query(s, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
var sql string
|
var name string
|
||||||
for _, record := range res {
|
for rows.Next() {
|
||||||
for name, content := range record {
|
err = rows.Scan(&name)
|
||||||
if name == "sql" {
|
if err != nil {
|
||||||
sql = string(content)
|
return nil, nil, err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nStart := strings.Index(sql, "(")
|
nStart := strings.Index(name, "(")
|
||||||
nEnd := strings.Index(sql, ")")
|
nEnd := strings.Index(name, ")")
|
||||||
colCreates := strings.Split(sql[nStart+1:nEnd], ",")
|
colCreates := strings.Split(name[nStart+1:nEnd], ",")
|
||||||
cols := make(map[string]*Column)
|
cols := make(map[string]*Column)
|
||||||
colSeq := make([]string, 0)
|
colSeq := make([]string, 0)
|
||||||
for _, colStr := range colCreates {
|
for _, colStr := range colCreates {
|
||||||
|
|
@ -148,24 +147,23 @@ func (db *sqlite3) GetTables() ([]*Table, error) {
|
||||||
args := []interface{}{}
|
args := []interface{}{}
|
||||||
s := "SELECT name FROM sqlite_master WHERE type='table'"
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer cnn.Close()
|
defer cnn.Close()
|
||||||
res, err := query(cnn, s, args...)
|
rows, err := cnn.Query(s, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
tables := make([]*Table, 0)
|
tables := make([]*Table, 0)
|
||||||
for _, record := range res {
|
for rows.Next() {
|
||||||
table := new(Table)
|
table := NewEmptyTable()
|
||||||
for name, content := range record {
|
err = rows.Scan(&table.Name)
|
||||||
switch name {
|
if err != nil {
|
||||||
case "name":
|
return nil, err
|
||||||
table.Name = string(content)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if table.Name == "sqlite_sequence" {
|
if table.Name == "sqlite_sequence" {
|
||||||
continue
|
continue
|
||||||
|
|
@ -178,25 +176,30 @@ func (db *sqlite3) GetTables() ([]*Table, error) {
|
||||||
func (db *sqlite3) GetIndexes(tableName string) (map[string]*Index, error) {
|
func (db *sqlite3) GetIndexes(tableName string) (map[string]*Index, error) {
|
||||||
args := []interface{}{tableName}
|
args := []interface{}{tableName}
|
||||||
s := "SELECT sql FROM sqlite_master WHERE type='index' and tbl_name = ?"
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer cnn.Close()
|
defer cnn.Close()
|
||||||
res, err := query(cnn, s, args...)
|
rows, err := cnn.Query(s, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
indexes := make(map[string]*Index, 0)
|
indexes := make(map[string]*Index, 0)
|
||||||
for _, record := range res {
|
for rows.Next() {
|
||||||
index := new(Index)
|
var sql string
|
||||||
sql := string(record["sql"])
|
err = rows.Scan(&sql)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if sql == "" {
|
if sql == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
index := new(Index)
|
||||||
nNStart := strings.Index(sql, "INDEX")
|
nNStart := strings.Index(sql, "INDEX")
|
||||||
nNEnd := strings.Index(sql, "ON")
|
nNEnd := strings.Index(sql, "ON")
|
||||||
if nNStart == -1 || nNEnd == -1 {
|
if nNStart == -1 || nNEnd == -1 {
|
||||||
|
|
@ -230,3 +233,7 @@ func (db *sqlite3) GetIndexes(tableName string) (map[string]*Index, error) {
|
||||||
|
|
||||||
return indexes, nil
|
return indexes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *sqlite3) Filters() []Filter {
|
||||||
|
return []Filter{&IdFilter{}}
|
||||||
|
}
|
||||||
|
|
@ -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¶mN=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
|
||||||
|
}
|
||||||
|
|
@ -1,20 +1,22 @@
|
||||||
package xorm
|
package drivers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/lunny/xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mymysql struct {
|
func init() {
|
||||||
mysql
|
core.RegisterDriver("mymysql", &mymysqlDriver{})
|
||||||
}
|
}
|
||||||
|
|
||||||
type mymysqlParser struct {
|
type mymysqlDriver struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *mymysqlParser) parse(driverName, dataSourceName string) (*uri, error) {
|
func (p *mymysqlDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
|
||||||
db := &uri{dbType: MYSQL}
|
db := &core.Uri{DbType: core.MYSQL}
|
||||||
|
|
||||||
pd := strings.SplitN(dataSourceName, "*", 2)
|
pd := strings.SplitN(dataSourceName, "*", 2)
|
||||||
if len(pd) == 2 {
|
if len(pd) == 2 {
|
||||||
|
|
@ -23,9 +25,9 @@ func (p *mymysqlParser) parse(driverName, dataSourceName string) (*uri, error) {
|
||||||
if len(p) != 2 {
|
if len(p) != 2 {
|
||||||
return nil, errors.New("Wrong protocol part of URI")
|
return nil, errors.New("Wrong protocol part of URI")
|
||||||
}
|
}
|
||||||
db.proto = p[0]
|
db.Proto = p[0]
|
||||||
options := strings.Split(p[1], ",")
|
options := strings.Split(p[1], ",")
|
||||||
db.raddr = options[0]
|
db.Raddr = options[0]
|
||||||
for _, o := range options[1:] {
|
for _, o := range options[1:] {
|
||||||
kv := strings.SplitN(o, "=", 2)
|
kv := strings.SplitN(o, "=", 2)
|
||||||
var k, v string
|
var k, v string
|
||||||
|
|
@ -36,13 +38,13 @@ func (p *mymysqlParser) parse(driverName, dataSourceName string) (*uri, error) {
|
||||||
}
|
}
|
||||||
switch k {
|
switch k {
|
||||||
case "laddr":
|
case "laddr":
|
||||||
db.laddr = v
|
db.Laddr = v
|
||||||
case "timeout":
|
case "timeout":
|
||||||
to, err := time.ParseDuration(v)
|
to, err := time.ParseDuration(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
db.timeout = to
|
db.Timeout = to
|
||||||
default:
|
default:
|
||||||
return nil, errors.New("Unknown option: " + k)
|
return nil, errors.New("Unknown option: " + k)
|
||||||
}
|
}
|
||||||
|
|
@ -55,13 +57,9 @@ func (p *mymysqlParser) parse(driverName, dataSourceName string) (*uri, error) {
|
||||||
if len(dup) != 3 {
|
if len(dup) != 3 {
|
||||||
return nil, errors.New("Wrong database part of URI")
|
return nil, errors.New("Wrong database part of URI")
|
||||||
}
|
}
|
||||||
db.dbName = dup[0]
|
db.DbName = dup[0]
|
||||||
db.user = dup[1]
|
db.User = dup[1]
|
||||||
db.passwd = dup[2]
|
db.Passwd = dup[2]
|
||||||
|
|
||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *mymysql) Init(drivername, uri string) error {
|
|
||||||
return db.mysql.base.init(&mymysqlParser{}, drivername, uri)
|
|
||||||
}
|
|
||||||
|
|
@ -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¶mN=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
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -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
174
engine.go
|
|
@ -12,40 +12,10 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"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{}
|
type PK []interface{}
|
||||||
|
|
||||||
// Engine is the major struct of xorm, it means a database manager.
|
// Engine is the major struct of xorm, it means a database manager.
|
||||||
|
|
@ -56,18 +26,19 @@ type Engine struct {
|
||||||
TagIdentifier string
|
TagIdentifier string
|
||||||
DriverName string
|
DriverName string
|
||||||
DataSourceName string
|
DataSourceName string
|
||||||
dialect dialect
|
dialect core.Dialect
|
||||||
Tables map[reflect.Type]*Table
|
Tables map[reflect.Type]*core.Table
|
||||||
|
|
||||||
mutex *sync.Mutex
|
mutex *sync.Mutex
|
||||||
ShowSQL bool
|
ShowSQL bool
|
||||||
ShowErr bool
|
ShowErr bool
|
||||||
ShowDebug bool
|
ShowDebug bool
|
||||||
ShowWarn bool
|
ShowWarn bool
|
||||||
Pool IConnectPool
|
Pool IConnectPool
|
||||||
Filters []Filter
|
Filters []core.Filter
|
||||||
Logger io.Writer
|
Logger io.Writer
|
||||||
Cacher Cacher
|
Cacher Cacher
|
||||||
UseCache bool
|
tableCachers map[reflect.Type]Cacher
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) SetMapper(mapper IMapper) {
|
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()
|
return engine.dialect.QuoteStr() + sql + engine.dialect.QuoteStr()
|
||||||
}
|
}
|
||||||
|
|
||||||
// A simple wrapper to dialect's SqlType method
|
// A simple wrapper to dialect's core.SqlType method
|
||||||
func (engine *Engine) SqlType(c *Column) string {
|
func (engine *Engine) SqlType(c *core.Column) string {
|
||||||
return engine.dialect.SqlType(c)
|
return engine.dialect.SqlType(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -130,12 +101,7 @@ func (engine *Engine) SetMaxIdleConns(conns int) {
|
||||||
|
|
||||||
// SetDefaltCacher set the default cacher. Xorm's default not enable cacher.
|
// SetDefaltCacher set the default cacher. Xorm's default not enable cacher.
|
||||||
func (engine *Engine) SetDefaultCacher(cacher Cacher) {
|
func (engine *Engine) SetDefaultCacher(cacher Cacher) {
|
||||||
if cacher == nil {
|
|
||||||
engine.UseCache = false
|
|
||||||
} else {
|
|
||||||
engine.UseCache = true
|
|
||||||
engine.Cacher = cacher
|
engine.Cacher = cacher
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If you has set default cacher, and you want temporilly stop use cache,
|
// If you has set default cacher, and you want temporilly stop use cache,
|
||||||
|
|
@ -156,7 +122,7 @@ func (engine *Engine) NoCascade() *Session {
|
||||||
func (engine *Engine) MapCacher(bean interface{}, cacher Cacher) {
|
func (engine *Engine) MapCacher(bean interface{}, cacher Cacher) {
|
||||||
t := rType(bean)
|
t := rType(bean)
|
||||||
engine.autoMapType(t)
|
engine.autoMapType(t)
|
||||||
engine.Tables[t].Cacher = cacher
|
engine.tableCachers[t] = cacher
|
||||||
}
|
}
|
||||||
|
|
||||||
// OpenDB provides a interface to operate database directly.
|
// 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.
|
// 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()
|
tables, err := engine.dialect.GetTables()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -246,8 +212,11 @@ func (engine *Engine) DBMetas() ([]*Table, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
table.Columns = cols
|
for _, name := range colSeq {
|
||||||
table.ColumnsSeq = colSeq
|
table.AddColumn(cols[name])
|
||||||
|
}
|
||||||
|
//table.Columns = cols
|
||||||
|
//table.ColumnsSeq = colSeq
|
||||||
|
|
||||||
indexes, err := engine.dialect.GetIndexes(table.Name)
|
indexes, err := engine.dialect.GetIndexes(table.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -257,10 +226,10 @@ func (engine *Engine) DBMetas() ([]*Table, error) {
|
||||||
|
|
||||||
for _, index := range indexes {
|
for _, index := range indexes {
|
||||||
for _, name := range index.Cols {
|
for _, name := range index.Cols {
|
||||||
if col, ok := table.Columns[name]; ok {
|
if col := table.GetColumn(name); col != nil {
|
||||||
col.Indexes[index.Name] = true
|
col.Indexes[index.Name] = true
|
||||||
} else {
|
} 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)
|
return session.Having(conditions)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) autoMapType(t reflect.Type) *Table {
|
func (engine *Engine) autoMapType(t reflect.Type) *core.Table {
|
||||||
engine.mutex.Lock()
|
engine.mutex.Lock()
|
||||||
defer engine.mutex.Unlock()
|
defer engine.mutex.Unlock()
|
||||||
table, ok := engine.Tables[t]
|
table, ok := engine.Tables[t]
|
||||||
|
|
@ -431,37 +400,31 @@ func (engine *Engine) autoMapType(t reflect.Type) *Table {
|
||||||
return table
|
return table
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) autoMap(bean interface{}) *Table {
|
func (engine *Engine) autoMap(bean interface{}) *core.Table {
|
||||||
t := rType(bean)
|
t := rType(bean)
|
||||||
return engine.autoMapType(t)
|
return engine.autoMapType(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) newTable() *Table {
|
func (engine *Engine) mapType(t reflect.Type) *core.Table {
|
||||||
table := &Table{}
|
return mappingTable(t, engine.tableMapper, engine.columnMapper, engine.dialect, engine.TagIdentifier)
|
||||||
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) *Table {
|
func mappingTable(t reflect.Type, tableMapper IMapper, colMapper IMapper, dialect core.Dialect, tagId string) *core.Table {
|
||||||
table := engine.newTable()
|
table := core.NewEmptyTable()
|
||||||
table.Name = engine.tableMapper.Obj2Table(t.Name())
|
table.Name = tableMapper.Obj2Table(t.Name())
|
||||||
table.Type = t
|
table.Type = t
|
||||||
|
|
||||||
var idFieldColName string
|
var idFieldColName string
|
||||||
|
|
||||||
for i := 0; i < t.NumField(); i++ {
|
for i := 0; i < t.NumField(); i++ {
|
||||||
tag := t.Field(i).Tag
|
tag := t.Field(i).Tag
|
||||||
ormTagStr := tag.Get(engine.TagIdentifier)
|
ormTagStr := tag.Get(tagId)
|
||||||
var col *Column
|
var col *core.Column
|
||||||
fieldType := t.Field(i).Type
|
fieldType := t.Field(i).Type
|
||||||
|
|
||||||
if ormTagStr != "" {
|
if ormTagStr != "" {
|
||||||
col = &Column{FieldName: t.Field(i).Name, Nullable: true, IsPrimaryKey: false,
|
col = &core.Column{FieldName: t.Field(i).Name, Nullable: true, IsPrimaryKey: false,
|
||||||
IsAutoIncrement: false, MapType: TWOSIDES, Indexes: make(map[string]bool)}
|
IsAutoIncrement: false, MapType: core.TWOSIDES, Indexes: make(map[string]bool)}
|
||||||
tags := strings.Split(ormTagStr, " ")
|
tags := strings.Split(ormTagStr, " ")
|
||||||
|
|
||||||
if len(tags) > 0 {
|
if len(tags) > 0 {
|
||||||
|
|
@ -470,25 +433,24 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
|
||||||
}
|
}
|
||||||
if (strings.ToUpper(tags[0]) == "EXTENDS") &&
|
if (strings.ToUpper(tags[0]) == "EXTENDS") &&
|
||||||
(fieldType.Kind() == reflect.Struct) {
|
(fieldType.Kind() == reflect.Struct) {
|
||||||
parentTable := engine.mapType(fieldType)
|
parentTable := mappingTable(fieldType, tableMapper, colMapper, dialect, tagId)
|
||||||
for name, col := range parentTable.Columns {
|
for _, col := range parentTable.Columns() {
|
||||||
col.FieldName = fmt.Sprintf("%v.%v", fieldType.Name(), col.FieldName)
|
col.FieldName = fmt.Sprintf("%v.%v", fieldType.Name(), col.FieldName)
|
||||||
table.Columns[strings.ToLower(name)] = col
|
table.AddColumn(col)
|
||||||
table.ColumnsSeq = append(table.ColumnsSeq, name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
table.PrimaryKeys = parentTable.PrimaryKeys
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var indexType int
|
var indexType int
|
||||||
var indexName string
|
var indexName string
|
||||||
for j, key := range tags {
|
for j, key := range tags {
|
||||||
k := strings.ToUpper(key)
|
k := strings.ToUpper(key)
|
||||||
switch {
|
switch {
|
||||||
case k == "<-":
|
case k == "<-":
|
||||||
col.MapType = ONLYFROMDB
|
col.MapType = core.ONLYFROMDB
|
||||||
case k == "->":
|
case k == "->":
|
||||||
col.MapType = ONLYTODB
|
col.MapType = core.ONLYTODB
|
||||||
case k == "PK":
|
case k == "PK":
|
||||||
col.IsPrimaryKey = true
|
col.IsPrimaryKey = true
|
||||||
col.Nullable = false
|
col.Nullable = false
|
||||||
|
|
@ -506,15 +468,15 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
|
||||||
case k == "UPDATED":
|
case k == "UPDATED":
|
||||||
col.IsUpdated = true
|
col.IsUpdated = true
|
||||||
case strings.HasPrefix(k, "INDEX(") && strings.HasSuffix(k, ")"):
|
case strings.HasPrefix(k, "INDEX(") && strings.HasSuffix(k, ")"):
|
||||||
indexType = IndexType
|
indexType = core.IndexType
|
||||||
indexName = k[len("INDEX")+1 : len(k)-1]
|
indexName = k[len("INDEX")+1 : len(k)-1]
|
||||||
case k == "INDEX":
|
case k == "INDEX":
|
||||||
indexType = IndexType
|
indexType = core.IndexType
|
||||||
case strings.HasPrefix(k, "UNIQUE(") && strings.HasSuffix(k, ")"):
|
case strings.HasPrefix(k, "UNIQUE(") && strings.HasSuffix(k, ")"):
|
||||||
indexName = k[len("UNIQUE")+1 : len(k)-1]
|
indexName = k[len("UNIQUE")+1 : len(k)-1]
|
||||||
indexType = UniqueType
|
indexType = core.UniqueType
|
||||||
case k == "UNIQUE":
|
case k == "UNIQUE":
|
||||||
indexType = UniqueType
|
indexType = core.UniqueType
|
||||||
case k == "NOTNULL":
|
case k == "NOTNULL":
|
||||||
col.Nullable = false
|
col.Nullable = false
|
||||||
case k == "NOT":
|
case k == "NOT":
|
||||||
|
|
@ -525,10 +487,10 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
|
||||||
}
|
}
|
||||||
} else if strings.Contains(k, "(") && strings.HasSuffix(k, ")") {
|
} else if strings.Contains(k, "(") && strings.HasSuffix(k, ")") {
|
||||||
fs := strings.Split(k, "(")
|
fs := strings.Split(k, "(")
|
||||||
if _, ok := sqlTypes[fs[0]]; !ok {
|
if _, ok := core.SqlTypes[fs[0]]; !ok {
|
||||||
continue
|
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], ",")
|
fs2 := strings.Split(fs[1][0:len(fs[1])-1], ",")
|
||||||
if len(fs2) == 2 {
|
if len(fs2) == 2 {
|
||||||
col.Length, _ = strconv.Atoi(fs2[0])
|
col.Length, _ = strconv.Atoi(fs2[0])
|
||||||
|
|
@ -537,17 +499,17 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
|
||||||
col.Length, _ = strconv.Atoi(fs2[0])
|
col.Length, _ = strconv.Atoi(fs2[0])
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if _, ok := sqlTypes[k]; ok {
|
if _, ok := core.SqlTypes[k]; ok {
|
||||||
col.SQLType = SQLType{k, 0, 0}
|
col.SQLType = core.SQLType{k, 0, 0}
|
||||||
} else if key != col.Default {
|
} else if key != col.Default {
|
||||||
col.Name = key
|
col.Name = key
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
engine.SqlType(col)
|
dialect.SqlType(col)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if col.SQLType.Name == "" {
|
if col.SQLType.Name == "" {
|
||||||
col.SQLType = Type2SQLType(fieldType)
|
col.SQLType = core.Type2SQLType(fieldType)
|
||||||
}
|
}
|
||||||
if col.Length == 0 {
|
if col.Length == 0 {
|
||||||
col.Length = col.SQLType.DefaultLength
|
col.Length = col.SQLType.DefaultLength
|
||||||
|
|
@ -556,9 +518,9 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
|
||||||
col.Length2 = col.SQLType.DefaultLength2
|
col.Length2 = col.SQLType.DefaultLength2
|
||||||
}
|
}
|
||||||
if col.Name == "" {
|
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 == "" {
|
if indexName == "" {
|
||||||
indexName = col.Name
|
indexName = col.Name
|
||||||
}
|
}
|
||||||
|
|
@ -566,12 +528,12 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
|
||||||
index.AddColumn(col.Name)
|
index.AddColumn(col.Name)
|
||||||
col.Indexes[index.Name] = true
|
col.Indexes[index.Name] = true
|
||||||
} else {
|
} else {
|
||||||
index := NewIndex(indexName, IndexType)
|
index := core.NewIndex(indexName, core.IndexType)
|
||||||
index.AddColumn(col.Name)
|
index.AddColumn(col.Name)
|
||||||
table.AddIndex(index)
|
table.AddIndex(index)
|
||||||
col.Indexes[index.Name] = true
|
col.Indexes[index.Name] = true
|
||||||
}
|
}
|
||||||
} else if indexType == UniqueType {
|
} else if indexType == core.UniqueType {
|
||||||
if indexName == "" {
|
if indexName == "" {
|
||||||
indexName = col.Name
|
indexName = col.Name
|
||||||
}
|
}
|
||||||
|
|
@ -579,7 +541,7 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
|
||||||
index.AddColumn(col.Name)
|
index.AddColumn(col.Name)
|
||||||
col.Indexes[index.Name] = true
|
col.Indexes[index.Name] = true
|
||||||
} else {
|
} else {
|
||||||
index := NewIndex(indexName, UniqueType)
|
index := core.NewIndex(indexName, core.UniqueType)
|
||||||
index.AddColumn(col.Name)
|
index.AddColumn(col.Name)
|
||||||
table.AddIndex(index)
|
table.AddIndex(index)
|
||||||
col.Indexes[index.Name] = true
|
col.Indexes[index.Name] = true
|
||||||
|
|
@ -587,10 +549,9 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sqlType := Type2SQLType(fieldType)
|
sqlType := core.Type2SQLType(fieldType)
|
||||||
col = &Column{engine.columnMapper.Obj2Table(t.Field(i).Name), t.Field(i).Name, sqlType,
|
col = core.NewColumn(colMapper.Obj2Table(t.Field(i).Name), t.Field(i).Name, sqlType,
|
||||||
sqlType.DefaultLength, sqlType.DefaultLength2, true, "", make(map[string]bool), false, false,
|
sqlType.DefaultLength, sqlType.DefaultLength2, true)
|
||||||
TWOSIDES, false, false, false, false}
|
|
||||||
}
|
}
|
||||||
if col.IsAutoIncrement {
|
if col.IsAutoIncrement {
|
||||||
col.Nullable = false
|
col.Nullable = false
|
||||||
|
|
@ -604,7 +565,7 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
|
||||||
}
|
}
|
||||||
|
|
||||||
if idFieldColName != "" && len(table.PrimaryKeys) == 0 {
|
if idFieldColName != "" && len(table.PrimaryKeys) == 0 {
|
||||||
col := table.Columns[strings.ToLower(idFieldColName)]
|
col := table.GetColumn(idFieldColName)
|
||||||
col.IsPrimaryKey = true
|
col.IsPrimaryKey = true
|
||||||
col.IsAutoIncrement = true
|
col.IsAutoIncrement = true
|
||||||
col.Nullable = false
|
col.Nullable = false
|
||||||
|
|
@ -666,6 +627,13 @@ func (engine *Engine) CreateUniques(bean interface{}) error {
|
||||||
return session.CreateUniques(bean)
|
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
|
// If enabled cache, clear the cache bean
|
||||||
func (engine *Engine) ClearCacheBean(bean interface{}, id int64) error {
|
func (engine *Engine) ClearCacheBean(bean interface{}, id int64) error {
|
||||||
t := rType(bean)
|
t := rType(bean)
|
||||||
|
|
@ -673,9 +641,10 @@ func (engine *Engine) ClearCacheBean(bean interface{}, id int64) error {
|
||||||
return errors.New("error params")
|
return errors.New("error params")
|
||||||
}
|
}
|
||||||
table := engine.autoMap(bean)
|
table := engine.autoMap(bean)
|
||||||
if table.Cacher != nil {
|
cacher := engine.getCacher(t)
|
||||||
table.Cacher.ClearIds(table.Name)
|
if cacher != nil {
|
||||||
table.Cacher.DelBean(table.Name, id)
|
cacher.ClearIds(table.Name)
|
||||||
|
cacher.DelBean(table.Name, id)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -688,9 +657,10 @@ func (engine *Engine) ClearCache(beans ...interface{}) error {
|
||||||
return errors.New("error params")
|
return errors.New("error params")
|
||||||
}
|
}
|
||||||
table := engine.autoMap(bean)
|
table := engine.autoMap(bean)
|
||||||
if table.Cacher != nil {
|
cacher := engine.getCacher(t)
|
||||||
table.Cacher.ClearIds(table.Name)
|
if cacher != nil {
|
||||||
table.Cacher.ClearBeans(table.Name)
|
cacher.ClearIds(table.Name)
|
||||||
|
cacher.ClearBeans(table.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -730,7 +700,7 @@ func (engine *Engine) Sync(beans ...interface{}) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _, col := range table.Columns {
|
for _, col := range table.Columns() {
|
||||||
session := engine.NewSession()
|
session := engine.NewSession()
|
||||||
session.Statement.RefTable = table
|
session.Statement.RefTable = table
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
|
|
@ -753,7 +723,7 @@ func (engine *Engine) Sync(beans ...interface{}) error {
|
||||||
session := engine.NewSession()
|
session := engine.NewSession()
|
||||||
session.Statement.RefTable = table
|
session.Statement.RefTable = table
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
if index.Type == UniqueType {
|
if index.Type == core.UniqueType {
|
||||||
//isExist, err := session.isIndexExist(table.Name, name, true)
|
//isExist, err := session.isIndexExist(table.Name, name, true)
|
||||||
isExist, err := session.isIndexExist2(table.Name, index.Cols, true)
|
isExist, err := session.isIndexExist2(table.Name, index.Cols, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -768,7 +738,7 @@ func (engine *Engine) Sync(beans ...interface{}) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if index.Type == IndexType {
|
} else if index.Type == core.IndexType {
|
||||||
isExist, err := session.isIndexExist2(table.Name, index.Cols, false)
|
isExist, err := session.isIndexExist2(table.Name, index.Cols, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
49
filter.go
49
filter.go
|
|
@ -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
344
mysql.go
|
|
@ -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¶mN=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
|
|
||||||
}
|
|
||||||
|
|
@ -35,6 +35,30 @@ func TestMysql(t *testing.T) {
|
||||||
testAll3(engine, 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) {
|
func TestMysqlWithCache(t *testing.T) {
|
||||||
err := mysqlDdlImport()
|
err := mysqlDdlImport()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
2
rows.go
2
rows.go
|
|
@ -41,7 +41,7 @@ func newRows(session *Session, bean interface{}) (*Rows, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, filter := range rows.session.Engine.Filters {
|
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)
|
rows.session.Engine.LogSQL(sql)
|
||||||
|
|
|
||||||
236
session.go
236
session.go
|
|
@ -9,6 +9,8 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/lunny/xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Struct Session keep a pointer to sql.DB and provides all execution of all
|
// 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
|
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 {
|
func (session *Session) Table(tableNameOrBean interface{}) *Session {
|
||||||
session.Statement.Table(tableNameOrBean)
|
session.Statement.Table(tableNameOrBean)
|
||||||
return session
|
return session
|
||||||
|
|
@ -354,13 +356,10 @@ func (session *Session) scanMapIntoStruct(obj interface{}, objMap map[string][]b
|
||||||
}
|
}
|
||||||
|
|
||||||
table := session.Engine.autoMapType(rType(obj))
|
table := session.Engine.autoMapType(rType(obj))
|
||||||
|
var col *core.Column
|
||||||
for key, data := range objMap {
|
for key, data := range objMap {
|
||||||
key = strings.ToLower(key)
|
if col = table.GetColumn(key); col == nil {
|
||||||
var col *Column
|
session.Engine.LogWarn(fmt.Sprintf("table %v's has not column %v. %v", table.Name, key, table.Columns()))
|
||||||
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))
|
|
||||||
continue
|
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) {
|
func (session *Session) exec(sqlStr string, args ...interface{}) (sql.Result, error) {
|
||||||
for _, filter := range session.Engine.Filters {
|
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)
|
session.Engine.LogSQL(sqlStr)
|
||||||
|
|
@ -596,14 +595,14 @@ func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interf
|
||||||
return false, ErrCacheFailed
|
return false, ErrCacheFailed
|
||||||
}
|
}
|
||||||
for _, filter := range session.Engine.Filters {
|
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)
|
newsql := session.Statement.convertIdSql(sqlStr)
|
||||||
if newsql == "" {
|
if newsql == "" {
|
||||||
return false, ErrCacheFailed
|
return false, ErrCacheFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
cacher := session.Statement.RefTable.Cacher
|
cacher := session.Engine.getCacher(session.Statement.RefTable.Type)
|
||||||
tableName := session.Statement.TableName()
|
tableName := session.Statement.TableName()
|
||||||
session.Engine.LogDebug("[xorm:cacheGet] find sql:", newsql, args)
|
session.Engine.LogDebug("[xorm:cacheGet] find sql:", newsql, args)
|
||||||
ids, err := getCacheSql(cacher, tableName, 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 {
|
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)
|
newsql := session.Statement.convertIdSql(sqlStr)
|
||||||
|
|
@ -688,7 +687,7 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in
|
||||||
}
|
}
|
||||||
|
|
||||||
table := session.Statement.RefTable
|
table := session.Statement.RefTable
|
||||||
cacher := table.Cacher
|
cacher := session.Engine.getCacher(t)
|
||||||
ids, err := getCacheSql(cacher, session.Statement.TableName(), newsql, args)
|
ids, err := getCacheSql(cacher, session.Statement.TableName(), newsql, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//session.Engine.LogError(err)
|
//session.Engine.LogError(err)
|
||||||
|
|
@ -892,7 +891,7 @@ func (session *Session) Get(bean interface{}) (bool, error) {
|
||||||
args = session.Statement.RawParams
|
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...)
|
has, err := session.cacheGet(bean, sqlStr, args...)
|
||||||
if err != ErrCacheFailed {
|
if err != ErrCacheFailed {
|
||||||
return has, err
|
return has, err
|
||||||
|
|
@ -1003,7 +1002,7 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
sliceElementType := sliceValue.Type().Elem()
|
sliceElementType := sliceValue.Type().Elem()
|
||||||
var table *Table
|
var table *core.Table
|
||||||
if session.Statement.RefTable == nil {
|
if session.Statement.RefTable == nil {
|
||||||
if sliceElementType.Kind() == reflect.Ptr {
|
if sliceElementType.Kind() == reflect.Ptr {
|
||||||
if sliceElementType.Elem().Kind() == reflect.Struct {
|
if sliceElementType.Elem().Kind() == reflect.Struct {
|
||||||
|
|
@ -1045,7 +1044,7 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
args = session.Statement.RawParams
|
args = session.Statement.RawParams
|
||||||
}
|
}
|
||||||
|
|
||||||
if table.Cacher != nil &&
|
if cacher := session.Engine.getCacher(table.Type); cacher != nil &&
|
||||||
session.Statement.UseCache &&
|
session.Statement.UseCache &&
|
||||||
!session.Statement.IsDistinct {
|
!session.Statement.IsDistinct {
|
||||||
err = session.cacheFind(sliceElementType, sqlStr, rowsSlicePtr, args...)
|
err = session.cacheFind(sliceElementType, sqlStr, rowsSlicePtr, args...)
|
||||||
|
|
@ -1092,23 +1091,39 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
|
|
||||||
fieldsCount := len(fields)
|
fieldsCount := len(fields)
|
||||||
|
|
||||||
for rawRows.Next() {
|
var newElemFunc func() reflect.Value
|
||||||
var newValue reflect.Value
|
|
||||||
if sliceElementType.Kind() == reflect.Ptr {
|
if sliceElementType.Kind() == reflect.Ptr {
|
||||||
newValue = reflect.New(sliceElementType.Elem())
|
newElemFunc = func() reflect.Value {
|
||||||
} else {
|
return reflect.New(sliceElementType.Elem())
|
||||||
newValue = reflect.New(sliceElementType)
|
|
||||||
}
|
}
|
||||||
|
} 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())
|
err := session.row2Bean(rawRows, fields, fieldsCount, newValue.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if sliceValue.Kind() == reflect.Slice {
|
sliceValueSetFunc(&newValue)
|
||||||
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()))))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -1236,9 +1251,9 @@ func (session *Session) isIndexExist2(tableName string, cols []string, unique bo
|
||||||
for _, index := range indexes {
|
for _, index := range indexes {
|
||||||
if sliceEq(index.Cols, cols) {
|
if sliceEq(index.Cols, cols) {
|
||||||
if unique {
|
if unique {
|
||||||
return index.Type == UniqueType, nil
|
return index.Type == core.UniqueType, nil
|
||||||
} else {
|
} 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)
|
//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)
|
sql, args := session.Statement.genAddColumnStr(col)
|
||||||
_, err = session.exec(sql, args...)
|
_, err = session.exec(sql, args...)
|
||||||
return err
|
return err
|
||||||
|
|
@ -1345,34 +1360,25 @@ func row2map(rows *sql.Rows, fields []string) (resultsMap map[string][]byte, err
|
||||||
return result, nil
|
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)
|
fieldValue, err := col.ValueOfV(dataStruct)
|
||||||
if _, ok := table.Columns[key]; !ok {
|
if err != nil {
|
||||||
session.Engine.LogWarn(fmt.Sprintf("table %v's has not column %v. %v", table.Name, key, table.ColumnsSeq))
|
session.Engine.LogError(err)
|
||||||
return nil
|
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() {
|
if !fieldValue.IsValid() || !fieldValue.CanSet() {
|
||||||
session.Engine.LogWarn("table %v's column %v is not valid or cannot set",
|
session.Engine.LogWarn("table %v's column %v is not valid or cannot set",
|
||||||
table.Name, key)
|
table.Name, key)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &fieldValue
|
return fieldValue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *Session) row2Bean(rows *sql.Rows, fields []string, fieldsCount int, bean interface{}) error {
|
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 {
|
for ii, key := range fields {
|
||||||
if fieldValue := session.getField(&dataStruct, key, table); fieldValue != nil {
|
if fieldValue := session.getField(&dataStruct, key, table); fieldValue != nil {
|
||||||
|
|
||||||
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
|
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
|
||||||
|
|
||||||
//if row is null then ignore
|
//if row is null then ignore
|
||||||
|
|
@ -1404,7 +1409,7 @@ func (session *Session) row2Bean(rows *sql.Rows, fields []string, fieldsCount in
|
||||||
continue
|
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 {
|
if data, err := value2Bytes(&rawValue); err == nil {
|
||||||
structConvert.FromDB(data)
|
structConvert.FromDB(data)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -1634,7 +1639,7 @@ func (session *Session) row2Bean(rows *sql.Rows, fields []string, fieldsCount in
|
||||||
if !hasAssigned {
|
if !hasAssigned {
|
||||||
data, err := value2Bytes(&rawValue)
|
data, err := value2Bytes(&rawValue)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
session.bytes2Value(table.Columns[key], fieldValue, data)
|
session.bytes2Value(table.GetColumn(key), fieldValue, data)
|
||||||
} else {
|
} else {
|
||||||
session.Engine.LogError(err.Error())
|
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{}) {
|
func (session *Session) queryPreprocess(sqlStr *string, paramStr ...interface{}) {
|
||||||
for _, filter := range session.Engine.Filters {
|
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)
|
session.Engine.LogSQL(*sqlStr)
|
||||||
|
|
@ -1763,7 +1768,7 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
|
||||||
colNames := make([]string, 0)
|
colNames := make([]string, 0)
|
||||||
colMultiPlaces := make([]string, 0)
|
colMultiPlaces := make([]string, 0)
|
||||||
var args = make([]interface{}, 0)
|
var args = make([]interface{}, 0)
|
||||||
cols := make([]*Column, 0)
|
cols := make([]*core.Column, 0)
|
||||||
|
|
||||||
for i := 0; i < size; i++ {
|
for i := 0; i < size; i++ {
|
||||||
elemValue := sliceValue.Index(i).Interface()
|
elemValue := sliceValue.Index(i).Interface()
|
||||||
|
|
@ -1781,12 +1786,12 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
|
||||||
// --
|
// --
|
||||||
|
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
for _, col := range table.Columns {
|
for _, col := range table.Columns() {
|
||||||
fieldValue := reflect.Indirect(reflect.ValueOf(elemValue)).FieldByName(col.FieldName)
|
fieldValue := reflect.Indirect(reflect.ValueOf(elemValue)).FieldByName(col.FieldName)
|
||||||
if col.IsAutoIncrement && fieldValue.Int() == 0 {
|
if col.IsAutoIncrement && fieldValue.Int() == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if col.MapType == ONLYFROMDB {
|
if col.MapType == core.ONLYFROMDB {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if session.Statement.ColumnStr != "" {
|
if session.Statement.ColumnStr != "" {
|
||||||
|
|
@ -1814,7 +1819,7 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
|
||||||
if col.IsAutoIncrement && fieldValue.Int() == 0 {
|
if col.IsAutoIncrement && fieldValue.Int() == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if col.MapType == ONLYFROMDB {
|
if col.MapType == core.ONLYFROMDB {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if session.Statement.ColumnStr != "" {
|
if session.Statement.ColumnStr != "" {
|
||||||
|
|
@ -1853,7 +1858,7 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
|
||||||
return 0, err
|
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())
|
session.cacheInsert(session.Statement.TableName())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1904,7 +1909,7 @@ func (session *Session) InsertMulti(rowsSlicePtr interface{}) (int64, error) {
|
||||||
return session.innerInsertMulti(rowsSlicePtr)
|
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))
|
sdata := strings.TrimSpace(string(data))
|
||||||
var x time.Time
|
var x time.Time
|
||||||
var err error
|
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)
|
x, err = time.Parse("2006-01-02 15:04:05", sdata)
|
||||||
} else if len(sdata) == 10 && sdata[4] == '-' && sdata[7] == '-' {
|
} else if len(sdata) == 10 && sdata[4] == '-' && sdata[7] == '-' {
|
||||||
x, err = time.Parse("2006-01-02", sdata)
|
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, " ") {
|
if strings.Contains(sdata, " ") {
|
||||||
ssd := strings.Split(sdata, " ")
|
ssd := strings.Split(sdata, " ")
|
||||||
sdata = ssd[1]
|
sdata = ssd[1]
|
||||||
|
|
@ -1937,7 +1942,7 @@ func (session *Session) byte2Time(col *Column, data []byte) (outTime time.Time,
|
||||||
|
|
||||||
sdata = strings.TrimSpace(sdata)
|
sdata = strings.TrimSpace(sdata)
|
||||||
//fmt.Println(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:]
|
sdata = sdata[len(sdata)-8:]
|
||||||
}
|
}
|
||||||
//fmt.Println(sdata)
|
//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
|
// convert a db data([]byte) to a field value
|
||||||
func (session *Session) bytes2Value(col *Column, fieldValue *reflect.Value, data []byte) error {
|
func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value, data []byte) error {
|
||||||
if structConvert, ok := fieldValue.Addr().Interface().(Conversion); ok {
|
if structConvert, ok := fieldValue.Addr().Interface().(core.Conversion); ok {
|
||||||
return structConvert.FromDB(data)
|
return structConvert.FromDB(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2018,8 +2023,8 @@ func (session *Session) bytes2Value(col *Column, fieldValue *reflect.Value, data
|
||||||
var x int64
|
var x int64
|
||||||
var err error
|
var err error
|
||||||
// for mysql, when use bit, it returned \x01
|
// for mysql, when use bit, it returned \x01
|
||||||
if col.SQLType.Name == Bit &&
|
if col.SQLType.Name == core.Bit &&
|
||||||
session.Engine.dialect.DBType() == MYSQL {
|
session.Engine.dialect.DBType() == core.MYSQL {
|
||||||
if len(data) == 1 {
|
if len(data) == 1 {
|
||||||
x = int64(data[0])
|
x = int64(data[0])
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -2199,7 +2204,7 @@ func (session *Session) bytes2Value(col *Column, fieldValue *reflect.Value, data
|
||||||
var x int64
|
var x int64
|
||||||
var err error
|
var err error
|
||||||
// for mysql, when use bit, it returned \x01
|
// 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") {
|
strings.Contains(session.Engine.DriverName, "mysql") {
|
||||||
if len(data) == 1 {
|
if len(data) == 1 {
|
||||||
x = int64(data[0])
|
x = int64(data[0])
|
||||||
|
|
@ -2225,7 +2230,7 @@ func (session *Session) bytes2Value(col *Column, fieldValue *reflect.Value, data
|
||||||
var x1 int64
|
var x1 int64
|
||||||
var err error
|
var err error
|
||||||
// for mysql, when use bit, it returned \x01
|
// 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") {
|
strings.Contains(session.Engine.DriverName, "mysql") {
|
||||||
if len(data) == 1 {
|
if len(data) == 1 {
|
||||||
x = int(data[0])
|
x = int(data[0])
|
||||||
|
|
@ -2254,7 +2259,7 @@ func (session *Session) bytes2Value(col *Column, fieldValue *reflect.Value, data
|
||||||
var x1 int64
|
var x1 int64
|
||||||
var err error
|
var err error
|
||||||
// for mysql, when use bit, it returned \x01
|
// 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") {
|
strings.Contains(session.Engine.DriverName, "mysql") {
|
||||||
if len(data) == 1 {
|
if len(data) == 1 {
|
||||||
x = int32(data[0])
|
x = int32(data[0])
|
||||||
|
|
@ -2283,7 +2288,7 @@ func (session *Session) bytes2Value(col *Column, fieldValue *reflect.Value, data
|
||||||
var x1 int64
|
var x1 int64
|
||||||
var err error
|
var err error
|
||||||
// for mysql, when use bit, it returned \x01
|
// 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") {
|
strings.Contains(session.Engine.DriverName, "mysql") {
|
||||||
if len(data) == 1 {
|
if len(data) == 1 {
|
||||||
x = int8(data[0])
|
x = int8(data[0])
|
||||||
|
|
@ -2312,7 +2317,7 @@ func (session *Session) bytes2Value(col *Column, fieldValue *reflect.Value, data
|
||||||
var x1 int64
|
var x1 int64
|
||||||
var err error
|
var err error
|
||||||
// for mysql, when use bit, it returned \x01
|
// 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") {
|
strings.Contains(session.Engine.DriverName, "mysql") {
|
||||||
if len(data) == 1 {
|
if len(data) == 1 {
|
||||||
x = int16(data[0])
|
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
|
// 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 fieldValue.CanAddr() {
|
||||||
if fieldConvert, ok := fieldValue.Addr().Interface().(Conversion); ok {
|
if fieldConvert, ok := fieldValue.Addr().Interface().(core.Conversion); ok {
|
||||||
data, err := fieldConvert.ToDB()
|
data, err := fieldConvert.ToDB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
|
@ -2384,19 +2389,19 @@ func (session *Session) value2Interface(col *Column, fieldValue reflect.Value) (
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if fieldType == reflect.TypeOf(c_TIME_DEFAULT) {
|
if fieldType == reflect.TypeOf(c_TIME_DEFAULT) {
|
||||||
t := fieldValue.Interface().(time.Time)
|
t := fieldValue.Interface().(time.Time)
|
||||||
if session.Engine.dialect.DBType() == MSSQL {
|
if session.Engine.dialect.DBType() == core.MSSQL {
|
||||||
if t.IsZero() {
|
if t.IsZero() {
|
||||||
return nil, nil
|
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("2006-01-02 15:04:05 -0700")
|
||||||
s := fieldValue.Interface().(time.Time).Format(time.RFC3339)
|
s := fieldValue.Interface().(time.Time).Format(time.RFC3339)
|
||||||
return s[11:19], nil
|
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
|
return fieldValue.Interface().(time.Time).Format("2006-01-02"), nil
|
||||||
} else if col.SQLType.Name == TimeStampz {
|
} else if col.SQLType.Name == core.TimeStampz {
|
||||||
if session.Engine.dialect.DBType() == MSSQL {
|
if session.Engine.dialect.DBType() == core.MSSQL {
|
||||||
tf := t.Format("2006-01-02T15:04:05.9999999Z07:00")
|
tf := t.Format("2006-01-02T15:04:05.9999999Z07:00")
|
||||||
return tf, nil
|
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 {
|
if err != nil {
|
||||||
return 0, err
|
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
|
// for postgres, many of them didn't implement lastInsertId, so we should
|
||||||
// implemented it ourself.
|
// implemented it ourself.
|
||||||
|
|
||||||
if session.Engine.DriverName != POSTGRES || table.AutoIncrement == "" {
|
if session.Engine.DriverName != core.POSTGRES || table.AutoIncrement == "" {
|
||||||
res, err := session.exec(sqlStr, args...)
|
res, err := session.exec(sqlStr, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
|
@ -2527,13 +2532,15 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
|
||||||
handleAfterInsertProcessorFunc(bean)
|
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())
|
session.cacheInsert(session.Statement.TableName())
|
||||||
}
|
}
|
||||||
|
|
||||||
if table.Version != "" && session.Statement.checkVersion {
|
if table.Version != "" && session.Statement.checkVersion {
|
||||||
verValue := table.VersionColumn().ValueOf(bean)
|
verValue, err := table.VersionColumn().ValueOf(bean)
|
||||||
if verValue.IsValid() && verValue.CanSet() {
|
if err != nil {
|
||||||
|
session.Engine.LogError(err)
|
||||||
|
} else if verValue.IsValid() && verValue.CanSet() {
|
||||||
verValue.SetInt(1)
|
verValue.SetInt(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2548,8 +2555,12 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
|
||||||
return res.RowsAffected()
|
return res.RowsAffected()
|
||||||
}
|
}
|
||||||
|
|
||||||
aiValue := table.AutoIncrColumn().ValueOf(bean)
|
aiValue, err := table.AutoIncrColumn().ValueOf(bean)
|
||||||
if !aiValue.IsValid() /*|| aiValue.Int() != 0*/ || !aiValue.CanSet() {
|
if err != nil {
|
||||||
|
session.Engine.LogError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if aiValue == nil || !aiValue.IsValid() /*|| aiValue.Int() != 0*/ || !aiValue.CanSet() {
|
||||||
return res.RowsAffected()
|
return res.RowsAffected()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2580,13 +2591,15 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
|
||||||
handleAfterInsertProcessorFunc(bean)
|
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())
|
session.cacheInsert(session.Statement.TableName())
|
||||||
}
|
}
|
||||||
|
|
||||||
if table.Version != "" && session.Statement.checkVersion {
|
if table.Version != "" && session.Statement.checkVersion {
|
||||||
verValue := table.VersionColumn().ValueOf(bean)
|
verValue, err := table.VersionColumn().ValueOf(bean)
|
||||||
if verValue.IsValid() && verValue.CanSet() {
|
if err != nil {
|
||||||
|
session.Engine.LogError(err)
|
||||||
|
} else if verValue.IsValid() && verValue.CanSet() {
|
||||||
verValue.SetInt(1)
|
verValue.SetInt(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2601,8 +2614,12 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
|
||||||
return 1, err
|
return 1, err
|
||||||
}
|
}
|
||||||
|
|
||||||
aiValue := table.AutoIncrColumn().ValueOf(bean)
|
aiValue, err := table.AutoIncrColumn().ValueOf(bean)
|
||||||
if !aiValue.IsValid() /*|| aiValue. != 0*/ || !aiValue.CanSet() {
|
if err != nil {
|
||||||
|
session.Engine.LogError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if aiValue == nil || !aiValue.IsValid() /*|| aiValue. != 0*/ || !aiValue.CanSet() {
|
||||||
return 1, nil
|
return 1, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2659,9 +2676,9 @@ func (statement *Statement) convertUpdateSql(sqlStr string) (string, string) {
|
||||||
|
|
||||||
//TODO: for postgres only, if any other database?
|
//TODO: for postgres only, if any other database?
|
||||||
var paraStr string
|
var paraStr string
|
||||||
if statement.Engine.dialect.DBType() == POSTGRES {
|
if statement.Engine.dialect.DBType() == core.POSTGRES {
|
||||||
paraStr = "$"
|
paraStr = "$"
|
||||||
} else if statement.Engine.dialect.DBType() == MSSQL {
|
} else if statement.Engine.dialect.DBType() == core.MSSQL {
|
||||||
paraStr = ":"
|
paraStr = ":"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2687,7 +2704,7 @@ func (session *Session) cacheInsert(tables ...string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
table := session.Statement.RefTable
|
table := session.Statement.RefTable
|
||||||
cacher := table.Cacher
|
cacher := session.Engine.getCacher(table.Type)
|
||||||
|
|
||||||
for _, t := range tables {
|
for _, t := range tables {
|
||||||
session.Engine.LogDebug("cache clear:", t)
|
session.Engine.LogDebug("cache clear:", t)
|
||||||
|
|
@ -2707,7 +2724,7 @@ func (session *Session) cacheUpdate(sqlStr string, args ...interface{}) error {
|
||||||
return ErrCacheFailed
|
return ErrCacheFailed
|
||||||
}
|
}
|
||||||
for _, filter := range session.Engine.Filters {
|
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)
|
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
|
table := session.Statement.RefTable
|
||||||
cacher := table.Cacher
|
cacher := session.Engine.getCacher(table.Type)
|
||||||
tableName := session.Statement.TableName()
|
tableName := session.Statement.TableName()
|
||||||
session.Engine.LogDebug("[xorm:cacheUpdate] get cache sql", newsql, args[nStart:])
|
session.Engine.LogDebug("[xorm:cacheUpdate] get cache sql", newsql, args[nStart:])
|
||||||
ids, err := getCacheSql(cacher, tableName, 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
|
return ErrCacheFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
if col, ok := table.Columns[strings.ToLower(colName)]; ok {
|
if col := table.GetColumn(colName); col != nil {
|
||||||
fieldValue := col.ValueOf(bean)
|
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())
|
session.Engine.LogDebug("[xorm:cacheUpdate] set bean field", bean, colName, fieldValue.Interface())
|
||||||
if col.IsVersion && session.Statement.checkVersion {
|
if col.IsVersion && session.Statement.checkVersion {
|
||||||
fieldValue.SetInt(fieldValue.Int() + 1)
|
fieldValue.SetInt(fieldValue.Int() + 1)
|
||||||
} else {
|
} else {
|
||||||
fieldValue.Set(reflect.ValueOf(args[idx]))
|
fieldValue.Set(reflect.ValueOf(args[idx]))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
session.Engine.LogError("[xorm:cacheUpdate] ERROR: column %v is not table %v's",
|
session.Engine.LogError("[xorm:cacheUpdate] ERROR: column %v is not table %v's",
|
||||||
colName, table.Name)
|
colName, table.Name)
|
||||||
|
|
@ -2820,7 +2841,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
|
||||||
|
|
||||||
var colNames []string
|
var colNames []string
|
||||||
var args []interface{}
|
var args []interface{}
|
||||||
var table *Table
|
var table *core.Table
|
||||||
|
|
||||||
// handle before update processors
|
// handle before update processors
|
||||||
for _, closure := range session.beforeClosures {
|
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,
|
colNames, args = buildConditions(session.Engine, table, bean, false, false,
|
||||||
false, false, session.Statement.allUseBool, session.Statement.boolColumnMap)
|
false, false, session.Statement.allUseBool, session.Statement.boolColumnMap)
|
||||||
} else {
|
} else {
|
||||||
colNames, args, err = table.genCols(session, bean, true, true)
|
colNames, args, err = genCols(table, session, bean, true, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
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",
|
session.Engine.Quote(table.Version)+" = "+session.Engine.Quote(table.Version)+" + 1",
|
||||||
condition)
|
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 {
|
} else {
|
||||||
if condition != "" {
|
if condition != "" {
|
||||||
condition = "WHERE " + condition
|
condition = "WHERE " + condition
|
||||||
|
|
@ -2946,10 +2972,10 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
|
||||||
return 0, err
|
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...)
|
//session.cacheUpdate(sqlStr, args...)
|
||||||
table.Cacher.ClearIds(session.Statement.TableName())
|
cacher.ClearIds(session.Statement.TableName())
|
||||||
table.Cacher.ClearBeans(session.Statement.TableName())
|
cacher.ClearBeans(session.Statement.TableName())
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle after update processors
|
// handle after update processors
|
||||||
|
|
@ -2990,7 +3016,7 @@ func (session *Session) cacheDelete(sqlStr string, args ...interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, filter := range session.Engine.Filters {
|
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)
|
newsql := session.Statement.convertIdSql(sqlStr)
|
||||||
|
|
@ -2998,7 +3024,7 @@ func (session *Session) cacheDelete(sqlStr string, args ...interface{}) error {
|
||||||
return ErrCacheFailed
|
return ErrCacheFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
cacher := session.Statement.RefTable.Cacher
|
cacher := session.Engine.getCacher(session.Statement.RefTable.Type)
|
||||||
tableName := session.Statement.TableName()
|
tableName := session.Statement.TableName()
|
||||||
ids, err := getCacheSql(cacher, tableName, newsql, args)
|
ids, err := getCacheSql(cacher, tableName, newsql, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -3090,7 +3116,7 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
|
||||||
|
|
||||||
args = append(session.Statement.Params, args...)
|
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...)
|
session.cacheDelete(sqlStr, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
112
statement.go
112
statement.go
|
|
@ -8,11 +8,13 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/lunny/xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// statement save all the sql info for executing SQL
|
// statement save all the sql info for executing SQL
|
||||||
type Statement struct {
|
type Statement struct {
|
||||||
RefTable *Table
|
RefTable *core.Table
|
||||||
Engine *Engine
|
Engine *Engine
|
||||||
Start int
|
Start int
|
||||||
LimitN int
|
LimitN int
|
||||||
|
|
@ -64,7 +66,7 @@ func (statement *Statement) Init() {
|
||||||
statement.RawSQL = ""
|
statement.RawSQL = ""
|
||||||
statement.RawParams = make([]interface{}, 0)
|
statement.RawParams = make([]interface{}, 0)
|
||||||
statement.BeanArgs = make([]interface{}, 0)
|
statement.BeanArgs = make([]interface{}, 0)
|
||||||
statement.UseCache = statement.Engine.UseCache
|
statement.UseCache = true
|
||||||
statement.UseAutoTime = true
|
statement.UseAutoTime = true
|
||||||
statement.IsDistinct = false
|
statement.IsDistinct = false
|
||||||
statement.allUseBool = false
|
statement.allUseBool = false
|
||||||
|
|
@ -237,13 +239,13 @@ func (statement *Statement) Table(tableNameOrBean interface{}) *Statement {
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
// Auto generating conditions according a struct
|
// 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,
|
includeVersion bool, includeUpdated bool, includeNil bool, includeAutoIncr bool, allUseBool bool,
|
||||||
boolColumnMap map[string]bool) ([]string, []interface{}) {
|
boolColumnMap map[string]bool) ([]string, []interface{}) {
|
||||||
|
|
||||||
colNames := make([]string, 0)
|
colNames := make([]string, 0)
|
||||||
var args = make([]interface{}, 0)
|
var args = make([]interface{}, 0)
|
||||||
for _, col := range table.Columns {
|
for _, col := range table.Columns() {
|
||||||
if !includeVersion && col.IsVersion {
|
if !includeVersion && col.IsVersion {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
@ -255,10 +257,16 @@ func buildConditions(engine *Engine, table *Table, bean interface{},
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
//fmt.Println(engine.dialect.DBType(), Text)
|
//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
|
continue
|
||||||
}
|
}
|
||||||
fieldValue := col.ValueOf(bean)
|
fieldValuePtr, err := col.ValueOf(bean)
|
||||||
|
if err != nil {
|
||||||
|
engine.LogError(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
fieldValue := *fieldValuePtr
|
||||||
fieldType := reflect.TypeOf(fieldValue.Interface())
|
fieldType := reflect.TypeOf(fieldValue.Interface())
|
||||||
|
|
||||||
requiredField := false
|
requiredField := false
|
||||||
|
|
@ -323,10 +331,10 @@ func buildConditions(engine *Engine, table *Table, bean interface{},
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var str string
|
var str string
|
||||||
if col.SQLType.Name == Time {
|
if col.SQLType.Name == core.Time {
|
||||||
s := t.UTC().Format("2006-01-02 15:04:05")
|
s := t.UTC().Format("2006-01-02 15:04:05")
|
||||||
val = s[11:19]
|
val = s[11:19]
|
||||||
} else if col.SQLType.Name == Date {
|
} else if col.SQLType.Name == core.Date {
|
||||||
str = t.Format("2006-01-02")
|
str = t.Format("2006-01-02")
|
||||||
val = str
|
val = str
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -510,7 +518,7 @@ func (statement *Statement) Distinct(columns ...string) *Statement {
|
||||||
func (statement *Statement) Cols(columns ...string) *Statement {
|
func (statement *Statement) Cols(columns ...string) *Statement {
|
||||||
newColumns := col2NewCols(columns...)
|
newColumns := col2NewCols(columns...)
|
||||||
for _, nc := range newColumns {
|
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(", ")))
|
statement.ColumnStr = statement.Engine.Quote(strings.Join(newColumns, statement.Engine.Quote(", ")))
|
||||||
return statement
|
return statement
|
||||||
|
|
@ -521,7 +529,7 @@ func (statement *Statement) UseBool(columns ...string) *Statement {
|
||||||
if len(columns) > 0 {
|
if len(columns) > 0 {
|
||||||
newColumns := col2NewCols(columns...)
|
newColumns := col2NewCols(columns...)
|
||||||
for _, nc := range newColumns {
|
for _, nc := range newColumns {
|
||||||
statement.boolColumnMap[nc] = true
|
statement.boolColumnMap[strings.ToLower(nc)] = true
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
statement.allUseBool = true
|
statement.allUseBool = true
|
||||||
|
|
@ -533,7 +541,7 @@ func (statement *Statement) UseBool(columns ...string) *Statement {
|
||||||
func (statement *Statement) Omit(columns ...string) {
|
func (statement *Statement) Omit(columns ...string) {
|
||||||
newColumns := col2NewCols(columns...)
|
newColumns := col2NewCols(columns...)
|
||||||
for _, nc := range newColumns {
|
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(", ")))
|
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 {
|
func (statement *Statement) genColumnStr() string {
|
||||||
table := statement.RefTable
|
table := statement.RefTable
|
||||||
colNames := make([]string, 0)
|
colNames := make([]string, 0)
|
||||||
for _, col := range table.Columns {
|
for _, col := range table.Columns() {
|
||||||
if statement.OmitStr != "" {
|
if statement.OmitStr != "" {
|
||||||
if _, ok := statement.columnMap[col.Name]; ok {
|
if _, ok := statement.columnMap[strings.ToLower(col.Name)]; ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if col.MapType == ONLYTODB {
|
if col.MapType == core.ONLYTODB {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
colNames = append(colNames, statement.Engine.Quote(statement.TableName())+"."+statement.Engine.Quote(col.Name))
|
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 {
|
func (statement *Statement) genCreateTableSQL() string {
|
||||||
var sql string
|
return statement.Engine.dialect.CreateTableSql(statement.RefTable, statement.AltTableName,
|
||||||
if statement.Engine.dialect.DBType() == MSSQL {
|
statement.StoreEngine, statement.Charset)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func indexName(tableName, idxName string) string {
|
func indexName(tableName, idxName string) string {
|
||||||
|
|
@ -658,7 +620,7 @@ func (s *Statement) genIndexSQL() []string {
|
||||||
tbName := s.TableName()
|
tbName := s.TableName()
|
||||||
quote := s.Engine.Quote
|
quote := s.Engine.Quote
|
||||||
for idxName, index := range s.RefTable.Indexes {
|
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)),
|
sql := fmt.Sprintf("CREATE INDEX %v ON %v (%v);", quote(indexName(tbName, idxName)),
|
||||||
quote(tbName), quote(strings.Join(index.Cols, quote(","))))
|
quote(tbName), quote(strings.Join(index.Cols, quote(","))))
|
||||||
sqls = append(sqls, sql)
|
sqls = append(sqls, sql)
|
||||||
|
|
@ -676,7 +638,7 @@ func (s *Statement) genUniqueSQL() []string {
|
||||||
tbName := s.TableName()
|
tbName := s.TableName()
|
||||||
quote := s.Engine.Quote
|
quote := s.Engine.Quote
|
||||||
for idxName, unique := range s.RefTable.Indexes {
|
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)),
|
sql := fmt.Sprintf("CREATE UNIQUE INDEX %v ON %v (%v);", quote(uniqueName(tbName, idxName)),
|
||||||
quote(tbName), quote(strings.Join(unique.Cols, quote(","))))
|
quote(tbName), quote(strings.Join(unique.Cols, quote(","))))
|
||||||
sqls = append(sqls, sql)
|
sqls = append(sqls, sql)
|
||||||
|
|
@ -689,9 +651,9 @@ func (s *Statement) genDelIndexSQL() []string {
|
||||||
var sqls []string = make([]string, 0)
|
var sqls []string = make([]string, 0)
|
||||||
for idxName, index := range s.RefTable.Indexes {
|
for idxName, index := range s.RefTable.Indexes {
|
||||||
var rIdxName string
|
var rIdxName string
|
||||||
if index.Type == UniqueType {
|
if index.Type == core.UniqueType {
|
||||||
rIdxName = uniqueName(s.TableName(), idxName)
|
rIdxName = uniqueName(s.TableName(), idxName)
|
||||||
} else if index.Type == IndexType {
|
} else if index.Type == core.IndexType {
|
||||||
rIdxName = indexName(s.TableName(), idxName)
|
rIdxName = indexName(s.TableName(), idxName)
|
||||||
}
|
}
|
||||||
sql := fmt.Sprintf("DROP INDEX %v", s.Engine.Quote(rIdxName))
|
sql := fmt.Sprintf("DROP INDEX %v", s.Engine.Quote(rIdxName))
|
||||||
|
|
@ -704,7 +666,7 @@ func (s *Statement) genDelIndexSQL() []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Statement) genDropSQL() 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'" +
|
return "IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N'" +
|
||||||
s.TableName() + "') and OBJECTPROPERTY(id, N'IsUserTable') = 1) " +
|
s.TableName() + "') and OBJECTPROPERTY(id, N'IsUserTable') = 1) " +
|
||||||
"DROP TABLE " + s.Engine.Quote(s.TableName()) + ";"
|
"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...)
|
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
|
quote := s.Engine.Quote
|
||||||
sql := fmt.Sprintf("ALTER TABLE %v ADD COLUMN %v;", quote(s.TableName()),
|
sql := fmt.Sprintf("ALTER TABLE %v ADD COLUMN %v;", quote(s.TableName()),
|
||||||
col.String(s.Engine.dialect))
|
col.String(s.Engine.dialect))
|
||||||
|
|
@ -804,13 +766,14 @@ func (statement *Statement) genSelectSql(columnStr string) (a string) {
|
||||||
if statement.OrderStr != "" {
|
if statement.OrderStr != "" {
|
||||||
a = fmt.Sprintf("%v ORDER BY %v", a, 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 {
|
if statement.Start > 0 {
|
||||||
a = fmt.Sprintf("%v LIMIT %v OFFSET %v", a, statement.LimitN, statement.Start)
|
a = fmt.Sprintf("%v LIMIT %v OFFSET %v", a, statement.LimitN, statement.Start)
|
||||||
} else if statement.LimitN > 0 {
|
} else if statement.LimitN > 0 {
|
||||||
a = fmt.Sprintf("%v LIMIT %v", a, statement.LimitN)
|
a = fmt.Sprintf("%v LIMIT %v", a, statement.LimitN)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
//TODO: for mssql, should handler limit.
|
||||||
/*SELECT * FROM (
|
/*SELECT * FROM (
|
||||||
SELECT *, ROW_NUMBER() OVER (ORDER BY id desc) as row FROM "userinfo"
|
SELECT *, ROW_NUMBER() OVER (ORDER BY id desc) as row FROM "userinfo"
|
||||||
) a WHERE row > [start] and row <= [start+limit] order by id desc*/
|
) a WHERE row > [start] and row <= [start+limit] order by id desc*/
|
||||||
|
|
@ -823,11 +786,12 @@ func (statement *Statement) processIdParam() {
|
||||||
|
|
||||||
if statement.IdParam != nil {
|
if statement.IdParam != nil {
|
||||||
i := 0
|
i := 0
|
||||||
colCnt := len(statement.RefTable.ColumnsSeq)
|
columns := statement.RefTable.ColumnsSeq()
|
||||||
|
colCnt := len(columns)
|
||||||
for _, elem := range *(statement.IdParam) {
|
for _, elem := range *(statement.IdParam) {
|
||||||
for ; i < colCnt; i++ {
|
for ; i < colCnt; i++ {
|
||||||
colName := statement.RefTable.ColumnsSeq[i]
|
colName := columns[i]
|
||||||
col := statement.RefTable.Columns[strings.ToLower(colName)]
|
col := statement.RefTable.GetColumn(colName)
|
||||||
if col.IsPrimaryKey {
|
if col.IsPrimaryKey {
|
||||||
statement.And(fmt.Sprintf("%v=?", col.Name), elem)
|
statement.And(fmt.Sprintf("%v=?", col.Name), elem)
|
||||||
i++
|
i++
|
||||||
|
|
@ -840,8 +804,8 @@ func (statement *Statement) processIdParam() {
|
||||||
// as empty string for now, so this will result sql exec failed instead of unexpected
|
// as empty string for now, so this will result sql exec failed instead of unexpected
|
||||||
// false update/delete
|
// false update/delete
|
||||||
for ; i < colCnt; i++ {
|
for ; i < colCnt; i++ {
|
||||||
colName := statement.RefTable.ColumnsSeq[i]
|
colName := columns[i]
|
||||||
col := statement.RefTable.Columns[strings.ToLower(colName)]
|
col := statement.RefTable.GetColumn(colName)
|
||||||
if col.IsPrimaryKey {
|
if col.IsPrimaryKey {
|
||||||
statement.And(fmt.Sprintf("%v=?", col.Name), "")
|
statement.And(fmt.Sprintf("%v=?", col.Name), "")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
409
table.go
409
table.go
|
|
@ -2,120 +2,12 @@ package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"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 (
|
var (
|
||||||
c_EMPTY_STRING string
|
c_EMPTY_STRING string
|
||||||
c_BOOL_DEFAULT bool
|
c_BOOL_DEFAULT bool
|
||||||
|
|
@ -137,290 +29,28 @@ var (
|
||||||
c_TIME_DEFAULT time.Time
|
c_TIME_DEFAULT time.Time
|
||||||
)
|
)
|
||||||
|
|
||||||
func Type2SQLType(t reflect.Type) (st SQLType) {
|
func genCols(table *core.Table, session *Session, bean interface{}, useCol bool, includeQuote bool) ([]string, []interface{}, error) {
|
||||||
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) {
|
|
||||||
colNames := make([]string, 0)
|
colNames := make([]string, 0)
|
||||||
args := make([]interface{}, 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 useCol && !col.IsVersion && !col.IsCreated && !col.IsUpdated {
|
||||||
if _, ok := session.Statement.columnMap[col.Name]; !ok {
|
if _, ok := session.Statement.columnMap[lColName]; !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if col.MapType == ONLYFROMDB {
|
if col.MapType == core.ONLYFROMDB {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldValue := col.ValueOf(bean)
|
fieldValuePtr, err := col.ValueOf(bean)
|
||||||
|
if err != nil {
|
||||||
|
session.Engine.LogError(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fieldValue := *fieldValuePtr
|
||||||
|
|
||||||
if col.IsAutoIncrement {
|
if col.IsAutoIncrement {
|
||||||
switch fieldValue.Type().Kind() {
|
switch fieldValue.Type().Kind() {
|
||||||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
|
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 session.Statement.ColumnStr != "" {
|
||||||
if _, ok := session.Statement.columnMap[col.Name]; !ok {
|
if _, ok := session.Statement.columnMap[lColName]; !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if session.Statement.OmitStr != "" {
|
if session.Statement.OmitStr != "" {
|
||||||
if _, ok := session.Statement.columnMap[col.Name]; ok {
|
if _, ok := session.Statement.columnMap[lColName]; ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -469,10 +99,3 @@ func (table *Table) genCols(session *Session, bean interface{}, useCol bool, inc
|
||||||
}
|
}
|
||||||
return colNames, args, nil
|
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
51
xorm.go
|
|
@ -7,6 +7,10 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/lunny/xorm/core"
|
||||||
|
_ "github.com/lunny/xorm/dialects"
|
||||||
|
_ "github.com/lunny/xorm/drivers"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -20,39 +24,38 @@ func close(engine *Engine) {
|
||||||
// new a db manager according to the parameter. Currently support four
|
// new a db manager according to the parameter. Currently support four
|
||||||
// drivers
|
// drivers
|
||||||
func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
|
func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
|
||||||
engine := &Engine{DriverName: driverName,
|
driver := core.QueryDriver(driverName)
|
||||||
DataSourceName: dataSourceName, Filters: make([]Filter, 0)}
|
if driver == nil {
|
||||||
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 {
|
|
||||||
return nil, errors.New(fmt.Sprintf("Unsupported driver name: %v", driverName))
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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.mutex = &sync.Mutex{}
|
||||||
engine.TagIdentifier = "xorm"
|
engine.TagIdentifier = "xorm"
|
||||||
|
|
||||||
engine.Filters = append(engine.Filters, &IdFilter{})
|
|
||||||
engine.Logger = os.Stdout
|
engine.Logger = os.Stdout
|
||||||
|
|
||||||
//engine.Pool = NewSimpleConnectPool()
|
//engine.Pool = NewSimpleConnectPool()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue