A More Secure Internet Connection for Your Home https://fen.gg
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

116 wiersze
3.0 KiB

  1. package model
  2. //
  3. // Fengg Security Gateway Server Application
  4. // Copyright (C) 2020 Lukas Matt <support@fen.gg>
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. //
  18. import (
  19. "fmt"
  20. "errors"
  21. "tea.fen.gg/fengg/server/types"
  22. "github.com/jmoiron/sqlx"
  23. _ "github.com/lib/pq"
  24. )
  25. const (
  26. StatisticQueryTmpl = `SELECT * FROM statistics %s;`
  27. StatisticNamedUpdateTmpl = `UPDATE statistics SET count=:count WHERE %s;`
  28. StatisticNamedInsertTmpl = `INSERT INTO statistics (key, count) VALUES (:key, :count);`
  29. )
  30. type Statistic struct {
  31. ID uint `db:"id" json:"id"`
  32. Key types.StatisticKey `db:"key" json:"key"`
  33. Count float64 `db:"count" json:"count"`
  34. }
  35. type Statistics []Statistic
  36. func NewStatistic() *Statistic {
  37. return &Statistic{Count: 0.0}
  38. }
  39. func (statistic Statistic) Valid() bool {
  40. return len(statistic.Key) > 0 || statistic.ID > 0
  41. }
  42. func (statistics *Statistics) FindAll() error {
  43. db, err := sqlx.Connect(dbDriver, dbConnect)
  44. if err != nil {
  45. return err
  46. }
  47. defer db.Close()
  48. return db.Select(statistics, fmt.Sprintf(StatisticQueryTmpl, ""))
  49. }
  50. func (statistic *Statistic) Find() error {
  51. if !statistic.Valid() {
  52. return errors.New("we need at least a Key or ID")
  53. }
  54. db, err := sqlx.Connect(dbDriver, dbConnect)
  55. if err != nil {
  56. return err
  57. }
  58. defer db.Close()
  59. if statistic.ID > 0 {
  60. return db.Get(statistic, fmt.Sprintf(StatisticQueryTmpl, "WHERE id=$1"), statistic.ID)
  61. }
  62. return db.Get(statistic, fmt.Sprintf(StatisticQueryTmpl, "WHERE key=$1"), statistic.Key)
  63. }
  64. func (statistics *Statistics) Find() error {
  65. db, err := sqlx.Connect(dbDriver, dbConnect)
  66. if err != nil {
  67. return err
  68. }
  69. defer db.Close()
  70. return db.Select(statistics, fmt.Sprintf(StatisticQueryTmpl, ""))
  71. }
  72. func (statistic *Statistic) CreateOrUpdate() error {
  73. if !statistic.Valid() {
  74. return errors.New("we need at least a Key or ID")
  75. }
  76. db, err := sqlx.Connect(dbDriver, dbConnect)
  77. if err != nil {
  78. return err
  79. }
  80. defer db.Close()
  81. err = db.Get(&statistic.ID,
  82. `SELECT id FROM statistics WHERE key=$1 or id=$2;`, statistic.Key, statistic.ID)
  83. if err == nil {
  84. _, err = db.NamedExec(fmt.Sprintf(StatisticNamedUpdateTmpl, "id=:id"), statistic)
  85. } else {
  86. _, err = db.NamedExec(StatisticNamedInsertTmpl, statistic)
  87. }
  88. return err
  89. }
  90. func (statistics Statistics) CreateOrUpdate() (errs []error) {
  91. for _, statistic := range statistics {
  92. err := statistic.CreateOrUpdate()
  93. if err != nil {
  94. errs = append(errs, err)
  95. }
  96. }
  97. return
  98. }