This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
322 lines (287 loc) · 9.1 KB
/
index.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
'use strict';
var tableau = require('./build/Release/tableau'),
_ = require('underscore'),
moment = require('moment'),
enums = require('./enums'),
priv = {},
defaultTable = 'Extract';
/**
* Given a WDC API TableInfo object, this function will return a native Tableau
* extract table definition object.
*
* @param {{id: string, columns: Array}} def
* @returns {Object}
*/
function convertWdcTableDefToTdeApi(def) {
var tableDef = tableau.TableDefinition(),
i;
for (i = 0; i < def.columns.length; i++) {
tableDef.addColumn(def.columns[i].id, enums.wdcType(def.columns[i].dataType));
}
return tableDef;
}
/**
* Given a native Tableau extract table definition object, this function will
* return a TableInfo object according to the specifications of the WDC API.
*
* @param tableDef
* @param tableName
* @returns {{id: string, columns: Array}}
*/
function convertTdeApiTableDefToWdc(tableDef, tableName) {
var columnCount = tableDef.getColumnCount(),
template = {id: '', columns: []},
columnTemplate = {},
i;
template.id = tableName || defaultTable;
for (i = 0; i < columnCount; i++) {
columnTemplate.dataType = enums.wdcTypeName(tableDef.getColumnType(i));
columnTemplate.id = tableDef.getColumnName(i);
template.columns.push(columnTemplate);
columnTemplate = {};
}
return template;
}
/**
* Given a table and table definition, creates a map of column indexes to native
* extract setter methods. This is a performance optimization.
*/
function instantiateMethodMap(table, definition) {
// This should never change for a given table. Shortcut this relatively
// expensive process if it's already known.
if (priv[table].methodMap) {
return;
}
priv[table].methodMap = definition.columns.map(function (col, i) {
return enums.typeNameSetMethod(priv[table].nativeTableDefinition.getColumnType(i));
});
}
/**
* Helper function that instantiates and stashes a table for subsequent
* use.
*
* @param table
* @param definition
*/
function instantiateNativeTable(table, definition) {
priv[table] = priv[table] || {};
priv[table].tableName = table;
// If a definition was provided, apply the table definition to the extract.
if (definition) {
// Convert the given definition to a native table definition object.
priv[table].nativeTableDefinition = convertWdcTableDefToTdeApi(definition);
// If a table exists, open and store a reference to the existing table.
if (priv.nativeExtract.hasTable(table)) {
priv[table].nativeTable = priv.nativeExtract.openTable(table);
}
// Otherwise, create the table with the given definition.
else {
priv[table].nativeTable = priv.nativeExtract.addTable(table, priv[table].nativeTableDefinition);
}
}
else {
if (priv.nativeExtract.hasTable(table)) {
priv[table].nativeTable = priv.nativeExtract.openTable(table);
priv[table].nativeTableDefinition = priv[table].nativeTable.getTableDefinition();
definition = convertTdeApiTableDefToWdc(priv[table].nativeTableDefinition, table);
}
}
priv[table].definition = definition;
instantiateMethodMap(table, definition);
}
/**
* Sets a given value on the given tableRow for the given table, using them
* provided index. Just encapsulates row insertion logic.
* @param {String} table
*
* @param {TableRow} tableRow
* The native TableauExtract tableRow to set values on.
* @param {Integer} index
* The column number the value should be written to.
* @param {*} value
* The value to be written to the row.
*/
function smartSetValueOnTableRow(table, tableRow, index, value) {
var method = priv[table].methodMap[index],
dateTime;
// If the value is specifically null (or undefined), then set a null value.
if (value == null) {
tableRow.setNull(index);
}
// Date has a slightly more annoying argument set.
else if (method === 'setDate') {
// If the date exactly matches the format "YYYY-MM-DD", then regex it out.
if (typeof value === 'string') {
dateTime = value.match(/^(\d{4})-(\d{2})-(\d{2})$/) || moment(value);
}
// Otherwise, if the date provided is an instance of moment, use it.
else {
dateTime = value instanceof moment ? value : moment(value);
}
// If we were successfully able to parse the date via regex, set those values.
if (dateTime instanceof Array) {
tableRow[method](index,
parseInt(dateTime[1]),
parseInt(dateTime[2]),
parseInt(dateTime[3])
);
}
// Otherwise, pull the relevant values from moment.
else {
tableRow[method](index,
dateTime.get('year'),
dateTime.get('month') + 1,
dateTime.get('date')
);
}
}
// DateTime has an even more annoying argument set.
else if (method === 'setDateTime') {
if (typeof value === 'string') {
dateTime = value.match(/^(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})$/) || moment(value);
}
else {
dateTime = value instanceof moment ? value : moment(value);
}
if (dateTime instanceof Array) {
tableRow[method](index,
parseInt(dateTime[1]),
parseInt(dateTime[2]),
parseInt(dateTime[3]),
parseInt(dateTime[4]),
parseInt(dateTime[5]),
parseInt(dateTime[6]),
parseInt(dateTime[7])
);
}
else {
tableRow[method](index,
dateTime.get('year'),
dateTime.get('month') + 1,
dateTime.get('date'),
dateTime.get('hour'),
dateTime.get('minute'),
dateTime.get('second'),
dateTime.get('millisecond')
);
}
}
else {
tableRow[method](index, value);
}
}
/**
* Instantiates a new Tableau extract wrapper.
*
* @param {String} path
* @param {TableInfo} definition
* An optional table definition, in the same format as the Tableau WDC API.
* @constructor
*/
function Extract(path, definition) {
var table = definition && definition.id ? definition.id : defaultTable;
priv.path = path;
priv.nativeExtract = new tableau.Extract(priv.path);
instantiateNativeTable(table, definition);
}
/**
* Adds another table to the Tableau extract.
*
* @param {String} table
* The name of the table.
* @param {TableInfo} definition
* A table definition, in the same format as the Tableau WDC API.
*/
Extract.prototype.addTable = function (table, definition) {
instantiateNativeTable(table, definition);
};
/**
* Get the definition of the given table.
*
* @param {String} table
* Table name. If no table name is provided, "Extract" will be assumed.
*
* @returns {TableInfo}
*/
Extract.prototype.getDefinition = function (table) {
table = table || defaultTable;
// If the definition isn't already loaded, try instantiating it.
if (!priv[table] || !priv[table].definition) {
instantiateNativeTable(table);
}
return priv[table].definition;
};
/**
* Closes the extract, writing any new rows to disk and freeing resources.
*/
Extract.prototype.close = function () {
return priv.nativeExtract.close();
};
/**
* Given an array of row values (or an object, keyed by column IDs), this
* method appends the given data to the extract.
*
* @param {String} table
* @param {Array|Object} row
*/
Extract.prototype.insert = function insert(table, row) {
// Handle backward compatibility for pre-multi-table extracts.
if (row === undefined) {
row = table;
table = defaultTable;
}
var tableRow = tableau.Row(priv[table].nativeTableDefinition),
tempRow = [],
rowKey,
rowIndex,
method,
dateTime,
i;
// If the row is neither an array nor an object, we've got problems.
if (typeof row !== 'object') {
throw 'Expected row data in the form of an array or object.';
}
// If we were passed an object, iterate through the defined columns and try
// to set values according to the field name specified in the schema.
if (!(row instanceof Array)) {
for (i = 0; i < priv[table].definition.columns.length; i++) {
smartSetValueOnTableRow(table, tableRow, i, row[priv[table].definition.columns[i].id]);
}
}
// Otherwise, just iterate through the provided rows and set them.
else {
for (i = 0; i < row.length; i++) {
smartSetValueOnTableRow(table, tableRow, i, row[i]);
}
}
// Insert the row!
priv[table].nativeTable.insert(tableRow);
};
/**
* Identical to the insert method, but rather than taking a single row, it takes
* an array of row objects/arrays.
*
* @param {String} table
* @param {Array} rows
*/
Extract.prototype.insertMultiple = function insertMultiple(table, rows) {
// Handle backward compatibility for pre-multi-table extracts.
if (rows === undefined) {
rows = table;
table = defaultTable;
}
var i;
// If the row is not an array, we've got problems.
if (!(rows instanceof Array)) {
throw 'Expected an array of several rows.';
}
for (i = 0; i < rows.length; i++) {
this.insert(table, rows[i]);
}
};
// Make raw C++ APIs available for advanced use-cases / legacy usage.
Extract.dataExtract = tableau.Extract;
Extract.tableDefinition = tableau.TableDefinition;
Extract.tableRow = tableau.Row;
Extract.enums = require('./enums.js');
module.exports = Extract;