File tree Expand file tree Collapse file tree 2 files changed +37
-1
lines changed
main/java/net/sf/jsqlparser/expression
test/java/net/sf/jsqlparser/expression Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change 11
11
12
12
import net .sf .jsqlparser .parser .ASTNodeAccessImpl ;
13
13
14
+ import java .nio .charset .StandardCharsets ;
15
+
14
16
public class HexValue extends ASTNodeAccessImpl implements Expression {
15
17
16
18
private String value ;
@@ -61,4 +63,31 @@ public Long getLong() {
61
63
public LongValue getLongValue () {
62
64
return new LongValue (getLong ());
63
65
}
66
+
67
+ public static byte [] hexStringToByteArray (String s ) {
68
+ int len = s .length ();
69
+ byte [] data = new byte [len / 2 ];
70
+ for (int i = 0 ; i < len ; i += 2 ) {
71
+ data [i / 2 ] = (byte ) ((Character .digit (s .charAt (i ), 16 ) << 4 )
72
+ + Character .digit (s .charAt (i + 1 ), 16 ));
73
+ }
74
+ return data ;
75
+ }
76
+
77
+ // `X'C3BC'` --> `'ü'`
78
+ public StringValue getStringValue () {
79
+ return new StringValue (
80
+ new String (hexStringToByteArray (getDigits ()), StandardCharsets .UTF_8 ));
81
+ }
82
+
83
+ // `X'C3BC'` --> `\xC3\xBC`
84
+ public StringValue getBlob () {
85
+ StringBuilder builder = new StringBuilder ();
86
+ String digits = getDigits ();
87
+ int len = digits .length ();
88
+ for (int i = 0 ; i < len ; i += 2 ) {
89
+ builder .append ("\\ x" ).append (digits .charAt (i )).append (digits .charAt (i + 1 ));
90
+ }
91
+ return new StringValue (builder .toString ());
92
+ }
64
93
}
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ class HexValueTest {
19
19
20
20
@ Test
21
21
void testHexCode () throws JSQLParserException {
22
- String sqlString = "SELECT 0xF001, X'00A1'" ;
22
+ String sqlString = "SELECT 0xF001, X'00A1', X'C3BC' " ;
23
23
PlainSelect select = (PlainSelect ) CCJSqlParserUtil .parse (sqlString );
24
24
25
25
HexValue hex1 = (HexValue ) select .getSelectItem (0 ).getExpression ();
@@ -31,5 +31,12 @@ void testHexCode() throws JSQLParserException {
31
31
Assertions .assertEquals ("00A1" , hex2 .getDigits ());
32
32
Assertions .assertEquals (161 , hex2 .getLong ());
33
33
Assertions .assertEquals (161 , hex2 .getLongValue ().getValue ());
34
+
35
+ HexValue hex3 = (HexValue ) select .getSelectItem (2 ).getExpression ();
36
+ Assertions .assertEquals ("C3BC" , hex3 .getDigits ());
37
+ Assertions .assertEquals ("'ü'" , hex3 .getStringValue ().toString ());
38
+ Assertions .assertEquals ("ü" , hex3 .getStringValue ().getValue ());
39
+
40
+ Assertions .assertEquals ("'\\ xC3\\ xBC'" , hex3 .getBlob ().toString ());
34
41
}
35
42
}
You can’t perform that action at this time.
0 commit comments