pki/vendor/xorm.io/xorm/dialects/driver.go

86 lines
2.0 KiB
Go
Raw Normal View History

2020-11-25 20:36:07 +01:00
// Copyright 2019 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dialects
import (
2021-08-30 20:04:15 +02:00
"database/sql"
2020-11-25 20:36:07 +01:00
"fmt"
2021-08-30 20:04:15 +02:00
"time"
"xorm.io/xorm/core"
2020-11-25 20:36:07 +01:00
)
2021-08-30 20:04:15 +02:00
// ScanContext represents a context when Scan
type ScanContext struct {
DBLocation *time.Location
UserLocation *time.Location
}
// DriverFeatures represents driver feature
type DriverFeatures struct {
SupportReturnInsertedID bool
}
// Driver represents a database driver
2020-11-25 20:36:07 +01:00
type Driver interface {
Parse(string, string) (*URI, error)
2021-08-30 20:04:15 +02:00
Features() *DriverFeatures
GenScanResult(string) (interface{}, error) // according given column type generating a suitable scan interface
Scan(*ScanContext, *core.Rows, []*sql.ColumnType, ...interface{}) error
2020-11-25 20:36:07 +01:00
}
var (
drivers = map[string]Driver{}
)
2021-08-30 20:04:15 +02:00
// RegisterDriver register a driver
2020-11-25 20:36:07 +01:00
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
}
2021-08-30 20:04:15 +02:00
// QueryDriver query a driver with name
2020-11-25 20:36:07 +01:00
func QueryDriver(driverName string) Driver {
return drivers[driverName]
}
2021-08-30 20:04:15 +02:00
// RegisteredDriverSize returned all drivers's length
2020-11-25 20:36:07 +01:00
func RegisteredDriverSize() int {
return len(drivers)
}
// OpenDialect opens a dialect via driver name and connection string
func OpenDialect(driverName, connstr string) (Dialect, error) {
driver := QueryDriver(driverName)
if driver == nil {
2021-08-30 20:04:15 +02:00
return nil, fmt.Errorf("unsupported driver name: %v", driverName)
2020-11-25 20:36:07 +01:00
}
uri, err := driver.Parse(driverName, connstr)
if err != nil {
return nil, err
}
dialect := QueryDialect(uri.DBType)
if dialect == nil {
2021-08-30 20:04:15 +02:00
return nil, fmt.Errorf("unsupported dialect type: %v", uri.DBType)
2020-11-25 20:36:07 +01:00
}
dialect.Init(uri)
return dialect, nil
}
2021-08-30 20:04:15 +02:00
type baseDriver struct{}
func (b *baseDriver) Scan(ctx *ScanContext, rows *core.Rows, types []*sql.ColumnType, v ...interface{}) error {
return rows.Scan(v...)
}