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