kvdb.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Key Value database
  3. This is a file system based database
  4. that uses foldername as table name,
  5. filename as key and content as value
  6. Folder name and filename are limited to
  7. 5 characters as SDFS requirements.
  8. */
  9. //Root of the db on SD card, **must have tailing slash**
  10. const String DB_root = "/db/";
  11. //Clean the input for any input string
  12. String DBCleanInput(const String& inputString) {
  13. String trimmedString = inputString;
  14. //Replae all the slash that might breaks the file system
  15. trimmedString.replace("/", "");
  16. //Trim off the space before and after the string
  17. trimmedString.trim();
  18. return trimmedString;
  19. }
  20. //Database init create all the required table for basic system operations
  21. void DBInit() {
  22. DBNewTable("auth");
  23. DBNewTable("pref");
  24. DBNewTable("user"); //User Store
  25. DBNewTable("sess"); //Session Store
  26. }
  27. //Create a new Database table
  28. void DBNewTable(String tableName) {
  29. tableName = DBCleanInput(tableName);
  30. if (!SD.exists(DB_root + tableName)) {
  31. SD.mkdir(DB_root + tableName);
  32. }
  33. }
  34. //Check if a database table exists
  35. bool DBTableExists(String tableName) {
  36. tableName = DBCleanInput(tableName);
  37. return SD.exists(DB_root + tableName);
  38. }
  39. //Write a key to a table, return true if succ
  40. bool DBWrite(String tableName, String key, String value) {
  41. if (!DBTableExists(tableName)) {
  42. return false;
  43. }
  44. tableName = DBCleanInput(tableName);
  45. key = DBCleanInput(key);
  46. String fsDataPath = DB_root + tableName + "/" + key;
  47. if (SD.exists(fsDataPath)) {
  48. //Entry already exists. Delete it
  49. SD.remove(fsDataPath);
  50. }
  51. //Write new data to it
  52. File targetEntry = SD.open(fsDataPath, FILE_WRITE);
  53. targetEntry.print(value);
  54. targetEntry.close();
  55. return true;
  56. }
  57. //Read from database, require table name and key
  58. String DBRead(String tableName, String key) {
  59. if (!DBTableExists(tableName)) {
  60. return "";
  61. }
  62. tableName = DBCleanInput(tableName);
  63. key = DBCleanInput(key);
  64. String fsDataPath = DB_root + tableName + "/" + key;
  65. if (!SD.exists(fsDataPath)) {
  66. //Target not exists. Return empty string
  67. return "";
  68. }
  69. String value = "";
  70. File targetEntry = SD.open(fsDataPath, FILE_READ);
  71. while (targetEntry.available()) {
  72. value = value + targetEntry.readString();
  73. }
  74. targetEntry.close();
  75. return value;
  76. }
  77. //Check if a given key exists in the database
  78. bool DBKeyExists(String tableName, String key) {
  79. if (!DBTableExists(tableName)) {
  80. return false;
  81. }
  82. tableName = DBCleanInput(tableName);
  83. key = DBCleanInput(key);
  84. String fsDataPath = DB_root + tableName + "/" + key;
  85. return SD.exists(fsDataPath);
  86. }
  87. //Remove the key-value item from db, return true if succ
  88. bool DBRemove(String tableName, String key) {
  89. if (!DBKeyExists(tableName, key)){
  90. return false;
  91. }
  92. tableName = DBCleanInput(tableName);
  93. key = DBCleanInput(key);
  94. String fsDataPath = DB_root + tableName + "/" + key;
  95. SD.remove(fsDataPath);
  96. return true;
  97. }