1
0
mirror of https://github.com/uptrace/go-clickhouse.git synced 2024-11-21 17:56:48 +02:00

chore: sync API with Bun

This commit is contained in:
Vladimir Mihailenco 2023-10-01 14:43:38 +03:00
parent 48aa57b286
commit 12a9b1e85d
15 changed files with 78 additions and 75 deletions

View File

@ -12,6 +12,7 @@ import (
type (
Safe = chschema.Safe
Name = chschema.Name
Ident = chschema.Ident
CHModel = chschema.CHModel
AfterScanRowHook = chschema.AfterScanRowHook

View File

@ -27,12 +27,12 @@ func NewFormatter() Formatter {
return Formatter{}
}
func (f Formatter) AppendIdent(b []byte, ident string) []byte {
return AppendIdent(b, ident)
func (f Formatter) AppendName(b []byte, ident string) []byte {
return AppendName(b, ident)
}
func (f Formatter) AppendFQN(b []byte, ident string) []byte {
return AppendFQN(b, ident)
func (f Formatter) AppendIdent(b []byte, ident string) []byte {
return AppendIdent(b, ident)
}
func (f Formatter) WithArg(arg NamedArgAppender) Formatter {

View File

@ -28,20 +28,50 @@ func (s Safe) AppendQuery(fmter Formatter, b []byte) ([]byte, error) {
//------------------------------------------------------------------------------
// FQN represents a fully qualified SQL name, for example, table or column name.
type FQN string
// Name represents a SQL identifier, for example, table or column name.
type Name string
var _ QueryAppender = (*FQN)(nil)
var _ QueryAppender = (*Name)(nil)
func (s FQN) AppendQuery(fmter Formatter, b []byte) ([]byte, error) {
return fmter.AppendFQN(b, string(s)), nil
func (s Name) AppendQuery(fmter Formatter, b []byte) ([]byte, error) {
return fmter.AppendName(b, string(s)), nil
}
func AppendFQN(b []byte, field string) []byte {
return appendFQN(b, internal.Bytes(field))
func AppendName(b []byte, field string) []byte {
return appendName(b, internal.Bytes(field))
}
func appendFQN(b, src []byte) []byte {
func appendName(b, src []byte) []byte {
const quote = '"'
b = append(b, quote)
for _, c := range src {
if c == quote {
b = append(b, quote, quote)
} else {
b = append(b, c)
}
}
b = append(b, quote)
return b
}
//------------------------------------------------------------------------------
// Ident represents a fully qualified SQL name, for example, table or column name.
type Ident string
var _ QueryAppender = (*Name)(nil)
func (s Ident) AppendQuery(fmter Formatter, b []byte) ([]byte, error) {
return fmter.AppendIdent(b, string(s)), nil
}
func AppendIdent(b []byte, field string) []byte {
return appendIdent(b, internal.Bytes(field))
}
func appendIdent(b, src []byte) []byte {
const quote = '"'
var quoted bool
@ -78,34 +108,6 @@ loop:
return b
}
// Ident represents a SQL identifier, for example, table or column name.
type Ident string
var _ QueryAppender = (*Ident)(nil)
func (s Ident) AppendQuery(fmter Formatter, b []byte) ([]byte, error) {
return fmter.AppendIdent(b, string(s)), nil
}
func AppendIdent(b []byte, field string) []byte {
return appendIdent(b, internal.Bytes(field))
}
func appendIdent(b, src []byte) []byte {
const quote = '"'
b = append(b, quote)
for _, c := range src {
if c == quote {
b = append(b, quote, quote)
} else {
b = append(b, c)
}
}
b = append(b, quote)
return b
}
//------------------------------------------------------------------------------
type QueryWithArgs struct {
@ -127,7 +129,7 @@ func SafeQuery(query string, args []any) QueryWithArgs {
}
}
func UnsafeIdent(ident string) QueryWithArgs {
func UnsafeName(ident string) QueryWithArgs {
return QueryWithArgs{Query: ident}
}
@ -141,7 +143,7 @@ func (q QueryWithArgs) IsEmpty() bool {
func (q QueryWithArgs) AppendQuery(fmter Formatter, b []byte) ([]byte, error) {
if q.Args == nil {
return fmter.AppendIdent(b, q.Query), nil
return fmter.AppendName(b, q.Query), nil
}
return fmter.AppendQuery(b, q.Query, q.Args...), nil
}

View File

@ -288,7 +288,7 @@ func (t *Table) AppendNamedArg(
}
func quoteTableName(s string) Safe {
return Safe(appendFQN(nil, internal.Bytes(s)))
return Safe(appendIdent(nil, internal.Bytes(s)))
}
func quoteColumnName(s string) Safe {

View File

@ -138,7 +138,7 @@ func (db *DB) autoCreateDatabase() {
tmp := newDB(conf)
defer tmp.Close()
if _, err := tmp.Exec(query, Ident(db.conf.Database), Ident(db.conf.Cluster)); err != nil {
if _, err := tmp.Exec(query, Name(db.conf.Database), Name(db.conf.Cluster)); err != nil {
internal.Logger.Printf("create database %q failed: %s", db.conf.Database, err)
}
}

View File

@ -41,7 +41,7 @@ func TestAutoCreateDatabase(t *testing.T) {
db := ch.Connect()
defer db.Close()
_, err := db.Exec("DROP DATABASE IF EXISTS ?", ch.Ident(dbName))
_, err := db.Exec("DROP DATABASE IF EXISTS ?", ch.Name(dbName))
require.NoError(t, err)
}
@ -160,7 +160,7 @@ func TestPlaceholder(t *testing.T) {
params := struct {
A int
B int
Alias ch.Ident
Alias ch.Name
}{
A: 1,
B: 2,

View File

@ -298,7 +298,7 @@ func (q *baseQuery) addColumn(column chschema.QueryWithArgs) {
func (q *baseQuery) excludeColumn(columns []string) {
if q.columns == nil {
for _, f := range q.table.Fields {
q.columns = append(q.columns, chschema.UnsafeIdent(f.CHName))
q.columns = append(q.columns, chschema.UnsafeName(f.CHName))
}
}

View File

@ -33,7 +33,7 @@ func (q *InsertQuery) Model(model any) *InsertQuery {
func (q *InsertQuery) Table(tables ...string) *InsertQuery {
for _, table := range tables {
q.addTable(chschema.UnsafeIdent(table))
q.addTable(chschema.UnsafeName(table))
}
return q
}
@ -44,7 +44,7 @@ func (q *InsertQuery) TableExpr(query string, args ...any) *InsertQuery {
}
func (q *InsertQuery) ModelTable(table string) *InsertQuery {
q.modelTableName = chschema.UnsafeIdent(table)
q.modelTableName = chschema.UnsafeName(table)
return q
}
@ -62,7 +62,7 @@ func (q *InsertQuery) Setting(query string, args ...any) *InsertQuery {
func (q *InsertQuery) Column(columns ...string) *InsertQuery {
for _, column := range columns {
q.addColumn(chschema.UnsafeIdent(column))
q.addColumn(chschema.UnsafeName(column))
}
return q
}

View File

@ -117,7 +117,7 @@ func (q *SelectQuery) DistinctOn(query string, args ...any) *SelectQuery {
func (q *SelectQuery) Table(tables ...string) *SelectQuery {
for _, table := range tables {
q.addTable(chschema.UnsafeIdent(table))
q.addTable(chschema.UnsafeName(table))
}
return q
}
@ -128,7 +128,7 @@ func (q *SelectQuery) TableExpr(query string, args ...any) *SelectQuery {
}
func (q *SelectQuery) ModelTable(table string) *SelectQuery {
q.modelTableName = chschema.UnsafeIdent(table)
q.modelTableName = chschema.UnsafeName(table)
return q
}
@ -146,7 +146,7 @@ func (q *SelectQuery) Sample(query string, args ...any) *SelectQuery {
func (q *SelectQuery) Column(columns ...string) *SelectQuery {
for _, column := range columns {
q.addColumn(chschema.UnsafeIdent(column))
q.addColumn(chschema.UnsafeName(column))
}
return q
}
@ -262,7 +262,7 @@ func (q *SelectQuery) WhereGroup(sep string, fn func(*SelectQuery) *SelectQuery)
func (q *SelectQuery) Group(columns ...string) *SelectQuery {
for _, column := range columns {
q.group = append(q.group, chschema.UnsafeIdent(column))
q.group = append(q.group, chschema.UnsafeName(column))
}
return q
}
@ -285,7 +285,7 @@ func (q *SelectQuery) Order(orders ...string) *SelectQuery {
index := strings.IndexByte(order, ' ')
if index == -1 {
q.order = append(q.order, chschema.UnsafeIdent(order))
q.order = append(q.order, chschema.UnsafeName(order))
continue
}
@ -296,11 +296,11 @@ func (q *SelectQuery) Order(orders ...string) *SelectQuery {
case "ASC", "DESC", "ASC NULLS FIRST", "DESC NULLS FIRST",
"ASC NULLS LAST", "DESC NULLS LAST":
q.order = append(q.order, chschema.SafeQuery("? ?", []any{
Ident(field),
Name(field),
Safe(sort),
}))
default:
q.order = append(q.order, chschema.UnsafeIdent(order))
q.order = append(q.order, chschema.UnsafeName(order))
}
}
return q
@ -522,7 +522,7 @@ func (q *SelectQuery) appendWith(fmter chschema.Formatter, b []byte) (_ []byte,
}
if with.cte {
b = chschema.AppendIdent(b, with.name)
b = chschema.AppendName(b, with.name)
b = append(b, " AS "...)
b = append(b, "("...)
}
@ -536,7 +536,7 @@ func (q *SelectQuery) appendWith(fmter chschema.Formatter, b []byte) (_ []byte,
b = append(b, ")"...)
} else {
b = append(b, " AS "...)
b = chschema.AppendIdent(b, with.name)
b = chschema.AppendName(b, with.name)
}
}
b = append(b, ' ')

View File

@ -43,7 +43,7 @@ func (q *CreateTableQuery) Apply(fn func(*CreateTableQuery) *CreateTableQuery) *
func (q *CreateTableQuery) Table(tables ...string) *CreateTableQuery {
for _, table := range tables {
q.addTable(chschema.UnsafeIdent(table))
q.addTable(chschema.UnsafeName(table))
}
return q
}
@ -54,7 +54,7 @@ func (q *CreateTableQuery) TableExpr(query string, args ...any) *CreateTableQuer
}
func (q *CreateTableQuery) ModelTable(table string) *CreateTableQuery {
q.modelTableName = chschema.UnsafeIdent(table)
q.modelTableName = chschema.UnsafeName(table)
return q
}
@ -64,7 +64,7 @@ func (q *CreateTableQuery) ModelTableExpr(query string, args ...any) *CreateTabl
}
func (q *CreateTableQuery) As(table string) *CreateTableQuery {
q.as = chschema.UnsafeIdent(table)
q.as = chschema.UnsafeName(table)
return q
}
@ -81,7 +81,7 @@ func (q *CreateTableQuery) IfNotExists() *CreateTableQuery {
}
func (q *CreateTableQuery) OnCluster(cluster string) *CreateTableQuery {
q.onCluster = chschema.UnsafeIdent(cluster)
q.onCluster = chschema.UnsafeName(cluster)
return q
}

View File

@ -35,7 +35,7 @@ func (q *DropTableQuery) Model(model any) *DropTableQuery {
func (q *DropTableQuery) Table(tables ...string) *DropTableQuery {
for _, table := range tables {
q.addTable(chschema.UnsafeIdent(table))
q.addTable(chschema.UnsafeName(table))
}
return q
}
@ -58,7 +58,7 @@ func (q *DropTableQuery) IfExists() *DropTableQuery {
}
func (q *DropTableQuery) OnCluster(cluster string) *DropTableQuery {
q.onCluster = chschema.UnsafeIdent(cluster)
q.onCluster = chschema.UnsafeName(cluster)
return q
}

View File

@ -34,7 +34,7 @@ func (q *TruncateTableQuery) Model(model any) *TruncateTableQuery {
func (q *TruncateTableQuery) Table(tables ...string) *TruncateTableQuery {
for _, table := range tables {
q.addTable(chschema.UnsafeIdent(table))
q.addTable(chschema.UnsafeName(table))
}
return q
}

View File

@ -130,7 +130,7 @@ func TestQuery(t *testing.T) {
Table("my-table_dist").
As("my-table").
Engine("Distributed(?, currentDatabase(), ?, rand())",
ch.Ident("my-cluster"), ch.Ident("my-table")).
ch.Name("my-cluster"), ch.Name("my-table")).
OnCluster("my-cluster").
IfNotExists()
},

View File

@ -43,7 +43,7 @@ func (q *CreateViewQuery) Apply(fn func(*CreateViewQuery) *CreateViewQuery) *Cre
//------------------------------------------------------------------------------
func (q *CreateViewQuery) View(view string) *CreateViewQuery {
q.view = chschema.UnsafeIdent(view)
q.view = chschema.UnsafeName(view)
return q
}
@ -53,7 +53,7 @@ func (q *CreateViewQuery) ViewExpr(query string, args ...any) *CreateViewQuery {
}
func (q *CreateViewQuery) OnCluster(cluster string) *CreateViewQuery {
q.onCluster = chschema.UnsafeIdent(cluster)
q.onCluster = chschema.UnsafeName(cluster)
return q
}
@ -63,7 +63,7 @@ func (q *CreateViewQuery) OnClusterExpr(query string, args ...any) *CreateViewQu
}
func (q *CreateViewQuery) To(to string) *CreateViewQuery {
q.to = chschema.UnsafeIdent(to)
q.to = chschema.UnsafeName(to)
return q
}
@ -74,7 +74,7 @@ func (q *CreateViewQuery) ToExpr(query string, args ...any) *CreateViewQuery {
func (q *CreateViewQuery) Table(tables ...string) *CreateViewQuery {
for _, table := range tables {
q.addTable(chschema.UnsafeIdent(table))
q.addTable(chschema.UnsafeName(table))
}
return q
}
@ -93,7 +93,7 @@ func (q *CreateViewQuery) ModelTableExpr(query string, args ...any) *CreateViewQ
func (q *CreateViewQuery) Column(columns ...string) *CreateViewQuery {
for _, column := range columns {
q.addColumn(chschema.UnsafeIdent(column))
q.addColumn(chschema.UnsafeName(column))
}
return q
}
@ -150,7 +150,7 @@ func (q *CreateViewQuery) WhereGroup(sep string, fn func(*CreateViewQuery) *Crea
func (q *CreateViewQuery) Group(columns ...string) *CreateViewQuery {
for _, column := range columns {
q.group = append(q.group, chschema.UnsafeIdent(column))
q.group = append(q.group, chschema.UnsafeName(column))
}
return q
}

View File

@ -44,7 +44,7 @@ func (q *DropViewQuery) IfExists() *DropViewQuery {
}
func (q *DropViewQuery) View(view string) *DropViewQuery {
q.view = chschema.UnsafeIdent(view)
q.view = chschema.UnsafeName(view)
return q
}
@ -54,7 +54,7 @@ func (q *DropViewQuery) ViewExpr(query string, args ...any) *DropViewQuery {
}
func (q *DropViewQuery) OnCluster(cluster string) *DropViewQuery {
q.onCluster = chschema.UnsafeIdent(cluster)
q.onCluster = chschema.UnsafeName(cluster)
return q
}