package database import ( "database/sql" "fmt" "log" _ "github.com/mattn/go-sqlite3" ) var DB *sql.DB func InitDB(filepath string) (err error) { log.Printf("Init DB") DB, err = sql.Open("sqlite3", filepath) if err != nil { panic(err) } if DB == nil { panic("DB couldn't be initialized") } handleError(initDeviceTable()) handleError(initKeyTable()) handleError(initEventTable()) handleError(initRoomTable()) handleError(initTransactionTable()) handleError(initUserTable()) return } func initDeviceTable() (err error) { log.Printf("Init Device Table") statement, err := DB.Prepare(`CREATE TABLE IF NOT EXISTS device ( id TEXT PRIMARY KEY, name TEXT, userId TEXT )`) if err != nil { return } statement.Exec() /* newDevice := &device.Device{Id: "test", Name: "TEST", Keys: nil} err = CreateDevice(db, newDevice, "test") if err != nil { log.Printf("Error Create: %s", err) return } newDevice.Name = "TEST2" err = UpdateDevice(db, newDevice) if err != nil { log.Printf("Error Update: %s", err) return } devices, err := ReadDevicesForUser(db, "test") if err != nil { log.Printf("Error Read User: %s", err) return } log.Println(devices) err = DeleteDevice(db, newDevice.Id) if err != nil { log.Printf("Error Delete: %s", err) return } de, err := ReadDevice(db, "test") if err != nil { log.Printf("Error Read: %s", err) return } if de != nil { log.Printf("Device ID: %s Name: %s", de.Id, de.Name) } else { log.Printf("No Device found") } */ return } func initKeyTable() (err error) { log.Printf("Init Key Table") statement, err := DB.Prepare(`CREATE TABLE IF NOT EXISTS key ( id TEXT PRIMARY KEY, type TEXT, key TEXT, deviceId TEXT )`) if err != nil { return } statement.Exec() /* newKey := &device.Key{Id: "test", Type: "test", Key: "test"} err = CreateKey(db, newKey, "test") if err != nil { log.Printf("Error Create: %s", err) return } keys, err := ReadKeysForDevice(db, "test") if err != nil { log.Printf("Error Read Multiple: %s", err) return } log.Println(keys) newKey.Key = "TEST123" err = UpdateKey(db, newKey) if err != nil { log.Printf("Error Update: %s", err) return } err = DeleteKey(db, newKey.Id) if err != nil { log.Printf("Error Delete: %s", err) return } readKey, err := ReadKey(db, "test") if err != nil { log.Printf("Error Read: %s", err) return } if readKey != nil { log.Printf("Key ID: %s Type: %s, Key: %s", readKey.Id, readKey.Type, readKey.Key) } else { log.Printf("No Key found") } */ return } func initEventTable() (err error) { log.Printf("Init Event Table") statement, err := DB.Prepare(`CREATE TABLE IF NOT EXISTS event ( id TEXT PRIMARY KEY, roomId TEXT, txnId TEXT, eventType TEXT, content TEXT, parentId TEXT, depth INTEGER )`) if err != nil { return } statement.Exec() /* newEvent := &event.Event{ Id: "test", RoomId: "test", EventType: "test", Content: "{TEST}", ParentId: "test1", Depth: 0, } err = CreateEvent(db, newEvent, "test") if err != nil { log.Printf("Error Create: %s", err) return } eventsRoom, err := ReadEventsFromRoom(db, "test") if err != nil { log.Printf("Error Read User: %s", err) return } log.Println(eventsRoom) eventsTxn, err := ReadEventsFromTransaction(db, "test") if err != nil { log.Printf("Error Read User: %s", err) return } log.Println(eventsTxn) newEvent.Content = "{TEST123}" err = UpdateEvent(db, newEvent) if err != nil { log.Printf("Error Update: %s", err) return } err = DeleteEvent(db, newEvent.Id) if err != nil { log.Printf("Error Delete: %s", err) return } readEvent, err := ReadEvent(db, "test") if err != nil { log.Printf("Error Read: %s", err) return } if readEvent != nil { log.Printf("Event ID: %s RoomId: %s EventType: %s Content: %s ParentId: %s Depth: %s", readEvent.Id, readEvent.RoomId, readEvent.EventType, readEvent.Content, readEvent.ParentId, readEvent.Depth) } else { log.Printf("No Event found") } */ return } func initRoomTable() (err error) { log.Printf("Init Room Table") statement, err := DB.Prepare(`CREATE TABLE IF NOT EXISTS room ( id TEXT PRIMARY KEY, version TEXT )`) if err != nil { return } statement.Exec() statement, err = DB.Prepare(`CREATE TABLE IF NOT EXISTS roomMember ( userId TEXT, roomId TEXT, PRIMARY KEY (userId, roomId) )`) if err != nil { return } statement.Exec() /* newRoom := &room.Room{Id: "test", Version: "test"} err = CreateRoom(db, newRoom, "test") if err != nil { log.Printf("Error Create: %s", err) return } err = CreateRoomMember(db, newRoom.Id, "test2") if err != nil { log.Printf("Error Create: %s", err) return } roomMembers, err := ReadRoomMembers(db, newRoom.Id) if err != nil { log.Printf("Error Read Members: %s", err) return } log.Println(roomMembers) newRoom.Version = "test2" err = UpdateRoom(db, newRoom) if err != nil { log.Printf("Error Update: %s", err) return } err = DeleteRoomMember(db, newRoom.Id, "test") if err != nil { log.Printf("Error Delete: %s", err) return } err = DeleteAllRoomMemberForUser(db, "test2") if err != nil { log.Printf("Error Delete: %s", err) return } err = DeleteRoom(db, newRoom.Id) if err != nil { log.Printf("Error Delete: %s", err) return } readRoom, err := ReadRoom(db, newRoom.Id) if err != nil { log.Printf("Error Read: %s", err) return } if readRoom != nil { log.Printf("Room ID: %s Version: %s Members: %s", readRoom.Id, readRoom.Version, readRoom.Members) } else { log.Printf("No Room found") } */ return } func initTransactionTable() (err error) { log.Printf("Init Transaction Table") statement, err := DB.Prepare(`CREATE TABLE IF NOT EXISTS txn ( id TEXT PRIMARY KEY, origin TEXT, timestamp INTEGER )`) if err != nil { return } statement.Exec() /* newTransaction := &transaction.Transaction{Id: "test", Origin: "test.de", Timestamp: 1234} err = CreateTransaction(db, newTransaction) if err != nil { log.Printf("Error Create: %s", err) return } newTransaction.Origin = "test2.de" err = UpdateTransaction(db, newTransaction) if err != nil { log.Printf("Error Update: %s", err) return } err = DeleteTransaction(db, newTransaction.Id) if err != nil { log.Printf("Error Delete: %s", err) return } readTransaction, err := ReadTransaction(db, newTransaction.Id) if err != nil { log.Printf("Error Read: %s", err) return } if readTransaction != nil { log.Printf("Transaction ID: %s Origin: %s Timestamp: %s PDUS: %s", readTransaction.Id, readTransaction.Origin, readTransaction.Timestamp, readTransaction.PDUS) } else { log.Printf("No Transaction found") } */ return } func initUserTable() (err error) { log.Printf("Init User Table") statement, err := DB.Prepare(`CREATE TABLE IF NOT EXISTS user ( id TEXT PRIMARY KEY, name TEXT, password TEXT )`) if err != nil { return } statement.Exec() /* newUser := &user.User{Id: "test", Name: "test", Password: "test"} err = CreateUser(db, newUser) if err != nil { log.Printf("Error Create: %s", err) return } newUser.Name = "TEST2" err = UpdateUser(db, newUser) if err != nil { log.Printf("Error Update: %s", err) return } err = DeleteUser(db, newUser.Id) if err != nil { log.Printf("Error Delete: %s", err) return } readUser, err := ReadUser(db, newUser.Id) if err != nil { log.Printf("Error Read: %s", err) return } if readUser != nil { log.Printf("User ID: %s Name: %s Password: %s Devices: %s", readUser.Id, readUser.Name, readUser.Password, readUser.Devices) } else { log.Printf("No User found") } */ return } func handleError(err error) { if err != nil { panic(fmt.Sprintf("Could not execute Database Query: %s", err)) } }