-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathmodels.go
More file actions
54 lines (44 loc) · 1.36 KB
/
models.go
File metadata and controls
54 lines (44 loc) · 1.36 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
package models
import (
"database/sql"
"fmt"
_ "github.com/CovenantSQL/go-sqlite3-encrypt" // sqlite3 driver
"github.com/go-gorp/gorp"
"github.com/pkg/errors"
)
var (
chaindb *gorp.DbMap
)
// InitModels setup the models package.
func InitModels(dbFile string) error {
return initChainDBConnection(dbFile)
}
// OpenSQLiteDBAsGorp opens a sqlite database an wrapped it in gorp.DbMap.
func OpenSQLiteDBAsGorp(dbFile, mode string, maxOpen, maxIdle int) (db *gorp.DbMap, err error) {
dsn := fmt.Sprintf("%s?_journal=WAL&mode=%s", dbFile, mode)
underdb, err := sql.Open("sqlite3", dsn)
if err != nil {
return nil, errors.Wrapf(err, "unable to open database %q", dsn)
}
underdb.SetMaxOpenConns(maxOpen)
underdb.SetMaxIdleConns(maxIdle)
if err := underdb.Ping(); err != nil {
return nil, errors.Wrapf(err, "ping to database %q failed", dsn)
}
db = &gorp.DbMap{
Db: underdb,
Dialect: gorp.SqliteDialect{},
}
return db, nil
}
func initChainDBConnection(dbFile string) (err error) {
chaindb, err = OpenSQLiteDBAsGorp(dbFile, "ro", 100, 30)
if err != nil {
return err
}
// register tables
chaindb.AddTableWithName(Block{}, "indexed_blocks").SetKeys(false, "Height")
chaindb.AddTableWithName(Transaction{}, "indexed_transactions").SetKeys(false, "BlockHeight", "TxIndex")
chaindb.AddTableWithName(Account{}, "accounts").SetKeys(false, "Address")
return nil
}