diff --git a/date_util.go b/date_util.go index 22f2284..ac67c67 100644 --- a/date_util.go +++ b/date_util.go @@ -24,6 +24,8 @@ const ( DDMMYYYY_HHMMSS_dt = "02.01.2006. 15:04:05" ) +const DaySeconds = 24 * 60 * 60 + var ( regularYear = [12]int64{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 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 { } // FirstDayOfNextMonthEpoch ... -func FirstDayOfNextMonthEpoch(e int64) int64 { +func NextMonths1st(e int64) int64 { d, m, y := EpochToDayMonthYear(e) m++ if m > 12 { @@ -83,3 +85,30 @@ func FirstDayOfNextMonthEpoch(e int64) int64 { return DateToEpoch(date, DDMMYYYY_sl) } + +func SameDate(e1, e2 int64) bool { + d1, m1, y1 := EpochToDayMonthYear(e1) + d2, m2, y2 := EpochToDayMonthYear(e2) + + if d1 == d2 && m1 == m2 && y1 == y2 { + return true + } + + return false +} + +func ToStartOfDay(d int64) int64 { + rem := d % DaySeconds + if rem != 0 { + d -= rem + } + return d +} + +func ParseTime(date, format string) time.Time { + t, err := time.Parse(format, date) + if err != nil { + fmt.Println(err.Error()) + } + return t +} diff --git a/float_util.go b/float_util.go index 8167f52..9a20f28 100644 --- a/float_util.go +++ b/float_util.go @@ -83,7 +83,7 @@ func FormatFloat64Number(f float64, dec int) string { return res } -func MaxFlaot64(vars ...float64) (max float64) { +func MaxFloat64(vars ...float64) (max float64) { max = vars[0] for _, v := range vars { if v > max { diff --git a/nullables.go b/nullables.go index bb42966..5e6ee2d 100644 --- a/nullables.go +++ b/nullables.go @@ -81,6 +81,11 @@ func (ns *NullString) Value() (driver.Value, error) { return ns.String, nil } +// Val ... +func (ns *NullString) Val() string { + return ns.String +} + // MarshalJSON ... func (ns NullString) MarshalJSON() ([]byte, error) { if ns.Valid { @@ -437,6 +442,14 @@ func (nt *NullDate) CastToSQL() NullDate { return *nt } +func (nd *NullDate) Format(f string) string { + if !nd.Valid { + return "" + } + + return EpochToDate(nd.Time.Unix(), f) +} + func parseSQLDate(str string, loc *time.Location) (t time.Time, err error) { base := "0000-00-00" timeFormat := "2006-01-02" diff --git a/server.go b/server.go index bbfb7ab..27ad8ea 100644 --- a/server.go +++ b/server.go @@ -4,21 +4,22 @@ import ( "database/sql" "fmt" "net/http" + "time" "git.to-net.rs/marko.tikvic/gologger" "github.com/gorilla/mux" ) type Server struct { - DB *sql.DB - Router *mux.Router - Logger *gologger.Logger - Port string - UTCOffset int64 - DBs map[string]*sql.DB + DB *sql.DB + Router *mux.Router + Logger *gologger.Logger + Port string + DBs map[string]*sql.DB + dsn map[string]string } -func NewODBCServer(dsn, port, logDir string, utcOffset int64) (s *Server, err error) { +func NewODBCServer(dsn, port, logDir string) (s *Server, err error) { s = new(Server) s.Port = port @@ -33,11 +34,12 @@ func NewODBCServer(dsn, port, logDir string, utcOffset int64) (s *Server, err er return nil, fmt.Errorf("can't create logger: %s", err.Error()) } - s.UTCOffset = utcOffset - s.DBs = make(map[string]*sql.DB) s.DBs["default"] = s.DB + s.dsn = make(map[string]string) + s.DBs["default"] = s.DB + return s, nil } @@ -77,3 +79,18 @@ func CommitChanges(tx *sql.Tx, err *error, opt ...error) { tx.Rollback() } } + +func (s *Server) RefreshDatabaseConnections(period time.Duration) { + for { + for k, db := range s.DBs { + if err := db.Ping(); err != nil { + if s.Logger != nil { + s.Logger.PrintAndTrace("failed to ping database (%s): %s", s.dsn[k], err.Error()) + } else { + fmt.Println("failed to ping database (%s): %s", s.dsn[k], err.Error()) + } + } + } + time.Sleep(period) + } +} diff --git a/string_util.go b/string_util.go index 1b915e2..8ab5202 100644 --- a/string_util.go +++ b/string_util.go @@ -5,8 +5,6 @@ import ( "strconv" "strings" "unicode" - - "golang.org/x/exp/utf8string" ) const sanitisationPatern = "\"';&*<>=\\`:" @@ -115,10 +113,9 @@ func StringAt(s string, index int) string { } func StringAtRune(s string, index int) string { - str := utf8string.NewString(s) - max := str.RuneCount() - if index < max { - return string(str.At(index)) + str := []rune(s) + if index < len(str) { + return string(str[index]) } return "" }