Skip to content
Snippets Groups Projects
Unverified Commit 36255ef4 authored by Markus Lutteropp's avatar Markus Lutteropp :computer:
Browse files

Fixed SQLite Logging backend

parent 5279dd1a
No related branches found
No related tags found
No related merge requests found
......@@ -28,6 +28,7 @@ type sqlCache struct {
}
func (back *SQLiteBackend) Initialize(dbpath string) error {
back.senderKeys, back.severityKeys = make(map[string]int), make(map[string]int)
dbconn, err := sql.Open("sqlite3", dbpath)
if err != nil {
return err
......@@ -77,7 +78,7 @@ func (back *SQLiteBackend) Initialize(dbpath string) error {
CREATE INDEX IF NOT EXISTS "%[3]s_sender_idx" ON "%[3]s" (
"sender" ASC
);
CREATE VIEW "%[3]s_readable" AS
CREATE VIEW IF NOT EXISTS "%[3]s_readable" AS
SELECT
"%[3]s"."serial",
"%[3]s"."timestamp",
......@@ -100,7 +101,6 @@ func (back *SQLiteBackend) Initialize(dbpath string) error {
if err != nil {
return err
}
back.senderKeys, back.severityKeys = make(map[string]int), make(map[string]int)
// Prepare statements for inserts
if statement, err := dbconn.Prepare(
......@@ -115,7 +115,7 @@ func (back *SQLiteBackend) Initialize(dbpath string) error {
}
if statement, err := dbconn.Prepare(
fmt.Sprintf(
"INSERT INTO \"%s\" (name) VALUES (?)",
"INSERT OR IGNORE INTO \"%s\" (name) VALUES (?)",
back.Tables.Severities,
),
); err == nil {
......@@ -125,7 +125,7 @@ func (back *SQLiteBackend) Initialize(dbpath string) error {
}
if statement, err := dbconn.Prepare(
fmt.Sprintf(
"INSERT INTO \"%s\" (name) VALUES (?)",
"INSERT OR IGNORE INTO \"%s\" (name) VALUES (?)",
back.Tables.Senders,
),
); err == nil {
......@@ -156,20 +156,23 @@ func (back *SQLiteBackend) Initialize(dbpath string) error {
return err
}
severityQuery, _ := transaction.Query(fmt.Sprintf("SELECT * FROM \"%s\"", back.Tables.Severities))
senderQuery, _ := transaction.Query(fmt.Sprintf("SELECT * FROM \"%s\"", back.Tables.Senders))
for senderQuery.Next() {
cache := sqlCache{}
senderQuery.Scan(&cache.id, &cache.name)
back.senderKeys[cache.name] = cache.id
if severityQuery, err := transaction.Query(fmt.Sprintf("SELECT * FROM \"%s\"", back.Tables.Severities)); err == nil {
for severityQuery.Next() {
cache := sqlCache{}
severityQuery.Scan(&cache.id, &cache.name)
back.severityKeys[cache.name] = cache.id
}
} else {
return err
}
for severityQuery.Next() {
cache := sqlCache{}
senderQuery.Scan(&cache.id, &cache.name)
back.severityKeys[cache.name] = cache.id
if senderQuery, err := transaction.Query(fmt.Sprintf("SELECT * FROM \"%s\"", back.Tables.Senders)); err == nil {
for senderQuery.Next() {
cache := sqlCache{}
senderQuery.Scan(&cache.id, &cache.name)
back.senderKeys[cache.name] = cache.id
}
}
return nil
}
......
......@@ -7,8 +7,8 @@ import (
func (back *SQLiteBackend) Write(entry *log.Entry) error {
_, err := back.preparedInserts.log.Exec(
entry.Time,
entry.Sender,
entry.Severity.Representation,
back.senderKeys[entry.Sender],
back.severityKeys[entry.Severity.Representation],
entry.Message,
)
return err
......@@ -33,10 +33,10 @@ func (back *SQLiteBackend) NotifyAboutSeverity(severity *log.Severity) {
if _, ok := back.severityKeys[severity.Representation]; ok {
return
}
if _, err := back.preparedInserts.severities.Exec(severity); err != nil {
if _, err := back.preparedInserts.severities.Exec(severity.Representation); err != nil {
panic(err)
}
res := back.preparedQueries.severities.QueryRow(severity)
res := back.preparedQueries.severities.QueryRow(severity.Representation)
cache := sqlCache{}
if err := res.Scan(&cache.id, &cache.name); err != nil {
panic(err)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment