未验证 提交 9502313a 编写于 作者: L Liang Zhang 提交者: GitHub

Fixes #4644 (#4646)

* Add alias for rewrited encrypt projection if absent

* fix test cases

* add test case for mix rewrite

* remove mapping for cipher column and logic column

* refactor EncryptResultSetMetaData.getColumnLabel

* refactor EncryptResultSetMetaData.getColumnLabel
上级 7f7267b5
......@@ -81,6 +81,9 @@ public final class EncryptProjectionTokenGenerator extends BaseEncryptSQLTokenGe
private SubstitutableColumnNameToken generateSQLToken(final ColumnProjectionSegment segment, final String tableName) {
String encryptColumnName = getEncryptColumnName(tableName, segment.getColumn().getIdentifier().getValue());
if (!segment.getAlias().isPresent()) {
encryptColumnName += " AS " + segment.getColumn().getIdentifier().getValue();
}
return segment.getColumn().getOwner().isPresent() ? new SubstitutableColumnNameToken(segment.getColumn().getOwner().get().getStopIndex() + 2, segment.getStopIndex(), encryptColumnName)
: new SubstitutableColumnNameToken(segment.getStartIndex(), segment.getStopIndex(), encryptColumnName);
}
......
......@@ -19,44 +19,44 @@
<rewrite-assertions yaml-rule="yaml/encrypt/query-with-cipher.yaml">
<rewrite-assertion id="select_for_parameters">
<input sql="SELECT account_id, password, amount AS a, status AS s FROM t_account WHERE account_id = ? AND password = ? AND amount = ? AND status = ?" parameters="1, aaa, 1000, OK" />
<output sql="SELECT account_id, cipher_password, cipher_amount AS a, status AS s FROM t_account WHERE account_id = ? AND assisted_query_password = ? AND cipher_amount = ? AND status = ?" parameters="1, assisted_query_aaa, encrypt_1000, OK" />
<output sql="SELECT account_id, cipher_password AS password, cipher_amount AS a, status AS s FROM t_account WHERE account_id = ? AND assisted_query_password = ? AND cipher_amount = ? AND status = ?" parameters="1, assisted_query_aaa, encrypt_1000, OK" />
</rewrite-assertion>
<rewrite-assertion id="select_for_literals">
<input sql="SELECT account_id, password, amount AS a, status AS s FROM t_account WHERE account_id = 1 AND password = 'aaa' AND amount = 1000 AND status = 'OK'" />
<output sql="SELECT account_id, cipher_password, cipher_amount AS a, status AS s FROM t_account WHERE account_id = 1 AND assisted_query_password = 'assisted_query_aaa' AND cipher_amount = 'encrypt_1000' AND status = 'OK'" />
<output sql="SELECT account_id, cipher_password AS password, cipher_amount AS a, status AS s FROM t_account WHERE account_id = 1 AND assisted_query_password = 'assisted_query_aaa' AND cipher_amount = 'encrypt_1000' AND status = 'OK'" />
</rewrite-assertion>
<rewrite-assertion id="select_plain_for_parameters">
<input sql="SELECT a.account_id, a.password, a.amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.account_id = ? AND a.password = ? AND a.amount = ? AND a.status = ?" parameters="1, aaa, 1000, OK" />
<output sql="SELECT a.account_id, a.cipher_password, a.cipher_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.account_id = ? AND a.assisted_query_password = ? AND a.cipher_amount = ? AND a.status = ?" parameters="1, assisted_query_aaa, encrypt_1000, OK" />
<output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.account_id = ? AND a.assisted_query_password = ? AND a.cipher_amount = ? AND a.status = ?" parameters="1, assisted_query_aaa, encrypt_1000, OK" />
</rewrite-assertion>
<rewrite-assertion id="select_plain_for_literals">
<input sql="SELECT a.account_id, a.password, a.amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.account_id = 1 AND a.password = 'aaa' AND a.amount = 1000 AND a.status = 'OK'" />
<output sql="SELECT a.account_id, a.cipher_password, a.cipher_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.account_id = 1 AND a.assisted_query_password = 'assisted_query_aaa' AND a.cipher_amount = 'encrypt_1000' AND a.status = 'OK'" />
<output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.account_id = 1 AND a.assisted_query_password = 'assisted_query_aaa' AND a.cipher_amount = 'encrypt_1000' AND a.status = 'OK'" />
</rewrite-assertion>
<rewrite-assertion id="select_plain_for_parameters_with_in_has_no_left_space">
<input sql="SELECT a.account_id, a.password, a.amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.password in(?, ?) AND a.amount in (?, ?)" parameters="aaa, aaa, 1000, 1000" />
<output sql="SELECT a.account_id, a.cipher_password, a.cipher_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.assisted_query_password in(?, ?) AND a.cipher_amount in (?, ?)" parameters="assisted_query_aaa, assisted_query_aaa, encrypt_1000, encrypt_1000" />
<output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.assisted_query_password in(?, ?) AND a.cipher_amount in (?, ?)" parameters="assisted_query_aaa, assisted_query_aaa, encrypt_1000, encrypt_1000" />
</rewrite-assertion>
<rewrite-assertion id="select_plain_for_parameters_with_in_has_no_left_space_and_parameter_has_left_space">
<input sql="SELECT a.account_id, a.password, a.amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.password in( ?, ?) AND a.amount in (?, ?)" parameters="aaa, aaa, 1000, 1000" />
<output sql="SELECT a.account_id, a.cipher_password, a.cipher_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.assisted_query_password in(?, ?) AND a.cipher_amount in (?, ?)" parameters="assisted_query_aaa, assisted_query_aaa, encrypt_1000, encrypt_1000" />
<output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.assisted_query_password in(?, ?) AND a.cipher_amount in (?, ?)" parameters="assisted_query_aaa, assisted_query_aaa, encrypt_1000, encrypt_1000" />
</rewrite-assertion>
<rewrite-assertion id="select_plain_for_parameters_with_in_has_no_left_space_and_parameter_has_left_newline">
<input sql="SELECT a.account_id, a.password, a.amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.password in(
?, ?) AND a.amount in (?, ?)" parameters="aaa, aaa, 1000, 1000" />
<output sql="SELECT a.account_id, a.cipher_password, a.cipher_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.assisted_query_password in(?, ?) AND a.cipher_amount in (?, ?)" parameters="assisted_query_aaa, assisted_query_aaa, encrypt_1000, encrypt_1000" />
<output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.assisted_query_password in(?, ?) AND a.cipher_amount in (?, ?)" parameters="assisted_query_aaa, assisted_query_aaa, encrypt_1000, encrypt_1000" />
</rewrite-assertion>
<rewrite-assertion id="select_plain_for_parameters_with_in_has_more_than_one_left_space_and_parameter_has_left_newline">
<input sql="SELECT a.account_id, a.password, a.amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.password in (
?, ?) AND a.amount in (?, ?)" parameters="aaa, aaa, 1000, 1000" />
<output sql="SELECT a.account_id, a.cipher_password, a.cipher_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.assisted_query_password in (?, ?) AND a.cipher_amount in (?, ?)" parameters="assisted_query_aaa, assisted_query_aaa, encrypt_1000, encrypt_1000" />
<output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.assisted_query_password in (?, ?) AND a.cipher_amount in (?, ?)" parameters="assisted_query_aaa, assisted_query_aaa, encrypt_1000, encrypt_1000" />
</rewrite-assertion>
<rewrite-assertion id="select_with_unqualified_shorthand">
......
......@@ -19,12 +19,12 @@
<rewrite-assertions yaml-rule="yaml/encrypt/query-with-plain.yaml">
<rewrite-assertion id="select_plain_for_parameters">
<input sql="SELECT a.account_id, a.password, a.amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.account_id = ? AND a.password = ? AND a.amount = ? AND a.status = ?" parameters="1, aaa, 1000, OK" />
<output sql="SELECT a.account_id, a.plain_password, a.plain_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.account_id = ? AND a.plain_password = ? AND a.plain_amount = ? AND a.status = ?" parameters="1, aaa, 1000, OK" />
<output sql="SELECT a.account_id, a.plain_password AS password, a.plain_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.account_id = ? AND a.plain_password = ? AND a.plain_amount = ? AND a.status = ?" parameters="1, aaa, 1000, OK" />
</rewrite-assertion>
<rewrite-assertion id="select_plain_for_literals">
<input sql="SELECT a.account_id, a.password, a.amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.account_id = 1 AND a.password = 'aaa' AND a.amount = 1000 AND a.status = 'OK'" />
<output sql="SELECT a.account_id, a.plain_password, a.plain_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.account_id = 1 AND a.plain_password = 'aaa' AND a.plain_amount = 1000 AND a.status = 'OK'" />
<output sql="SELECT a.account_id, a.plain_password AS password, a.plain_amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.account_id = 1 AND a.plain_password = 'aaa' AND a.plain_amount = 1000 AND a.status = 'OK'" />
</rewrite-assertion>
<rewrite-assertion id="select_with_unqualified_shorthand">
......
......@@ -19,26 +19,44 @@
<rewrite-assertions yaml-rule="yaml/mix/query-with-cipher.yaml">
<rewrite-assertion id="select_for_parameters">
<input sql="SELECT account_id, password, amount AS a, status AS s FROM t_account WHERE account_id = ? AND password = ? AND amount = ? AND status = ?" parameters="1, aaa, 1000, OK" />
<output sql="SELECT account_id, cipher_password, cipher_amount AS a, status AS s FROM t_account_1 WHERE account_id = ? AND assisted_query_password = ? AND cipher_amount = ? AND status = ?" parameters="1, assisted_query_aaa, encrypt_1000, OK" />
<output sql="SELECT account_id, cipher_password AS password, cipher_amount AS a, status AS s FROM t_account_1 WHERE account_id = ? AND assisted_query_password = ? AND cipher_amount = ? AND status = ?" parameters="1, assisted_query_aaa, encrypt_1000, OK" />
</rewrite-assertion>
<rewrite-assertion id="select_for_literals">
<input sql="SELECT account_id, password, amount AS a, status AS s FROM t_account WHERE account_id = 1 AND password = 'aaa' AND amount = 1000 AND status = 'OK'" />
<output sql="SELECT account_id, cipher_password, cipher_amount AS a, status AS s FROM t_account_1 WHERE account_id = 1 AND assisted_query_password = 'assisted_query_aaa' AND cipher_amount = 'encrypt_1000' AND status = 'OK'" />
<output sql="SELECT account_id, cipher_password AS password, cipher_amount AS a, status AS s FROM t_account_1 WHERE account_id = 1 AND assisted_query_password = 'assisted_query_aaa' AND cipher_amount = 'encrypt_1000' AND status = 'OK'" />
</rewrite-assertion>
<rewrite-assertion id="select_plain_for_parameters">
<input sql="SELECT a.account_id, a.password, a.amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.account_id = ? AND a.password = ? AND a.amount = ? AND a.status = ?" parameters="1, aaa, 1000, OK" />
<output sql="SELECT a.account_id, a.cipher_password, a.cipher_amount AS a, a.status AS s FROM t_account_bak_1 AS a WHERE a.account_id = ? AND a.assisted_query_password = ? AND a.cipher_amount = ? AND a.status = ?" parameters="1, assisted_query_aaa, encrypt_1000, OK" />
<output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS a, a.status AS s FROM t_account_bak_1 AS a WHERE a.account_id = ? AND a.assisted_query_password = ? AND a.cipher_amount = ? AND a.status = ?" parameters="1, assisted_query_aaa, encrypt_1000, OK" />
</rewrite-assertion>
<rewrite-assertion id="select_plain_for_literals">
<input sql="SELECT a.account_id, a.password, a.amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.account_id = 1 AND a.password = 'aaa' AND a.amount = 1000 AND a.status = 'OK'" />
<output sql="SELECT a.account_id, a.cipher_password, a.cipher_amount AS a, a.status AS s FROM t_account_bak_1 AS a WHERE a.account_id = 1 AND a.assisted_query_password = 'assisted_query_aaa' AND a.cipher_amount = 'encrypt_1000' AND a.status = 'OK'" />
<output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS a, a.status AS s FROM t_account_bak_1 AS a WHERE a.account_id = 1 AND a.assisted_query_password = 'assisted_query_aaa' AND a.cipher_amount = 'encrypt_1000' AND a.status = 'OK'" />
</rewrite-assertion>
<rewrite-assertion id="select_with_table_as_qualified_name">
<input sql="SELECT t_account.account_id, t_account.password, t_account.amount AS a, status AS s FROM t_account WHERE t_account.account_id = 1 AND t_account.password = 'aaa' AND t_account.amount = 1000 AND t_account.status = 'OK'" />
<output sql="SELECT t_account_1.account_id, t_account_1.cipher_password, t_account_1.cipher_amount AS a, status AS s FROM t_account_1 WHERE t_account_1.account_id = 1 AND t_account_1.assisted_query_password = 'assisted_query_aaa' AND t_account_1.cipher_amount = 'encrypt_1000' AND t_account_1.status = 'OK'" />
<output sql="SELECT t_account_1.account_id, t_account_1.cipher_password AS password, t_account_1.cipher_amount AS a, status AS s FROM t_account_1 WHERE t_account_1.account_id = 1 AND t_account_1.assisted_query_password = 'assisted_query_aaa' AND t_account_1.cipher_amount = 'encrypt_1000' AND t_account_1.status = 'OK'" />
</rewrite-assertion>
<rewrite-assertion id="select_with_unqualified_shorthand">
<input sql="SELECT * FROM t_account" />
<output sql="SELECT account_id, cipher_password AS password, cipher_amount AS amount, status FROM t_account_0" />
<output sql="SELECT account_id, cipher_password AS password, cipher_amount AS amount, status FROM t_account_1" />
</rewrite-assertion>
<rewrite-assertion id="select_with_qualified_shorthand">
<input sql="SELECT a.* FROM t_account a" />
<output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS amount, a.status FROM t_account_0 a" />
<output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS amount, a.status FROM t_account_1 a" />
</rewrite-assertion>
<rewrite-assertion id="select_with_mix_qualified_shorthand_and_other_projection">
<input sql="SELECT a.*, account_id, 1+1 FROM t_account a" />
<output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS amount, a.status, account_id, 1+1 FROM t_account_0 a" />
<output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS amount, a.status, account_id, 1+1 FROM t_account_1 a" />
</rewrite-assertion>
</rewrite-assertions>
......@@ -19,11 +19,29 @@
<rewrite-assertions yaml-rule="yaml/mix/query-with-plain.yaml">
<rewrite-assertion id="select_plain_for_parameters">
<input sql="SELECT a.account_id, a.password, a.amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.account_id = ? AND a.password = ? AND a.amount = ? AND a.status = ?" parameters="1, aaa, 1000, OK" />
<output sql="SELECT a.account_id, a.plain_password, a.plain_amount AS a, a.status AS s FROM t_account_bak_1 AS a WHERE a.account_id = ? AND a.plain_password = ? AND a.plain_amount = ? AND a.status = ?" parameters="1, aaa, 1000, OK" />
<output sql="SELECT a.account_id, a.plain_password AS password, a.plain_amount AS a, a.status AS s FROM t_account_bak_1 AS a WHERE a.account_id = ? AND a.plain_password = ? AND a.plain_amount = ? AND a.status = ?" parameters="1, aaa, 1000, OK" />
</rewrite-assertion>
<rewrite-assertion id="select_plain_for_literals">
<input sql="SELECT a.account_id, a.password, a.amount AS a, a.status AS s FROM t_account_bak AS a WHERE a.account_id = 1 AND a.password = 'aaa' AND a.amount = 1000 AND a.status = 'OK'" />
<output sql="SELECT a.account_id, a.plain_password, a.plain_amount AS a, a.status AS s FROM t_account_bak_1 AS a WHERE a.account_id = 1 AND a.plain_password = 'aaa' AND a.plain_amount = 1000 AND a.status = 'OK'" />
<output sql="SELECT a.account_id, a.plain_password AS password, a.plain_amount AS a, a.status AS s FROM t_account_bak_1 AS a WHERE a.account_id = 1 AND a.plain_password = 'aaa' AND a.plain_amount = 1000 AND a.status = 'OK'" />
</rewrite-assertion>
<rewrite-assertion id="select_with_unqualified_shorthand">
<input sql="SELECT * FROM t_account_bak" />
<output sql="SELECT account_id, plain_password AS password, plain_amount AS amount, status FROM t_account_bak_0" />
<output sql="SELECT account_id, plain_password AS password, plain_amount AS amount, status FROM t_account_bak_1" />
</rewrite-assertion>
<rewrite-assertion id="select_with_qualified_shorthand">
<input sql="SELECT a.* FROM t_account_bak a" />
<output sql="SELECT a.account_id, a.plain_password AS password, a.plain_amount AS amount, a.status FROM t_account_bak_0 a" />
<output sql="SELECT a.account_id, a.plain_password AS password, a.plain_amount AS amount, a.status FROM t_account_bak_1 a" />
</rewrite-assertion>
<rewrite-assertion id="select_with_mix_qualified_shorthand_and_other_projection">
<input sql="SELECT a.*, account_id, 1+1 FROM t_account_bak a" />
<output sql="SELECT a.account_id, a.plain_password AS password, a.plain_amount AS amount, a.status, account_id, 1+1 FROM t_account_bak_0 a" />
<output sql="SELECT a.account_id, a.plain_password AS password, a.plain_amount AS amount, a.status, account_id, 1+1 FROM t_account_bak_1 a" />
</rewrite-assertion>
</rewrite-assertions>
......@@ -135,7 +135,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public boolean getBoolean(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (boolean) ResultSetUtil.convertValue(mergedResult.getValue(columnIndex, boolean.class), boolean.class);
}
......@@ -146,7 +146,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public byte getByte(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (byte) ResultSetUtil.convertValue(mergedResult.getValue(columnIndex, byte.class), byte.class);
}
......@@ -157,7 +157,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public short getShort(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (short) ResultSetUtil.convertValue(mergedResult.getValue(columnIndex, short.class), short.class);
}
......@@ -168,7 +168,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public int getInt(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (int) ResultSetUtil.convertValue(mergedResult.getValue(columnIndex, int.class), int.class);
}
......@@ -179,7 +179,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public long getLong(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (long) ResultSetUtil.convertValue(mergedResult.getValue(columnIndex, long.class), long.class);
}
......@@ -190,7 +190,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public float getFloat(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (float) ResultSetUtil.convertValue(mergedResult.getValue(columnIndex, float.class), float.class);
}
......@@ -201,7 +201,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public double getDouble(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (double) ResultSetUtil.convertValue(mergedResult.getValue(columnIndex, double.class), double.class);
}
......@@ -212,7 +212,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public String getString(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (String) ResultSetUtil.convertValue(mergedResult.getValue(columnIndex, String.class), String.class);
}
......@@ -223,7 +223,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public BigDecimal getBigDecimal(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (BigDecimal) ResultSetUtil.convertValue(mergedResult.getValue(columnIndex, BigDecimal.class), BigDecimal.class);
}
......@@ -236,7 +236,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@SuppressWarnings("deprecation")
@Override
public BigDecimal getBigDecimal(final String columnLabel, final int scale) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (BigDecimal) ResultSetUtil.convertValue(mergedResult.getValue(columnIndex, BigDecimal.class), BigDecimal.class);
}
......@@ -247,7 +247,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public byte[] getBytes(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (byte[]) ResultSetUtil.convertValue(mergedResult.getValue(columnIndex, byte[].class), byte[].class);
}
......@@ -258,7 +258,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public Date getDate(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (Date) ResultSetUtil.convertValue(mergedResult.getValue(columnIndex, Date.class), Date.class);
}
......@@ -269,7 +269,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public Date getDate(final String columnLabel, final Calendar cal) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (Date) ResultSetUtil.convertValue(mergedResult.getCalendarValue(columnIndex, Date.class, cal), Date.class);
}
......@@ -280,7 +280,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public Time getTime(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (Time) ResultSetUtil.convertValue(mergedResult.getValue(columnIndex, Time.class), Time.class);
}
......@@ -291,7 +291,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public Time getTime(final String columnLabel, final Calendar cal) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (Time) ResultSetUtil.convertValue(mergedResult.getCalendarValue(columnIndex, Time.class, cal), Time.class);
}
......@@ -302,7 +302,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public Timestamp getTimestamp(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (Timestamp) ResultSetUtil.convertValue(mergedResult.getValue(columnIndex, Timestamp.class), Timestamp.class);
}
......@@ -313,7 +313,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public Timestamp getTimestamp(final String columnLabel, final Calendar cal) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (Timestamp) ResultSetUtil.convertValue(mergedResult.getCalendarValue(columnIndex, Timestamp.class, cal), Timestamp.class);
}
......@@ -324,7 +324,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public InputStream getAsciiStream(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return mergedResult.getInputStream(columnIndex, "Ascii");
}
......@@ -337,7 +337,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@SuppressWarnings("deprecation")
@Override
public InputStream getUnicodeStream(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return mergedResult.getInputStream(columnIndex, "Unicode");
}
......@@ -348,7 +348,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public InputStream getBinaryStream(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return mergedResult.getInputStream(columnIndex, "Binary");
}
......@@ -359,7 +359,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public Reader getCharacterStream(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (Reader) mergedResult.getValue(columnIndex, Reader.class);
}
......@@ -370,7 +370,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public Blob getBlob(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (Blob) mergedResult.getValue(columnIndex, Blob.class);
}
......@@ -381,7 +381,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public Clob getClob(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (Clob) mergedResult.getValue(columnIndex, Clob.class);
}
......@@ -392,7 +392,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public URL getURL(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (URL) mergedResult.getValue(columnIndex, URL.class);
}
......@@ -403,7 +403,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public SQLXML getSQLXML(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return (SQLXML) mergedResult.getValue(columnIndex, SQLXML.class);
}
......@@ -414,7 +414,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public Object getObject(final String columnLabel) throws SQLException {
int columnIndex = columnLabelAndIndexMap.get(getActualColumnLabel(columnLabel));
int columnIndex = columnLabelAndIndexMap.get(columnLabel);
return mergedResult.getValue(columnIndex, Object.class);
}
......@@ -425,11 +425,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe
@Override
public int findColumn(final String columnLabel) throws SQLException {
return originalResultSet.findColumn(getActualColumnLabel(columnLabel));
}
private String getActualColumnLabel(final String columnLabel) {
return logicAndActualColumns.keySet().contains(columnLabel) ? logicAndActualColumns.get(columnLabel) : columnLabel;
return originalResultSet.findColumn(columnLabel);
}
@Override
......
......@@ -17,6 +17,7 @@
package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.underlying.common.constant.ShardingConstant;
import org.apache.shardingsphere.sql.parser.relation.statement.SQLStatementContext;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
......@@ -32,6 +33,7 @@ import java.util.Map.Entry;
/**
* Encrypt result set meta data.
*/
@RequiredArgsConstructor
public final class EncryptResultSetMetaData extends WrapperAdapter implements ResultSetMetaData {
private final ResultSetMetaData resultSetMetaData;
......@@ -42,14 +44,6 @@ public final class EncryptResultSetMetaData extends WrapperAdapter implements Re
private final Map<String, String> logicAndActualColumns;
public EncryptResultSetMetaData(final ResultSetMetaData resultSetMetaData,
final EncryptRule encryptRule, final SQLStatementContext sqlStatementContext, final Map<String, String> logicAndActualColumns) {
this.resultSetMetaData = resultSetMetaData;
this.encryptRule = encryptRule;
this.sqlStatementContext = sqlStatementContext;
this.logicAndActualColumns = logicAndActualColumns;
}
@Override
public int getColumnCount() throws SQLException {
return resultSetMetaData.getColumnCount() - getDerivedColumnCount();
......@@ -112,8 +106,7 @@ public final class EncryptResultSetMetaData extends WrapperAdapter implements Re
@Override
public String getColumnLabel(final int column) throws SQLException {
String result = resultSetMetaData.getColumnLabel(column);
return logicAndActualColumns.values().contains(result) ? getLogicColumn(result) : result;
return resultSetMetaData.getColumnLabel(column);
}
@Override
......
......@@ -35,6 +35,7 @@ import javax.sql.DataSource;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
......@@ -69,24 +70,34 @@ public abstract class AbstractEncryptJDBCDatabaseAndTableTest extends AbstractSQ
}
private static EncryptRuleConfiguration createEncryptRuleConfiguration() {
EncryptorRuleConfiguration encryptorConfig = new EncryptorRuleConfiguration("test", new Properties());
EncryptorRuleConfiguration encryptorQueryConfig = new EncryptorRuleConfiguration("assistedTest", new Properties());
EncryptColumnRuleConfiguration columnConfig1 = new EncryptColumnRuleConfiguration("plain_pwd", "cipher_pwd", "", "test");
EncryptTableRuleConfiguration tableConfig1 = new EncryptTableRuleConfiguration(Collections.singletonMap("pwd", columnConfig1));
EncryptColumnRuleConfiguration columnConfig2 = new EncryptColumnRuleConfiguration("", "cipher_pwd", "assist_pwd", "assistedTest");
EncryptTableRuleConfiguration tableConfig2 = new EncryptTableRuleConfiguration(Collections.singletonMap("pwd", columnConfig2));
EncryptColumnRuleConfiguration columnConfig3 = new EncryptColumnRuleConfiguration("plain_pwd2", "cipher_pwd2", "", "test");
EncryptTableRuleConfiguration tableConfig3 = new EncryptTableRuleConfiguration(Collections.singletonMap("plain_pwd2", columnConfig3));
tableConfig3.getColumns().put("plain_pwd", columnConfig1);
EncryptRuleConfiguration result = new EncryptRuleConfiguration();
result.getEncryptors().put("test", encryptorConfig);
result.getEncryptors().put("assistedTest", encryptorQueryConfig);
result.getTables().put("t_encrypt", tableConfig1);
result.getTables().put("t_query_encrypt", tableConfig2);
result.getTables().put("t_encrypt_contains_column", tableConfig3);
result.getEncryptors().put("test", new EncryptorRuleConfiguration("test", new Properties()));
result.getEncryptors().put("assistedTest", new EncryptorRuleConfiguration("assistedTest", new Properties()));
result.getTables().put("t_encrypt", createEncryptTableRule());
result.getTables().put("t_query_encrypt", createQueryEncryptTableRule());
result.getTables().put("t_encrypt_contains_column", createEncryptContainsColumnTableRule());
return result;
}
private static EncryptTableRuleConfiguration createEncryptTableRule() {
EncryptColumnRuleConfiguration columnRuleConfig = new EncryptColumnRuleConfiguration("plain_pwd", "cipher_pwd", "", "test");
return new EncryptTableRuleConfiguration(Collections.singletonMap("pwd", columnRuleConfig));
}
private static EncryptTableRuleConfiguration createQueryEncryptTableRule() {
EncryptColumnRuleConfiguration columnRuleConfig = new EncryptColumnRuleConfiguration("", "cipher_pwd", "assist_pwd", "assistedTest");
return new EncryptTableRuleConfiguration(Collections.singletonMap("pwd", columnRuleConfig));
}
private static EncryptTableRuleConfiguration createEncryptContainsColumnTableRule() {
EncryptColumnRuleConfiguration columnConfig1 = new EncryptColumnRuleConfiguration("plain_pwd", "cipher_pwd", "", "test");
EncryptColumnRuleConfiguration columnConfig2 = new EncryptColumnRuleConfiguration("plain_pwd2", "cipher_pwd2", "", "test");
Map<String, EncryptColumnRuleConfiguration> columns = new LinkedHashMap<>(2, 1);
columns.put("plain_pwd", columnConfig1);
columns.put("plain_pwd2", columnConfig2);
return new EncryptTableRuleConfiguration(columns);
}
@Before
public void initTable() {
try {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册