Skip to content

Commit

Permalink
fix an issue on casting long in query_result
Browse files Browse the repository at this point in the history
  • Loading branch information
shaxunyeman committed Nov 6, 2017
1 parent e02f93d commit 16fdae0
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 13 deletions.
9 changes: 8 additions & 1 deletion src/ripple/app/misc/SQLConditionTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,14 +512,21 @@ namespace conditionParse {
% Json::jsonAsString(condition_value)).str() };
}
}
else {
else if (condition_value.isBool() || condition_value.isInt()
|| condition_value.isDouble() || condition_value.isUInt()
|| condition_value.isString() || condition_value.isIntegral()
|| condition_value.isNumeric()) {
conditionTree node(conditionTree::NodeType::Expression);
Json::Value expression;
expression[key_name] = condition_value;
node.set_expression(expression);

root.add_child(node);
}
else {
result = { -1, (boost::format("value is error. [%s]")
% Json::jsonAsString(condition_value)).str() };
}

return result;
}
Expand Down
29 changes: 18 additions & 11 deletions src/ripple/app/misc/STTx2SQL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1347,8 +1347,8 @@ class DisposeSQL {
auto join_on_condition = build_join_on_condition();
if (join_on_condition.first != 0) {
last_error(std::make_pair<int, std::string>(-1,
"Parsing On-conditions maybe unsuccessfully"));
return join_on_condition.second;
std::move(join_on_condition.second)));
return "";
}

join_on_condition_str = join_on_condition.second;
Expand All @@ -1363,8 +1363,8 @@ class DisposeSQL {
auto conditions = build_conditions();
if (conditions.first != 0) {
last_error(std::make_pair<int, std::string>(-1,
"Parsing where-conditions maybe unsuccessfully"));
return conditions.second;
std::move(conditions.second)));
return "";
}
std::string where_conditions_str = conditions.second;

Expand Down Expand Up @@ -2465,10 +2465,15 @@ namespace helper {
else
e[r->get_properties(i).get_name()] = 0.0;
}
else if (r->get_properties(i).get_data_type() == soci::dt_long_long
|| r->get_properties(i).get_data_type() == soci::dt_unsigned_long_long) {
else if (r->get_properties(i).get_data_type() == soci::dt_long_long) {
if (r->get_indicator(i) == soci::i_ok)
e[r->get_properties(i).get_name()] = static_cast<int>(r->get<long long>(i));
else
e[r->get_properties(i).get_name()] = 0;
}
else if (r->get_properties(i).get_data_type() == soci::dt_unsigned_long_long) {
if (r->get_indicator(i) == soci::i_ok)
e[r->get_properties(i).get_name()] = static_cast<int>(r->get<long>(i));
e[r->get_properties(i).get_name()] = static_cast<int>(r->get<unsigned long long>(i));
else
e[r->get_properties(i).get_name()] = 0;
}
Expand Down Expand Up @@ -2521,13 +2526,15 @@ namespace helper {

try {
std::string sql = buildsql->asString();
auto last_error = buildsql->last_error();
if (last_error.first != 0) {
obj[jss::status] = "failure";
obj[jss::error] = last_error.second;
return obj;
}
LockedSociSession query = conn->checkoutDb();
soci::rowset<soci::row> records = ((*query).prepare << sql);
obj = query_result(records);
/*soci::statement st =
(query->prepare << sql);
st.execute();*/
}
catch (soci::soci_error& e) {
obj[jss::error] = e.what();
Expand Down
77 changes: 76 additions & 1 deletion src/test/app/Transaction2Sql_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ class Transaction2Sql_test : public beast::unit_test::suite {
BEAST_EXPECT(f("[[]]", 10).first == false);
}

void test_crashfix_on_select() {
{
std::string query = "[[],{\"$and\":[{\"$order\":[{\"id\" : -1}]},{\"id\":{\"$ge\" : 3}}]}]";
Json::Value result = getRecords(query);
BEAST_EXPECT(result[jss::status] == "failure");
}
}

void test_CreateTableTransaction() {
const auto keypair = randomKeyPair(KeyType::ed25519);
ripple::uint128 hex_table = ripple::from_hex_text<ripple::uint128>(table_name_);
Expand Down Expand Up @@ -537,6 +545,12 @@ class Transaction2Sql_test : public beast::unit_test::suite {
BEAST_EXPECT(boost::iequals(expected_sql, result_sql));
}

// 处理 count(*)
{
std::string query = "[[\"count(*)\"]]";
Json::Value result = getRecords(query);
BEAST_EXPECT(result[jss::status] == "success");
}
}

Json::Value getRecords(const std::string& query_fields) {
Expand Down Expand Up @@ -2017,6 +2031,66 @@ class Transaction2Sql_test : public beast::unit_test::suite {
BEAST_EXPECT(result_conditions == expect_conditions);
}
}

{
std::string raw_string = "[{\"$and\":[{\"$order\":[{\"id\" : -1}]},{\"id\":{\"$ge\":3}}]}]";
Json::Reader reader = Json::Reader();
Json::Value conditions;
if (reader.parse(raw_string, conditions) == false) {
std::cout << "parse error. " << reader.getFormatedErrorMessages() << std::endl;
return;
}
auto result = createConditionTree(conditions);
BEAST_EXPECT(result.first == 0);

auto result2 = parse_conditions(conditions, result.second);
BEAST_EXPECT(result2.first != 0);
}

{
std::string raw_string = "[{\"$and\":[{},{}]}]";
Json::Reader reader = Json::Reader();
Json::Value conditions;
if (reader.parse(raw_string, conditions) == false) {
std::cout << "parse error. " << reader.getFormatedErrorMessages() << std::endl;
return;
}
auto result = createConditionTree(conditions);
BEAST_EXPECT(result.first == 0);

auto result2 = parse_conditions(conditions, result.second);
BEAST_EXPECT(result2.first != 0);
}

{
std::string raw_string = "[{\"$and\":[{}]}]";
Json::Reader reader = Json::Reader();
Json::Value conditions;
if (reader.parse(raw_string, conditions) == false) {
std::cout << "parse error. " << reader.getFormatedErrorMessages() << std::endl;
return;
}
auto result = createConditionTree(conditions);
BEAST_EXPECT(result.first == 0);

auto result2 = parse_conditions(conditions, result.second);
BEAST_EXPECT(result2.first != 0);
}

{
std::string raw_string = "[{\"$and\":\"fixhotbug\"}]";
Json::Reader reader = Json::Reader();
Json::Value conditions;
if (reader.parse(raw_string, conditions) == false) {
std::cout << "parse error. " << reader.getFormatedErrorMessages() << std::endl;
return;
}
auto result = createConditionTree(conditions);
BEAST_EXPECT(result.first == 0);

auto result2 = parse_conditions(conditions, result.second);
BEAST_EXPECT(result2.first != 0);
}
}

void test_mongodb_json_style() {
Expand All @@ -2035,6 +2109,7 @@ class Transaction2Sql_test : public beast::unit_test::suite {
init_env();

test_fixbug_RR207();
test_crashfix_on_select();

//test_CreateTableForeginTransaction();
test_CreateTableTransaction();
Expand All @@ -2047,8 +2122,8 @@ class Transaction2Sql_test : public beast::unit_test::suite {
test_DeleteRecordTransaction();

test_DropTableTransaction();
test_mongodb_json_style();

test_mongodb_json_style();
test_join_select();
pass();
}
Expand Down

0 comments on commit 16fdae0

Please sign in to comment.