Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add clickhouse #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ before_install:

before_script:
# Start everything except oracle servvice.
- docker-compose up -d mssql mysql postgres
- docker-compose up -d mssql mysql postgres clickhouse

script:
- go test -race -coverprofile=coverage.txt -covermode=atomic -tags=travis
Expand Down
37 changes: 19 additions & 18 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@ type dialect interface {
// driverDialect is a registry, mapping database/sql driver names to database dialects.
// This is somewhat fragile.
var driverDialect = map[string]dialect{
"*sqlite3.SQLiteDriver": sqliteDialect{}, // github.com/mattn/go-sqlite3
"*sqlite.impl": sqliteDialect{}, // github.com/gwenn/gosqlite
"sqlite3.Driver": sqliteDialect{}, // github.com/mxk/go-sqlite
"*pq.Driver": postgresDialect{}, // github.com/lib/pq
"*stdlib.Driver": postgresDialect{}, // github.com/jackc/pgx
"*pgsqldriver.postgresDriver": postgresDialect{}, // github.com/jbarham/gopgsqldriver
"*gosnowflake.SnowflakeDriver": postgresDialect{}, // github.com/snowflakedb/gosnowflake
"*mysql.MySQLDriver": mysqlDialect{}, // github.com/go-sql-driver/mysql
"*godrv.Driver": mysqlDialect{}, // github.com/ziutek/mymysql
"vitessdriver.drv": mysqlDialect{}, // github.com/vitessio/vitess
"*mssql.Driver": mssqlDialect{}, // github.com/denisenkom/go-mssqldb
"*mssql.MssqlDriver": mssqlDialect{}, // github.com/denisenkom/go-mssqldb
"*freetds.MssqlDriver": mssqlDialect{}, // github.com/minus5/gofreetds
"*goracle.drv": oracleDialect{}, // gopkg.in/goracle.v2
"*godror.drv": oracleDialect{}, // github.com/godror/godror
"*ora.Drv": oracleDialect{}, // gopkg.in/rana/ora.v4
"*oci8.OCI8DriverStruct": oracleDialect{}, // github.com/mattn/go-oci8
"*oci8.OCI8Driver": oracleDialect{}, // github.com/mattn/go-oci8
"*sqlite3.SQLiteDriver": sqliteDialect{}, // github.com/mattn/go-sqlite3
"*sqlite.impl": sqliteDialect{}, // github.com/gwenn/gosqlite
"sqlite3.Driver": sqliteDialect{}, // github.com/mxk/go-sqlite
"*pq.Driver": postgresDialect{}, // github.com/lib/pq
"*stdlib.Driver": postgresDialect{}, // github.com/jackc/pgx
"*pgsqldriver.postgresDriver": postgresDialect{}, // github.com/jbarham/gopgsqldriver
"*gosnowflake.SnowflakeDriver": postgresDialect{}, // github.com/snowflakedb/gosnowflake
"*mysql.MySQLDriver": mysqlDialect{}, // github.com/go-sql-driver/mysql
"*godrv.Driver": mysqlDialect{}, // github.com/ziutek/mymysql
"vitessdriver.drv": mysqlDialect{}, // github.com/vitessio/vitess
"*mssql.Driver": mssqlDialect{}, // github.com/denisenkom/go-mssqldb
"*mssql.MssqlDriver": mssqlDialect{}, // github.com/denisenkom/go-mssqldb
"*freetds.MssqlDriver": mssqlDialect{}, // github.com/minus5/gofreetds
"*goracle.drv": oracleDialect{}, // gopkg.in/goracle.v2
"*godror.drv": oracleDialect{}, // github.com/godror/godror
"*ora.Drv": oracleDialect{}, // gopkg.in/rana/ora.v4
"*oci8.OCI8DriverStruct": oracleDialect{}, // github.com/mattn/go-oci8
"*oci8.OCI8Driver": oracleDialect{}, // github.com/mattn/go-oci8
"*clickhouse.stdDriver": clickhouseDialect{}, // github.com/clickhouse-go/v2
}

// TODO Should we expose a method of registering a driver string/dialect in our registry?
Expand Down
83 changes: 83 additions & 0 deletions dialect_clickhouse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package schema

import (
"database/sql"
)

// TODO(js) Should we be filtering out system tables, like we currently do?

const clickhouseAllColumns = `SELECT * FROM %s LIMIT 0`

const clickhouseTableNamesWithSchema = `
SELECT database AS table_schema,
name AS table_name
FROM system.tables
WHERE NOT is_temporary
AND engine NOT LIKE '%View'
AND engine NOT LIKE 'System%'
AND has_own_data != 0
AND database NOT IN ('information_schema', 'system', 'INFORMATION_SCHEMA')
ORDER BY database, name;
`

const clickhouseViewNamesWithSchema = `
SELECT database AS table_schema,
name AS table_name
FROM system.tables
WHERE engine LIKE '%View'
AND database NOT IN ('information_schema', 'system', 'INFORMATION_SCHEMA')
ORDER BY table_schema,
table_name
`

const clickhousePrimaryKey = `
SELECT
name
FROM
system.columns
WHERE
database = currentDatabase() AND
table = $1 AND
is_in_primary_key
ORDER BY
position DESC
`

const clickhousePrimaryKeyWithSchema = `
SELECT
name
FROM
system.columns
WHERE
database = $1 AND
table = $2 AND
is_in_primary_key
ORDER BY
position DESC
`

type clickhouseDialect struct{}

func (clickhouseDialect) escapeIdent(ident string) string {
// "tablename"
return escapeWithDoubleQuotes(ident)
}

func (d clickhouseDialect) ColumnTypes(db *sql.DB, schema, name string) ([]*sql.ColumnType, error) {
return fetchColumnTypes(db, clickhouseAllColumns, schema, name, d.escapeIdent)
}

func (clickhouseDialect) PrimaryKey(db *sql.DB, schema, name string) ([]string, error) {
if schema == "" {
return fetchNames(db, clickhousePrimaryKey, "", name)
}
return fetchNames(db, clickhousePrimaryKeyWithSchema, schema, name)
}

func (clickhouseDialect) TableNames(db *sql.DB) ([][2]string, error) {
return fetchObjectNames(db, clickhouseTableNamesWithSchema)
}

func (clickhouseDialect) ViewNames(db *sql.DB) ([][2]string, error) {
return fetchObjectNames(db, clickhouseViewNamesWithSchema)
}
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ services:
- 127.0.0.1:45432:5432
environment:
POSTGRES_HOST_AUTH_METHOD: trust

clickhouse:
image: clickhouse/clickhouse-server
user: "101:101"
hostname: clickhouse
ports:
- "127.0.0.1:49000:9000"
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/jimsmart/schema
go 1.16

require (
github.com/ClickHouse/clickhouse-go/v2 v2.11.0
github.com/denisenkom/go-mssqldb v0.12.3
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-sql-driver/mysql v1.7.0
Expand Down
Loading