Skip to content

Commit

Permalink
Add support for using datetime type in parameterised statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
pekim committed May 7, 2012
1 parent 9a8b6d4 commit 9ede878
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/data-type.coffee
Expand Up @@ -102,6 +102,30 @@ TYPE =
0x3D:
type: 'DATETIME'
name: 'DateTime'
declaration: () ->
'datetime'
writeParameterData: (buffer, parameter) ->
# ParamMetaData (TYPE_INFO)
buffer.writeUInt8(typeByName.DateTimeN.id)
buffer.writeUInt8(8)

# ParamLenData
if parameter.value?
days = (parameter.value.getTime() - EPOCH_DATE.getTime()) / (1000 * 60 * 60 * 24)
days = Math.floor(days)

seconds = parameter.value.getHours() * 60 * 60
seconds += parameter.value.getMinutes() * 60
seconds += parameter.value.getSeconds()
milliseconds = (seconds * 1000) + parameter.value.getMilliseconds()
threeHundredthsOfSecond = milliseconds / (3 + (1 / 3))
threeHundredthsOfSecond = Math.floor(threeHundredthsOfSecond)

buffer.writeUInt8(8)
buffer.writeUInt32LE(days)
buffer.writeUInt32LE(threeHundredthsOfSecond)
else
buffer.writeUInt8(0)
0x3E:
type: 'FLT8'
name: 'Float'
Expand Down
12 changes: 12 additions & 0 deletions test/integration/parameterised-statements-test.coffee
Expand Up @@ -69,6 +69,12 @@ exports.smallDateTime = (test) ->
exports.smallDateTimeNull = (test) ->
execSql(test, TYPES.SmallDateTime, null)

exports.dateTime = (test) ->
execSql(test, TYPES.DateTime, new Date('December 4, 2011 10:04:23'))

exports.dateTimeNull = (test) ->
execSql(test, TYPES.DateTime, null)

exports.outputBitTrue = (test) ->
execSqlOutput(test, TYPES.Bit, true)

Expand Down Expand Up @@ -114,6 +120,12 @@ exports.outputSmallDateTime = (test) ->
exports.outputSmallDateTimeNull = (test) ->
execSqlOutput(test, TYPES.SmallDateTime, null)

exports.outputDateTime = (test) ->
execSqlOutput(test, TYPES.DateTime, new Date('December 4, 2011 10:04:23'))

exports.outputDateTimeNull = (test) ->
execSqlOutput(test, TYPES.DateTime, null)

exports.multipleParameters = (test) ->
test.expect(6)

Expand Down

0 comments on commit 9ede878

Please sign in to comment.