Skip to content

Commit f459005

Browse files
committed
create collection automaticlly if not exists
1 parent e511348 commit f459005

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

engine/store.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,20 @@ func (db *DB) insertMany(query gjson.Result) (res string) {
6969

7070
//fmt.Println("bulk data: ", strData[:len(strData)-1])
7171

72-
_, err := db.db.Exec(`insert into ` + coll + `(record) values` + strData[:len(strData)-1]) // fast
72+
_, err := db.db.Exec(`insert into ` + coll + `(record) values` + strData[:len(strData)-1]) // `+` is fast
7373
if err != nil {
7474
db.lastid[coll] = lid
75+
if strings.Contains(err.Error(), "no such table") {
76+
err = db.CreateCollection(coll)
77+
if err != nil {
78+
return err.Error()
79+
}
80+
return db.insertMany(query)
81+
}
7582
return err.Error()
7683
}
7784

78-
return "in progress"
85+
return "inserted"
7986
}
8087

8188
// insert new record

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
var content embed.FS
1414

1515
func main() {
16-
// TODO close programe greatfully.
16+
// TODO: Close program gracefully.
1717

1818
db := engine.NewDB("test.db")
19-
db.CreateCollection("users")
19+
db.CreateCollection("test")
2020
defer db.Close()
2121

2222
fmt.Printf("interacte with zaradb from %s:%s\n", Host, Port)

0 commit comments

Comments
 (0)