source.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2017 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "fmt"
  7. "os"
  8. "github.com/go-xorm/core"
  9. "github.com/go-xorm/xorm"
  10. )
  11. var CmdSource = &Command{
  12. UsageLine: "source driverName datasourceName",
  13. Short: "source execute std in to datasourceName",
  14. Long: `
  15. source from standard std in for sqlite3, mysql, postgres.
  16. driverName Database driver name, now supported four: mysql mymysql sqlite3 postgres
  17. datasourceName Database connection uri, for detail infomation please visit driver's project page
  18. `,
  19. }
  20. func init() {
  21. CmdSource.Run = runSource
  22. CmdSource.Flags = map[string]bool{}
  23. }
  24. func runSource(cmd *Command, args []string) {
  25. if len(args) != 2 {
  26. fmt.Println("params error, please see xorm help source")
  27. return
  28. }
  29. var err error
  30. engine, err = xorm.NewEngine(args[0], args[1])
  31. if err != nil {
  32. fmt.Println(err)
  33. return
  34. }
  35. engine.ShowSQL(false)
  36. engine.Logger().SetLevel(core.LOG_UNKNOWN)
  37. err = engine.Ping()
  38. if err != nil {
  39. fmt.Println(err)
  40. return
  41. }
  42. _, err = engine.Import(os.Stdin)
  43. if err.Error() == "not an error" {
  44. err = nil
  45. }
  46. if err != nil {
  47. fmt.Println(err)
  48. return
  49. }
  50. }