openflags.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2021 Ross Light
  2. // SPDX-License-Identifier: ISC
  3. package sqlite
  4. import (
  5. "fmt"
  6. "strings"
  7. lib "modernc.org/sqlite/lib"
  8. )
  9. // OpenFlags are [flags] used when opening a [Conn] via [OpenConn].
  10. //
  11. // [flags]: https://www.sqlite.org/c3ref/c_open_autoproxy.html
  12. type OpenFlags uint
  13. // One of the following flags must be passed to [OpenConn].
  14. const (
  15. // OpenReadOnly opens the database in read-only mode.
  16. // If the database does not already exist, an error is returned.
  17. OpenReadOnly OpenFlags = lib.SQLITE_OPEN_READONLY
  18. // OpenReadWrite opens the database for reading and writing if possible,
  19. // or reading only if the file is write protected by the operating system.
  20. // If the database does not already exist,
  21. // an error is returned unless OpenCreate is also passed.
  22. OpenReadWrite OpenFlags = lib.SQLITE_OPEN_READWRITE
  23. )
  24. // Optional flags to pass to [OpenConn].
  25. const (
  26. // OpenCreate will create the file if it does not already exist.
  27. // It is only valid with [OpenReadWrite].
  28. OpenCreate OpenFlags = lib.SQLITE_OPEN_CREATE
  29. // OpenURI allows the path to be interpreted as a URI.
  30. OpenURI OpenFlags = lib.SQLITE_OPEN_URI
  31. // OpenMemory will be opened as an in-memory database.
  32. // The path is ignored unless [OpenSharedCache] is used.
  33. OpenMemory OpenFlags = lib.SQLITE_OPEN_MEMORY
  34. // OpenSharedCache opens the database with [shared-cache].
  35. // This is mostly only useful for sharing in-memory databases:
  36. // it's [not recommended] for other purposes.
  37. //
  38. // [shared-cache]: https://www.sqlite.org/sharedcache.html
  39. // [not recommended]: https://www.sqlite.org/sharedcache.html#dontuse
  40. OpenSharedCache OpenFlags = lib.SQLITE_OPEN_SHAREDCACHE
  41. // OpenPrivateCache forces the database to not use shared-cache.
  42. OpenPrivateCache OpenFlags = lib.SQLITE_OPEN_PRIVATECACHE
  43. // OpenWAL enables the [write-ahead log] for the database.
  44. //
  45. // [write-ahead log]: https://www.sqlite.org/wal.html
  46. OpenWAL OpenFlags = lib.SQLITE_OPEN_WAL
  47. // OpenNoMutex has no effect.
  48. //
  49. // Deprecated: This flag is now implied.
  50. OpenNoMutex OpenFlags = lib.SQLITE_OPEN_NOMUTEX
  51. // OpenFullMutex has no effect.
  52. //
  53. // Deprecated: This flag has no equivalent and is ignored.
  54. OpenFullMutex OpenFlags = lib.SQLITE_OPEN_FULLMUTEX
  55. )
  56. // String returns a pipe-separated list of the C constant names set in flags.
  57. func (flags OpenFlags) String() string {
  58. var parts []string
  59. if flags&OpenReadOnly != 0 {
  60. parts = append(parts, "SQLITE_OPEN_READONLY")
  61. flags &^= OpenReadOnly
  62. }
  63. if flags&OpenReadWrite != 0 {
  64. parts = append(parts, "SQLITE_OPEN_READWRITE")
  65. flags &^= OpenReadWrite
  66. }
  67. if flags&OpenCreate != 0 {
  68. parts = append(parts, "SQLITE_OPEN_CREATE")
  69. flags &^= OpenCreate
  70. }
  71. if flags&OpenURI != 0 {
  72. parts = append(parts, "SQLITE_OPEN_URI")
  73. flags &^= OpenURI
  74. }
  75. if flags&OpenMemory != 0 {
  76. parts = append(parts, "SQLITE_OPEN_MEMORY")
  77. flags &^= OpenMemory
  78. }
  79. if flags&OpenNoMutex != 0 {
  80. parts = append(parts, "SQLITE_OPEN_NOMUTEX")
  81. flags &^= OpenNoMutex
  82. }
  83. if flags&OpenFullMutex != 0 {
  84. parts = append(parts, "SQLITE_OPEN_FULLMUTEX")
  85. flags &^= OpenFullMutex
  86. }
  87. if flags&OpenSharedCache != 0 {
  88. parts = append(parts, "SQLITE_OPEN_SHAREDCACHE")
  89. flags &^= OpenSharedCache
  90. }
  91. if flags&OpenPrivateCache != 0 {
  92. parts = append(parts, "SQLITE_OPEN_PRIVATECACHE")
  93. flags &^= OpenPrivateCache
  94. }
  95. if flags&OpenWAL != 0 {
  96. parts = append(parts, "SQLITE_OPEN_WAL")
  97. flags &^= OpenWAL
  98. }
  99. if flags != 0 || len(parts) == 0 {
  100. parts = append(parts, fmt.Sprintf("%#x", uint(flags)))
  101. }
  102. return strings.Join(parts, "|")
  103. }