/* * * User: David RENE * Date: 11/12/2007 * Time: 01:16 * (c) Calexium * */ read tools/basis.anubis read system/logger.anubis read system/string.anubis read data_base/db_tools.anubis read calexium_lib/net_services_protocols/logger_service.anubis define String __utime_to_string ( UTime t ) = to_decimal(t.seconds) + "." + zero_pad_n(6, t.microseconds ) + " s". define One __logLongQueries ( UTime t0, String sql_query ) = with t = unow - t0, if t.seconds > 0 then logTrace("SQL", logMask("sql_profiling"), "[SQL] " + __utime_to_string(t) + " executing [" + sql_query + "]") else unique. public define Result(DbError, One) sql_transaction ( Database db, String sql_command, String message ) = // logDebug(debug_log, "Entering into transaction [" + message + "]."); with t0 = unow, if db_do_transaction( db, (One _) |-> if db_query(db, sql_command) is { error(err) then logError("DB", db_error(err,message));failure //error in the SQL request ok(_,cursor,_) then success(unique) }, success( (DbError err) |-> logError("DB", db_error(err,message))), 60000, // max 60s 100, // retry every 100 ms (DbError _) |-> false ) is { error(err_and_result) then // logDebug(debug_log, "Exiting from transaction [" + message + "] with error."); __logLongQueries(t0, sql_command); if err_and_result is (err, mb_result) then error(err), ok(_) then // logDebug(debug_log, "Exiting from transaction [" + message + "]."); __logLongQueries(t0, sql_command); ok(unique) }. public define DbQueryResult sql_query_timeout ( Database db, //database handle String sql_query, //sql query itself List(DbBind) initial_bindings, String msg //message to be shown if an error occure ) = //we try with 30 sec of timeout with t0 = unow, if sql_query_timeout(db, sql_query, initial_bindings, 60, 100, (DbError _) |-> false) is { error(sql_error) then logError("DB", db_error(sql_error,msg)); __logLongQueries(t0, sql_query); error(sql_error), ok(headers, cursor, reset) then __logLongQueries(t0, sql_query); ok(headers, cursor, reset) }. // deprecated. Use one of the previous ones. public define Maybe(SQLite3HeadersOrRow -> DbRow) sql_query_timeout ( Database db, //database handle String sql_query, //sql query itself String msg //message to be shown if an error occure ) = //we try with 30 sec of timeout with t0 = unow, if sql_query_timeout(db, sql_query, [], 60, 100, (DbError _) |-> false) is { error(sql_error) then logError("DB", db_error(sql_error,msg)); __logLongQueries(t0, sql_query); failure, ok(headers, cursor, reset) then __logLongQueries(t0, sql_query); success((SQLite3HeadersOrRow h_or_r) |-> if h_or_r is { headers then with cols = map((String c) |-> (DbDatum)db_text(c), headers(unique)), row((Int n) |-> force(nth(n, cols), no_such_column)), next_row then cursor(unique) }) }. // -- Extractors HELPERS --------------- public define List(String) db_get_string_list ( One -> DbRow table_cursor, List(String) so_far ) = if table_cursor(unique) is { error(sql_error) then logError("DB", db_error(sql_error, "db_get_string_list")); reverse(so_far), no_more_row then reverse(so_far), //can't find the symbol in the table, because the row is empty row(explorer) then with s = text(explorer)(0), db_get_string_list(table_cursor, [s . so_far]) }. // Old sqlite3 API public define List(String) db_get_string_list ( SQLite3HeadersOrRow -> DbRow table_cursor, List(String) so_far ) = if table_cursor(next_row) is { error(sql_error) then logError("DB", db_error(sql_error, "db_get_string_list")); reverse(so_far), no_more_row then reverse(so_far), //can't find the symbol in the table, because the row is empty row(explorer) then with s = text(explorer)(0), db_get_string_list(table_cursor, [s . so_far]) }. // Old sqlite3 API public define List(Int) db_get_integer_list ( SQLite3HeadersOrRow -> DbRow table_cursor, List(Int) so_far ) = if table_cursor(next_row) is { error(sql_error) then logError("DB", db_error(sql_error, "db_get_integer_list")); reverse(so_far), no_more_row then reverse(so_far), //can't find the symbol in the table, because the row is empty row(explorer) then with s = (Int)db_integer(explorer)(0), db_get_integer_list(table_cursor, [s . so_far]) }. public define List(Int) db_get_integer_list ( One -> DbRow table_cursor, List(Int) so_far ) = if table_cursor(unique) is { error(sql_error) then logError("DB", db_error(sql_error, "db_get_integer_list")); reverse(so_far), no_more_row then reverse(so_far), //can't find the symbol in the table, because the row is empty row(explorer) then with s = (Int)db_integer(explorer)(0), db_get_integer_list(table_cursor, [s . so_far]) }. // -- Migration HELPERS --------------- public define DbBind bind_String ( String name, String value ) = db_bind(force(sub_string(name, 1, length(name) - 1), name), db_text(value)). public define DbBind bind_ByteArray ( String name, ByteArray value ) = db_bind(force(sub_string(name, 1, length(name) - 1), name), db_blob(value)). public define DbBind bind_Int ( String name, Int value ) = db_bind(force(sub_string(name, 1, length(name) - 1), name), db_integer(value)). public define DbBind bind_NULL ( String name, ) = db_bind(force(sub_string(name, 1, length(name) - 1), name), null).