Commit 3dd0561da0b48e8bb51f63a6e5282b47ece84fe9

Authored by Cédric RICARD
1 parent 5e43e620

adding sql_query_timeout() and sql_transaction() functions

Showing 1 changed file with 93 additions and 0 deletions   Show diff stats
calexium_lib/database/db_utils.anubis 0 → 100644
  1 +/*
  2 + *
  3 + * User: David RENE
  4 + * Date: 11/12/2007
  5 + * Time: 01:16
  6 + * (c) Calexium
  7 + *
  8 + */
  9 +
  10 +read tools/basis.anubis
  11 +read system/logger.anubis
  12 +read data_base/sqlite.anubis
  13 +
  14 +read calexium_lib/net_services_protocols/logger_service.anubis
  15 +
  16 +
  17 +define Result(SQLite3Error, SQLite3HeadersOrRow -> SQLite3Row)
  18 + sql_query_timeout
  19 + (
  20 + SQLite3DataBase db,
  21 + String sql_command,
  22 + Int32 timeout
  23 + ) =
  24 +
  25 + if sql_query(db, sql_command) is
  26 + {
  27 + error(sql_error) then (
  28 + if (sql_error.code /= 1 & sql_error.code /= 5 & sql_error.code /= 6) | now > timeout then
  29 + error(sql_error)
  30 + else
  31 + (
  32 + if sql_error.code = 5 then
  33 + unique
  34 + else
  35 + print_db_error(sql_error)
  36 + );
  37 + sleep(100);
  38 + sql_query_timeout(db, sql_command, timeout)),
  39 + ok(cursor) then ok(cursor)
  40 + }.
  41 +
  42 +public define Result(SQLite3Error, One)
  43 + sql_transaction
  44 + (
  45 + SQLite3DataBase db,
  46 + String sql_command,
  47 + String message
  48 + ) =
  49 +// logDebug(debug_log, "Entering into transaction [" + message + "].");
  50 + if db_do_transaction(
  51 + db,
  52 + (One _) |->
  53 + if sql_query(db, sql_command) is
  54 + {
  55 + error(err) then logError("DB", db_error(err,message));failure //error in the SQL request
  56 + ok(cursor) then success(unique)
  57 + },
  58 + success( (SQLite3Error err) |-> logError("DB", db_error(err,message))),
  59 + 60000 // max 60s
  60 + ) is
  61 + {
  62 + error(err_and_result) then
  63 +// logDebug(debug_log, "Exiting from transaction [" + message + "] with error.");
  64 + if err_and_result is (err, mb_result) then
  65 + error(err),
  66 + ok(_) then
  67 +// logDebug(debug_log, "Exiting from transaction [" + message + "].");
  68 + ok(unique)
  69 + }.
  70 +
  71 + public define Maybe(SQLite3HeadersOrRow -> SQLite3Row)
  72 + sql_query_timeout
  73 + (
  74 + SQLite3DataBase db, //database handle
  75 + String sql_query, //sql query itself
  76 + String msg //message to be shown if an error occure
  77 + ) =
  78 + //we try with 30 sec of timeout
  79 + sql_query_timeout(db, sql_query, now + 30, msg).
  80 +
  81 +public define Maybe(SQLite3HeadersOrRow -> SQLite3Row)
  82 + sql_query_timeout
  83 + (
  84 + SQLite3DataBase db, //database handle
  85 + String sql_query, //sql query itself
  86 + String msg //message to be shown if an error occure
  87 + ) =
  88 + //we try with 30 sec of timeout
  89 + if sql_query_timeout(db, sql_query, now + 60) is
  90 + {
  91 + error(sql_error) then logError("DB", db_error(sql_error,msg));failure,
  92 + ok(cursor) then success(cursor)
  93 + }.
... ...