kvdb.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. }
  25. //Create a new Database table
  26. void DBNewTable(String tableName) {
  27. tableName = DBCleanInput(tableName);
  28. if (!SD.exists(DB_root + tableName)) {
  29. SD.mkdir(DB_root + tableName);
  30. }
  31. }
  32. //Check if a database table exists
  33. bool DBTableExists(String tableName) {
  34. tableName = DBCleanInput(tableName);
  35. return SD.exists(DB_root + tableName);
  36. }
  37. //Write a key to a table, return true if succ
  38. bool DBWrite(String tableName, String key, String value) {
  39. if (!DBTableExists(tableName)) {
  40. return false;
  41. }
  42. tableName = DBCleanInput(tableName);
  43. key = DBCleanInput(key);
  44. String fsDataPath = DB_root + tableName + "/" + key;
  45. if (SD.exists(fsDataPath)) {
  46. //Entry already exists. Delete it
  47. SD.remove(fsDataPath);
  48. }
  49. //Write new data to it
  50. File targetEntry = SD.open(fsDataPath, FILE_WRITE);
  51. targetEntry.print(value);
  52. targetEntry.close();
  53. return true;
  54. }
  55. //Read from database, require table name and key
  56. String DBRead(String tableName, String key) {
  57. if (!DBTableExists(tableName)) {
  58. return "";
  59. }
  60. tableName = DBCleanInput(tableName);
  61. key = DBCleanInput(key);
  62. String fsDataPath = DB_root + tableName + "/" + key;
  63. if (!SD.exists(fsDataPath)) {
  64. //Target not exists. Return empty string
  65. return "";
  66. }
  67. String value = "";
  68. File targetEntry = SD.open(fsDataPath, FILE_READ);
  69. while (targetEntry.available()) {
  70. value = value + targetEntry.readString();
  71. }
  72. targetEntry.close();
  73. return value;
  74. }
  75. //Check if a given key exists in the database
  76. bool DBKeyExists(String tableName, String key) {
  77. if (!DBTableExists(tableName)) {
  78. return false;
  79. }
  80. tableName = DBCleanInput(tableName);
  81. key = DBCleanInput(key);
  82. String fsDataPath = DB_root + tableName + "/" + key;
  83. return SD.exists(fsDataPath);
  84. }
  85. //Remove the key-value item from db, return true if succ
  86. bool DBRemove(String tableName, String key) {
  87. if (!DBKeyExists(tableName, key)){
  88. return false;
  89. }
  90. tableName = DBCleanInput(tableName);
  91. key = DBCleanInput(key);
  92. String fsDataPath = DB_root + tableName + "/" + key;
  93. SD.remove(fsDataPath);
  94. return true;
  95. }