forked from runreveal/chdump
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchdump.go
More file actions
68 lines (59 loc) · 1.7 KB
/
Copy pathchdump.go
File metadata and controls
68 lines (59 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"database/sql"
"fmt"
"log"
"os"
_ "github.com/ClickHouse/clickhouse-go/v2"
)
func main() {
// Replace these with your actual database credentials and connection details
const (
address = "tcp://localhost:9000"
username = "default"
password = ""
database = "default"
)
clickhouseDSN := os.Args[1]
// Establish a connection to the database
// connStr := fmt.Sprintf("%s/%s?username=%s&password=%s", address, database, username, password)
db, err := sql.Open("clickhouse", clickhouseDSN)
if err != nil {
log.Fatalf("Failed to open database: %v", err)
}
// Ping the database to ensure connection is established
if err := db.Ping(); err != nil {
log.Fatalf("Failed to connect: %v", err)
}
defer db.Close()
// Query to retrieve table names in the specified database
tableNamesQuery := "SHOW TABLES"
rows, err := db.Query(tableNamesQuery)
if err != nil {
log.Fatalf("Failed to execute query: %v", err)
}
defer rows.Close()
var tableName string
for rows.Next() {
if err := rows.Scan(&tableName); err != nil {
log.Fatalf("Failed to scan row: %v", err)
}
// Query to retrieve the DDL of each table
showCreateTableQuery := fmt.Sprintf("SHOW CREATE TABLE `%s`", tableName)
tableDDL, err := db.Query(showCreateTableQuery)
if err != nil {
log.Fatalf("Failed to get DDL for table %s: %v", tableName, err)
}
defer tableDDL.Close()
if tableDDL.Next() {
var ddl string
if err := tableDDL.Scan(&ddl); err != nil {
log.Fatalf("Failed to scan DDL of table %s: %v", tableName, err)
}
fmt.Printf("%s\n;\n----------------------------------------\n", ddl)
}
}
if err := rows.Err(); err != nil {
log.Fatalf("Error during row iteration: %v", err)
}
}