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.

62 lines
1.0 KiB

  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. err error
  12. )
  13. row = DB.QueryRow(`
  14. SELECT id FROM installed_packages WHERE name = ? AND version = ?;
  15. `, name, version)
  16. err = row.Scan(&id)
  17. return id, err
  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. err error
  26. )
  27. stmt, err = DB.Prepare("INSERT INTO installed_packages(name, version, installed_at) VALUES(?,?,?)")
  28. if err != nil {
  29. return err
  30. }
  31. result, err = stmt.Exec(name, version, time.Now().Unix())
  32. if err != nil {
  33. return err
  34. }
  35. pkgId, err = result.LastInsertId()
  36. if err != nil {
  37. return err
  38. }
  39. for _, depId = range depIds {
  40. stmt, err = DB.Prepare("INSERT INTO dependancy_linker(package_id, dependancy_id) VALUES(?,?)")
  41. if err != nil {
  42. return err
  43. }
  44. _, err = stmt.Exec(pkgId, depId)
  45. if err != nil {
  46. return err
  47. }
  48. }
  49. return err
  50. }