pgsql.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package dataprovider
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "runtime"
  6. "github.com/drakkan/sftpgo/logger"
  7. )
  8. // PGSQLProvider auth provider for PostgreSQL database
  9. type PGSQLProvider struct {
  10. }
  11. func initializePGSQLProvider() error {
  12. var err error
  13. var connectionString string
  14. if len(config.ConnectionString) == 0 {
  15. connectionString = fmt.Sprintf("host='%v' port=%v dbname='%v' user='%v' password='%v' sslmode=%v connect_timeout=10",
  16. config.Host, config.Port, config.Name, config.Username, config.Password, getSSLMode())
  17. } else {
  18. connectionString = config.ConnectionString
  19. }
  20. dbHandle, err = sql.Open("postgres", connectionString)
  21. if err == nil {
  22. numCPU := runtime.NumCPU()
  23. logger.Debug(logSender, "postgres database handle created, connection string: \"%v\", pool size: %v", connectionString, numCPU)
  24. dbHandle.SetMaxIdleConns(numCPU)
  25. dbHandle.SetMaxOpenConns(numCPU)
  26. } else {
  27. logger.Warn(logSender, "error creating postgres database handler, connection string: \"%v\", error: %v", connectionString, err)
  28. }
  29. return err
  30. }
  31. func (p PGSQLProvider) validateUserAndPass(username string, password string) (User, error) {
  32. return sqlCommonValidateUserAndPass(username, password)
  33. }
  34. func (p PGSQLProvider) validateUserAndPubKey(username string, publicKey string) (User, error) {
  35. return sqlCommonValidateUserAndPubKey(username, publicKey)
  36. }
  37. func (p PGSQLProvider) getUserByID(ID int64) (User, error) {
  38. return sqlCommonGetUserByID(ID)
  39. }
  40. func (p PGSQLProvider) updateQuota(username string, filesAdd int, sizeAdd int64, reset bool) error {
  41. tx, err := dbHandle.Begin()
  42. if err != nil {
  43. logger.Warn(logSender, "error starting transaction to update quota for user %v: %v", username, err)
  44. return err
  45. }
  46. err = sqlCommonUpdateQuota(username, filesAdd, sizeAdd, reset, p)
  47. if err == nil {
  48. err = tx.Commit()
  49. } else {
  50. err = tx.Rollback()
  51. }
  52. if err != nil {
  53. logger.Warn(logSender, "error closing transaction to update quota for user %v: %v", username, err)
  54. }
  55. return err
  56. }
  57. func (p PGSQLProvider) getUsedQuota(username string) (int, int64, error) {
  58. return sqlCommonGetUsedQuota(username)
  59. }
  60. func (p PGSQLProvider) userExists(username string) (User, error) {
  61. return sqlCommonCheckUserExists(username)
  62. }
  63. func (p PGSQLProvider) addUser(user User) error {
  64. return sqlCommonAddUser(user)
  65. }
  66. func (p PGSQLProvider) updateUser(user User) error {
  67. return sqlCommonUpdateUser(user)
  68. }
  69. func (p PGSQLProvider) deleteUser(user User) error {
  70. return sqlCommonDeleteUser(user)
  71. }
  72. func (p PGSQLProvider) getUsers(limit int, offset int, order string, username string) ([]User, error) {
  73. return sqlCommonGetUsers(limit, offset, order, username)
  74. }