-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
101 lines (87 loc) · 2.11 KB
/
main.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"context"
"database/sql"
"database/sql/driver"
"flag"
"fmt"
"github.com/marcboeker/go-duckdb"
"log"
"net"
"os"
)
var options struct {
listenAddress string
duckDBFile string
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [options]\n", os.Args[0])
flag.PrintDefaults()
}
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] <duckdb-file>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Example: %s somefile.db\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s \"somefile.db?access_mode=read_only&threads=4\"\n", os.Args[0])
flag.PrintDefaults()
}
flag.StringVar(&options.listenAddress, "listen", "127.0.0.1:15432", "Listen address")
flag.Parse()
if flag.NArg() > 0 {
options.duckDBFile = flag.Arg(0)
} else {
fmt.Fprintf(os.Stderr, "Error: DuckDB file is required.\n\n")
flag.Usage()
os.Exit(1)
}
ln, err := net.Listen("tcp", options.listenAddress)
if err != nil {
log.Fatal(err)
}
log.Println("Listening on", ln.Addr())
//
connector, err := duckdb.NewConnector(options.duckDBFile, func(execer driver.ExecerContext) error {
bootQueries := []string{
"INSTALL 'json'",
"LOAD 'json'",
}
for _, qry := range bootQueries {
_, err = execer.ExecContext(context.Background(), qry, nil)
if err != nil {
return err
}
}
return nil
})
//conn, err := connector.Connect(context.Background())
//if err != nil {
// panic(err)
//}
//db, err := sql.Open("duckdb", "duckdb.db?access_mode=READ_WRITE")
db := sql.OpenDB(connector)
if err != nil {
log.Fatal(err)
}
defer db.Close()
db.Ping()
setting := db.QueryRow("SELECT current_setting('access_mode')")
var am string
setting.Scan(&am)
log.Printf("DB opened with access mode %s", am)
ctx, cancel := context.WithCancel(context.Background())
for {
conn, err := ln.Accept()
if err != nil {
log.Fatal(err)
}
log.Println("Accepted connection from", conn.RemoteAddr())
b := NewDuckDbBackend(conn, db, ctx, cancel)
go func() {
err := b.Run()
if err != nil {
log.Println(err)
}
log.Println("Closed connection from", conn.RemoteAddr())
}()
}
}