You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.9 KiB

  1. // +build linux darwin windows
  2. // limited GOOS due to modernc.org/libc/unistd
  3. package sqlite
  4. import (
  5. "context"
  6. "database/sql"
  7. "fmt"
  8. "github.com/chrislusf/seaweedfs/weed/filer"
  9. "github.com/chrislusf/seaweedfs/weed/filer/abstract_sql"
  10. "github.com/chrislusf/seaweedfs/weed/filer/mysql"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. _ "modernc.org/sqlite"
  13. )
  14. func init() {
  15. filer.Stores = append(filer.Stores, &SqliteStore{})
  16. }
  17. type SqliteStore struct {
  18. abstract_sql.AbstractSqlStore
  19. }
  20. func (store *SqliteStore) GetName() string {
  21. return "sqlite"
  22. }
  23. func (store *SqliteStore) Initialize(configuration util.Configuration, prefix string) (err error) {
  24. dbFile := configuration.GetString(prefix + "dbFile")
  25. createTable := `CREATE TABLE IF NOT EXISTS "%s" (
  26. dirhash BIGINT,
  27. name VARCHAR(1000),
  28. directory TEXT,
  29. meta BLOB,
  30. PRIMARY KEY (dirhash, name)
  31. ) WITHOUT ROWID;`
  32. upsertQuery := `INSERT INTO "%s"(dirhash,name,directory,meta)VALUES(?,?,?,?)
  33. ON CONFLICT(dirhash,name) DO UPDATE SET
  34. directory=excluded.directory,
  35. meta=excluded.meta;
  36. `
  37. return store.initialize(
  38. dbFile,
  39. createTable,
  40. upsertQuery,
  41. )
  42. }
  43. func (store *SqliteStore) initialize(dbFile, createTable, upsertQuery string) (err error) {
  44. store.SupportBucketTable = true
  45. store.SqlGenerator = &mysql.SqlGenMysql{
  46. CreateTableSqlTemplate: createTable,
  47. DropTableSqlTemplate: "drop table `%s`",
  48. UpsertQueryTemplate: upsertQuery,
  49. }
  50. var dbErr error
  51. store.DB, dbErr = sql.Open("sqlite", dbFile)
  52. if dbErr != nil {
  53. store.DB.Close()
  54. store.DB = nil
  55. return fmt.Errorf("can not connect to %s error:%v", dbFile, err)
  56. }
  57. if err = store.DB.Ping(); err != nil {
  58. return fmt.Errorf("connect to %s error:%v", dbFile, err)
  59. }
  60. if err = store.CreateTable(context.Background(), abstract_sql.DEFAULT_TABLE); err != nil {
  61. return fmt.Errorf("init table %s: %v", abstract_sql.DEFAULT_TABLE, err)
  62. }
  63. return nil
  64. }