PackageManager just because
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
2.5 KiB

  1. package Database
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "time"
  6. )
  7. type FilesystemHashRow struct {
  8. Path string
  9. Hash string
  10. UpdatedAt time.Time
  11. }
  12. func FindOrCreateFileHash(rows []FilesystemHashRow) error {
  13. var (
  14. stmtString string
  15. stmt *sql.Stmt
  16. values []interface{} = []interface{}{}
  17. e error
  18. )
  19. stmtString = "INSERT OR REPLACE INTO filesystem_hash (id, path, hash, updated_at) VALUES "
  20. for _, row := range rows {
  21. stmtString += `(
  22. (SELECT id FROM filesystem_hash WHERE path = ?),
  23. ?,
  24. ?,
  25. ?
  26. ),`
  27. values = append(values, row.Path, row.Path, row.Hash, row.UpdatedAt.String())
  28. }
  29. stmtString = stmtString[0 : len(stmtString)-1]
  30. stmt, e = DB.Prepare(stmtString)
  31. if e != nil {
  32. return e
  33. }
  34. _, e = stmt.Exec(values...)
  35. return e
  36. }
  37. func FindModifiedFiles(hashes []string) ([]string, error) {
  38. var (
  39. stmtString string
  40. stmt *sql.Stmt
  41. values []interface{} = []interface{}{}
  42. result *sql.Rows
  43. dirtyFiles []string = []string{}
  44. e error
  45. )
  46. stmtString = "SELECT id, path FROM filesystem_hash WHERE hash NOT IN ("
  47. for _, row := range hashes {
  48. stmtString += "?,"
  49. values = append(values, row)
  50. }
  51. stmtString = stmtString[0:len(stmtString)-1] + ")"
  52. stmt, e = DB.Prepare(stmtString)
  53. if e != nil {
  54. return dirtyFiles, e
  55. }
  56. result, e = stmt.Query(values...)
  57. if e != nil {
  58. return dirtyFiles, e
  59. }
  60. defer result.Close()
  61. for result.Next() {
  62. var id string
  63. var hash string
  64. e = result.Scan(&id, &hash)
  65. if e != nil {
  66. return dirtyFiles, e
  67. }
  68. fmt.Println(id, hash)
  69. dirtyFiles = append(dirtyFiles, hash)
  70. }
  71. e = result.Err()
  72. return dirtyFiles, e
  73. }
  74. /*
  75. func FindNewFiles(files []string) ([]string, error) {
  76. var (
  77. stmtString string
  78. stmt *sql.Stmt
  79. values []interface{} = []interface{}{}
  80. result *sql.Rows
  81. dirtyFiles []string = []string{}
  82. e error
  83. )
  84. stmtString = `select *
  85. from (
  86. values (4),(5),(6)
  87. ) as v(id)
  88. where not exists (select *
  89. from images i
  90. where i.id = v.id);`
  91. for _, row := range hashes {
  92. stmtString += "?,"
  93. values = append(values, row)
  94. }
  95. stmtString = stmtString[0:len(stmtString)-1] + ")"
  96. stmt, e = DB.Prepare(stmtString)
  97. if e != nil {
  98. return dirtyFiles, e
  99. }
  100. result, e = stmt.Query(values...)
  101. if e != nil {
  102. return dirtyFiles, e
  103. }
  104. defer result.Close()
  105. for result.Next() {
  106. var id string
  107. var hash string
  108. e = result.Scan(&id, &hash)
  109. if e != nil {
  110. return dirtyFiles, e
  111. }
  112. fmt.Println(id, hash)
  113. dirtyFiles = append(dirtyFiles, hash)
  114. }
  115. e = result.Err()
  116. return dirtyFiles, e
  117. }
  118. */