database.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package database
  2. /*
  3. ArOZ Online Database Access Module
  4. author: tobychui
  5. This is an improved Object oriented base solution to the original
  6. aroz online database script.
  7. */
  8. import (
  9. "log"
  10. "runtime"
  11. "imuslab.com/zoraxy/mod/database/dbinc"
  12. )
  13. type Database struct {
  14. Db interface{} //This will be nil on openwrt, leveldb.DB on x64 platforms or bolt.DB on other platforms
  15. BackendType dbinc.BackendType
  16. Backend dbinc.Backend
  17. }
  18. func NewDatabase(dbfile string, backendType dbinc.BackendType) (*Database, error) {
  19. if runtime.GOARCH == "riscv64" {
  20. log.Println("RISCV hardware detected, ignoring the backend type and using FS emulated database")
  21. }
  22. return newDatabase(dbfile, backendType)
  23. }
  24. func GetRecommendedBackendType() dbinc.BackendType {
  25. //Check if the system is running on RISCV hardware
  26. if runtime.GOARCH == "riscv64" {
  27. //RISCV hardware, currently only support FS emulated database
  28. return dbinc.BackendFSOnly
  29. } else if runtime.GOOS == "windows" || (runtime.GOOS == "linux" && runtime.GOARCH == "amd64") {
  30. //Powerful hardware, use LevelDB
  31. return dbinc.BackendLevelDB
  32. }
  33. //Default to BoltDB, the safest option
  34. return dbinc.BackendBoltDB
  35. }
  36. /*
  37. Create / Drop a table
  38. Usage:
  39. err := sysdb.NewTable("MyTable")
  40. err := sysdb.DropTable("MyTable")
  41. */
  42. // Create a new table
  43. func (d *Database) NewTable(tableName string) error {
  44. return d.newTable(tableName)
  45. }
  46. // Check is table exists
  47. func (d *Database) TableExists(tableName string) bool {
  48. return d.tableExists(tableName)
  49. }
  50. // Drop the given table
  51. func (d *Database) DropTable(tableName string) error {
  52. return d.dropTable(tableName)
  53. }
  54. /*
  55. Write to database with given tablename and key. Example Usage:
  56. type demo struct{
  57. content string
  58. }
  59. thisDemo := demo{
  60. content: "Hello World",
  61. }
  62. err := sysdb.Write("MyTable", "username/message",thisDemo);
  63. */
  64. func (d *Database) Write(tableName string, key string, value interface{}) error {
  65. return d.write(tableName, key, value)
  66. }
  67. /*
  68. Read from database and assign the content to a given datatype. Example Usage:
  69. type demo struct{
  70. content string
  71. }
  72. thisDemo := new(demo)
  73. err := sysdb.Read("MyTable", "username/message",&thisDemo);
  74. */
  75. func (d *Database) Read(tableName string, key string, assignee interface{}) error {
  76. return d.read(tableName, key, assignee)
  77. }
  78. /*
  79. Check if a key exists in the database table given tablename and key
  80. if sysdb.KeyExists("MyTable", "username/message"){
  81. log.Println("Key exists")
  82. }
  83. */
  84. func (d *Database) KeyExists(tableName string, key string) bool {
  85. return d.keyExists(tableName, key)
  86. }
  87. /*
  88. Delete a value from the database table given tablename and key
  89. err := sysdb.Delete("MyTable", "username/message");
  90. */
  91. func (d *Database) Delete(tableName string, key string) error {
  92. return d.delete(tableName, key)
  93. }
  94. /*
  95. //List table example usage
  96. //Assume the value is stored as a struct named "groupstruct"
  97. entries, err := sysdb.ListTable("test")
  98. if err != nil {
  99. panic(err)
  100. }
  101. for _, keypairs := range entries{
  102. log.Println(string(keypairs[0]))
  103. group := new(groupstruct)
  104. json.Unmarshal(keypairs[1], &group)
  105. log.Println(group);
  106. }
  107. */
  108. func (d *Database) ListTable(tableName string) ([][][]byte, error) {
  109. return d.listTable(tableName)
  110. }
  111. /*
  112. Close the database connection
  113. */
  114. func (d *Database) Close() {
  115. d.close()
  116. }