Skip to content

Commit

Permalink
Fixes #1662 [API] Database - Support for NCLOB and CLOB data types
Browse files Browse the repository at this point in the history
  • Loading branch information
ThuF committed Mar 21, 2022
1 parent 01695e9 commit d5252d4
Showing 1 changed file with 184 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,6 @@ function PreparedStatement(internalStatement) {
return this.native.executeUpdate();
};

// getMetaData
// setBigDecimal
// setBlob

this.setNull = function (index, sqlType) {
this.native.setNull(index, sqlType);
};
Expand All @@ -239,19 +235,30 @@ function PreparedStatement(internalStatement) {
}
};

this.setBlob = function (index, value) {
if (value !== null && value !== undefined) {
let blob = createBlobValue(this.native, value);
this.native.setBlob(index, blob);
} else {
this.setNull(index, SQLTypes.BLOB);
}
};

this.setClob = function (index, value) {
if (value !== null && value !== undefined) {
this.native.setClob(index, value);
let clob = createClobValue(this.native, value);
this.native.setClob(index, clob);
} else {
this.setNull(index, SQLTypes.CLOB);
}
};

this.setBlob = function (index, value) {
this.setNClob = function (index, value) {
if (value !== null && value !== undefined) {
this.native.setBlob(index, value);
let nclob = createNClobValue(this.native, value);
this.native.setNClob(index, nclob);
} else {
this.setNull(index, SQLTypes.BLOB);
this.setNull(parameter, SQLTypes.NCLOB);
}
};

Expand Down Expand Up @@ -348,6 +355,22 @@ function PreparedStatement(internalStatement) {
}
};

this.setBigDecimal = function (index, value) {
if (value !== null && value !== undefined) {
this.native.setBigDecimal(index, value);
} else {
this.setNull(parameter, SQLTypes.DECIMAL);
}
};

this.setNString = function (index, value) {
if (value !== null && value !== undefined) {
this.native.setNString(index, value);
} else {
this.setNull(parameter, SQLTypes.NVARCHAR);
}
};

this.execute = function () {
return this.native.execute();
};
Expand Down Expand Up @@ -379,15 +402,6 @@ function PreparedStatement(internalStatement) {
this.isClosed = function () {
return this.native.isClosed();
};
this.setDecimal = function (index, value) {
this.native.setBigDecimal(index, value);
};
this.setNClob = function (index, value) {
this.native.setNClob(index, value);
};
this.setNString = function (index, value) {
this.native.setNString(index, value);
};
}

function CallableStatement() {
Expand Down Expand Up @@ -456,10 +470,6 @@ function CallableStatement() {
return this.native.getDouble(parameter);
};

this.getBytes = function (parameter) {
return this.native.getBytes(parameter);
};

this.getDate = function (parameter) {
return this.native.getDate(parameter);
};
Expand All @@ -484,16 +494,30 @@ function CallableStatement() {
return this.native.getRef(parameter);
};

this.getBytes = function (parameter) {
let data = this.native.getBytes(parameter);
return bytes.toJavaScriptBytes(data);
};

this.getBytesNative = function (parameter) {
return this.native.getBytes(parameter);
};

this.getBlob = function (parameter) {
return this.native.getBlob(parameter);
let data = readBlobValue(this.native.getBlob(parameter));
return bytes.toJavaScriptBytes(data);
};

this.getBlobNative = function (parameter) {
return readBlobValue(this.native.getBlob(parameter));
};

this.getClob = function (parameter) {
return this.native.getClob(parameter);
return readClobValue(this.native.getClob(parameter));
};

this.getNClob = function (parameter) {
return this.native.getNClob(parameter);
return readNClobValue(this.native.getNClob(parameter));
};

this.getNString = function (parameter) {
Expand Down Expand Up @@ -681,23 +705,26 @@ function CallableStatement() {

this.setBlob = function (parameter, value) {
if (value !== null && value !== undefined) {
this.native.setBlob(parameter, value);
let blob = createBlobValue(this.native, value);
this.native.setBlob(parameter, blob);
} else {
this.setNull(parameter, SQLTypes.BLOB);
}
};

this.setClob = function (parameter, value) {
if (value !== null && value !== undefined) {
this.native.setClob(parameter, value);
let clob = createClobValue(this.native, value);
this.native.setClob(parameter, clob);
} else {
this.setNull(parameter, SQLTypes.CLOB);
}
};

this.setNClob = function (parameter, value) {
if (value !== null && value !== undefined) {
this.native.setNClob(parameter, value);
let nclob = createNClobValue(this.native, value);
this.native.setNClob(parameter, nclob);
} else {
this.setNull(parameter, SQLTypes.NCLOB);
}
Expand Down Expand Up @@ -741,10 +768,6 @@ function ResultSet(internalResultset) {
this.native.close();
};

this.getBlob = function (identifier) {
return this.native.getBlob(identifier);
};

this.getBigDecimal = function (identifier) {
return this.native.getBigDecimal(identifier);
};
Expand All @@ -757,19 +780,32 @@ function ResultSet(internalResultset) {
return this.native.getByte(identifier);
};

this.getBytes = function (identifier) {
let data = this.native.getBytes(identifier);
return bytes.toJavaScriptBytes(data);
};

this.getBytesNative = function (identifier) {
return this.native.getBytes(identifier);
};

this.getBytes = function (identifier) {
var data = this.native.getBytes(identifier);
this.getBlob = function (identifier) {
let data = readBlobValue(this.native.getBlob(identifier));
return bytes.toJavaScriptBytes(data);
};

this.getBlobNative = function (identifier) {
return readBlobValue(this.native.getBlob(identifier));
};

this.getClob = function (identifier) {
return this.native.getClob(identifier);
return readClobValue(this.native.getClob(identifier));
};

this.getNClob = function (columnIndex) {
return readNClobValue(this.native.getNClob(columnIndex));
}

this.getDate = function (identifier) {
const dateInstance = this.native.getDate(identifier);
return dateInstance !== null && dateInstance !== undefined ? new Date(dateInstance.getTime()) : null;
Expand Down Expand Up @@ -837,11 +873,121 @@ function ResultSet(internalResultset) {
return this.native.getMetaData();
}

this.getNClob = function (columnIndex) {
return this.native.getNClob(columnIndex);
}

this.getNString = function (columnIndex) {
return this.native.getNString(columnIndex);
}
}

function isHanaDatabase(connection) {
let isHanaDatabase = false;
let metadata = connection.getMetaData();
if (metadata !== null && metadata !== undefined) {
isHanaDatabase = metadata.getDatabaseProductName() === "HDB";
}
return isHanaDatabase;
}

function readBlobValue(value) {
return value.getBytes(1, value.length());
}

function createBlobValue(native, value) {
try {
let connection = native.getConnection();
if (connection === null || connection === undefined) {
throw new Error("Can't create new 'Blob' value as the connection is null");
}
let blob = null;
if (isHanaDatabase(connection)) {
let ps = null;
try {
ps = connection.prepareStatement("SELECT TO_BLOB (?) FROM DUMMY;");
ps.setBytes(1, value);
let rs = ps.executeQuery();
if (rs.next()) {
blob = rs.getBlob(1);
}
} finally {
if (ps !== null && ps !== undefined) {
ps.close();
}
}
} else {
blob = connection.createBlob();
blob.setBytes(1, value);
}
return blob;
} catch (e) {
throw new Error(`Error occured during creation of 'Clob' value: ${e.message}`);
}
}

function readClobValue(value) {
return value.getSubString(1, value.length());
}

function createClobValue(native, value) {
try {
let connection = native.getConnection();
if (connection === null || connection === undefined) {
throw new Error("Can't create new 'Clob' value as the connection is null");
}
let clob = null;
if (isHanaDatabase(connection)) {
let ps = null;
try {
ps = connection.prepareStatement("SELECT TO_CLOB (?) FROM DUMMY;");
ps.setString(1, value);
let rs = ps.executeQuery();
if (rs.next()) {
clob = rs.getClob(1);
}
} finally {
if (ps !== null && ps !== undefined) {
ps.close();
}
}
} else {
clob = connection.createClob();
clob.setString(1, value);
}
return clob;
} catch (e) {
throw new Error(`Error occured during creation of 'Clob' value: ${e.message}`);
}
}

function readNClobValue(value) {
return value.getSubString(1, value.length());
}

function createNClobValue(native, value) {
try {
let connection = native.getConnection();
if (connection === null || connection === undefined) {
throw new Error("Can't create new 'NClob' value as the connection is null");
}
let nclob = null;
if (isHanaDatabase(connection)) {
let ps = null;
try {
ps = connection.prepareStatement("SELECT TO_NCLOB (?) FROM DUMMY;");
ps.setString(1, value);
let rs = ps.executeQuery();
if (rs.next()) {
nclob = rs.getNClob(1);
}
} finally {
if (ps !== null && ps !== undefined) {
ps.close();
}
}
} else {
nclob = connection.createNClob();
nclob.setString(1, value);
}
return nclob;
} catch (e) {
throw new Error(`Error occured during creation of 'NClob' value: ${e.message}`);
}
}

0 comments on commit d5252d4

Please sign in to comment.