Skip to content

Commit 54711b6

Browse files
committed
Added dbResultToParsedArray
Fixes #4
1 parent af0a79b commit 54711b6

3 files changed

Lines changed: 77 additions & 2 deletions

File tree

src/config.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void Config::reloadConfig() {
5858

5959
for (auto& it : config["statements"]) {
6060
r_string stmtName = it.first.as<r_string>();
61-
statements[stmtName] = it.second.as<r_string>();
61+
statements[stmtName] = it.second.as<r_string>();
6262
}
6363

6464

src/res.cpp

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ game_value Result::cmd_toArray(game_state&, game_value_parameter right) {
3838
while (res->next()) {
3939
auto_array<game_value> row;
4040

41-
for (int i = 0; i < res->column_count(); ++i) {
41+
for (size_t i = 0u; i < res->column_count(); ++i) {
4242

4343
switch (res->column_type(i)) {
4444
case mariadb::value::null: row.emplace_back(game_value{}); break;
@@ -67,6 +67,78 @@ game_value Result::cmd_toArray(game_state&, game_value_parameter right) {
6767
return result;
6868
}
6969

70+
game_value Result::cmd_toParsedArray(game_state& state, game_value_parameter right) {
71+
auto& res = right.get_as<GameDataDBResult>()->res;
72+
if (!res) return auto_array<game_value>();
73+
auto_array<game_value> result;
74+
75+
while (res->next()) {
76+
auto_array<game_value> row;
77+
78+
for (size_t i = 0u; i < res->column_count(); ++i) {
79+
80+
switch (res->column_type(i)) {
81+
case mariadb::value::null: row.emplace_back(game_value{}); break;
82+
case mariadb::value::date: row.emplace_back(res->get_date(i).str()); break;
83+
case mariadb::value::date_time: row.emplace_back(res->get_date_time(i).str()); break;
84+
case mariadb::value::time: row.emplace_back(res->get_time(i).str_time()); break;
85+
case mariadb::value::string: {
86+
auto content = res->get_string(i);
87+
88+
if (content.front() == '[') {//array
89+
//attempt parse
90+
auto arrayContent = sqf::parse_simple_array(content);
91+
92+
if (state.get_evaluator()->_errorType == game_state::game_evaluator::evaluator_error_type::gen
93+
||
94+
(!arrayContent.empty() && arrayContent.front().is_null())
95+
) {
96+
state.get_evaluator()->_errorType = game_state::game_evaluator::evaluator_error_type::ok; //Force ignore
97+
row.emplace_back(std::move(content));
98+
} else
99+
row.emplace_back(game_value(arrayContent));
100+
} else if (
101+
content.front() == 't'//true
102+
|| content.front() == 'T'//True
103+
|| content.front() == 'f'//false
104+
|| content.front() == 'F'//False
105+
|| (content.front() >= '0' && content.front() <= '9')//number
106+
) {
107+
108+
auto arrayContent = sqf::parse_simple_array(r_string("["sv)+content+"]"sv);
109+
110+
if (state.get_evaluator()->_errorType == game_state::game_evaluator::evaluator_error_type::gen
111+
|| arrayContent.empty()
112+
|| arrayContent.front().is_null()) {
113+
state.get_evaluator()->_errorType = game_state::game_evaluator::evaluator_error_type::ok; //Force ignore
114+
row.emplace_back(std::move(content));
115+
} else
116+
row.emplace_back(arrayContent.front());
117+
} else {
118+
row.emplace_back(std::move(content));
119+
}
120+
} break;
121+
case mariadb::value::boolean: row.emplace_back(res->get_boolean(i)); break;
122+
case mariadb::value::decimal: row.emplace_back(res->get_decimal(i).float32()); break;
123+
case mariadb::value::unsigned8: row.emplace_back(static_cast<float>(res->get_unsigned8(i))); break;
124+
case mariadb::value::signed8: row.emplace_back(static_cast<float>(res->get_signed8(i))); break;
125+
case mariadb::value::unsigned16: row.emplace_back(static_cast<float>(res->get_unsigned16(i))); break;
126+
case mariadb::value::signed16: row.emplace_back(static_cast<float>(res->get_signed16(i))); break;
127+
case mariadb::value::unsigned32: row.emplace_back(static_cast<float>(res->get_unsigned32(i))); break;
128+
case mariadb::value::signed32: row.emplace_back(static_cast<float>(res->get_signed32(i))); break;
129+
case mariadb::value::unsigned64: row.emplace_back(static_cast<float>(res->get_unsigned64(i))); break;
130+
case mariadb::value::signed64: row.emplace_back(static_cast<float>(res->get_signed64(i))); break;
131+
case mariadb::value::float32: row.emplace_back(res->get_float(i)); break;
132+
case mariadb::value::double64: row.emplace_back(static_cast<float>(res->get_double(i))); break;
133+
case mariadb::value::enumeration: row.emplace_back(res->get_string(i)); break;
134+
default:;
135+
}
136+
}
137+
result.emplace_back(std::move(row));
138+
}
139+
return result;
140+
}
141+
70142
game_value Result::cmd_bindCallback(game_state&, game_value_parameter left, game_value_parameter right) {
71143
auto& res = left.get_as<GameDataDBAsyncResult>();
72144

@@ -111,6 +183,7 @@ void Result::initCommands() {
111183
handle_cmd_affectedRows = client::host::register_sqf_command("dbResultAffectedRows", "TODO", Result::cmd_affectedRows, game_data_type::SCALAR, GameDataDBResult_typeE);
112184
handle_cmd_lastInsertId = client::host::register_sqf_command("dbResultLastInsertId", "TODO", Result::cmd_lastInsertId, game_data_type::SCALAR, GameDataDBResult_typeE);
113185
handle_cmd_toArray = client::host::register_sqf_command("dbResultToArray", "TODO", Result::cmd_toArray, game_data_type::ARRAY, GameDataDBResult_typeE);
186+
handle_cmd_toParsedArray = client::host::register_sqf_command("dbResultToParsedArray", "TODO", Result::cmd_toParsedArray, game_data_type::ARRAY, GameDataDBResult_typeE);
114187
handle_cmd_bindCallback = client::host::register_sqf_command("dbBindCallback", "TODO", Result::cmd_bindCallback, game_data_type::NOTHING, GameDataDBAsyncResult_typeE, game_data_type::ARRAY);
115188
handle_cmd_waitForResult = client::host::register_sqf_command("dbWaitForResult", "TODO", Result::cmd_waitForResult, GameDataDBResult_typeE, GameDataDBAsyncResult_typeE);
116189
}

src/res.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Result {
1313
static game_value cmd_affectedRows(game_state&, game_value_parameter right);
1414
static game_value cmd_lastInsertId(game_state&, game_value_parameter right);
1515
static game_value cmd_toArray(game_state&, game_value_parameter right);
16+
static game_value cmd_toParsedArray(game_state&, game_value_parameter right);
1617
static game_value cmd_bindCallback(game_state&, game_value_parameter left, game_value_parameter right);
1718
static game_value cmd_waitForResult(game_state&, game_value_parameter right);
1819

@@ -25,6 +26,7 @@ class Result {
2526
static inline types::registered_sqf_function handle_cmd_affectedRows;
2627
static inline types::registered_sqf_function handle_cmd_lastInsertId;
2728
static inline types::registered_sqf_function handle_cmd_toArray;
29+
static inline types::registered_sqf_function handle_cmd_toParsedArray;
2830
static inline types::registered_sqf_function handle_cmd_bindCallback;
2931
static inline types::registered_sqf_function handle_cmd_waitForResult;
3032
};

0 commit comments

Comments
 (0)