From 16fdae02840f48b1cbf3c8dc85d96b6885590e65 Mon Sep 17 00:00:00 2001 From: "db.liu" Date: Mon, 6 Nov 2017 15:10:33 +0800 Subject: [PATCH] fix an issue on casting long in query_result --- src/ripple/app/misc/SQLConditionTree.cpp | 9 ++- src/ripple/app/misc/STTx2SQL.cpp | 29 +++++---- src/test/app/Transaction2Sql_test.cpp | 77 +++++++++++++++++++++++- 3 files changed, 102 insertions(+), 13 deletions(-) diff --git a/src/ripple/app/misc/SQLConditionTree.cpp b/src/ripple/app/misc/SQLConditionTree.cpp index 88ca2fc971a..a157e688b4c 100644 --- a/src/ripple/app/misc/SQLConditionTree.cpp +++ b/src/ripple/app/misc/SQLConditionTree.cpp @@ -512,7 +512,10 @@ 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; @@ -520,6 +523,10 @@ namespace conditionParse { root.add_child(node); } + else { + result = { -1, (boost::format("value is error. [%s]") + % Json::jsonAsString(condition_value)).str() }; + } return result; } diff --git a/src/ripple/app/misc/STTx2SQL.cpp b/src/ripple/app/misc/STTx2SQL.cpp index 7b241358700..1f4e2507b9e 100644 --- a/src/ripple/app/misc/STTx2SQL.cpp +++ b/src/ripple/app/misc/STTx2SQL.cpp @@ -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(-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; @@ -1363,8 +1363,8 @@ class DisposeSQL { auto conditions = build_conditions(); if (conditions.first != 0) { last_error(std::make_pair(-1, - "Parsing where-conditions maybe unsuccessfully")); - return conditions.second; + std::move(conditions.second))); + return ""; } std::string where_conditions_str = conditions.second; @@ -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(r->get(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(r->get(i)); + e[r->get_properties(i).get_name()] = static_cast(r->get(i)); else e[r->get_properties(i).get_name()] = 0; } @@ -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 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(); diff --git a/src/test/app/Transaction2Sql_test.cpp b/src/test/app/Transaction2Sql_test.cpp index 377c39e471f..91a6c2cd8f5 100644 --- a/src/test/app/Transaction2Sql_test.cpp +++ b/src/test/app/Transaction2Sql_test.cpp @@ -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(table_name_); @@ -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) { @@ -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() { @@ -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(); @@ -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(); }