-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathExecuteRequest.ts
68 lines (54 loc) · 2.23 KB
/
ExecuteRequest.ts
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
// Copyright 2024 by Teradata Corporation. All rights reserved.
// This sample program demonstrates how to execute a SQL request and display results.
// @ts-ignore
import * as teradatasql from "teradatasql";
if (process.argv.length != 6) {
console.log("Parameters: Host User Password SQL");
process.exit(1);
}
const sHost: string = process.argv[2];
const sUser: string = process.argv[3];
const sPassword: string = process.argv[4];
const sSQL: string = process.argv[5];
type Rows = any[];
type Row = any[] | null;
let con: teradatasql.TeradataConnection = teradatasql.connect({ host: sHost, user: sUser, password: sPassword });
try {
let cur: teradatasql.TeradataCursor = con.cursor();
try {
console.log(`Executing ${sSQL}`);
cur.execute(sSQL);
for (let nResult: number = 1; ; nResult++) {
const rows: Rows = cur.fetchall();
for (let iRow: number = 0; iRow < rows.length; iRow++) {
const row: Row = rows[iRow];
for (let iColumn: number = 0; row != null && iColumn < row.length; iColumn++) {
let oValue: any = row[iColumn];
let sType: string = typeof oValue;
if (sType === "number") {
const n: number = Number(cur.description[iColumn][5]);
if (n > 0) {
oValue = oValue.toFixed(n);
}
} else if (sType === "object") {
sType = oValue.constructor.name;
}
if (sType === "Date") {
oValue = "toISOString: " + oValue.toISOString() + " toString: " + oValue.toString();
}
if (sType === "Uint8Array") {
oValue = Buffer.from(oValue).toString("hex");
}
console.log(`Result ${nResult} Row ${iRow + 1} Column ${iColumn + 1} ${cur.description[iColumn][0]} ${sType} = ${oValue}`);
} // end for iColumn
} // end for iRow
if (!cur.nextset()) {
break;
}
} // end for nResult
} finally {
cur.close();
}
} finally {
con.close();
}