teradatasql
Version:
Teradata SQL Driver for Node.js
109 lines • 4.97 kB
JavaScript
;
// Copyright 2023 by Teradata Corporation. All rights reserved.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
// This sample program demonstrates how to insert BLOB and CLOB values.
// @ts-ignore
const teradatasql = __importStar(require("teradatasql"));
function byte(s, encoding) {
return Uint8Array.from(Buffer.from(s, encoding));
}
function FormatValue(oValue) {
let s;
if (typeof oValue === "string") {
s = oValue;
if (s.length > 20) {
s = s.slice(0, 20) + " ...";
}
return s + ` (len=${oValue.length})`;
}
else if (oValue instanceof Uint8Array) {
s = Buffer.from(oValue).toString();
if (s.length > 20) {
s = s.slice(0, 20) + " ...";
}
return `byte('${s}')` + ` (len=${oValue.length})`;
}
else {
return oValue;
}
}
let con = teradatasql.connect({ host: "whomooz", user: "guest", password: "please", log: "8" });
try {
let cur = con.cursor();
try {
let sSQL = "create volatile table voltab (c1 integer, c2 blob, c3 clob) on commit preserve rows";
console.log(sSQL);
cur.execute(sSQL);
const abySmallBlob = byte("abc"); // bytes value with len=3
const abyLargeBlob = byte("ABC".repeat(75000)); // bytes value with len=75000
const sSmallClob = "xyz"; // str value with len=3
const sLargeClob = "XYZ".repeat(25000); // str value with len=75000
sSQL = "insert into voltab values (?, ?, ?)";
console.log(sSQL);
// Small LOB values <= 64K are inserted as inline values, contained in a single request message sent to the database.
cur.execute(sSQL, [1, abySmallBlob, sSmallClob]);
// TIMING log lines show a single send/receive message round trip:
// GOSQL-TIMING NetworkIO.go:426 Receive header Start Response message took 24 ms
// GOSQL-TIMING NetworkIO.go:464 Receive Start Response message body took 0 ms, send and receive took 56 ms
// GOSIDE-TIMING goside.go:789 createRows call to QueryContext took 56 ms
console.log(sSQL);
// Large LOB values > 64K are inserted as deferred values, and require multiple message round trips to the database.
cur.execute(sSQL, [2, abyLargeBlob, sLargeClob]);
// TIMING log lines show multiple send/receive message round trips:
// GOSQL-TIMING NetworkIO.go:426 Receive header Elicit Request message took 9 ms
// GOSQL-TIMING NetworkIO.go:464 Receive Elicit Request message body took 0 ms, send and receive took 22 ms
// GOSQL-TIMING NetworkIO.go:426 Receive header Elicit Request message took 38 ms
// GOSQL-TIMING NetworkIO.go:464 Receive Elicit Request message body took 0 ms, send and receive took 54 ms
// GOSQL-TIMING NetworkIO.go:426 Receive header Start Response message took 27 ms
// GOSQL-TIMING NetworkIO.go:464 Receive Start Response message body took 0 ms, send and receive took 29 ms
// GOSIDE-TIMING goside.go:789 createRows call to QueryContext took 115 ms
sSQL = "select * from voltab order by 1";
console.log(sSQL);
cur.execute(sSQL);
let nRow = 0;
let row = null;
while (true) {
row = cur.fetchone();
if (!row) {
break;
}
nRow += 1;
if (cur.description) {
for (let iColumn = 0; iColumn < row.length; iColumn++) {
console.log(`Row ${nRow} Column ${iColumn + 1} "${cur.description[iColumn][0]}" = ${FormatValue(row[iColumn])}`);
}
}
}
console.log(`Fetched ${nRow} rows`);
}
finally {
cur.close();
}
}
finally {
con.close();
}
//# sourceMappingURL=InsertLob.js.map