Repository navigation
Expand file tree
/
Copy pathsimple.go
More file actions
87 lines (72 loc) · 2.04 KB
/
Copy pathsimple.go
File metadata and controls
87 lines (72 loc) · 2.04 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Copyright 2018 The CovenantSQL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"database/sql"
"flag"
"fmt"
"github.com/CovenantSQL/CovenantSQL/client"
"github.com/CovenantSQL/CovenantSQL/utils/log"
)
func main() {
log.SetLevel(log.InfoLevel)
var config, password, dsn string
flag.StringVar(&config, "config", "./conf/config.yaml", "config file path")
flag.StringVar(&dsn, "dsn", "", "database url")
flag.StringVar(&password, "password", "", "master key password for covenantsql")
flag.Parse()
err := client.Init(config, []byte(password))
if err != nil {
log.Fatal(err)
}
if dsn == "" {
meta := client.ResourceMeta{}
meta.Node = 2
dsn, err = client.Create(meta)
if err != nil {
log.Fatal(err)
}
defer client.Drop(dsn)
}
db, err := sql.Open("covenantsql", dsn)
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec("DROP TABLE IF EXISTS testSimple;")
if err != nil {
log.Fatal(err)
}
_, err = db.Exec("CREATE TABLE testSimple ( indexedColumn, nonIndexedColumn );")
if err != nil {
log.Fatal(err)
}
_, err = db.Exec("CREATE INDEX testIndexedColumn ON testSimple ( indexedColumn );")
if err != nil {
log.Fatal(err)
}
_, err = db.Exec("INSERT INTO testSimple VALUES(?, ?);", 4, 400)
if err != nil {
log.Fatal(err)
}
row := db.QueryRow("SELECT nonIndexedColumn FROM testSimple LIMIT 1;")
var result int
err = row.Scan(&result)
if err != nil {
log.Fatal(err)
}
fmt.Printf("SELECT nonIndexedColumn FROM testSimple LIMIT 1; result %d\n", result)
}