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.

61 lines
1023 B

  1. package Database
  2. import (
  3. "database/sql"
  4. "time"
  5. _ "github.com/mattn/go-sqlite3"
  6. )
  7. func IsPackageInstalled(name, version string) (int64, error) {
  8. var (
  9. row *sql.Row
  10. id int64
  11. e error
  12. )
  13. row = DB.QueryRow(`
  14. SELECT id FROM installed_packages WHERE name = ? AND version = ?;
  15. `, name, version)
  16. e = row.Scan(&id)
  17. return id, e
  18. }
  19. func InsertPackage(name, version string, depIds []int64) error {
  20. var (
  21. stmt *sql.Stmt
  22. result sql.Result
  23. pkgId int64
  24. depId int64
  25. e error
  26. )
  27. stmt, e = DB.Prepare("INSERT INTO installed_packages(name, version, installed_at) VALUES(?,?,?)")
  28. if e != nil {
  29. return e
  30. }
  31. result, e = stmt.Exec(name, version, time.Now().Unix())
  32. if e != nil {
  33. return e
  34. }
  35. pkgId, e = result.LastInsertId()
  36. if e != nil {
  37. return e
  38. }
  39. for _, depId = range depIds {
  40. stmt, e = DB.Prepare("INSERT INTO dependancy_linker(package_id, dependancy_id) VALUES(?,?)")
  41. if e != nil {
  42. return e
  43. }
  44. _, e = stmt.Exec(pkgId, depId)
  45. if e != nil {
  46. return e
  47. }
  48. }
  49. return e
  50. }