forked from tediousjs/node-mssql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.coffee
115 lines (89 loc) · 2.44 KB
/
table.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
{TYPES, declare} = require './datatypes'
MAX = 65535 # (1 << 16) - 1
JSON_COLUMN_ID = 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B'
class Table
constructor: (name) ->
if name
length = name.length
cursor = -1
buffer = ''
escaped = false
path = []
while ++cursor < length
char = name.charAt cursor
if char is '['
if escaped
buffer += char
else
escaped = true
else if char is ']'
if escaped
escaped = false
else
throw new Error "Invalid table name."
else if char is '.'
if escaped
buffer += char
else
path.push buffer
buffer = ''
else
buffer += char
if buffer
path.push buffer
switch path.length
when 1
@name = path[0]
@schema = null
@database = null
when 2
@name = path[1]
@schema = path[0]
@database = null
when 3
@name = path[2]
@schema = path[1]
@database = path[0]
@path = "#{if @database then "[#{@database}]." else ""}#{if @schema then "[#{@schema}]." else ""}[#{@name}]"
@temporary = @name.charAt(0) is '#'
@columns = []
@rows = []
Object.defineProperty @columns, "add",
value: (name, column, options = {}) ->
unless column? then throw new Error "Column data type is not defined."
if column instanceof Function then column = column()
column.name = name
column.nullable = options.nullable
@push column
Object.defineProperty @rows, "add",
value: (values...) ->
@push values
###
@private
###
_makeBulk: ->
for col in @columns
switch col.type
when TYPES.Xml then col.type = TYPES.NVarChar(MAX).type
when TYPES.UDT, TYPES.Geography, TYPES.Geometry then col.type = TYPES.VarBinary(MAX).type
@
declare: ->
"create table #{@path} (#{("[#{col.name}] #{declare col.type, col}#{if col.nullable is true then " null" else if col.nullable is false then " not null" else ""}" for col in @columns).join ', '})"
@fromRecordset: (recordset) ->
t = new @
for name, col of recordset.columns
t.columns.add name,
type: col.type
length: col.length
scale: col.scale
precision: col.precision
,
nullable: col.nullable
if t.columns.length is 1 and t.columns[0].name is JSON_COLUMN_ID
for row in recordset
t.rows.add JSON.stringify row
else
for row in recordset
t.rows.add (row[col.name] for col in t.columns)...
t
module.exports = Table