Skip to content

Commit 72c12cb

Browse files
committed
Play around
1 parent 774c35e commit 72c12cb

File tree

7 files changed

+217
-5
lines changed

7 files changed

+217
-5
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
55
## use these variables to configure module installation
66

77
set(OATPP_THIS_MODULE_NAME oatpp-postgresql) ## name of the module (also name of folders in installation dirs)
8-
set(OATPP_THIS_MODULE_VERSION "1.2.0") ## version of the module (also sufix of folders in installation dirs)
8+
set(OATPP_THIS_MODULE_VERSION "1.2.5") ## version of the module (also sufix of folders in installation dirs)
99
set(OATPP_THIS_MODULE_LIBRARIES oatpp-postgresql) ## list of libraries to find when find_package is called
1010
set(OATPP_THIS_MODULE_TARGETS oatpp-postgresql) ## list of targets to install
1111
set(OATPP_THIS_MODULE_DIRECTORIES oatpp-postgresql) ## list of directories to install

src/oatpp-postgresql/ql_template/Parser.cpp

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,83 @@
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) {
33+
34+
data::stream::BufferOutputStream ss;
35+
parser::Caret caret(text);
36+
37+
bool ignore = false;
38+
bool writeChar = true;
39+
40+
while(caret.canContinue()) {
41+
42+
v_char8 c = *caret.getCurrData();
43+
writeChar = true;
44+
45+
switch(c) {
46+
47+
case '\'': {
48+
auto l = caret.putLabel();
49+
skipStringInQuotes(caret);
50+
ss.writeSimple(l.getData(), l.getSize());
51+
writeChar = false;
52+
break;
53+
}
54+
case '$': {
55+
auto l = caret.putLabel();
56+
skipStringInDollars(caret);
57+
ss.writeSimple(l.getData(), l.getSize());
58+
writeChar = false;
59+
break;
60+
}
61+
case '<': {
62+
caret.inc();
63+
if(!ignore) {
64+
ignore = caret.canContinue() && caret.isAtChar('[');
65+
if(ignore) {
66+
caret.inc();
67+
writeChar = false;
68+
}
69+
}
70+
break;
71+
}
72+
73+
case ']': {
74+
caret.inc();
75+
if(ignore) {
76+
ignore = !(caret.canContinue() && caret.isAtChar('>'));
77+
if(!ignore) {
78+
caret.inc();
79+
writeChar = false;
80+
}
81+
}
82+
break;
83+
}
84+
85+
default:
86+
caret.inc();
87+
88+
}
89+
90+
if(writeChar) {
91+
if (ignore) {
92+
ss.writeCharSimple('_');
93+
} else {
94+
ss.writeCharSimple(c);
95+
}
96+
}
97+
98+
}
99+
100+
return ss.toString();
101+
102+
}
103+
31104
data::share::StringTemplate::Variable Parser::parseIdentifier(parser::Caret& caret) {
32105
data::share::StringTemplate::Variable result;
33106
result.posStart = caret.getPosition();
@@ -101,7 +174,7 @@ data::share::StringTemplate Parser::parseTemplate(const oatpp::String& text) {
101174

102175
std::vector<data::share::StringTemplate::Variable> variables;
103176

104-
parser::Caret caret(text);
177+
parser::Caret caret(preprocess(text));
105178
while(caret.canContinue()) {
106179

107180
v_char8 c = *caret.getCurrData();

src/oatpp-postgresql/ql_template/Parser.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ class Parser {
7272
static void skipStringInDollars(parser::Caret& caret);
7373
public:
7474

75+
/**
76+
* Preprocess text.
77+
* @param text
78+
* @return
79+
*/
80+
static oatpp::String preprocess(const oatpp::String& text);
81+
7582
/**
7683
* Parse query template.
7784
* @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
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/***************************************************************************
2+
*
3+
* Project _____ __ ____ _ _
4+
* ( _ ) /__\ (_ _)_| |_ _| |_
5+
* )(_)( /(__)\ )( (_ _)(_ _)
6+
* (_____)(__)(__)(__) |_| |_|
7+
*
8+
*
9+
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
10+
*
11+
* Licensed under the Apache License, Version 2.0 (the "License");
12+
* you may not use this file except in compliance with the License.
13+
* You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing, software
18+
* distributed under the License is distributed on an "AS IS" BASIS,
19+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
* See the License for the specific language governing permissions and
21+
* limitations under the License.
22+
*
23+
***************************************************************************/
24+
25+
#include "ParserTest.hpp"
26+
27+
#include "oatpp-postgresql/ql_template/Parser.hpp"
28+
29+
namespace oatpp { namespace test { namespace postgresql { namespace ql_template {
30+
31+
namespace {
32+
33+
typedef oatpp::postgresql::ql_template::Parser Parser;
34+
35+
}
36+
37+
void ParserTest::onRun() {
38+
39+
// {
40+
// oatpp::String text = "";
41+
// auto result = Parser::preprocess(text);
42+
// OATPP_ASSERT(result == text);
43+
// }
44+
//
45+
// {
46+
// oatpp::String text = "SELECT * FROM my_table;";
47+
// auto result = Parser::preprocess(text);
48+
// OATPP_ASSERT(result == text);
49+
// }
50+
51+
{
52+
oatpp::String text = "SELECT <[ * ]> FROM my_table;";
53+
auto result = Parser::preprocess(text);
54+
OATPP_ASSERT(result == "SELECT ___ FROM my_table;");
55+
}
56+
57+
{
58+
oatpp::String text = "<[SELECT * FROM my_table;]>";
59+
auto result = Parser::preprocess(text);
60+
OATPP_ASSERT(result == "_______________________");
61+
}
62+
63+
{
64+
oatpp::String text = "SELECT <[ * ]> FROM]> my_table;";
65+
auto result = Parser::preprocess(text);
66+
OATPP_LOGD(TAG, "sql='%s'", text->getData());
67+
OATPP_LOGD(TAG, "res='%s'", result->getData());
68+
}
69+
70+
{
71+
oatpp::String text = "SELECT < [ * ] > FROM]> my_table;";
72+
auto result = Parser::preprocess(text);
73+
OATPP_LOGD(TAG, "sql='%s'", text->getData());
74+
OATPP_LOGD(TAG, "res='%s'", result->getData());
75+
}
76+
77+
{
78+
oatpp::String text = "SELECT <[ * ']>' FROM my_table;";
79+
auto result = Parser::preprocess(text);
80+
OATPP_LOGD(TAG, "sql='%s'", text->getData());
81+
OATPP_LOGD(TAG, "res='%s'", result->getData());
82+
}
83+
84+
}
85+
86+
}}}}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/***************************************************************************
2+
*
3+
* Project _____ __ ____ _ _
4+
* ( _ ) /__\ (_ _)_| |_ _| |_
5+
* )(_)( /(__)\ )( (_ _)(_ _)
6+
* (_____)(__)(__)(__) |_| |_|
7+
*
8+
*
9+
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
10+
*
11+
* Licensed under the Apache License, Version 2.0 (the "License");
12+
* you may not use this file except in compliance with the License.
13+
* You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing, software
18+
* distributed under the License is distributed on an "AS IS" BASIS,
19+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
* See the License for the specific language governing permissions and
21+
* limitations under the License.
22+
*
23+
***************************************************************************/
24+
25+
#ifndef oatpp_test_postgresql_ql_template_ParserTest_hpp
26+
#define oatpp_test_postgresql_ql_template_ParserTest_hpp
27+
28+
#include "oatpp-test/UnitTest.hpp"
29+
30+
namespace oatpp { namespace test { namespace postgresql { namespace ql_template {
31+
32+
class ParserTest : public UnitTest {
33+
public:
34+
ParserTest() : UnitTest("TEST[postgresql::ql_template::ParserTest]") {}
35+
void onRun() override;
36+
};
37+
38+
}}}}
39+
40+
#endif // oatpp_test_postgresql_ql_template_ParserTest_hpp

test/oatpp-postgresql/tests.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
#include "ql_template/ParserTest.hpp"
3+
24
#include "types/IntTest.hpp"
35
#include "types/FloatTest.hpp"
46
#include "types/InterpretationTest.hpp"
@@ -9,9 +11,11 @@ namespace {
911

1012
void runTests() {
1113

12-
OATPP_RUN_TEST(oatpp::test::postgresql::types::IntTest);
13-
OATPP_RUN_TEST(oatpp::test::postgresql::types::FloatTest);
14-
OATPP_RUN_TEST(oatpp::test::postgresql::types::InterpretationTest);
14+
OATPP_RUN_TEST(oatpp::test::postgresql::ql_template::ParserTest);
15+
16+
// OATPP_RUN_TEST(oatpp::test::postgresql::types::IntTest);
17+
// OATPP_RUN_TEST(oatpp::test::postgresql::types::FloatTest);
18+
// OATPP_RUN_TEST(oatpp::test::postgresql::types::InterpretationTest);
1519

1620
}
1721

0 commit comments

Comments
 (0)