Skip to content

Commit 1e1f0f0

Browse files
authored
Merge pull request #2 from oatpp/clean_section
Clean section
2 parents 06df476 + 4bed148 commit 1e1f0f0

File tree

6 files changed

+418
-2
lines changed

6 files changed

+418
-2
lines changed

src/oatpp-postgresql/ql_template/Parser.cpp

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,86 @@
2424

2525
#include "Parser.hpp"
2626

27+
#include "oatpp/core/data/stream/BufferStream.hpp"
2728
#include "oatpp/core/parser/ParsingError.hpp"
2829

2930
namespace oatpp { namespace postgresql { namespace ql_template {
3031

32+
oatpp::String Parser::preprocess(const oatpp::String& text, std::vector<CleanSection>& cleanSections) {
33+
34+
data::stream::BufferOutputStream ss;
35+
parser::Caret caret(text);
36+
37+
bool writeChar = true;
38+
39+
v_buff_size sectionStart = -1;
40+
41+
while(caret.canContinue()) {
42+
43+
v_char8 c = *caret.getCurrData();
44+
writeChar = true;
45+
46+
switch(c) {
47+
48+
case '\'': {
49+
auto l = caret.putLabel();
50+
skipStringInQuotes(caret);
51+
ss.writeSimple(l.getData(), l.getSize());
52+
writeChar = false;
53+
break;
54+
}
55+
case '$': {
56+
auto l = caret.putLabel();
57+
skipStringInDollars(caret);
58+
ss.writeSimple(l.getData(), l.getSize());
59+
writeChar = false;
60+
break;
61+
}
62+
63+
case '<': {
64+
if(sectionStart == -1) {
65+
if(caret.isAtText((p_char8) "<!!", 3, true)) {
66+
sectionStart = ss.getCurrentPosition();
67+
writeChar = false;
68+
} else {
69+
caret.inc();
70+
}
71+
} else {
72+
caret.inc();
73+
}
74+
break;
75+
}
76+
77+
case '!': {
78+
if(sectionStart != -1) {
79+
if(caret.isAtText((p_char8) "!!>", 3, true)) {
80+
cleanSections.emplace_back(CleanSection(sectionStart, ss.getCurrentPosition() - sectionStart));
81+
sectionStart = -1;
82+
writeChar = false;
83+
} else {
84+
caret.inc();
85+
}
86+
} else {
87+
caret.inc();
88+
}
89+
break;
90+
}
91+
92+
default:
93+
caret.inc();
94+
95+
}
96+
97+
if(writeChar) {
98+
ss.writeCharSimple(c);
99+
}
100+
101+
}
102+
103+
return ss.toString();
104+
105+
}
106+
31107
data::share::StringTemplate::Variable Parser::parseIdentifier(parser::Caret& caret) {
32108
data::share::StringTemplate::Variable result;
33109
result.posStart = caret.getPosition();
@@ -99,11 +175,23 @@ void Parser::skipStringInDollars(parser::Caret& caret) {
99175

100176
data::share::StringTemplate Parser::parseTemplate(const oatpp::String& text) {
101177

178+
std::vector<CleanSection> cleanSections;
179+
auto processedText = preprocess(text, cleanSections);
180+
181+
parser::Caret caret(processedText);
182+
102183
std::vector<data::share::StringTemplate::Variable> variables;
103184

104-
parser::Caret caret(text);
185+
v_buff_size currSection = 0;
186+
105187
while(caret.canContinue()) {
106188

189+
if(currSection < cleanSections.size() && cleanSections[currSection].position == caret.getPosition()) {
190+
caret.inc(cleanSections[currSection].size);
191+
currSection ++;
192+
continue;
193+
}
194+
107195
v_char8 c = *caret.getCurrData();
108196

109197
switch(c) {
@@ -130,7 +218,7 @@ data::share::StringTemplate Parser::parseTemplate(const oatpp::String& text) {
130218
throw oatpp::parser::ParsingError(caret.getErrorMessage(), caret.getErrorCode(), caret.getPosition());
131219
}
132220

133-
return data::share::StringTemplate(text, std::move(variables));
221+
return data::share::StringTemplate(processedText, std::move(variables));
134222

135223
}
136224

src/oatpp-postgresql/ql_template/Parser.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,31 @@ class Parser {
6565

6666
};
6767

68+
public:
69+
70+
struct CleanSection {
71+
CleanSection(v_buff_size p, v_buff_size s)
72+
: position(p)
73+
, size(s)
74+
{}
75+
v_buff_size position;
76+
v_buff_size size;
77+
};
6878

6979
private:
7080
static data::share::StringTemplate::Variable parseIdentifier(parser::Caret& caret);
7181
static void skipStringInQuotes(parser::Caret& caret);
7282
static void skipStringInDollars(parser::Caret& caret);
7383
public:
7484

85+
/**
86+
* Preprocess text.
87+
* @param text
88+
* @param cleanSections - out vector of clean sections.
89+
* @return
90+
*/
91+
static oatpp::String preprocess(const oatpp::String& text, std::vector<CleanSection>& cleanSections);
92+
7593
/**
7694
* Parse query template.
7795
* @param text

test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ add_definitions (
1212
)
1313

1414
add_executable(module-tests
15+
oatpp-postgresql/ql_template/ParserTest.cpp
16+
oatpp-postgresql/ql_template/ParserTest.hpp
1517
oatpp-postgresql/types/FloatTest.cpp
1618
oatpp-postgresql/types/FloatTest.hpp
1719
oatpp-postgresql/types/InterpretationTest.cpp

0 commit comments

Comments
 (0)