Skip to content

Commit c4b41ff

Browse files
oneby-wangdywangbh
andauthored
feat: stop using regex pattern to escape single quota (#275)
* feat: stop using regex pattern to escape single quota * feat: escape single quota unit test --------- Co-authored-by: dywangbh <dywangbh@chint.com>
1 parent b0a6a10 commit c4b41ff

2 files changed

Lines changed: 73 additions & 41 deletions

File tree

src/main/java/com/taosdata/jdbc/utils/Utils.java

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,10 @@
33
import com.google.common.collect.Range;
44
import com.google.common.collect.RangeSet;
55
import com.google.common.collect.TreeRangeSet;
6-
import com.taosdata.jdbc.TSDBError;
7-
import com.taosdata.jdbc.TSDBErrorNumbers;
8-
import com.taosdata.jdbc.rs.ConnectionParam;
9-
import com.taosdata.jdbc.ws.entity.ConnectReq;
10-
import com.taosdata.jdbc.ws.tmq.WSConsumer;
116
import io.netty.buffer.ByteBuf;
127
import io.netty.channel.EventLoopGroup;
13-
import io.netty.channel.MultithreadEventLoopGroup;
148
import io.netty.channel.nio.NioEventLoopGroup;
159
import io.netty.util.ReferenceCountUtil;
16-
import io.netty.util.ResourceLeakDetector;
1710
import io.netty.util.concurrent.DefaultThreadFactory;
1811
import org.slf4j.Logger;
1912
import org.slf4j.LoggerFactory;
@@ -23,17 +16,10 @@
2316
import java.net.UnknownHostException;
2417
import java.nio.charset.StandardCharsets;
2518
import java.sql.*;
26-
import java.time.LocalDateTime;
27-
import java.time.ZoneId;
28-
import java.time.ZonedDateTime;
29-
import java.time.format.DateTimeFormatter;
30-
import java.time.format.DateTimeFormatterBuilder;
31-
import java.time.format.DateTimeParseException;
3219
import java.util.Arrays;
3320
import java.util.HashMap;
3421
import java.util.Map;
3522
import java.util.concurrent.ForkJoinPool;
36-
import java.util.concurrent.TimeUnit;
3723
import java.util.regex.Matcher;
3824
import java.util.regex.Pattern;
3925
import java.util.stream.Collectors;
@@ -42,42 +28,40 @@
4228
public class Utils {
4329
private static final Logger log = LoggerFactory.getLogger(Utils.class);
4430
private static final ForkJoinPool forkJoinPool = new ForkJoinPool();
45-
private static final Pattern ptn = Pattern.compile(".*?'");
4631

4732
private static EventLoopGroup eventLoopGroup = null;
4833

4934
private Utils() {}
5035

5136
public static String escapeSingleQuota(String origin) {
52-
Matcher m = ptn.matcher(origin);
37+
int length = origin.length();
38+
boolean singleQuoteFound = false;
5339
StringBuilder sb = new StringBuilder();
54-
int end = 0;
55-
while (m.find()) {
56-
end = m.end();
57-
String seg = origin.substring(m.start(), end);
58-
int len = seg.length();
59-
if (len == 1) {
60-
if ('\'' == seg.charAt(0)) {
61-
sb.append("\\'");
62-
} else {
63-
sb.append(seg);
40+
for (int i = 0; i < length; i++) {
41+
char chr = origin.charAt(i);
42+
if (chr == '\\') {
43+
if (singleQuoteFound) {
44+
sb.append(chr);
45+
}
46+
if (i < length - 1 && origin.charAt(i + 1) == '\'') {
47+
i++;
48+
if (singleQuoteFound) {
49+
sb.append('\'');
50+
}
6451
}
65-
} else { // len > 1
66-
sb.append(seg, 0, seg.length() - 2);
67-
char lastcSec = seg.charAt(seg.length() - 2);
68-
if (lastcSec == '\\') {
52+
} else if (chr == '\'') {
53+
if (!singleQuoteFound) {
54+
singleQuoteFound = true;
55+
sb.append(origin, 0, i);
6956
sb.append("\\'");
7057
} else {
71-
sb.append(lastcSec);
7258
sb.append("\\'");
7359
}
60+
} else if (singleQuoteFound) {
61+
sb.append(chr);
7462
}
7563
}
76-
77-
if (end < origin.length()) {
78-
sb.append(origin.substring(end));
79-
}
80-
return sb.toString();
64+
return singleQuoteFound ? sb.toString() : origin;
8165
}
8266

8367
public static String getNativeSql(String rawSql, Object[] parameters) {

src/test/java/com/taosdata/jdbc/utils/UtilsTest.java

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.taosdata.jdbc.utils;
22

3-
import com.fasterxml.jackson.annotation.JsonTypeInfo;
43
import com.taosdata.jdbc.TSDBConstants;
54
import org.junit.Assert;
65
import org.junit.Test;
@@ -21,18 +20,67 @@ public void escapeSingleQuota() {
2120
Assert.assertEquals("\\'\\'\\'\\'\\'a\\'", news);
2221

2322
// given
24-
s = "'''''a\\'";
23+
s = "abc";
2524
// when
2625
news = Utils.escapeSingleQuota(s);
2726
// then
28-
Assert.assertEquals("\\'\\'\\'\\'\\'a\\'", news);
27+
Assert.assertEquals("abc", news);
2928

3029
// given
31-
s = "'''''a\\'";
30+
s = "\\a\\\\b\\\\c\\\\\\";
3231
// when
3332
news = Utils.escapeSingleQuota(s);
3433
// then
35-
Assert.assertEquals("\\'\\'\\'\\'\\'a\\'", news);
34+
Assert.assertEquals("\\a\\\\b\\\\c\\\\\\", news);
35+
36+
// given
37+
s = "abc'";
38+
// when
39+
news = Utils.escapeSingleQuota(s);
40+
// then
41+
Assert.assertEquals("abc\\'", news);
42+
43+
// given
44+
s = "a'bc";
45+
// when
46+
news = Utils.escapeSingleQuota(s);
47+
// then
48+
Assert.assertEquals("a\\'bc", news);
49+
50+
// given
51+
s = "'abc";
52+
// when
53+
news = Utils.escapeSingleQuota(s);
54+
// then
55+
Assert.assertEquals("\\'abc", news);
56+
57+
// given
58+
s = "'''a'''b'''c'''";
59+
// when
60+
news = Utils.escapeSingleQuota(s);
61+
// then
62+
Assert.assertEquals("\\'\\'\\'a\\'\\'\\'b\\'\\'\\'c\\'\\'\\'", news);
63+
64+
// given
65+
s = "'''a\\'\\'\\'b'''c\\'\\'\\'";
66+
// when
67+
news = Utils.escapeSingleQuota(s);
68+
// then
69+
Assert.assertEquals("\\'\\'\\'a\\'\\'\\'b\\'\\'\\'c\\'\\'\\'", news);
70+
71+
// given
72+
s = "'''a\\\\'\\'\\\\'b'''c\\'\\'\\\\'";
73+
// when
74+
news = Utils.escapeSingleQuota(s);
75+
// then
76+
Assert.assertEquals("\\'\\'\\'a\\\\'\\'\\\\'b\\'\\'\\'c\\'\\'\\\\'", news);
77+
78+
// given
79+
s = "\\'\\'\\'a'''b'''c\\'\\'\\'";
80+
// when
81+
news = Utils.escapeSingleQuota(s);
82+
// then
83+
Assert.assertEquals("\\'\\'\\'a\\'\\'\\'b\\'\\'\\'c\\'\\'\\'", news);
3684
}
3785

3886
@Test

0 commit comments

Comments
 (0)