|
@@ -49,7 +49,7 @@ type Engine struct {
|
|
|
disableGlobalCache bool
|
|
disableGlobalCache bool
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// ShowSQL show SQL statment or not on logger if log level is great than INFO
|
|
|
|
|
|
|
+// ShowSQL show SQL statement or not on logger if log level is great than INFO
|
|
|
func (engine *Engine) ShowSQL(show ...bool) {
|
|
func (engine *Engine) ShowSQL(show ...bool) {
|
|
|
engine.logger.ShowSQL(show...)
|
|
engine.logger.ShowSQL(show...)
|
|
|
if len(show) == 0 {
|
|
if len(show) == 0 {
|
|
@@ -357,8 +357,6 @@ func (engine *Engine) DBMetas() ([]*core.Table, error) {
|
|
|
for _, name := range colSeq {
|
|
for _, name := range colSeq {
|
|
|
table.AddColumn(cols[name])
|
|
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 {
|
|
|
return nil, err
|
|
return nil, err
|
|
@@ -370,7 +368,7 @@ func (engine *Engine) DBMetas() ([]*core.Table, error) {
|
|
|
if col := table.GetColumn(name); col != nil {
|
|
if col := table.GetColumn(name); col != nil {
|
|
|
col.Indexes[index.Name] = index.Type
|
|
col.Indexes[index.Name] = index.Type
|
|
|
} else {
|
|
} else {
|
|
|
- return nil, fmt.Errorf("Unknown col %s in indexe %v of table %v, columns %v", name, index.Name, table.Name, table.ColumnsSeq())
|
|
|
|
|
|
|
+ return nil, fmt.Errorf("Unknown col %s in index %v of table %v, columns %v", name, index.Name, table.Name, table.ColumnsSeq())
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -379,18 +377,22 @@ func (engine *Engine) DBMetas() ([]*core.Table, error) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// DumpAllToFile dump database all table structs and data to a file
|
|
// DumpAllToFile dump database all table structs and data to a file
|
|
|
-func (engine *Engine) DumpAllToFile(fp string) error {
|
|
|
|
|
|
|
+func (engine *Engine) DumpAllToFile(fp string, tp ...core.DbType) error {
|
|
|
f, err := os.Create(fp)
|
|
f, err := os.Create(fp)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
defer f.Close()
|
|
defer f.Close()
|
|
|
- return engine.DumpAll(f)
|
|
|
|
|
|
|
+ return engine.DumpAll(f, tp...)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// DumpAll dump database all table structs and data to w
|
|
// DumpAll dump database all table structs and data to w
|
|
|
-func (engine *Engine) DumpAll(w io.Writer) error {
|
|
|
|
|
- return engine.dumpAll(w, engine.dialect.DBType())
|
|
|
|
|
|
|
+func (engine *Engine) DumpAll(w io.Writer, tp ...core.DbType) error {
|
|
|
|
|
+ tables, err := engine.DBMetas()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ return engine.DumpTables(tables, w, tp...)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// DumpTablesToFile dump specified tables to SQL file.
|
|
// DumpTablesToFile dump specified tables to SQL file.
|
|
@@ -408,140 +410,7 @@ func (engine *Engine) DumpTables(tables []*core.Table, w io.Writer, tp ...core.D
|
|
|
return engine.dumpTables(tables, w, tp...)
|
|
return engine.dumpTables(tables, w, tp...)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (engine *Engine) tableName(beanOrTableName interface{}) (string, error) {
|
|
|
|
|
- v := rValue(beanOrTableName)
|
|
|
|
|
- if v.Type().Kind() == reflect.String {
|
|
|
|
|
- return beanOrTableName.(string), nil
|
|
|
|
|
- } else if v.Type().Kind() == reflect.Struct {
|
|
|
|
|
- return engine.tbName(v), nil
|
|
|
|
|
- }
|
|
|
|
|
- return "", errors.New("bean should be a struct or struct's point")
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-func (engine *Engine) tbName(v reflect.Value) string {
|
|
|
|
|
- if tb, ok := v.Interface().(TableName); ok {
|
|
|
|
|
- return tb.TableName()
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if v.Type().Kind() == reflect.Ptr {
|
|
|
|
|
- if tb, ok := reflect.Indirect(v).Interface().(TableName); ok {
|
|
|
|
|
- return tb.TableName()
|
|
|
|
|
- }
|
|
|
|
|
- } else if v.CanAddr() {
|
|
|
|
|
- if tb, ok := v.Addr().Interface().(TableName); ok {
|
|
|
|
|
- return tb.TableName()
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return engine.TableMapper.Obj2Table(reflect.Indirect(v).Type().Name())
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// DumpAll dump database all table structs and data to w with specify db type
|
|
|
|
|
-func (engine *Engine) dumpAll(w io.Writer, tp ...core.DbType) error {
|
|
|
|
|
- tables, err := engine.DBMetas()
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- var dialect core.Dialect
|
|
|
|
|
- if len(tp) == 0 {
|
|
|
|
|
- dialect = engine.dialect
|
|
|
|
|
- } else {
|
|
|
|
|
- dialect = core.QueryDialect(tp[0])
|
|
|
|
|
- if dialect == nil {
|
|
|
|
|
- return errors.New("Unsupported database type")
|
|
|
|
|
- }
|
|
|
|
|
- dialect.Init(nil, engine.dialect.URI(), "", "")
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- _, err = io.WriteString(w, fmt.Sprintf("/*Generated by xorm v%s %s*/\n\n",
|
|
|
|
|
- Version, time.Now().In(engine.TZLocation).Format("2006-01-02 15:04:05")))
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- for i, table := range tables {
|
|
|
|
|
- if i > 0 {
|
|
|
|
|
- _, err = io.WriteString(w, "\n")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- _, err = io.WriteString(w, dialect.CreateTableSql(table, "", table.StoreEngine, "")+";\n")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
- for _, index := range table.Indexes {
|
|
|
|
|
- _, err = io.WriteString(w, dialect.CreateIndexSql(table.Name, index)+";\n")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- rows, err := engine.DB().Query("SELECT * FROM " + engine.Quote(table.Name))
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
- defer rows.Close()
|
|
|
|
|
-
|
|
|
|
|
- cols, err := rows.Columns()
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
- if len(cols) == 0 {
|
|
|
|
|
- continue
|
|
|
|
|
- }
|
|
|
|
|
- for rows.Next() {
|
|
|
|
|
- dest := make([]interface{}, len(cols))
|
|
|
|
|
- err = rows.ScanSlice(&dest)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- _, err = io.WriteString(w, "INSERT INTO "+dialect.Quote(table.Name)+" ("+dialect.Quote(strings.Join(cols, dialect.Quote(", ")))+") VALUES (")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- var temp string
|
|
|
|
|
- for i, d := range dest {
|
|
|
|
|
- col := table.GetColumn(cols[i])
|
|
|
|
|
- if d == nil {
|
|
|
|
|
- temp += ", NULL"
|
|
|
|
|
- } else if col.SQLType.IsText() || col.SQLType.IsTime() {
|
|
|
|
|
- var v = fmt.Sprintf("%s", d)
|
|
|
|
|
- temp += ", '" + strings.Replace(v, "'", "''", -1) + "'"
|
|
|
|
|
- } else if col.SQLType.IsBlob() {
|
|
|
|
|
- if reflect.TypeOf(d).Kind() == reflect.Slice {
|
|
|
|
|
- temp += fmt.Sprintf(", %s", dialect.FormatBytes(d.([]byte)))
|
|
|
|
|
- } else if reflect.TypeOf(d).Kind() == reflect.String {
|
|
|
|
|
- temp += fmt.Sprintf(", '%s'", d.(string))
|
|
|
|
|
- }
|
|
|
|
|
- } else if col.SQLType.IsNumeric() {
|
|
|
|
|
- switch reflect.TypeOf(d).Kind() {
|
|
|
|
|
- case reflect.Slice:
|
|
|
|
|
- temp += fmt.Sprintf(", %s", string(d.([]byte)))
|
|
|
|
|
- default:
|
|
|
|
|
- temp += fmt.Sprintf(", %v", d)
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- s := fmt.Sprintf("%v", d)
|
|
|
|
|
- if strings.Contains(s, ":") || strings.Contains(s, "-") {
|
|
|
|
|
- temp += fmt.Sprintf(", '%s'", s)
|
|
|
|
|
- } else {
|
|
|
|
|
- temp += fmt.Sprintf(", %s", s)
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- _, err = io.WriteString(w, temp[2:]+");\n")
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return nil
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// DumpAll dump database all table structs and data to w with specify db type
|
|
|
|
|
|
|
+// dumpTables dump database all table structs and data to w with specify db type
|
|
|
func (engine *Engine) dumpTables(tables []*core.Table, w io.Writer, tp ...core.DbType) error {
|
|
func (engine *Engine) dumpTables(tables []*core.Table, w io.Writer, tp ...core.DbType) error {
|
|
|
var dialect core.Dialect
|
|
var dialect core.Dialect
|
|
|
var distDBName string
|
|
var distDBName string
|
|
@@ -581,19 +450,15 @@ func (engine *Engine) dumpTables(tables []*core.Table, w io.Writer, tp ...core.D
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- rows, err := engine.DB().Query("SELECT * FROM " + engine.Quote(table.Name))
|
|
|
|
|
|
|
+ cols := table.ColumnsSeq()
|
|
|
|
|
+ colNames := dialect.Quote(strings.Join(cols, dialect.Quote(", ")))
|
|
|
|
|
+
|
|
|
|
|
+ rows, err := engine.DB().Query("SELECT " + colNames + " FROM " + engine.Quote(table.Name))
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
defer rows.Close()
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
- cols, err := rows.Columns()
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return err
|
|
|
|
|
- }
|
|
|
|
|
- if len(cols) == 0 {
|
|
|
|
|
- continue
|
|
|
|
|
- }
|
|
|
|
|
for rows.Next() {
|
|
for rows.Next() {
|
|
|
dest := make([]interface{}, len(cols))
|
|
dest := make([]interface{}, len(cols))
|
|
|
err = rows.ScanSlice(&dest)
|
|
err = rows.ScanSlice(&dest)
|
|
@@ -601,7 +466,7 @@ func (engine *Engine) dumpTables(tables []*core.Table, w io.Writer, tp ...core.D
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- _, err = io.WriteString(w, "INSERT INTO "+dialect.Quote(table.Name)+" ("+dialect.Quote(strings.Join(cols, dialect.Quote(", ")))+") VALUES (")
|
|
|
|
|
|
|
+ _, err = io.WriteString(w, "INSERT INTO "+dialect.Quote(table.Name)+" ("+colNames+") VALUES (")
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
@@ -609,6 +474,10 @@ func (engine *Engine) dumpTables(tables []*core.Table, w io.Writer, tp ...core.D
|
|
|
var temp string
|
|
var temp string
|
|
|
for i, d := range dest {
|
|
for i, d := range dest {
|
|
|
col := table.GetColumn(cols[i])
|
|
col := table.GetColumn(cols[i])
|
|
|
|
|
+ if col == nil {
|
|
|
|
|
+ return errors.New("unknow column error")
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if d == nil {
|
|
if d == nil {
|
|
|
temp += ", NULL"
|
|
temp += ", NULL"
|
|
|
} else if col.SQLType.IsText() || col.SQLType.IsTime() {
|
|
} else if col.SQLType.IsText() || col.SQLType.IsTime() {
|
|
@@ -628,6 +497,18 @@ func (engine *Engine) dumpTables(tables []*core.Table, w io.Writer, tp ...core.D
|
|
|
switch reflect.TypeOf(d).Kind() {
|
|
switch reflect.TypeOf(d).Kind() {
|
|
|
case reflect.Slice:
|
|
case reflect.Slice:
|
|
|
temp += fmt.Sprintf(", %s", string(d.([]byte)))
|
|
temp += fmt.Sprintf(", %s", string(d.([]byte)))
|
|
|
|
|
+ case reflect.Int16, reflect.Int8, reflect.Int32, reflect.Int64, reflect.Int:
|
|
|
|
|
+ if col.SQLType.Name == core.Bool {
|
|
|
|
|
+ temp += fmt.Sprintf(", %v", strconv.FormatBool(reflect.ValueOf(d).Int() > 0))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ temp += fmt.Sprintf(", %v", d)
|
|
|
|
|
+ }
|
|
|
|
|
+ case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
|
+ if col.SQLType.Name == core.Bool {
|
|
|
|
|
+ temp += fmt.Sprintf(", %v", strconv.FormatBool(reflect.ValueOf(d).Uint() > 0))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ temp += fmt.Sprintf(", %v", d)
|
|
|
|
|
+ }
|
|
|
default:
|
|
default:
|
|
|
temp += fmt.Sprintf(", %v", d)
|
|
temp += fmt.Sprintf(", %v", d)
|
|
|
}
|
|
}
|
|
@@ -653,6 +534,33 @@ func (engine *Engine) dumpTables(tables []*core.Table, w io.Writer, tp ...core.D
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func (engine *Engine) tableName(beanOrTableName interface{}) (string, error) {
|
|
|
|
|
+ v := rValue(beanOrTableName)
|
|
|
|
|
+ if v.Type().Kind() == reflect.String {
|
|
|
|
|
+ return beanOrTableName.(string), nil
|
|
|
|
|
+ } else if v.Type().Kind() == reflect.Struct {
|
|
|
|
|
+ return engine.tbName(v), nil
|
|
|
|
|
+ }
|
|
|
|
|
+ return "", errors.New("bean should be a struct or struct's point")
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (engine *Engine) tbName(v reflect.Value) string {
|
|
|
|
|
+ if tb, ok := v.Interface().(TableName); ok {
|
|
|
|
|
+ return tb.TableName()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if v.Type().Kind() == reflect.Ptr {
|
|
|
|
|
+ if tb, ok := reflect.Indirect(v).Interface().(TableName); ok {
|
|
|
|
|
+ return tb.TableName()
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if v.CanAddr() {
|
|
|
|
|
+ if tb, ok := v.Addr().Interface().(TableName); ok {
|
|
|
|
|
+ return tb.TableName()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return engine.TableMapper.Obj2Table(reflect.Indirect(v).Type().Name())
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// Cascade use cascade or not
|
|
// Cascade use cascade or not
|
|
|
func (engine *Engine) Cascade(trueOrFalse ...bool) *Session {
|
|
func (engine *Engine) Cascade(trueOrFalse ...bool) *Session {
|
|
|
session := engine.NewSession()
|
|
session := engine.NewSession()
|
|
@@ -674,7 +582,7 @@ func (engine *Engine) Id(id interface{}) *Session {
|
|
|
return session.Id(id)
|
|
return session.Id(id)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// ID mehtod provoide a condition as (id) = ?
|
|
|
|
|
|
|
+// ID method provoide a condition as (id) = ?
|
|
|
func (engine *Engine) ID(id interface{}) *Session {
|
|
func (engine *Engine) ID(id interface{}) *Session {
|
|
|
session := engine.NewSession()
|
|
session := engine.NewSession()
|
|
|
session.IsAutoClose = true
|
|
session.IsAutoClose = true
|
|
@@ -1707,6 +1615,8 @@ func (engine *Engine) formatTime(tz *time.Location, sqlTypeName string, t time.T
|
|
|
return t
|
|
return t
|
|
|
}
|
|
}
|
|
|
if tz != nil {
|
|
if tz != nil {
|
|
|
|
|
+ t = t.In(tz)
|
|
|
|
|
+ } else {
|
|
|
t = engine.TZTime(t)
|
|
t = engine.TZTime(t)
|
|
|
}
|
|
}
|
|
|
switch sqlTypeName {
|
|
switch sqlTypeName {
|