开始做clickhouse

This commit is contained in:
4color's home 2021-03-05 13:43:55 +08:00
parent 0b563c8774
commit d98469b30e
5 changed files with 134 additions and 14 deletions

82
dialects/clickhouse.go Normal file
View File

@ -0,0 +1,82 @@
// Copyright 2020 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 (
"context"
"net/url"
"xorm.io/xorm/core"
"xorm.io/xorm/schemas"
)
type clickhouse struct {
Base
}
func (db *clickhouse) Init(uri *URI) error {
return db.Base.Init(db, uri)
}
func (db *clickhouse) IsReserved(name string) bool {
return false
}
func (db *clickhouse) SQLType(c *schemas.Column) string {
return ""
}
func (db *clickhouse) SetQuotePolicy(quotePolicy QuotePolicy) {
}
func (*clickhouse) AutoIncrStr() string {
return ""
}
func (*clickhouse) CreateTableSQL(t *schemas.Table, tableName string) ([]string, bool) {
return nil, false
}
func (*clickhouse) IsTableExist(queryer core.Queryer, ctx context.Context, tableName string) (bool, error) {
return false, nil
}
func (*clickhouse) Filters() []Filter {
return []Filter{}
}
func (*clickhouse) GetColumns(core.Queryer, context.Context, string) ([]string, map[string]*schemas.Column, error) {
return nil, nil, nil
}
func (db *clickhouse) GetIndexes(queryer core.Queryer, ctx context.Context, tableName string) (map[string]*schemas.Index, error) {
return nil, nil
}
func (db *clickhouse) IndexCheckSQL(tableName, idxName string) (string, []interface{}) {
return "", nil
}
func (db *clickhouse) GetTables(queryer core.Queryer, ctx context.Context) ([]*schemas.Table, error) {
return nil, nil
}
// ParseClickHouse parsed clickhouse connection string
// tcp://host1:9000?username=user&password=qwerty&database=clicks&read_timeout=10&write_timeout=20&alt_hosts=host2:9000,host3:9000
func ParseClickHouse(connStr string) (*URI, error) {
u, err := url.Parse(connStr)
if err != nil {
return nil, err
}
forms := u.Query()
return &URI{
DBType: schemas.CLICKHOUSE,
Proto: u.Scheme,
Host: u.Hostname(),
Port: u.Port(),
DBName: forms.Get("database"),
User: forms.Get("username"),
Passwd: forms.Get("password"),
}, nil
}

View File

@ -0,0 +1,27 @@
// Copyright 2020 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 (
"github.com/stretchr/testify/assert"
"testing"
)
func TestParseClickHouse(t *testing.T) {
uri, err := ParseClickHouse("tcp://122.224.233.66:9823?username=&password=&database=clicks&read_timeout=10&write_timeout=20&alt_hosts=host2:9000,host3:9000")
assert.NoError(t, err)
//assert.EqualValues(t, &URI{
// DBType: schemas.CLICKHOUSE,
// Proto: "tcp",
// Host: "host1",
// Port: "9000",
// DBName: "clicks",
// User: "user",
// Passwd: "qwerty",
//}, uri)
println(uri)
}

View File

@ -199,15 +199,16 @@ func regDrvsNDialects() bool {
getDriver func() Driver
getDialect func() Dialect
}{
"mssql": {"mssql", func() Driver { return &odbcDriver{} }, func() Dialect { return &mssql{} }},
"odbc": {"mssql", func() Driver { return &odbcDriver{} }, func() Dialect { return &mssql{} }}, // !nashtsai! TODO change this when supporting MS Access
"mysql": {"mysql", func() Driver { return &mysqlDriver{} }, func() Dialect { return &mysql{} }},
"mymysql": {"mysql", func() Driver { return &mymysqlDriver{} }, func() Dialect { return &mysql{} }},
"postgres": {"postgres", func() Driver { return &pqDriver{} }, func() Dialect { return &postgres{} }},
"pgx": {"postgres", func() Driver { return &pqDriverPgx{} }, func() Dialect { return &postgres{} }},
"sqlite3": {"sqlite3", func() Driver { return &sqlite3Driver{} }, func() Dialect { return &sqlite3{} }},
"oci8": {"oracle", func() Driver { return &oci8Driver{} }, func() Dialect { return &oracle{} }},
"goracle": {"oracle", func() Driver { return &goracleDriver{} }, func() Dialect { return &oracle{} }},
"mssql": {"mssql", func() Driver { return &odbcDriver{} }, func() Dialect { return &mssql{} }},
"odbc": {"mssql", func() Driver { return &odbcDriver{} }, func() Dialect { return &mssql{} }}, // !nashtsai! TODO change this when supporting MS Access
"mysql": {"mysql", func() Driver { return &mysqlDriver{} }, func() Dialect { return &mysql{} }},
"mymysql": {"mysql", func() Driver { return &mymysqlDriver{} }, func() Dialect { return &mysql{} }},
"postgres": {"postgres", func() Driver { return &pqDriver{} }, func() Dialect { return &postgres{} }},
"pgx": {"postgres", func() Driver { return &pqDriverPgx{} }, func() Dialect { return &postgres{} }},
"sqlite3": {"sqlite3", func() Driver { return &sqlite3Driver{} }, func() Dialect { return &sqlite3{} }},
"oci8": {"oracle", func() Driver { return &oci8Driver{} }, func() Dialect { return &oracle{} }},
"goracle": {"oracle", func() Driver { return &goracleDriver{} }, func() Dialect { return &oracle{} }},
"clickhouse": {"clickhouse", func() Driver { return &driverProxy{ParseClickHouse} }, func() Dialect { return &clickhouse{} }},
}
for driverName, v := range providedDrvsNDialects {

View File

@ -55,3 +55,12 @@ func OpenDialect(driverName, connstr string) (Dialect, error) {
return dialect, nil
}
type driverProxy struct {
parser func(connStr string) (*URI, error)
}
func (p *driverProxy) Parse(driverName, dataSourceName string) (*URI, error) {
return p.parser(dataSourceName)
}

View File

@ -14,11 +14,12 @@ import (
type DBType string
const (
POSTGRES DBType = "postgres"
SQLITE DBType = "sqlite3"
MYSQL DBType = "mysql"
MSSQL DBType = "mssql"
ORACLE DBType = "oracle"
POSTGRES DBType = "postgres"
SQLITE DBType = "sqlite3"
MYSQL DBType = "mysql"
MSSQL DBType = "mssql"
ORACLE DBType = "oracle"
CLICKHOUSE DBType = "clickhouse"
)
// SQLType represents SQL types