Skip to content

Commit

Permalink
[CBRD-25375] [p11_3] Enhance String handling to support multiple char…
Browse files Browse the repository at this point in the history
…acter encodings (#5232)

http://jira.cubrid.org/browse/CBRD-25375

- make a static variable, charset in ExeucteThread configuable by 'file.encoding' property.
- resive CUBRIDPacker and CUBIRDUnpacker to handle StringValue with byte array type (byte[]).
- remove charset argument of CUBIRDPacker's packValue.
  • Loading branch information
hgryoo authored May 27, 2024
1 parent 0e110f5 commit eaaa51a
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 51 deletions.
16 changes: 9 additions & 7 deletions src/jsp/com/cubrid/jsp/ExecuteThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
public class ExecuteThread extends Thread {

// TODO: get charset from DB Server
public static String charSet = "UTF-8"; // System.getProperty("file.encoding");
public static String charSet = System.getProperty("file.encoding");

private static final int REQ_CODE_INVOKE_SP = 0x01;
private static final int REQ_CODE_RESULT = 0x02;
Expand Down Expand Up @@ -102,6 +102,10 @@ public class ExecuteThread extends Thread {

packer = new CUBRIDPacker(resultBuffer);
unpacker = new CUBRIDUnpacker(readbuffer);

if (charSet == null) {
charSet = "UTF-8";
}
}

public Socket getSocket() {
Expand Down Expand Up @@ -375,9 +379,7 @@ private void returnOutArgs(StoredProcedure sp, CUBRIDPacker packer)
if (args[i].getMode() > Value.IN) {
Value v = sp.makeOutValue(args[i].getResolved());
packer.packValue(
ValueUtilities.resolveValue(args[i].getDbType(), v),
args[i].getDbType(),
this.charSet);
ValueUtilities.resolveValue(args[i].getDbType(), v), args[i].getDbType());
}
}
}
Expand All @@ -392,7 +394,7 @@ private void sendResult(Value result, StoredProcedure procedure)
resultBuffer.clear(); /* prepare to put */
packer.setBuffer(resultBuffer);

packer.packValue(resolvedResult, procedure.getReturnType(), this.charSet);
packer.packValue(resolvedResult, procedure.getReturnType());
returnOutArgs(procedure, packer);
resultBuffer = packer.getBuffer();

Expand Down Expand Up @@ -423,8 +425,8 @@ private void sendError(String exception, Socket socket) throws IOException {
resultBuffer.clear();
packer.setBuffer(resultBuffer);

packer.packValue(new Integer(1), DBType.DB_INT, this.charSet);
packer.packValue(exception, DBType.DB_STRING, this.charSet);
packer.packValue(new Integer(1), DBType.DB_INT);
packer.packValue(exception, DBType.DB_STRING);

resultBuffer = packer.getBuffer();

Expand Down
4 changes: 2 additions & 2 deletions src/jsp/com/cubrid/jsp/StoredProcedure.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public Value makeReturnValue(Object o) throws ExecuteException {
} else if (o instanceof BigDecimal) {
val = new DoubleValue(((BigDecimal) o).doubleValue());
} else if (o instanceof String) {
val = new StringValue((String) o);
val = new StringValue(((String) o).getBytes());
} else if (o instanceof java.sql.Date) {
val = new DateValue((java.sql.Date) o);
} else if (o instanceof java.sql.Time) {
Expand All @@ -343,7 +343,7 @@ public Value makeReturnValue(Object o) throws ExecuteException {
} else if (o instanceof ResultSet) {
val = new ResultSetValue((ResultSet) o);
} else if (o instanceof byte[]) {
val = new SetValue((byte[]) o);
val = new StringValue((byte[]) o);
} else if (o instanceof short[]) {
val = new SetValue((short[]) o);
} else if (o instanceof int[]) {
Expand Down
26 changes: 14 additions & 12 deletions src/jsp/com/cubrid/jsp/data/CUBRIDPacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ public void packCString(byte[] value) {
}

// TODO: legacy implementation, this function will be modified
public void packValue(Object result, int ret_type, String charset)
throws UnsupportedEncodingException {
public void packValue(Object result, int ret_type) throws UnsupportedEncodingException {
if (result == null) {
packInt(DBType.DB_NULL);
} else if (result instanceof Short) {
Expand All @@ -160,16 +159,19 @@ public void packValue(Object result, int ret_type, String charset)
packDouble(((Double) result).doubleValue());
} else if (result instanceof BigDecimal) {
packInt(DBType.DB_NUMERIC);
packString(((BigDecimal) result).toString(), charset);
packString(((BigDecimal) result).toString());
} else if (result instanceof String) {
packInt(DBType.DB_STRING);
packString((String) result, charset);
packString((String) result);
} else if (result instanceof byte[]) {
packInt(DBType.DB_STRING);
packCString((byte[]) result);
} else if (result instanceof java.sql.Date) {
packInt(DBType.DB_DATE);
packString(result.toString(), charset);
packString(result.toString());
} else if (result instanceof java.sql.Time) {
packInt(DBType.DB_TIME);
packString(result.toString(), charset);
packString(result.toString());
} else if (result instanceof java.sql.Timestamp) {
packInt(ret_type);
if (ret_type == DBType.DB_DATETIME) {
Expand All @@ -193,40 +195,40 @@ public void packValue(Object result, int ret_type, String charset)
packInt(array.length);
for (int i = 0; i < array.length; i++) {
array[i] = new Integer(((int[]) result)[i]);
packValue(array[i], ret_type, charset);
packValue(array[i], ret_type);
}
packValue(array, ret_type, charset);
packValue(array, ret_type);
} else if (result instanceof short[]) {
int length = ((short[]) result).length;
Short[] array = new Short[length];
packInt(array.length);
for (int i = 0; i < array.length; i++) {
array[i] = new Short(((short[]) result)[i]);
packValue(array, ret_type, charset);
packValue(array, ret_type);
}
} else if (result instanceof float[]) {
int length = ((float[]) result).length;
Float[] array = new Float[length];
packInt(array.length);
for (int i = 0; i < array.length; i++) {
array[i] = new Float(((float[]) result)[i]);
packValue(array[i], ret_type, charset);
packValue(array[i], ret_type);
}
} else if (result instanceof double[]) {
int length = ((double[]) result).length;
Double[] array = new Double[length];
packInt(array.length);
for (int i = 0; i < array.length; i++) {
array[i] = new Double(((double[]) result)[i]);
packValue(array[i], ret_type, charset);
packValue(array[i], ret_type);
}
} else if (result instanceof Object[]) {
packInt(ret_type);
Object[] arr = (Object[]) result;

packInt(arr.length);
for (int i = 0; i < arr.length; i++) {
packValue(arr[i], ret_type, charset);
packValue(arr[i], ret_type);
}
} else {
// FIXME: treat as NULL
Expand Down
16 changes: 11 additions & 5 deletions src/jsp/com/cubrid/jsp/data/CUBRIDUnpacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,15 @@ public String unpackCString() {

public byte[] unpackCStringByteArray() {
int len = unpackStringSize();
byte[] str = new byte[len];
buffer.get(str);
return str;
if (len > 0) {
byte[] str = new byte[len];
buffer.get(str);
align(DataUtilities.INT_ALIGNMENT);
return str;
} else {
align(DataUtilities.INT_ALIGNMENT);
return new byte[0];
}
}

public int unpackStringSize() {
Expand Down Expand Up @@ -162,7 +168,7 @@ public Value unpackValue(int paramType) throws TypeMismatchException {
break;
case DBType.DB_CHAR:
case DBType.DB_STRING:
arg = new StringValue(unpackCString());
arg = new StringValue(unpackCStringByteArray());
break;
case DBType.DB_DATE:
{
Expand Down Expand Up @@ -263,7 +269,7 @@ public Value unpackValue(int paramType, int mode, int dbType) throws TypeMismatc
break;
case DBType.DB_CHAR:
case DBType.DB_STRING:
arg = new StringValue(unpackCString(), mode, dbType);
arg = new StringValue(unpackCStringByteArray(), mode, dbType);
break;
case DBType.DB_DATE:
{
Expand Down
3 changes: 1 addition & 2 deletions src/jsp/com/cubrid/jsp/impl/SUBindParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
*/
package com.cubrid.jsp.impl;

import com.cubrid.jsp.ExecuteThread;
import com.cubrid.jsp.data.CUBRIDPacker;
import com.cubrid.jsp.data.DBType;
import com.cubrid.jsp.jdbc.CUBRIDServerSideJDBCErrorCode;
Expand Down Expand Up @@ -108,7 +107,7 @@ synchronized void pack(CUBRIDPacker packer) throws UnsupportedEncodingException
int cnt = paramMode.length;
packer.packInt(cnt);
for (int i = 0; i < cnt; i++) {
packer.packValue(values[i], types[i], ExecuteThread.charSet);
packer.packValue(values[i], types[i]);
packer.packInt((int) paramMode[i]);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/jsp/com/cubrid/jsp/impl/SUConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public void putByOID(CUBRIDOID oid, String[] attributeName, Object values[])
}

int type = DBType.getObjectDBtype(values[i]);
packer.packValue(values[i], type, "UTF-8");
packer.packValue(values[i], type);
}
} else {
packer.packInt(0);
Expand Down Expand Up @@ -303,7 +303,7 @@ protected CUBRIDUnpacker collectionCmd(
if (value != null) {
packer.packInt(1); // has value
int type = DBType.getObjectDBtype(value);
packer.packValue(value, type, "UTF-8");
packer.packValue(value, type);
} else {
packer.packInt(0); // has value
}
Expand Down
Loading

0 comments on commit eaaa51a

Please sign in to comment.