// eventSinkDB is the default name of the events database
eventSinkDB="events.db"
createTableQuery="CREATE TABLE IF NOT EXISTS events "+
"(id INTEGER PRIMARY KEY AUTOINCREMENT, "+
"activity INTEGER, "+
"timestamp DATETIME, "+
"initiator_id TEXT,"+
"account_id TEXT,"+
"meta TEXT,"+
" target_id TEXT);"
creatTableDeletedUsersQuery=`CREATE TABLE IF NOT EXISTS deleted_users (id TEXT NOT NULL, email TEXT NOT NULL, name TEXT, enc_algo TEXT NOT NULL);`
selectDescQuery=`SELECT events.id, activity, timestamp, initiator_id, i.name as "initiator_name", i.email as "initiator_email", target_id, t.name as "target_name", t.email as "target_email", account_id, meta
FROM events
LEFT JOIN (
SELECT id, MAX(name) as name, MAX(email) as email
FROM deleted_users
GROUP BY id
) i ON events.initiator_id = i.id
LEFT JOIN (
SELECT id, MAX(name) as name, MAX(email) as email
FROM deleted_users
GROUP BY id
) t ON events.target_id = t.id
WHERE account_id = ?
ORDER BY timestamp DESC LIMIT ? OFFSET ?;`
selectAscQuery=`SELECT events.id, activity, timestamp, initiator_id, i.name as "initiator_name", i.email as "initiator_email", target_id, t.name as "target_name", t.email as "target_email", account_id, meta
FROM events
LEFT JOIN (
SELECT id, MAX(name) as name, MAX(email) as email
FROM deleted_users
GROUP BY id
) i ON events.initiator_id = i.id
LEFT JOIN (
SELECT id, MAX(name) as name, MAX(email) as email
FROM deleted_users
GROUP BY id
) t ON events.target_id = t.id
WHERE account_id = ?
ORDER BY timestamp ASC LIMIT ? OFFSET ?;`
insertQuery="INSERT INTO events(activity, timestamp, initiator_id, target_id, account_id, meta) "+
"VALUES(?, ?, ?, ?, ?, ?)"
/*
TODO:
The insert should avoid duplicated IDs in the table. So the query should be changes to something like:
`INSERT INTO deleted_users(id, email, name) VALUES(?, ?, ?) ON CONFLICT (id) DO UPDATE SET email = EXCLUDED.email, name = EXCLUDED.name;`
For this to work we have to set the id column as primary key. But this is not possible because the id column is not unique
and some selfhosted deployments might have duplicates already so we need to clean the table first.
*/
insertDeleteUserQuery=`INSERT INTO deleted_users(id, email, name, enc_algo) VALUES(?, ?, ?, ?)`
fallbackName="unknown"
fallbackEmail="unknown@unknown.com"
gcmEncAlgo="GCM"
)
// Store is the implementation of the activity.Store interface backed by SQLite
typeStorestruct{
db*sql.DB
fieldEncrypt*FieldEncrypt
insertStatement*sql.Stmt
selectAscStatement*sql.Stmt
selectDescStatement*sql.Stmt
deleteUserStmt*sql.Stmt
}
// NewSQLiteStore creates a new Store with an event table if not exists.