Skip to content

Commit 2063c6b

Browse files
committed
support filter in import cmd
1 parent b8ed011 commit 2063c6b

3 files changed

Lines changed: 46 additions & 60 deletions

File tree

internal/app.go

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,10 @@ type DbqAppImpl struct {
2525
}
2626

2727
func NewDbqApp(dbqConfigPath string) DbqApp {
28-
v := viper.New()
29-
30-
if dbqConfigPath != "" {
31-
v.SetConfigFile(dbqConfigPath)
32-
} else {
33-
home, err := os.UserHomeDir()
34-
cobra.CheckErr(err)
35-
v.AddConfigPath(home)
36-
v.SetConfigType("yaml")
37-
v.SetConfigName(".dbq.yaml")
38-
}
39-
40-
v.AutomaticEnv()
41-
if err := v.ReadInConfig(); err != nil {
42-
cobra.CheckErr(err)
43-
}
44-
45-
var dbqConfig DbqConfig
46-
if err := v.Unmarshal(&dbqConfig); err != nil {
47-
cobra.CheckErr(err)
48-
}
49-
28+
dbqConfig := initConfig(dbqConfigPath)
5029
return &DbqAppImpl{
5130
dbqConfigPath: dbqConfigPath,
52-
dbqConfig: &dbqConfig,
31+
dbqConfig: dbqConfig,
5332
}
5433
}
5534

@@ -77,7 +56,7 @@ func (app *DbqAppImpl) ImportDatasets(srcId string, filter string) ([]string, er
7756
return []string{}, err
7857
}
7958

80-
return cnn.ImportDatasets()
59+
return cnn.ImportDatasets(filter)
8160
}
8261

8362
func (app *DbqAppImpl) GetDbqConfig() *DbqConfig {
@@ -107,6 +86,32 @@ func (app *DbqAppImpl) FindDataSourceById(srcId string) *DataSource {
10786
return nil
10887
}
10988

89+
func initConfig(dbqConfigPath string) *DbqConfig {
90+
v := viper.New()
91+
92+
if dbqConfigPath != "" {
93+
v.SetConfigFile(dbqConfigPath)
94+
} else {
95+
home, err := os.UserHomeDir()
96+
cobra.CheckErr(err)
97+
v.AddConfigPath(home)
98+
v.SetConfigType("yaml")
99+
v.SetConfigName(".dbq.yaml")
100+
}
101+
102+
v.AutomaticEnv()
103+
if err := v.ReadInConfig(); err != nil {
104+
cobra.CheckErr(err)
105+
}
106+
107+
var dbqConfig DbqConfig
108+
if err := v.Unmarshal(&dbqConfig); err != nil {
109+
cobra.CheckErr(err)
110+
}
111+
112+
return &dbqConfig
113+
}
114+
110115
func getDbqConnector(ds DataSource) (DbqConnector, error) {
111116
dsType := strings.ToLower(ds.Type)
112117
switch dsType {

internal/clickhouse.go

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"github.com/ClickHouse/clickhouse-go/v2"
77
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
8+
"strings"
89
)
910

1011
type ClickhouseDbqConnector struct {
@@ -33,21 +34,29 @@ func (c *ClickhouseDbqConnector) Ping() error {
3334
return c.cnn.Ping(context.Background())
3435
}
3536

36-
func (c *ClickhouseDbqConnector) ImportDatasets() ([]string, error) {
37+
func (c *ClickhouseDbqConnector) ImportDatasets(filter string) ([]string, error) {
3738
if c.cnn == nil {
3839
return nil, fmt.Errorf("database connection is not initialized")
3940
}
4041

41-
// Query to select database and table names from the system.tables table.
42-
// We exclude the 'system' database as it usually contains internal tables.
43-
// You might want to exclude 'INFORMATION_SCHEMA' or others depending on your needs.
44-
query := `SELECT database, name FROM system.tables WHERE database NOT IN ('system', 'information_schema', 'INFORMATION_SCHEMA') ORDER BY database, name;`
42+
var args []interface{}
43+
query := `
44+
SELECT database, name
45+
FROM system.tables
46+
WHERE database NOT IN ('system', 'information_schema', 'INFORMATION_SCHEMA')`
4547

46-
rows, err := c.cnn.Query(context.Background(), query)
48+
filter = strings.TrimSpace(filter)
49+
if filter != "" {
50+
query += ` AND name LIKE ?`
51+
args = append(args, "%"+filter+"%")
52+
}
53+
query += ` ORDER BY database, name;`
54+
55+
rows, err := c.cnn.Query(context.Background(), query, args...)
4756
if err != nil {
4857
return nil, fmt.Errorf("failed to query system.tables: %w", err)
4958
}
50-
defer rows.Close() // todo: needed?
59+
defer rows.Close()
5160

5261
var datasets []string
5362
for rows.Next() {
@@ -64,31 +73,3 @@ func (c *ClickhouseDbqConnector) ImportDatasets() ([]string, error) {
6473

6574
return datasets, nil
6675
}
67-
68-
//func hehe_click() {
69-
// conn, err := connect()
70-
// if err != nil {
71-
// panic(err)
72-
// }
73-
//
74-
// ctx := context.Background()
75-
// rows, err := conn.Query(ctx, "SELECT name, toString(uuid) as uuid_str FROM system.tables LIMIT 5")
76-
// if err != nil {
77-
// log.Fatal(err)
78-
// }
79-
//
80-
// for rows.Next() {
81-
// var (
82-
// name, uuid string
83-
// )
84-
// if err := rows.Scan(
85-
// &name,
86-
// &uuid,
87-
// ); err != nil {
88-
// log.Fatal(err)
89-
// }
90-
// log.Printf("name: %s, uuid: %s",
91-
// name, uuid)
92-
// }
93-
//
94-
//}

internal/dbq_connector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package internal
22

33
type DbqConnector interface {
44
Ping() error
5-
ImportDatasets() ([]string, error)
5+
ImportDatasets(filter string) ([]string, error)
66
}

0 commit comments

Comments
 (0)