2020-03-22 11:12:55 -04: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-12 19:11:42 -04:00
|
|
|
"database/sql"
|
2020-03-22 11:12:55 -04:00
|
|
|
"fmt"
|
2021-08-12 19:11:42 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"xorm.io/xorm/core"
|
2020-03-22 11:12:55 -04:00
|
|
|
)
|
|
|
|
|
2021-08-12 19:11:42 -04: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
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:17:06 -04:00
|
|
|
// Driver represents a database driver
|
2020-03-22 11:12:55 -04:00
|
|
|
type Driver interface {
|
|
|
|
Parse(string, string) (*URI, error)
|
2021-08-12 19:11:42 -04: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-03-22 11:12:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
drivers = map[string]Driver{}
|
|
|
|
)
|
|
|
|
|
2021-05-14 15:17:06 -04:00
|
|
|
// RegisterDriver register a driver
|
2020-03-22 11:12:55 -04: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-05-14 15:17:06 -04:00
|
|
|
// QueryDriver query a driver with name
|
2020-03-22 11:12:55 -04:00
|
|
|
func QueryDriver(driverName string) Driver {
|
|
|
|
return drivers[driverName]
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:17:06 -04:00
|
|
|
// RegisteredDriverSize returned all drivers's length
|
2020-03-22 11:12:55 -04: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-05-14 15:17:06 -04:00
|
|
|
return nil, fmt.Errorf("unsupported driver name: %v", driverName)
|
2020-03-22 11:12:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uri, err := driver.Parse(driverName, connstr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dialect := QueryDialect(uri.DBType)
|
|
|
|
if dialect == nil {
|
2021-05-14 15:17:06 -04:00
|
|
|
return nil, fmt.Errorf("unsupported dialect type: %v", uri.DBType)
|
2020-03-22 11:12:55 -04:00
|
|
|
}
|
|
|
|
|
2020-03-25 10:32:23 -04:00
|
|
|
dialect.Init(uri)
|
2020-03-22 11:12:55 -04:00
|
|
|
|
|
|
|
return dialect, nil
|
|
|
|
}
|
2021-08-12 19:11:42 -04:00
|
|
|
|
|
|
|
type baseDriver struct{}
|
|
|
|
|
|
|
|
func (b *baseDriver) Scan(ctx *ScanContext, rows *core.Rows, types []*sql.ColumnType, v ...interface{}) error {
|
|
|
|
return rows.Scan(v...)
|
|
|
|
}
|