Skip to content

Commit

Permalink
Refactor types.
Browse files Browse the repository at this point in the history
  • Loading branch information
wvanbergen committed Sep 7, 2011
1 parent 54f9b47 commit 5eae85a
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 109 deletions.
47 changes: 18 additions & 29 deletions src/backend_message.coffee
@@ -1,4 +1,5 @@
AuthenticationMethods = require('./authentication').methods
AuthenticationMethods = require('./authentication').methods
typeOIDs = require('./types').typeOIDs

class BackendMessage
typeId: null
Expand Down Expand Up @@ -53,29 +54,6 @@ class BackendMessage.EmptyQueryResponse extends BackendMessage
class BackendMessage.RowDescription extends BackendMessage
typeId: 84 # T

fieldTypes:
5: "boolean"
6: "integer"
7: "real"
8: "string"
9: "string"
10: "date"
11: "time"
12: "timestamp"
13: "timestamp"
14: "interval"
15: "time"
16: "numeric"
25: "string"
1043: "string"
20: "integer"
21: "integer"
23: "integer"
26: "integer"
700: "integer"
701: "integer"
1700: "real"

read: (buffer) ->
numberOfFields = buffer.readUInt16(0)
pos = 2
Expand All @@ -88,7 +66,7 @@ class BackendMessage.RowDescription extends BackendMessage
pos += 4
tableFieldIndex = buffer.readUInt16(pos)
pos += 2
typeId = buffer.readUInt32(pos)
typeOID = buffer.readUInt32(pos)
pos += 4
size = buffer.readUInt16(pos)
pos += 2
Expand All @@ -102,12 +80,12 @@ class BackendMessage.RowDescription extends BackendMessage
name: name
tableId: tableId
tableFieldIndex: tableFieldIndex
typeId: typeId
type: @fieldTypes[typeId]
typeOID: typeOID
type: typeOIDs[typeOID]
size: size
modifier: modifier
formatCode: formatCode

@columns.push fieldDescriptor


Expand Down Expand Up @@ -139,6 +117,18 @@ class BackendMessage.CommandComplete extends BackendMessage
@status = buffer.readZeroTerminatedString()


class BackendMessage.ParameterDescription extends BackendMessage
typeId: 116 # t

read: (buffer) ->
count = buffer.readUInt16(0)
@types = (buffer.readUInt32(2 + i * 4) for i in [0 ... count])


class BackendMessage.ParseComplete extends BackendMessage
typeId: 49 # 1


class BackendMessage.ErrorResponse extends BackendMessage
typeId: 69 # E

Expand Down Expand Up @@ -195,7 +185,6 @@ class BackendMessage.CopyInResponse extends BackendMessage
pos += 1



##############################################################
# BackendMessage factory
##############################################################
Expand Down
6 changes: 3 additions & 3 deletions src/query.coffee
@@ -1,13 +1,13 @@
EventEmitter = require('events').EventEmitter
FrontendMessage = require('./frontend_message')
ValueDecorders = require('./value_decoders')
valueDecoders = require('./types').decoders

class Query extends EventEmitter

constructor: (@connection, @sql, @callback) ->
@_handlingCopyIn = false


execute: () ->
@emit 'start'

Expand Down Expand Up @@ -133,7 +133,7 @@ class Query.Field
@modifier = msg.modifier
@formatCode = msg.formatCode

@convert = ValueDecorders[@formatCode][@type] || ValueDecorders[@formatCode].default
@convert = valueDecoders[@formatCode][@type] || valueDecoders[@formatCode].default


module.exports = Query
109 changes: 104 additions & 5 deletions src/types.coffee
@@ -1,10 +1,37 @@

padWithZeroes = (str, length) ->
res = "#{str}"
res = "0#{res}" while res.length < length
return res


exports.typeOIDs =
5: "boolean"
6: "integer"
7: "real"
8: "string"
9: "string"
10: "date"
11: "time"
12: "timestamp"
13: "timestamp"
14: "interval"
15: "time"
16: "numeric"
25: "string"
1043: "string"
20: "integer"
21: "integer"
23: "integer"
26: "integer"
700: "integer"
701: "integer"
1700: "real"


################################################
# Vertica Date type
################################################

class VerticaDate
constructor: (year, month, day) ->
@year = +year
Expand All @@ -27,6 +54,12 @@ VerticaDate.fromDate = (date) ->
new VerticaDate(date.getFullYear(), date.getMonth() + 1, date.getDate())


exports.Date = VerticaDate

################################################
# Vertica Time type
################################################

class VerticaTime
constructor: (hour, minute, second) ->
@hour = +hour
Expand All @@ -43,8 +76,51 @@ VerticaTime.fromStringBuffer = (buffer) ->
else
throw 'Invalid time format!'

exports.Time = VerticaTime

################################################
# Vertica Timestamp type
################################################

# not implemented as a separate class as of yet

VerticaTimestamp =

fromStringBuffer: (buffer) ->
timezoneOffset = require('./vertica')
timestampRegexp = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})(\.\d{1,})?(?:([\+\-])(\d{2})(?:\:(\d{2}))?)?$/
if matches = buffer.toString('ascii').match(timestampRegexp)
utc = Date.UTC(+matches[1], +matches[2] - 1, +matches[3], +matches[4], +matches[5], +matches[6], Math.round(+matches[7] * 1000) || 0)

if matches[8]
timezoneOffset = +matches[9] * 60 + (+matches[10] || 0)
timezoneOffset = 0 - timezoneOffset if matches[8] == '-'
utc -= timezoneOffset * 60 * 1000
else if VerticaTimestamp.timezoneOffset
utc -= VerticaTimestamp.timezoneOffset

# class VerticaTimestamp
new Date(utc)

else
throw 'Invalid timestamp string returned'


setTimezoneOffset: (offset) ->
if !offset?
VerticaTimestamp.timezoneOffset = null
else if matches = offset.match(/^([\+\-])(\d{1,2})(?:\:(\d{2}))?$/)
timezoneOffset = +matches[2] * 60 + (+matches[3] || 0)
timezoneOffset = 0 - timezoneOffset if matches[1] == '-'
VerticaTimestamp.timezoneOffset = timezoneOffset * 60 * 1000
else
throw "Invalid timezone offset string: #{offset}!"


exports.Timestamp = VerticaTimestamp

################################################
# Vertica Interval type
################################################

class VerticaInterval
constructor: (days, hours, minutes, seconds) ->
Expand Down Expand Up @@ -87,7 +163,30 @@ VerticaInterval.fromStringBuffer = (buffer) ->
throw 'Invalid interval format!'



exports.Date = VerticaDate
exports.Time = VerticaTime
exports.Interval = VerticaInterval

################################################
# value decoders
################################################

stringDecoders =
string: (buffer) -> buffer.toString()
integer: (buffer) -> +buffer
real: (buffer) -> parseFloat(buffer)
numeric: (buffer) -> parseFloat(buffer)
boolean: (buffer) -> buffer.toString() == 't'
date: (buffer) -> VerticaDate.fromStringBuffer(buffer)
time: (buffer) -> VerticaTime.fromStringBuffer(buffer)
interval: (buffer) -> VerticaInterval.fromStringBuffer(buffer)
timestamp: (buffer) -> VerticaTimestamp.fromStringBuffer(buffer)
default: (buffer) -> buffer.toString()

binaryDecoders =
default: (buffer) -> throw 'Binary decoders not yet supported!'


exports.decoders =
0: stringDecoders,
1: binaryDecoders,
'string': stringDecoders,
'binary': binaryDecoders
38 changes: 0 additions & 38 deletions src/value_decoders.coffee

This file was deleted.

22 changes: 7 additions & 15 deletions src/vertica.coffee
Expand Up @@ -5,24 +5,16 @@ exports.connect = (connectionOptions, callback) ->
connection.connect(callback)
return connection

exports.setTimezoneOffset = (offset) ->
if !offset?
exports.timezoneOffset = null
else if matches = offset.match(/^([\+\-])(\d{1,2})(?:\:(\d{2}))?$/)
timezoneOffset = +matches[2] * 60 + (+matches[3] || 0)
timezoneOffset = 0 - timezoneOffset if matches[1] == '-'
exports.timezoneOffset = timezoneOffset * 60 * 1000
else
throw "Invalid timezone offset string: #{offset}!"


types = require('./types')
quoting = require('./quoting')

exports.Date = types.Date
exports.Time = types.Time
# exports.Timestamp = types.Timestamp
exports.Interval = types.Interval
exports.Date = types.Date
exports.Time = types.Time
exports.Timestamp = types.Timestamp
exports.Interval = types.Interval
exports.setTimezoneOffset = types.Timestamp.setTimezoneOffset

quoting = require('./quoting')

exports.escape = quoting.escape
exports.quote = quoting.quote
Expand Down
2 changes: 1 addition & 1 deletion test/backend_message_test.coffee
Expand Up @@ -51,7 +51,7 @@ vow.addBatch
assert.length message.columns, 1
assert.equal message.columns[0].tableId, 30110
assert.equal message.columns[0].tableFieldIndex, 1
assert.equal message.columns[0].typeId, 6
assert.equal message.columns[0].typeOID, 6
assert.equal message.columns[0].type, "integer"


Expand Down

0 comments on commit 5eae85a

Please sign in to comment.