-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathInsertXML.ts
57 lines (46 loc) · 1.91 KB
/
InsertXML.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
// Copyright 2023 by Teradata Corporation. All rights reserved.
// This sample program demonstrates how to insert and retrieve XML values.
// Use the escape function teradata_parameter to override the data type for a parameter marker bind value.
// @ts-ignore
import * as teradatasql from "teradatasql";
type Row = any[] | null;
type Rows = any[];
const con: teradatasql.TeradataConnection = teradatasql.connect({ host: "whomooz", user: "guest", password: "please" });
try {
const cur: teradatasql.TeradataCursor = con.cursor();
try {
cur.execute("create volatile table voltab (c1 integer, c2 xml) on commit preserve rows");
cur.execute("{fn teradata_parameter(2,XML)}insert into voltab values (?, ?)", [
[1, "<hello>world</hello>"],
[2, "<hello>moon</hello>"],
]);
cur.execute("{fn teradata_fake_result_sets}select * from voltab order by 1");
// obtain column metadata from the fake result set
const asTypeNames: string[] = [];
let row: Row = cur.fetchone();
if (row) {
const json: any = JSON.parse(row[7].toString());
for (const elem of json) {
for (const k in elem) {
if (k === "TypeName") {
asTypeNames.push(elem[k]);
}
}
}
}
cur.nextset(); // advance to the real result set
const rows: Rows = cur.fetchall();
for (let nRow: number = 0; nRow < rows.length; nRow++) {
row = rows[nRow];
if (row && cur.description) {
for (let iColumn: number = 0; iColumn < row.length; iColumn++) {
console.log(`Row ${nRow + 1} Column ${cur.description[iColumn][0]} ${asTypeNames[iColumn].padEnd(7)} ${row[iColumn]}`);
}
}
}
} finally {
cur.close();
}
} finally {
con.close();
}