database.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2018 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package database
  17. import (
  18. "database/sql"
  19. _ "github.com/mattn/go-sqlite3"
  20. log "maunium.net/go/maulogger"
  21. )
  22. type Database struct {
  23. *sql.DB
  24. log *log.Sublogger
  25. User *UserQuery
  26. }
  27. func New(file string) (*Database, error) {
  28. conn, err := sql.Open("sqlite3", file)
  29. if err != nil {
  30. return nil, err
  31. }
  32. db := &Database{
  33. DB: conn,
  34. log: log.CreateSublogger("Database", log.LevelDebug),
  35. }
  36. db.User = &UserQuery{
  37. db: db,
  38. log: log.CreateSublogger("Database/User", log.LevelDebug),
  39. }
  40. return db, nil
  41. }
  42. type Scannable interface {
  43. Scan(...interface{}) error
  44. }