// NewEngine new a db manager according to the parameter. Currently support four // drivers func NewEngine(driverName string, dataSourceName string) (*Engine, error) { regDrvsNDialects() driver := core.QueryDriver(driverName) if driver == nil { return nil, fmt.Errorf("Unsupported driver name: %v", driverName) } uri, err := driver.Parse(driverName, dataSourceName) if err != nil { return nil, err } dialect := core.QueryDialect(uri.DbType) if dialect == nil { return nil, fmt.Errorf("Unsupported dialect type: %v", uri.DbType) } db, err := core.Open(driverName, dataSourceName) if err != nil { return nil, err } err = dialect.Init(db, uri, driverName, dataSourceName) if err != nil { return nil, err } engine := &Engine{ db: db, dialect: dialect, Tables: make(map[reflect.Type]*core.Table), mutex: &sync.RWMutex{}, TagIdentifier: "xorm", TZLocation: time.Local, } logger := NewSimpleLogger(os.Stdout) logger.SetLevel(core.LOG_INFO) engine.SetLogger(logger) engine.SetMapper(core.NewCacheMapper(new(core.SnakeMapper))) runtime.SetFinalizer(engine, close) return engine, nil }
// DumpAll 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 { 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, from %s to %s*/\n\n", Version, time.Now().In(engine.TZLocation).Format("2006-01-02 15:04:05"), engine.dialect.DBType(), dialect.DBType())) 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) if strings.HasSuffix(v, " +0000 UTC") { temp += fmt.Sprintf(", '%s'", v[0:len(v)-len(" +0000 UTC")]) } else { 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, "-") { if strings.HasSuffix(s, " +0000 UTC") { temp += fmt.Sprintf(", '%s'", s[0:len(s)-len(" +0000 UTC")]) } else { 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 }