forked from p6spy/p6spy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Binary data representation can be customized according to database di…
…alect (p6spy#502) * Binary data representation can be customized according to database dialect * BinaryFormat#toString wraps its return value in quotes if needed Co-authored-by: Jonne Jyrylä <[email protected]>
- Loading branch information
Showing
11 changed files
with
321 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/com/p6spy/engine/logging/format/BinaryFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* P6Spy | ||
* | ||
* Copyright (C) 2002 - 2020 P6Spy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.p6spy.engine.logging.format; | ||
|
||
public interface BinaryFormat { | ||
/** | ||
* Transforms the supplied binary data to a string representation. | ||
* Wraps the value in quotes if the database dialect requires them. | ||
* | ||
* @param input | ||
* the binary data input value to convert to {@link String} | ||
* @return | ||
* the {@link String} representation of the given bytes | ||
*/ | ||
public String toString(byte[] input); | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/p6spy/engine/logging/format/HexEncodedBinaryFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* P6Spy | ||
* | ||
* Copyright (C) 2002 - 2020 P6Spy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.p6spy.engine.logging.format; | ||
|
||
/** | ||
* Transforms binary data to hex encoded strings. | ||
*/ | ||
public class HexEncodedBinaryFormat implements BinaryFormat { | ||
private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', | ||
'F' }; | ||
|
||
/** | ||
* The space needed for the opening and closing quote character. | ||
*/ | ||
private static final int QUOTE_COUNT = 2; | ||
|
||
@Override | ||
public String toString(byte[] input) { | ||
char[] result = new char[QUOTE_COUNT + input.length * 2]; | ||
int i = 0; | ||
result[i++] = '\''; // add opening quote | ||
hexEncode(input, result, i); | ||
result[result.length - 1] = '\''; // add closing quote | ||
return new String(result); | ||
} | ||
|
||
/** | ||
* Hex encodes the supplied input bytes to the supplied output array. | ||
* Writes two {@code char}s of output for every {@code byte} of input. | ||
* @param input the input array | ||
* @param output the output array | ||
* @param outputOffset the offset of the output array to start writing from | ||
*/ | ||
static void hexEncode(byte[] input, char[] output, int outputOffset) { | ||
int idx = outputOffset; | ||
for (byte b : input) { | ||
int temp = (int) b & 0xFF; | ||
output[idx++] = HEX_CHARS[temp / 16]; | ||
output[idx++] = HEX_CHARS[temp % 16]; | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/p6spy/engine/logging/format/MySQLBinaryFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* P6Spy | ||
* | ||
* Copyright (C) 2002 - 2020 P6Spy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.p6spy.engine.logging.format; | ||
|
||
/** | ||
* Transforms binary data to MySQL hex encoded strings, for example {@code 0x8088BAD639F}. | ||
* Such strings are not quoted when used in queries. | ||
* | ||
* @see <a href="https://dev.mysql.com/doc/refman/5.6/en/hexadecimal-literals.html">MySQL documentation</a> | ||
*/ | ||
public class MySQLBinaryFormat implements BinaryFormat { | ||
|
||
/** | ||
* Reserve space for the two prefix chars 0 and x | ||
*/ | ||
private static final int PREFIX_LENGTH = 2; | ||
|
||
@Override | ||
public String toString(byte[] input) { | ||
|
||
char[] result = new char[PREFIX_LENGTH + input.length * 2]; | ||
int i = 0; | ||
result[i++] = '0'; | ||
result[i++] = 'x'; | ||
HexEncodedBinaryFormat.hexEncode(input, result, i); | ||
return new String(result); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/p6spy/engine/logging/format/PostgreSQLBinaryFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* P6Spy | ||
* | ||
* Copyright (C) 2002 - 2020 P6Spy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.p6spy.engine.logging.format; | ||
|
||
/** | ||
* Transforms binary data to PostgreSQL hex encoded strings, for example {@code \x8088BAD639F} | ||
* | ||
* @see <a href="https://www.postgresql.org/docs/current/datatype-binary.html#id-1.5.7.12.9">PostgreSQL documentation</a> | ||
*/ | ||
public class PostgreSQLBinaryFormat implements BinaryFormat { | ||
|
||
/** | ||
* Reserve space for the two prefix chars \ and x | ||
*/ | ||
private static final int PREFIX_LENGTH = 2; | ||
|
||
/** | ||
* The space needed for the opening and closing quote character. | ||
*/ | ||
private static final int QUOTE_COUNT = 2; | ||
|
||
@Override | ||
public String toString(byte[] input) { | ||
char[] result = new char[PREFIX_LENGTH + QUOTE_COUNT + input.length * 2]; | ||
int i = 0; | ||
result[i++] = '\''; // opening quote | ||
result[i++] = '\\'; // PostgreSQL binary... | ||
result[i++] = 'x'; // ...data prefix | ||
HexEncodedBinaryFormat.hexEncode(input, result, i); | ||
result[result.length - 1] = '\''; // closing quote | ||
return new String(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.