Commit 0c71c5487302d1b7d5dda0210fe12d2be4a034f8

Authored by Marko Tikvić
1 parent 6cc94a06e9
Exists in master

minor changes

... ... @@ -24,6 +24,8 @@ const (
24 24 DDMMYYYY_HHMMSS_dt = "02.01.2006. 15:04:05"
25 25 )
26 26  
  27 +const DaySeconds = 24 * 60 * 60
  28 +
27 29 var (
28 30 regularYear = [12]int64{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
29 31 leapYear = [12]int64{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
... ... @@ -70,7 +72,7 @@ func IsLeapYear(year int64) bool {
70 72 }
71 73  
72 74 // FirstDayOfNextMonthEpoch ...
73   -func FirstDayOfNextMonthEpoch(e int64) int64 {
  75 +func NextMonths1st(e int64) int64 {
74 76 d, m, y := EpochToDayMonthYear(e)
75 77 m++
76 78 if m > 12 {
... ... @@ -83,3 +85,30 @@ func FirstDayOfNextMonthEpoch(e int64) int64 {
83 85  
84 86 return DateToEpoch(date, DDMMYYYY_sl)
85 87 }
  88 +
  89 +func SameDate(e1, e2 int64) bool {
  90 + d1, m1, y1 := EpochToDayMonthYear(e1)
  91 + d2, m2, y2 := EpochToDayMonthYear(e2)
  92 +
  93 + if d1 == d2 && m1 == m2 && y1 == y2 {
  94 + return true
  95 + }
  96 +
  97 + return false
  98 +}
  99 +
  100 +func ToStartOfDay(d int64) int64 {
  101 + rem := d % DaySeconds
  102 + if rem != 0 {
  103 + d -= rem
  104 + }
  105 + return d
  106 +}
  107 +
  108 +func ParseTime(date, format string) time.Time {
  109 + t, err := time.Parse(format, date)
  110 + if err != nil {
  111 + fmt.Println(err.Error())
  112 + }
  113 + return t
  114 +}
... ...
... ... @@ -83,7 +83,7 @@ func FormatFloat64Number(f float64, dec int) string {
83 83 return res
84 84 }
85 85  
86   -func MaxFlaot64(vars ...float64) (max float64) {
  86 +func MaxFloat64(vars ...float64) (max float64) {
87 87 max = vars[0]
88 88 for _, v := range vars {
89 89 if v > max {
... ...
... ... @@ -81,6 +81,11 @@ func (ns *NullString) Value() (driver.Value, error) {
81 81 return ns.String, nil
82 82 }
83 83  
  84 +// Val ...
  85 +func (ns *NullString) Val() string {
  86 + return ns.String
  87 +}
  88 +
84 89 // MarshalJSON ...
85 90 func (ns NullString) MarshalJSON() ([]byte, error) {
86 91 if ns.Valid {
... ... @@ -437,6 +442,14 @@ func (nt *NullDate) CastToSQL() NullDate {
437 442 return *nt
438 443 }
439 444  
  445 +func (nd *NullDate) Format(f string) string {
  446 + if !nd.Valid {
  447 + return ""
  448 + }
  449 +
  450 + return EpochToDate(nd.Time.Unix(), f)
  451 +}
  452 +
440 453 func parseSQLDate(str string, loc *time.Location) (t time.Time, err error) {
441 454 base := "0000-00-00"
442 455 timeFormat := "2006-01-02"
... ...
... ... @@ -4,21 +4,22 @@ import (
4 4 "database/sql"
5 5 "fmt"
6 6 "net/http"
  7 + "time"
7 8  
8 9 "git.to-net.rs/marko.tikvic/gologger"
9 10 "github.com/gorilla/mux"
10 11 )
11 12  
12 13 type Server struct {
13   - DB *sql.DB
14   - Router *mux.Router
15   - Logger *gologger.Logger
16   - Port string
17   - UTCOffset int64
18   - DBs map[string]*sql.DB
  14 + DB *sql.DB
  15 + Router *mux.Router
  16 + Logger *gologger.Logger
  17 + Port string
  18 + DBs map[string]*sql.DB
  19 + dsn map[string]string
19 20 }
20 21  
21   -func NewODBCServer(dsn, port, logDir string, utcOffset int64) (s *Server, err error) {
  22 +func NewODBCServer(dsn, port, logDir string) (s *Server, err error) {
22 23 s = new(Server)
23 24  
24 25 s.Port = port
... ... @@ -33,11 +34,12 @@ func NewODBCServer(dsn, port, logDir string, utcOffset int64) (s *Server, err er
33 34 return nil, fmt.Errorf("can't create logger: %s", err.Error())
34 35 }
35 36  
36   - s.UTCOffset = utcOffset
37   -
38 37 s.DBs = make(map[string]*sql.DB)
39 38 s.DBs["default"] = s.DB
40 39  
  40 + s.dsn = make(map[string]string)
  41 + s.DBs["default"] = s.DB
  42 +
41 43 return s, nil
42 44 }
43 45  
... ... @@ -77,3 +79,18 @@ func CommitChanges(tx *sql.Tx, err *error, opt ...error) {
77 79 tx.Rollback()
78 80 }
79 81 }
  82 +
  83 +func (s *Server) RefreshDatabaseConnections(period time.Duration) {
  84 + for {
  85 + for k, db := range s.DBs {
  86 + if err := db.Ping(); err != nil {
  87 + if s.Logger != nil {
  88 + s.Logger.PrintAndTrace("failed to ping database (%s): %s", s.dsn[k], err.Error())
  89 + } else {
  90 + fmt.Println("failed to ping database (%s): %s", s.dsn[k], err.Error())
  91 + }
  92 + }
  93 + }
  94 + time.Sleep(period)
  95 + }
  96 +}
... ...
... ... @@ -5,8 +5,6 @@ import (
5 5 "strconv"
6 6 "strings"
7 7 "unicode"
8   -
9   - "golang.org/x/exp/utf8string"
10 8 )
11 9  
12 10 const sanitisationPatern = "\"';&*<>=\\`:"
... ... @@ -115,10 +113,9 @@ func StringAt(s string, index int) string {
115 113 }
116 114  
117 115 func StringAtRune(s string, index int) string {
118   - str := utf8string.NewString(s)
119   - max := str.RuneCount()
120   - if index < max {
121   - return string(str.At(index))
  116 + str := []rune(s)
  117 + if index < len(str) {
  118 + return string(str[index])
122 119 }
123 120 return ""
124 121 }
... ...