database.go 3.2 KB

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