@@ -157,7 +157,7 @@ game_value Connection::cmd_execute(game_state& gs, game_value_parameter con, gam
157157 }
158158
159159 auto res = statement->query ();
160-
160+ // #TODO handle error in create_statement and in query()
161161 auto gd_res = new GameDataDBResult ();
162162 gd_res->res = res;
163163
@@ -190,9 +190,18 @@ game_value Connection::cmd_execute(game_state& gs, game_value_parameter con, gam
190190 return true ;
191191 }
192192 catch (mariadb::exception::connection& x) {
193- auto exText = r_string (" Intercept-DB exception " ) + x.what () + " \n at\n " + stmt;
194- invoker_lock l ();
195- sqf::diag_log (exText);
193+ invoker_lock l;
194+ if (con->account ()->hasErrorHandler ()) {
195+ for (auto & it : con->account ()->getErrorHandlers ()) {
196+
197+ auto res = sqf::call (it, { static_cast <r_string>(x.what ()), static_cast <size_t >(x.error_id ()), stmt });
198+
199+ if (res.type_enum () == game_data_type::BOOL && static_cast <bool >(res)) break ; // If returned true then error was handled.
200+ }
201+ } else {
202+ auto exText = r_string (" Intercept-DB exception " ) + x.what () + " \n at\n " + stmt;
203+ sqf::diag_log (exText);
204+ }
196205
197206 return false ;
198207 }
@@ -236,9 +245,20 @@ game_value Connection::cmd_executeAsync(game_state&, game_value_parameter con, g
236245 __itt_task_end (domainConnection);
237246 return true ;
238247 } catch (mariadb::exception::connection& x) {
239- auto exText = r_string (" Intercept-DB exception " ) + x.what () + " \n at\n " + stmt;
240- invoker_lock l ();
241- sqf::diag_log (exText);
248+ invoker_lock l;
249+ if (con->account ()->hasErrorHandler ()) {
250+ for (auto & it : con->account ()->getErrorHandlers ()) {
251+
252+ auto res = sqf::call (it, { static_cast <r_string>(x.what ()), static_cast <size_t >(x.error_id ()), stmt });
253+
254+ if (res.type_enum () == game_data_type::BOOL && static_cast <bool >(res)) break ; // If returned true then error was handled.
255+ }
256+ } else {
257+ auto exText = r_string (" Intercept-DB exception " ) + x.what () + " \n at\n " + stmt;
258+ sqf::diag_log (exText);
259+ }
260+
261+
242262
243263 __itt_task_end (domainConnection);
244264 return false ;
@@ -265,6 +285,14 @@ game_value Connection::cmd_isConnected(game_state&, game_value_parameter con) {
265285 return session && (session->connected () || Threading::get ().isConnected (session->account ()));
266286}
267287
288+ game_value Connection::cmd_addErrorHandler (game_state&, game_value_parameter con, game_value_parameter handler) {
289+ auto session = con.get_as <GameDataDBConnection>()->session ;
290+ if (!session) return {};
291+
292+ session->account ()->addErrorHandler (handler);
293+ return {};
294+ }
295+
268296void Connection::initCommands () {
269297
270298 auto dbType = host::register_sqf_type (" DBCON" sv, " databaseConnection" sv, " TODO" sv, " databaseConnection" sv, createGameDataDBConnection);
@@ -276,6 +304,10 @@ void Connection::initCommands() {
276304 handle_cmd_createConnectionConfig = host::register_sqf_command (" dbCreateConnection" , " TODO" , Connection::cmd_createConnectionConfig, GameDataDBConnection_typeE, game_data_type::STRING);
277305 handle_cmd_execute = host::register_sqf_command (" dbExecute" , " TODO" , Connection::cmd_execute, Result::GameDataDBResult_typeE, GameDataDBConnection_typeE, Query::GameDataDBQuery_typeE);
278306 handle_cmd_executeAsync = host::register_sqf_command (" dbExecuteAsync" , " TODO" , Connection::cmd_executeAsync, Result::GameDataDBAsyncResult_typeE, GameDataDBConnection_typeE, Query::GameDataDBQuery_typeE);
279- handle_cmd_ping = host::register_sqf_command (" dbPing" , " TODO" , Connection::cmd_ping, game_data_type::ARRAY, GameDataDBConnection_typeE);
280- handle_cmd_isConnected = host::register_sqf_command (" dbIsConnected" , " TODO" , Connection::cmd_isConnected, game_data_type::ARRAY, GameDataDBConnection_typeE);
307+ handle_cmd_ping = host::register_sqf_command (" dbPing" , " TODO" , Connection::cmd_ping, game_data_type::BOOL, GameDataDBConnection_typeE);
308+ handle_cmd_isConnected = host::register_sqf_command (" dbIsConnected" , " TODO" , Connection::cmd_isConnected, game_data_type::BOOL, GameDataDBConnection_typeE);
309+ handle_cmd_addErrorHandler = host::register_sqf_command (" dbIsConnected" , " TODO" , Connection::cmd_addErrorHandler, game_data_type::ARRAY, GameDataDBConnection_typeE, game_data_type::CODE);
310+
311+
312+
281313}
0 commit comments