2020import io .netty .buffer .ByteBuf ;
2121import io .netty .buffer .Unpooled ;
2222import io .netty .handler .codec .DecoderException ;
23+ import io .vertx .core .buffer .Buffer ;
2324import io .vertx .core .impl .logging .Logger ;
2425import io .vertx .core .impl .logging .LoggerFactory ;
2526import io .vertx .core .json .Json ;
26- import io .vertx .sqlclient .Tuple ;
27- import io .vertx .sqlclient .data .Numeric ;
28- import io .vertx .pgclient .data .*;
29- import io .vertx .pgclient .impl .util .UTF8StringEndDetector ;
30- import io .vertx .core .buffer .Buffer ;
3127import io .vertx .core .json .JsonArray ;
3228import io .vertx .core .json .JsonObject ;
29+ import io .vertx .pgclient .data .*;
30+ import io .vertx .pgclient .impl .util .UTF8StringEndDetector ;
31+ import io .vertx .sqlclient .Tuple ;
32+ import io .vertx .sqlclient .data .Numeric ;
3333import io .vertx .sqlclient .impl .codec .CommonCodec ;
3434
3535import java .net .Inet4Address ;
5050
5151import static java .time .format .DateTimeFormatter .ISO_LOCAL_DATE ;
5252import static java .time .format .DateTimeFormatter .ISO_LOCAL_TIME ;
53- import static java .util .concurrent .TimeUnit .* ;
53+ import static java .util .concurrent .TimeUnit .NANOSECONDS ;
5454
5555/**
5656 * @author <a href="mailto:[email protected] ">Julien Viet</a> @@ -91,6 +91,8 @@ public class DataTypeCodec {
9191 private static final OffsetDateTime OFFSET_DATE_TIME_EPOCH = LocalDateTime .of (2000 , 1 , 1 , 0 , 0 , 0 ).atOffset (ZoneOffset .UTC );
9292 private static final Inet [] empty_inet_array = new Inet [0 ];
9393 private static final Money [] empty_money_array = new Money [0 ];
94+ private static final PgSQLXML [] empty_pgsqlxml_array = new PgSQLXML [0 ];
95+
9496
9597 // Sentinel used when an object is refused by the data type
9698 public static final Object REFUSED_SENTINEL = new Object ();
@@ -107,7 +109,7 @@ public class DataTypeCodec {
107109 private static final IntFunction <OffsetTime []> OFFSETTIME_ARRAY_FACTORY = size -> size == 0 ? empty_offset_time_array : new OffsetTime [size ];
108110 private static final IntFunction <LocalDateTime []> LOCALDATETIME_ARRAY_FACTORY = size -> size == 0 ? empty_local_date_time_array : new LocalDateTime [size ];
109111 private static final IntFunction <OffsetDateTime []> OFFSETDATETIME_ARRAY_FACTORY = size -> size == 0 ? empty_offset_date_time_array : new OffsetDateTime [size ];
110- private static final IntFunction <Buffer []> BUFFER_ARRAY_FACTORY =size -> size == 0 ? empty_buffer_array : new Buffer [size ];
112+ private static final IntFunction <Buffer []> BUFFER_ARRAY_FACTORY = size -> size == 0 ? empty_buffer_array : new Buffer [size ];
111113 private static final IntFunction <UUID []> UUID_ARRAY_FACTORY = size -> size == 0 ? empty_uuid_array : new UUID [size ];
112114 private static final IntFunction <Object []> JSON_ARRAY_FACTORY = size -> size == 0 ? empty_json_array : new Object [size ];
113115 private static final IntFunction <Numeric []> NUMERIC_ARRAY_FACTORY = size -> size == 0 ? empty_numeric_array : new Numeric [size ];
@@ -121,20 +123,18 @@ public class DataTypeCodec {
121123 private static final IntFunction <Interval []> INTERVAL_ARRAY_FACTORY = size -> size == 0 ? empty_interval_array : new Interval [size ];
122124 private static final IntFunction <Inet []> INET_ARRAY_FACTORY = size -> size == 0 ? empty_inet_array : new Inet [size ];
123125 private static final IntFunction <Money []> MONEY_ARRAY_FACTORY = size -> size == 0 ? empty_money_array : new Money [size ];
124-
126+ private static final IntFunction < PgSQLXML []> PGSQLXML_ARRAY_FACTORY = size -> size == 0 ? empty_pgsqlxml_array : new PgSQLXML [ size ];
125127 private static final java .time .format .DateTimeFormatter TIMETZ_FORMAT = new DateTimeFormatterBuilder ()
126128 .parseCaseInsensitive ()
127129 .append (ISO_LOCAL_TIME )
128130 .appendOffset ("+HH:mm" , "00:00" )
129131 .toFormatter ();
130-
131132 private static final java .time .format .DateTimeFormatter TIMESTAMP_FORMAT = new DateTimeFormatterBuilder ()
132133 .parseCaseInsensitive ()
133134 .append (ISO_LOCAL_DATE )
134135 .appendLiteral (' ' )
135136 .append (ISO_LOCAL_TIME )
136137 .toFormatter ();
137-
138138 private static final java .time .format .DateTimeFormatter TIMESTAMPTZ_FORMAT = new DateTimeFormatterBuilder ()
139139 .append (TIMESTAMP_FORMAT )
140140 .appendOffset ("+HH:mm" , "00:00" )
@@ -360,6 +360,12 @@ public static void encodeBinary(DataType id, Object value, ByteBuf buff) {
360360 case MONEY_ARRAY :
361361 binaryEncodeArray ((Money []) value , DataType .MONEY , buff );
362362 break ;
363+ case XML :
364+ binaryEncodePgXMLSQL ((PgSQLXML ) value , buff );
365+ break ;
366+ case XML_ARRAY :
367+ binaryEncodeArray ((PgSQLXML []) value , DataType .XML , buff );
368+ break ;
363369 default :
364370 logger .debug ("Data type " + id + " does not support binary encoding" );
365371 defaultEncodeBinary (value , buff );
@@ -497,6 +503,10 @@ public static Object decodeBinary(DataType id, int index, int len, ByteBuf buff)
497503 return binaryDecodeMoney (index , len , buff );
498504 case MONEY_ARRAY :
499505 return binaryDecodeArray (MONEY_ARRAY_FACTORY , DataType .MONEY , index , len , buff );
506+ case XML :
507+ return binaryDecodePgXMLSQL (index , len , buff );
508+ case XML_ARRAY :
509+ return binaryDecodeArray (PGSQLXML_ARRAY_FACTORY , DataType .XML , index , len , buff );
500510 default :
501511 logger .debug ("Data type " + id + " does not support binary decoding" );
502512 return defaultDecodeBinary (index , len , buff );
@@ -669,7 +679,7 @@ private static Boolean binaryDecodeBOOL(int index, int len, ByteBuf buff) {
669679 }
670680
671681 private static Boolean textDecodeBOOL (int index , int len , ByteBuf buff ) {
672- if (buff .getByte (index ) == 't' ) {
682+ if (buff .getByte (index ) == 't' ) {
673683 return Boolean .TRUE ;
674684 } else {
675685 return Boolean .FALSE ;
@@ -770,7 +780,7 @@ private static Line textDecodeLine(int index, int len, ByteBuf buff) {
770780
771781 private static LineSegment textDecodeLseg (int index , int len , ByteBuf buff ) {
772782 // Lseg representation: [p1,p2]
773- int idxOfPointsSeparator = buff .indexOf (index , index + len , (byte ) ')' ) + 1 ;
783+ int idxOfPointsSeparator = buff .indexOf (index , index + len , (byte ) ')' ) + 1 ;
774784 int lenOfP1 = idxOfPointsSeparator - index - 1 ;
775785 Point p1 = textDecodePOINT (index + 1 , lenOfP1 , buff );
776786 Point p2 = textDecodePOINT (idxOfPointsSeparator + 1 , len - lenOfP1 - 3 , buff );
@@ -779,7 +789,7 @@ private static LineSegment textDecodeLseg(int index, int len, ByteBuf buff) {
779789
780790 private static Box textDecodeBox (int index , int len , ByteBuf buff ) {
781791 // Box representation: p1,p2
782- int idxOfPointsSeparator = buff .indexOf (index , index + len , (byte ) ')' ) + 1 ;
792+ int idxOfPointsSeparator = buff .indexOf (index , index + len , (byte ) ')' ) + 1 ;
783793 int lenOfUpperRightCornerPoint = idxOfPointsSeparator - index ;
784794 Point upperRightCorner = textDecodePOINT (index , lenOfUpperRightCornerPoint , buff );
785795 Point lowerLeftCorner = textDecodePOINT (idxOfPointsSeparator + 1 , len - lenOfUpperRightCornerPoint - 1 , buff );
@@ -905,7 +915,7 @@ private static Interval textDecodeINTERVAL(int index, int len, ByteBuf buff) {
905915 : Integer .parseInt (timeChunk .substring (sidx ));
906916 } else {
907917 // seconds with microseconds
908- seconds = isNeg ? -Integer .parseInt (timeChunk .substring (sidx ).substring (0 , m ))
918+ seconds = isNeg ? -Integer .parseInt (timeChunk .substring (sidx ).substring (0 , m ))
909919 : Integer .parseInt (timeChunk .substring (sidx ).substring (0 , m ));
910920 microseconds = isNeg ? -Integer .parseInt (timeChunk .substring (sidx ).substring (m + 1 ))
911921 : Integer .parseInt (timeChunk .substring (sidx ).substring (m + 1 ));
@@ -990,7 +1000,6 @@ private static String textDecodeNAME(int index, int len, ByteBuf buff) {
9901000 return buff .getCharSequence (index , len , StandardCharsets .UTF_8 ).toString ();
9911001 }
9921002
993-
9941003 private static void binaryEncodeNAME (String value , ByteBuf buff ) {
9951004 String s = String .valueOf (value );
9961005 buff .writeCharSequence (s , StandardCharsets .UTF_8 );
@@ -1466,7 +1475,7 @@ private static void binaryEncodeMoney(Money money, ByteBuf buff) {
14661475
14671476 private static Money binaryDecodeMoney (int index , int len , ByteBuf buff ) {
14681477 long value = binaryDecodeINT8 (index , len , buff );
1469- return new Money (value / 100 , Math .abs (((int )value % 100 )));
1478+ return new Money (value / 100 , Math .abs (((int ) value % 100 )));
14701479 }
14711480
14721481 private static String binaryDecodeTsQuery (int index , int len , ByteBuf buff ) {
@@ -1477,6 +1486,14 @@ private static void binaryEncodeTsQuery(String value, ByteBuf buff) {
14771486 buff .writeCharSequence (String .valueOf (value ), StandardCharsets .UTF_8 );
14781487 }
14791488
1489+ private static String binaryDecodePgXMLSQL (int index , int len , ByteBuf buff ) {
1490+ return buff .getCharSequence (index , len , StandardCharsets .UTF_8 ).toString ();
1491+ }
1492+
1493+ private static void binaryEncodePgXMLSQL (PgSQLXML value , ByteBuf buff ) {
1494+ buff .writeCharSequence (value .toString (), StandardCharsets .UTF_8 );
1495+ }
1496+
14801497 private static String textDecodeTsVector (int index , int len , ByteBuf buff ) {
14811498 return buff .getCharSequence (index , len , StandardCharsets .UTF_8 ).toString ();
14821499 }
@@ -1535,7 +1552,7 @@ private static Money textDecodeMoney(int index, int len, ByteBuf buff) {
15351552 * Decode the specified {@code buff} formatted as an hex string starting at the buffer readable index
15361553 * with the specified {@code length} to a {@link Buffer}.
15371554 *
1538- * @param len the hex string length
1555+ * @param len the hex string length
15391556 * @param buff the byte buff to read from
15401557 * @return the decoded value as a Buffer
15411558 */
@@ -1551,7 +1568,7 @@ private static Buffer decodeHexStringToBytes(int index, int len, ByteBuf buff) {
15511568 }
15521569
15531570 private static byte decodeHexChar (byte ch ) {
1554- return (byte )(((ch & 0x1F ) + ((ch >> 6 ) * 0x19 ) - 0x10 ) & 0x0F );
1571+ return (byte ) (((ch & 0x1F ) + ((ch >> 6 ) * 0x19 ) - 0x10 ) & 0x0F );
15551572 }
15561573
15571574 private static boolean isHexFormat (int index , int len , ByteBuf buff ) {
@@ -1619,7 +1636,7 @@ private static <T> T[] binaryDecodeArray(IntFunction<T[]> supplier, DataType typ
16191636 return array ;
16201637 }
16211638
1622- private static <T > void binaryEncodeArray (T [] values , DataType type , ByteBuf buff ){
1639+ private static <T > void binaryEncodeArray (T [] values , DataType type , ByteBuf buff ) {
16231640 int startIndex = buff .writerIndex ();
16241641 buff .writeInt (1 ); // ndim
16251642 buff .writeInt (0 ); // dataoffset
@@ -1681,7 +1698,7 @@ private static <T> T textDecodeArrayElement(DataType type, int index, int len, B
16811698 // Some escaping - improve that later...
16821699 String s = buff .toString (index + 1 , len - 2 , StandardCharsets .UTF_8 );
16831700 StringBuilder sb = new StringBuilder ();
1684- for (int i = 0 ;i < s .length ();i ++) {
1701+ for (int i = 0 ; i < s .length (); i ++) {
16851702 char c = s .charAt (i );
16861703 if (c == '\\' ) {
16871704 c = s .charAt (++i );
@@ -1696,7 +1713,7 @@ private static <T> T textDecodeArrayElement(DataType type, int index, int len, B
16961713 }
16971714 }
16981715
1699- private static <T > void textEncodeArray (T [] values , DataType type , ByteBuf buff ){
1716+ private static <T > void textEncodeArray (T [] values , DataType type , ByteBuf buff ) {
17001717 buff .writeByte ('{' );
17011718 int len = values .length ;
17021719 for (int i = 0 ; i < len ; i ++) {
0 commit comments