-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatastore.go
52 lines (43 loc) · 1.33 KB
/
datastore.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
// Ecca Authentication CA server
//
// Performs all client certificate signing duties.
//
// Copyright 2013, Guido Witmond <[email protected]>
// Licensed under AGPL v3 or later. See LICENSE
package main
// This file contains the data storage bits
import (
//"log"
//"os"
"github.com/coopernurse/gorp"
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
type Datastore struct {
Storename string
dbmap *gorp.DbMap
}
func DatastoreOpen(storename string) (*Datastore) {
db, err := sql.Open("sqlite3", storename)
check(err)
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
// set key to be unique. We can't allow multiple CN's anyway.
// false -> no autogenerate of key.
dbmap.AddTableWithName(Client{}, "clients").SetKeys(false, "CN")
dbmap.CreateTables() // if not exists
// dbmap.TraceOn("[gorp]", log.New(os.Stdout, "eccaCA:", log.Lmicroseconds))
return &Datastore{
Storename: storename,
dbmap: dbmap,
}
}
func (ds *Datastore) writeClient(client Client) {
check(ds.dbmap.Insert(&client))
}
func (ds *Datastore) getClient(CN string) (*Client) {
res, err := ds.dbmap.Get(Client{}, CN)
//log.Printf("Client is %#v, err is %#v\n", res, err)
check(err)
if res == nil { return nil } //type assert can't handle nil :-(
return res.(*Client)
}