Skip to content

Commit

Permalink
Implement datetime unparsing to restore symmetry :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Bex committed May 9, 2011
1 parent 4435f22 commit 702e01f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
26 changes: 26 additions & 0 deletions freetds.scm
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,32 @@ with the FreeTDS egg. If not, see <http://www.gnu.org/licenses/>.
(data-format-datatype-set!
fmt* (foreign-value "CS_INT_TYPE" CS_INT))
#t)
((date? param)
(data-format-datatype-set!
fmt* (foreign-value "CS_DATETIME_TYPE" CS_INT))
((foreign-lambda* c-pointer
(((c-pointer "CS_CONTEXT") ctx)
(c-string s)
((c-pointer "CS_DATAFMT") fmt))
"CS_CHAR *tmp = (CS_CHAR *)s;"
"CS_DATAFMT src;"
"CS_DATETIME *res;"
"CS_RETCODE ret;"
"res = malloc(sizeof(CS_DATETIME));"
"if (res == NULL)"
" C_return(res);"
"src.namelen = 0;"
"src.datatype = CS_CHAR_TYPE;"
"src.maxlength = strlen(s);"
"ret = cs_convert(ctx, &src, s, fmt, res, NULL);"
"if (ret != CS_SUCCEED) {"
" free(res);"
" C_return(NULL);"
"}"
"C_return(res);")
;; We can't use ~4 or ~5 because FreeTDS chokes on the 'T'
;; time separator. Also, it doesn't grok timezones.
*app-context* (date->string param "~1 ~3") fmt*))
(else (error "Unknown parameter type" param)))))
(data-format-name-length-set! fmt* 0) ; All params are nameless
(data-format-status-set! fmt* (foreign-value "CS_INPUTVALUE" CS_INT))
Expand Down
10 changes: 10 additions & 0 deletions tests/run.scm
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@
'((0.0 -1.5 110.12345))
(result-values
(send-query connection "SELECT ?, ?, ?" 0.0 -1.5 110.12345)))
(test "Datetime values are written correctly"
;; TODO: Figure out how to make this thing use timezones
`((,(make-date 0 0 0 0 1 1 2000 0)
,(make-date 0 56 1 17 9 5 2011 0)
,(make-date 0 58 14 17 9 5 2011 0)))
(result-values (send-query connection
"SELECT ?, ?, ?"
(make-date 0 0 0 0 1 1 2000 0)
(make-date 0 56 1 17 9 5 2011 0)
(make-date 0 58 14 17 9 5 2011 0))))
(test "NULL values are written correctly"
(list (list (sql-null) (sql-null)))
(result-values
Expand Down

0 comments on commit 702e01f

Please sign in to comment.