-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboltdb2sql.go
More file actions
55 lines (45 loc) · 1.09 KB
/
Copy pathboltdb2sql.go
File metadata and controls
55 lines (45 loc) · 1.09 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
package main
import(
"github.com/boltdb/bolt"
"fmt"
"os"
)
func ExternalDBListOfBuckets(db *bolt.DB) []string{
var list_of_strings []string
db.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(name []byte, _ *bolt.Bucket) error {
list_of_strings=append(list_of_strings,string(name))
return nil
})
})
return list_of_strings
}
func SqlCreateTable(bucket string) string{
return "create table if not exists "+bucket+"(k text primary key unique,v text);"
}
func SqlInsert(bucket,key,value string) string{
return `insert into `+bucket+`(k,v) values("`+key+`","`+value+`");`
}
func ExternaDBCreateSql(db *bolt.DB){
lista:=ExternalDBListOfBuckets(db)
for m:=range(lista){
fmt.Println(SqlCreateTable(lista[m]))
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(lista[m]))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
fmt.Println(SqlInsert(lista[m],string(k),string(v)))
}
return nil
})
}
}
func main(){
arg := os.Args[1]
db, err := bolt.Open(arg, 0600, nil)
if err==nil{
ExternaDBCreateSql(db)
} else {
fmt.Println("Probably Wrong File")
}
}