Skip to content

Commit

Permalink
ramsql: format dates correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
rjeczalik committed May 26, 2019
1 parent dcdf67c commit de528b3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
19 changes: 15 additions & 4 deletions driver/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"
"strconv"
"strings"
"time"

"github.com/proullon/ramsql/engine/log"
)
Expand Down Expand Up @@ -178,16 +179,26 @@ func replaceArguments(query string, args []driver.Value) string {
return replacedQuery
}

const mysqlFormat = `2006-01-02 15:04:05.999999`

func replaceArgumentsODBC(query string, args []driver.Value) string {
var finalQuery string

queryParts := strings.Split(query, "?")
finalQuery = queryParts[0]
for i := range args {
arg := fmt.Sprintf("%v", args[i])
_, ok := args[i].(string)
if ok && !strings.HasSuffix(query, "'") {
arg = "$$" + arg + "$$"
var arg string
switch v := args[i].(type) {
case time.Time:
arg = v.Format(mysqlFormat)
case string:
if !strings.HasSuffix(query, "'") {
arg = "$$" + v + "$$"
} else {
arg = v
}
default:
arg = fmt.Sprintf("%v", args[i])
}
finalQuery += arg
finalQuery += queryParts[i+1]
Expand Down
9 changes: 8 additions & 1 deletion engine/parser/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ const DateShortFormat = "2006-Jan-02"

const DateNumberFormat = "2006-01-02"

const DataMysqlFormat = `2006-01-02 15:04:05.999999`

// ParseDate intends to parse all SQL date format
func ParseDate(data string) (*time.Time, error) {
t, err := time.Parse(DateLongFormat, data)
t, err := time.Parse(DataMysqlFormat, data)
if err == nil {
return &t, nil
}

t, err = time.Parse(DateLongFormat, data)
if err == nil {
return &t, nil
}
Expand Down

0 comments on commit de528b3

Please sign in to comment.