Skip to content

Commit 96f31a1

Browse files
committed
Add parseTinyintAsBool config option
1 parent f409a4b commit 96f31a1

5 files changed

Lines changed: 35 additions & 6 deletions

File tree

docs/intro/config-file.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ The InterceptDB Config file
3737
#timestamp: Return Date/DateTime as a timestamp as a number (this can incur precision loss)
3838
#timestampString: Return Date/DateTime as a unix timestamp in a string
3939
#timestampStringMS: Return Date/DateTime as a millisecond unix timestamp in a string
40+
parseTinyintAsBool: false #returns tinyint as bool in dbResultTo(Parsed)Array
4041
4142
schemas:
4243
test: schema.sql #Filename relative to config.yaml to be used in dbLoadSchema

src/config.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ void Config::reloadConfig() {
9393
if (!config["global"].IsMap()) throw std::runtime_error("Config Global entry is not a map");
9494

9595
dynamicQueriesEnabled = config["global"]["enableDynamicQueries"].as<bool>(true);
96+
parseTinyintAsBool = config["global"]["parseTinyintAsBool"].as<bool>(false);
9697
dateType = dateTypeFromString(config["global"]["parseDateType"].as<r_string>("string"sv));
9798

9899
#pragma endregion global

src/config.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class Config : public intercept::singleton<Config> {
4848
return dateType;
4949
}
5050

51+
bool getTinyintAsBool() const {
52+
return parseTinyintAsBool;
53+
}
54+
5155
static void initCommands();
5256
static inline registered_sqf_function handle_cmd_reloadConfig;
5357
static inline registered_sqf_function handle_cmd_version;
@@ -57,7 +61,7 @@ class Config : public intercept::singleton<Config> {
5761
std::map<intercept::types::r_string, intercept::types::r_string> statements;
5862
std::map<intercept::types::r_string, std::filesystem::path> schemas;
5963
bool dynamicQueriesEnabled = true;
60-
64+
bool parseTinyintAsBool = false;
6165
ConfigDateType dateType = ConfigDateType::humanString;
6266

6367
};

src/res.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ game_value Result::cmd_toArray(game_state&, game_value_parameter right) {
155155
auto_array<game_value> result;
156156

157157
const auto [dateParser, dateTimeParser, timeParser] = getDateParser(Config::get().getDateType());
158+
const auto parseTinyintAsBool = Config::get().getTinyintAsBool();
159+
158160
while (res->next()) {
159161
auto_array<game_value> row;
160162

@@ -168,8 +170,18 @@ game_value Result::cmd_toArray(game_state&, game_value_parameter right) {
168170
case mariadb::value::string: row.emplace_back(res->get_string(i)); break;
169171
case mariadb::value::boolean: row.emplace_back(res->get_boolean(i)); break;
170172
case mariadb::value::decimal: row.emplace_back(res->get_decimal(i).float32()); break;
171-
case mariadb::value::unsigned8: row.emplace_back(static_cast<float>(res->get_unsigned8(i))); break;
172-
case mariadb::value::signed8: row.emplace_back(static_cast<float>(res->get_signed8(i))); break;
173+
case mariadb::value::unsigned8: {
174+
if (parseTinyintAsBool && res->column_type_raw(i) == MYSQL_TYPE_TINY)
175+
row.emplace_back(static_cast<bool>(res->get_unsigned8(i)));
176+
else
177+
row.emplace_back(static_cast<float>(res->get_unsigned8(i)));
178+
} break;
179+
case mariadb::value::signed8: {
180+
if (parseTinyintAsBool && res->column_type_raw(i) == MYSQL_TYPE_TINY)
181+
row.emplace_back(static_cast<bool>(res->get_signed8(i)));
182+
else
183+
row.emplace_back(static_cast<float>(res->get_signed8(i)));
184+
} break;
173185
case mariadb::value::unsigned16: row.emplace_back(static_cast<float>(res->get_unsigned16(i))); break;
174186
case mariadb::value::signed16: row.emplace_back(static_cast<float>(res->get_signed16(i))); break;
175187
case mariadb::value::unsigned32: row.emplace_back(static_cast<float>(res->get_unsigned32(i))); break;
@@ -197,6 +209,7 @@ game_value Result::cmd_toParsedArray(game_state& state, game_value_parameter rig
197209
auto_array<game_value> result;
198210

199211
const auto [dateParser, dateTimeParser, timeParser] = getDateParser(Config::get().getDateType());
212+
const auto parseTinyintAsBool = Config::get().getTinyintAsBool();
200213

201214
while (res->next()) {
202215
auto_array<game_value> row;
@@ -249,8 +262,18 @@ game_value Result::cmd_toParsedArray(game_state& state, game_value_parameter rig
249262
case mariadb::value::blob: addParsedString(res->get_blobString(i)); break;
250263
case mariadb::value::boolean: row.emplace_back(res->get_boolean(i)); break;
251264
case mariadb::value::decimal: row.emplace_back(res->get_decimal(i).float32()); break;
252-
case mariadb::value::unsigned8: row.emplace_back(static_cast<float>(res->get_unsigned8(i))); break;
253-
case mariadb::value::signed8: row.emplace_back(static_cast<float>(res->get_signed8(i))); break;
265+
case mariadb::value::unsigned8: {
266+
if (parseTinyintAsBool && res->column_type_raw(i) == MYSQL_TYPE_TINY)
267+
row.emplace_back(static_cast<bool>(res->get_unsigned8(i)));
268+
else
269+
row.emplace_back(static_cast<float>(res->get_unsigned8(i)));
270+
} break;
271+
case mariadb::value::signed8: {
272+
if (parseTinyintAsBool && res->column_type_raw(i) == MYSQL_TYPE_TINY)
273+
row.emplace_back(static_cast<bool>(res->get_signed8(i)));
274+
else
275+
row.emplace_back(static_cast<float>(res->get_signed8(i)));
276+
} break;
254277
case mariadb::value::unsigned16: row.emplace_back(static_cast<float>(res->get_unsigned16(i))); break;
255278
case mariadb::value::signed16: row.emplace_back(static_cast<float>(res->get_signed16(i))); break;
256279
case mariadb::value::unsigned32: row.emplace_back(static_cast<float>(res->get_unsigned32(i))); break;

0 commit comments

Comments
 (0)