function getBookingAppVersion(PhpgwContext $c)
{
    $sql = "SELECT app_version FROM phpgw_applications WHERE app_name = 'booking' LIMIT 1";
    echo $sql . "\n";
    $c->getDb()->query($sql, __LINE__, __FILE__);
    $c->getDb()->next_record();
    echo 'Current Version: ' . $c->getDb()->f('app_version') . "\n";
}
function cleanReservations(PhpgwContext $c)
{
    $reservation_tables = array('bb_booking', 'bb_allocation', 'bb_event');
    foreach ($reservation_tables as $table) {
        $sql = "TRUNCATE table {$table} CASCADE";
        echo $sql . "\n";
        $c->getDb()->query($sql, __LINE__, __FILE__);
    }
}
function cleanCompletedReservations(PhpgwContext $c)
{
    $reservation_tables = array('bb_completed_reservation');
    foreach ($reservation_tables as $table) {
        $sql = "TRUNCATE table {$table} CASCADE";
        echo $sql . "\n";
        $c->getDb()->query($sql, __LINE__, __FILE__);
    }
}
function setBookingAppVersion(PhpgwContext $c)
{
    $options = array('version' => null);
    $options['version'] = isset($_ENV['VERSION']) && strlen($_ENV['VERSION'] > 0) ? $_ENV['VERSION'] : null;
    if (!$options['version']) {
        throw new InvalidArgumentException('Missing VERSION');
    }
    $sql = sprintf("UPDATE phpgw_applications SET app_version = '%s' WHERE app_name = 'booking'", $options['version']);
    echo $sql . "\n";
    $c->getDb()->query($sql, __LINE__, __FILE__);
}
function testNumberGenerator(PhpgwContext $c)
{
    $numberGeneratorInstance = get_external_generator();
    ###############################################################
    $logic_exception_if_no_active_transaction = false;
    try {
        $numberGeneratorInstance->increment();
    } catch (LogicException $e) {
        $logic_exception_if_no_active_transaction = true;
    }
    assert('$logic_exception_if_no_active_transaction') and pass_test('logic_exception_if_no_active_transaction');
    #############################################################
    print_info("Pre Transaction");
    $c->getDb()->transaction_begin();
    //Begin transaction
    $no_logic_exception_if_active_transaction = false;
    try {
        $numberGeneratorInstance->increment();
        $currentValue = $numberGeneratorInstance->get_current();
        $numberGeneratorInstance->increment();
        $numberGeneratorInstance->increment();
        $no_logic_exception_if_active_transaction = true;
    } catch (LogicException $e) {
    }
    $c->getDb()->transaction_abort();
    assert($no_logic_exception_if_active_transaction) and pass_test("no_logic_exception_if_active_transaction");
    print_info("Post Transaction");
    #############################################################
    $previousNumberGeneratorInstance = $numberGeneratorInstance;
    $numberGeneratorInstance = get_external_generator();
    $new_generator_is_created_post_transaction = $previousNumberGeneratorInstance != $numberGeneratorInstance;
    assert('$new_generator_is_created_post_transaction') and pass_test("new_generator_is_created_post_transaction");
    #############################################################
    print_info("Start Transaction");
    $c->getDb()->transaction_begin();
    //Begin transaction
    #############################################################
    $cannotGetCurrentWithoutLock = false;
    try {
        $numberGeneratorInstance->get_current();
    } catch (Exception $e) {
        print_info($e->getMessage());
        $cannotGetCurrentWithoutLock = true;
    }
    assert('$cannotGetCurrentWithoutLock') and pass_test("Cannot get current without lock");
    ##############################################################
    $canGetCurrentWithLock = false;
    $numberGeneratorInstance->increment();
    //Locked
    print_info("Locked (->increment)");
    $previousValue = $numberGeneratorInstance->get_current();
    $numberGeneratorInstance->increment();
    $canGetCurrentWithLock = true;
    assert($canGetCurrentWithLock) and pass_test("Able to get_current with lock");
    ############################################################
    assert('$numberGeneratorInstance->get_current() === $previousValue+1') and pass_test("Incremented value by one");
    ############################################################
    $c->getDb()->transaction_abort();
    print_info("Abort transaction");
    ############################################################
    $unableToUseGeneratorPostTransaction = false;
    try {
        $numberGeneratorInstance->get_current();
    } catch (LogicException $e) {
        $unableToUseGeneratorPostTransaction = true;
    }
    assert($unableToUseGeneratorPostTransaction) and pass_test("Unable to use generator post transaction");
    print_info("Sleep test");
    $c->getDb()->transaction_begin();
    //Begin transaction
    $numberGeneratorInstance = get_external_generator();
    sleep(4);
    for ($i = 0; $i < 10; $i++) {
        print_info($numberGeneratorInstance->increment()->get_current());
    }
    $c->getDb()->transaction_abort();
    sleep(4);
    print_info("Sleep test 2");
    $c->getDb()->transaction_begin();
    //Begin transaction
    $numberGeneratorInstance = get_external_generator();
    sleep(4);
    for ($i = 0; $i < 10; $i++) {
        print_info($numberGeneratorInstance->increment()->get_current());
    }
    $c->getDb()->transaction_commit();
    sleep(4);
}