You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

driver.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2019 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 dialects
  5. import (
  6. "fmt"
  7. )
  8. type Driver interface {
  9. Parse(string, string) (*URI, error)
  10. }
  11. var (
  12. drivers = map[string]Driver{}
  13. )
  14. func RegisterDriver(driverName string, driver Driver) {
  15. if driver == nil {
  16. panic("core: Register driver is nil")
  17. }
  18. if _, dup := drivers[driverName]; dup {
  19. panic("core: Register called twice for driver " + driverName)
  20. }
  21. drivers[driverName] = driver
  22. }
  23. func QueryDriver(driverName string) Driver {
  24. return drivers[driverName]
  25. }
  26. func RegisteredDriverSize() int {
  27. return len(drivers)
  28. }
  29. // OpenDialect opens a dialect via driver name and connection string
  30. func OpenDialect(driverName, connstr string) (Dialect, error) {
  31. driver := QueryDriver(driverName)
  32. if driver == nil {
  33. return nil, fmt.Errorf("Unsupported driver name: %v", driverName)
  34. }
  35. uri, err := driver.Parse(driverName, connstr)
  36. if err != nil {
  37. return nil, err
  38. }
  39. dialect := QueryDialect(uri.DBType)
  40. if dialect == nil {
  41. return nil, fmt.Errorf("Unsupported dialect type: %v", uri.DBType)
  42. }
  43. dialect.Init(uri)
  44. return dialect, nil
  45. }